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
;
249 bool tg4_component_in_swizzle
;
251 variable_storage
*find_variable_storage(ir_variable
*var
);
253 int add_constant(gl_register_file file
, gl_constant_value values
[8],
254 int size
, GLenum datatype
, uint16_t *swizzle_out
);
256 st_src_reg
get_temp(const glsl_type
*type
);
257 void reladdr_to_temp(ir_instruction
*ir
, st_src_reg
*reg
, int *num_reladdr
);
259 st_src_reg
st_src_reg_for_double(double val
);
260 st_src_reg
st_src_reg_for_float(float val
);
261 st_src_reg
st_src_reg_for_int(int val
);
262 st_src_reg
st_src_reg_for_int64(int64_t val
);
263 st_src_reg
st_src_reg_for_type(enum glsl_base_type type
, int val
);
266 * \name Visit methods
268 * As typical for the visitor pattern, there must be one \c visit method for
269 * each concrete subclass of \c ir_instruction. Virtual base classes within
270 * the hierarchy should not have \c visit methods.
273 virtual void visit(ir_variable
*);
274 virtual void visit(ir_loop
*);
275 virtual void visit(ir_loop_jump
*);
276 virtual void visit(ir_function_signature
*);
277 virtual void visit(ir_function
*);
278 virtual void visit(ir_expression
*);
279 virtual void visit(ir_swizzle
*);
280 virtual void visit(ir_dereference_variable
*);
281 virtual void visit(ir_dereference_array
*);
282 virtual void visit(ir_dereference_record
*);
283 virtual void visit(ir_assignment
*);
284 virtual void visit(ir_constant
*);
285 virtual void visit(ir_call
*);
286 virtual void visit(ir_return
*);
287 virtual void visit(ir_discard
*);
288 virtual void visit(ir_demote
*);
289 virtual void visit(ir_texture
*);
290 virtual void visit(ir_if
*);
291 virtual void visit(ir_emit_vertex
*);
292 virtual void visit(ir_end_primitive
*);
293 virtual void visit(ir_barrier
*);
296 void visit_expression(ir_expression
*, st_src_reg
*) ATTRIBUTE_NOINLINE
;
298 void visit_atomic_counter_intrinsic(ir_call
*);
299 void visit_ssbo_intrinsic(ir_call
*);
300 void visit_membar_intrinsic(ir_call
*);
301 void visit_shared_intrinsic(ir_call
*);
302 void visit_image_intrinsic(ir_call
*);
303 void visit_generic_intrinsic(ir_call
*, enum tgsi_opcode op
);
307 /** List of variable_storage */
308 struct hash_table
*variables
;
310 /** List of immediate_storage */
311 exec_list immediates
;
312 unsigned num_immediates
;
314 /** List of glsl_to_tgsi_instruction */
315 exec_list instructions
;
317 glsl_to_tgsi_instruction
*emit_asm(ir_instruction
*ir
, enum tgsi_opcode op
,
318 st_dst_reg dst
= undef_dst
,
319 st_src_reg src0
= undef_src
,
320 st_src_reg src1
= undef_src
,
321 st_src_reg src2
= undef_src
,
322 st_src_reg src3
= undef_src
);
324 glsl_to_tgsi_instruction
*emit_asm(ir_instruction
*ir
, enum tgsi_opcode op
,
325 st_dst_reg dst
, st_dst_reg dst1
,
326 st_src_reg src0
= undef_src
,
327 st_src_reg src1
= undef_src
,
328 st_src_reg src2
= undef_src
,
329 st_src_reg src3
= undef_src
);
331 enum tgsi_opcode
get_opcode(enum tgsi_opcode op
,
333 st_src_reg src0
, st_src_reg src1
);
336 * Emit the correct dot-product instruction for the type of arguments
338 glsl_to_tgsi_instruction
*emit_dp(ir_instruction
*ir
,
344 void emit_scalar(ir_instruction
*ir
, enum tgsi_opcode op
,
345 st_dst_reg dst
, st_src_reg src0
);
347 void emit_scalar(ir_instruction
*ir
, enum tgsi_opcode op
,
348 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
350 void emit_arl(ir_instruction
*ir
, st_dst_reg dst
, st_src_reg src0
);
352 void get_deref_offsets(ir_dereference
*ir
,
353 unsigned *array_size
,
358 void calc_deref_offsets(ir_dereference
*tail
,
359 unsigned *array_elements
,
361 st_src_reg
*indirect
,
363 st_src_reg
canonicalize_gather_offset(st_src_reg offset
);
364 bool handle_bound_deref(ir_dereference
*ir
);
366 bool try_emit_mad(ir_expression
*ir
,
368 bool try_emit_mad_for_and_not(ir_expression
*ir
,
371 void emit_swz(ir_expression
*ir
);
373 bool process_move_condition(ir_rvalue
*ir
);
375 void simplify_cmp(void);
377 void rename_temp_registers(struct rename_reg_pair
*renames
);
378 void get_first_temp_read(int *first_reads
);
379 void get_first_temp_write(int *first_writes
);
380 void get_last_temp_read_first_temp_write(int *last_reads
, int *first_writes
);
381 void get_last_temp_write(int *last_writes
);
383 void copy_propagate(void);
384 int eliminate_dead_code(void);
386 void split_arrays(void);
387 void merge_two_dsts(void);
388 void merge_registers(void);
389 void renumber_registers(void);
391 void emit_block_mov(ir_assignment
*ir
, const struct glsl_type
*type
,
392 st_dst_reg
*l
, st_src_reg
*r
,
393 st_src_reg
*cond
, bool cond_swap
);
400 static st_dst_reg address_reg
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
,
402 static st_dst_reg address_reg2
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
,
404 static st_dst_reg sampler_reladdr
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
,
408 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
412 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
416 ralloc_vasprintf_append(&prog
->data
->InfoLog
, fmt
, args
);
419 prog
->data
->LinkStatus
= LINKING_FAILURE
;
423 swizzle_for_size(int size
)
425 static const int size_swizzles
[4] = {
426 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
427 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
428 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
429 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
432 assert((size
>= 1) && (size
<= 4));
433 return size_swizzles
[size
- 1];
437 glsl_to_tgsi_instruction
*
438 glsl_to_tgsi_visitor::emit_asm(ir_instruction
*ir
, enum tgsi_opcode op
,
439 st_dst_reg dst
, st_dst_reg dst1
,
440 st_src_reg src0
, st_src_reg src1
,
441 st_src_reg src2
, st_src_reg src3
)
443 glsl_to_tgsi_instruction
*inst
= new(mem_ctx
) glsl_to_tgsi_instruction();
444 int num_reladdr
= 0, i
, j
;
445 bool dst_is_64bit
[2];
447 op
= get_opcode(op
, dst
, src0
, src1
);
449 /* If we have to do relative addressing, we want to load the ARL
450 * reg directly for one of the regs, and preload the other reladdr
451 * sources into temps.
453 num_reladdr
+= dst
.reladdr
!= NULL
|| dst
.reladdr2
;
454 assert(!dst1
.reladdr
); /* should be lowered in earlier passes */
455 num_reladdr
+= src0
.reladdr
!= NULL
|| src0
.reladdr2
!= NULL
;
456 num_reladdr
+= src1
.reladdr
!= NULL
|| src1
.reladdr2
!= NULL
;
457 num_reladdr
+= src2
.reladdr
!= NULL
|| src2
.reladdr2
!= NULL
;
458 num_reladdr
+= src3
.reladdr
!= NULL
|| src3
.reladdr2
!= NULL
;
460 reladdr_to_temp(ir
, &src3
, &num_reladdr
);
461 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
462 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
463 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
465 if (dst
.reladdr
|| dst
.reladdr2
) {
467 emit_arl(ir
, address_reg
, *dst
.reladdr
);
469 emit_arl(ir
, address_reg2
, *dst
.reladdr2
);
473 assert(num_reladdr
== 0);
475 /* inst->op has only 8 bits. */
476 STATIC_ASSERT(TGSI_OPCODE_LAST
<= 255);
479 inst
->precise
= this->precise
;
480 inst
->info
= tgsi_get_opcode_info(op
);
487 inst
->is_64bit_expanded
= false;
490 inst
->tex_offsets
= NULL
;
491 inst
->tex_offset_num_offset
= 0;
493 inst
->tex_shadow
= 0;
494 /* default to float, for paths where this is not initialized
495 * (since 0==UINT which is likely wrong):
497 inst
->tex_type
= GLSL_TYPE_FLOAT
;
499 /* Update indirect addressing status used by TGSI */
500 if (dst
.reladdr
|| dst
.reladdr2
) {
502 case PROGRAM_STATE_VAR
:
503 case PROGRAM_CONSTANT
:
504 case PROGRAM_UNIFORM
:
505 this->indirect_addr_consts
= true;
507 case PROGRAM_IMMEDIATE
:
508 assert(!"immediates should not have indirect addressing");
515 for (i
= 0; i
< 4; i
++) {
516 if (inst
->src
[i
].reladdr
) {
517 switch (inst
->src
[i
].file
) {
518 case PROGRAM_STATE_VAR
:
519 case PROGRAM_CONSTANT
:
520 case PROGRAM_UNIFORM
:
521 this->indirect_addr_consts
= true;
523 case PROGRAM_IMMEDIATE
:
524 assert(!"immediates should not have indirect addressing");
534 * This section contains the double processing.
535 * GLSL just represents doubles as single channel values,
536 * however most HW and TGSI represent doubles as pairs of register channels.
538 * so we have to fixup destination writemask/index and src swizzle/indexes.
539 * dest writemasks need to translate from single channel write mask
540 * to a dual-channel writemask, but also need to modify the index,
541 * if we are touching the Z,W fields in the pre-translated writemask.
543 * src channels have similiar index modifications along with swizzle
544 * changes to we pick the XY, ZW pairs from the correct index.
546 * GLSL [0].x -> TGSI [0].xy
547 * GLSL [0].y -> TGSI [0].zw
548 * GLSL [0].z -> TGSI [1].xy
549 * GLSL [0].w -> TGSI [1].zw
551 for (j
= 0; j
< 2; j
++) {
552 dst_is_64bit
[j
] = glsl_base_type_is_64bit(inst
->dst
[j
].type
);
553 if (!dst_is_64bit
[j
] && inst
->dst
[j
].file
== PROGRAM_OUTPUT
&&
554 inst
->dst
[j
].type
== GLSL_TYPE_ARRAY
) {
555 enum glsl_base_type type
= find_array_type(this->outputs
,
557 inst
->dst
[j
].array_id
);
558 if (glsl_base_type_is_64bit(type
))
559 dst_is_64bit
[j
] = true;
563 if (dst_is_64bit
[0] || dst_is_64bit
[1] ||
564 glsl_base_type_is_64bit(inst
->src
[0].type
)) {
565 glsl_to_tgsi_instruction
*dinst
= NULL
;
566 int initial_src_swz
[4], initial_src_idx
[4];
567 int initial_dst_idx
[2], initial_dst_writemask
[2];
568 /* select the writemask for dst0 or dst1 */
569 unsigned writemask
= inst
->dst
[1].file
== PROGRAM_UNDEFINED
570 ? inst
->dst
[0].writemask
: inst
->dst
[1].writemask
;
572 /* copy out the writemask, index and swizzles for all src/dsts. */
573 for (j
= 0; j
< 2; j
++) {
574 initial_dst_writemask
[j
] = inst
->dst
[j
].writemask
;
575 initial_dst_idx
[j
] = inst
->dst
[j
].index
;
578 for (j
= 0; j
< 4; j
++) {
579 initial_src_swz
[j
] = inst
->src
[j
].swizzle
;
580 initial_src_idx
[j
] = inst
->src
[j
].index
;
584 * scan all the components in the dst writemask
585 * generate an instruction for each of them if required.
590 int i
= u_bit_scan(&writemask
);
592 /* before emitting the instruction, see if we have to adjust
593 * load / store address */
594 if (i
> 1 && (inst
->op
== TGSI_OPCODE_LOAD
||
595 inst
->op
== TGSI_OPCODE_STORE
) &&
596 addr
.file
== PROGRAM_UNDEFINED
) {
597 /* We have to advance the buffer address by 16 */
598 addr
= get_temp(glsl_type::uint_type
);
599 emit_asm(ir
, TGSI_OPCODE_UADD
, st_dst_reg(addr
),
600 inst
->src
[0], st_src_reg_for_int(16));
603 /* first time use previous instruction */
607 /* create a new instructions for subsequent attempts */
608 dinst
= new(mem_ctx
) glsl_to_tgsi_instruction();
613 this->instructions
.push_tail(dinst
);
614 dinst
->is_64bit_expanded
= true;
616 /* modify the destination if we are splitting */
617 for (j
= 0; j
< 2; j
++) {
618 if (dst_is_64bit
[j
]) {
619 dinst
->dst
[j
].writemask
= (i
& 1) ? WRITEMASK_ZW
: WRITEMASK_XY
;
620 dinst
->dst
[j
].index
= initial_dst_idx
[j
];
622 if (dinst
->op
== TGSI_OPCODE_LOAD
||
623 dinst
->op
== TGSI_OPCODE_STORE
)
624 dinst
->src
[0] = addr
;
625 if (dinst
->op
!= TGSI_OPCODE_STORE
)
626 dinst
->dst
[j
].index
++;
629 /* if we aren't writing to a double, just get the bit of the
630 * initial writemask for this channel
632 dinst
->dst
[j
].writemask
= initial_dst_writemask
[j
] & (1 << i
);
636 /* modify the src registers */
637 for (j
= 0; j
< 4; j
++) {
638 int swz
= GET_SWZ(initial_src_swz
[j
], i
);
640 if (glsl_base_type_is_64bit(dinst
->src
[j
].type
)) {
641 dinst
->src
[j
].index
= initial_src_idx
[j
];
643 dinst
->src
[j
].double_reg2
= true;
644 dinst
->src
[j
].index
++;
648 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_W
,
649 SWIZZLE_Z
, SWIZZLE_W
);
651 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
652 SWIZZLE_X
, SWIZZLE_Y
);
655 /* some opcodes are special case in what they use as sources
656 * - [FUI]2D/[UI]2I64 is a float/[u]int src0, (D)LDEXP is
659 if (op
== TGSI_OPCODE_F2D
|| op
== TGSI_OPCODE_U2D
||
660 op
== TGSI_OPCODE_I2D
||
661 op
== TGSI_OPCODE_I2I64
|| op
== TGSI_OPCODE_U2I64
||
662 op
== TGSI_OPCODE_DLDEXP
|| op
== TGSI_OPCODE_LDEXP
||
663 (op
== TGSI_OPCODE_UCMP
&& dst_is_64bit
[0])) {
664 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(swz
, swz
, swz
, swz
);
671 this->instructions
.push_tail(inst
);
678 glsl_to_tgsi_instruction
*
679 glsl_to_tgsi_visitor::emit_asm(ir_instruction
*ir
, enum tgsi_opcode op
,
681 st_src_reg src0
, st_src_reg src1
,
682 st_src_reg src2
, st_src_reg src3
)
684 return emit_asm(ir
, op
, dst
, undef_dst
, src0
, src1
, src2
, src3
);
688 * Determines whether to use an integer, unsigned integer, or float opcode
689 * based on the operands and input opcode, then emits the result.
692 glsl_to_tgsi_visitor::get_opcode(enum tgsi_opcode op
,
694 st_src_reg src0
, st_src_reg src1
)
696 enum glsl_base_type type
= GLSL_TYPE_FLOAT
;
698 if (op
== TGSI_OPCODE_MOV
)
701 assert(src0
.type
!= GLSL_TYPE_ARRAY
);
702 assert(src0
.type
!= GLSL_TYPE_STRUCT
);
703 assert(src1
.type
!= GLSL_TYPE_ARRAY
);
704 assert(src1
.type
!= GLSL_TYPE_STRUCT
);
706 if (is_resource_instruction(op
))
708 else if (src0
.type
== GLSL_TYPE_INT64
|| src1
.type
== GLSL_TYPE_INT64
)
709 type
= GLSL_TYPE_INT64
;
710 else if (src0
.type
== GLSL_TYPE_UINT64
|| src1
.type
== GLSL_TYPE_UINT64
)
711 type
= GLSL_TYPE_UINT64
;
712 else if (src0
.type
== GLSL_TYPE_DOUBLE
|| src1
.type
== GLSL_TYPE_DOUBLE
)
713 type
= GLSL_TYPE_DOUBLE
;
714 else if (src0
.type
== GLSL_TYPE_FLOAT
|| src1
.type
== GLSL_TYPE_FLOAT
)
715 type
= GLSL_TYPE_FLOAT
;
716 else if (native_integers
)
717 type
= src0
.type
== GLSL_TYPE_BOOL
? GLSL_TYPE_INT
: src0
.type
;
719 #define case7(c, f, i, u, d, i64, ui64) \
720 case TGSI_OPCODE_##c: \
721 if (type == GLSL_TYPE_UINT64) \
722 op = TGSI_OPCODE_##ui64; \
723 else if (type == GLSL_TYPE_INT64) \
724 op = TGSI_OPCODE_##i64; \
725 else if (type == GLSL_TYPE_DOUBLE) \
726 op = TGSI_OPCODE_##d; \
727 else if (type == GLSL_TYPE_INT) \
728 op = TGSI_OPCODE_##i; \
729 else if (type == GLSL_TYPE_UINT) \
730 op = TGSI_OPCODE_##u; \
732 op = TGSI_OPCODE_##f; \
735 #define casecomp(c, f, i, u, d, i64, ui64) \
736 case TGSI_OPCODE_##c: \
737 if (type == GLSL_TYPE_INT64) \
738 op = TGSI_OPCODE_##i64; \
739 else if (type == GLSL_TYPE_UINT64) \
740 op = TGSI_OPCODE_##ui64; \
741 else if (type == GLSL_TYPE_DOUBLE) \
742 op = TGSI_OPCODE_##d; \
743 else if (type == GLSL_TYPE_INT || type == GLSL_TYPE_SUBROUTINE) \
744 op = TGSI_OPCODE_##i; \
745 else if (type == GLSL_TYPE_UINT) \
746 op = TGSI_OPCODE_##u; \
747 else if (native_integers) \
748 op = TGSI_OPCODE_##f; \
750 op = TGSI_OPCODE_##c; \
754 /* Some instructions are initially selected without considering the type.
755 * This fixes the type:
757 * INIT FLOAT SINT UINT DOUBLE SINT64 UINT64
759 case7(ADD
, ADD
, UADD
, UADD
, DADD
, U64ADD
, U64ADD
);
760 case7(CEIL
, CEIL
, LAST
, LAST
, DCEIL
, LAST
, LAST
);
761 case7(DIV
, DIV
, IDIV
, UDIV
, DDIV
, I64DIV
, U64DIV
);
762 case7(FMA
, FMA
, UMAD
, UMAD
, DFMA
, LAST
, LAST
);
763 case7(FLR
, FLR
, LAST
, LAST
, DFLR
, LAST
, LAST
);
764 case7(FRC
, FRC
, LAST
, LAST
, DFRAC
, LAST
, LAST
);
765 case7(MUL
, MUL
, UMUL
, UMUL
, DMUL
, U64MUL
, U64MUL
);
766 case7(MAD
, MAD
, UMAD
, UMAD
, DMAD
, LAST
, LAST
);
767 case7(MAX
, MAX
, IMAX
, UMAX
, DMAX
, I64MAX
, U64MAX
);
768 case7(MIN
, MIN
, IMIN
, UMIN
, DMIN
, I64MIN
, U64MIN
);
769 case7(RCP
, RCP
, LAST
, LAST
, DRCP
, LAST
, LAST
);
770 case7(ROUND
, ROUND
,LAST
, LAST
, DROUND
, LAST
, LAST
);
771 case7(RSQ
, RSQ
, LAST
, LAST
, DRSQ
, LAST
, LAST
);
772 case7(SQRT
, SQRT
, LAST
, LAST
, DSQRT
, LAST
, LAST
);
773 case7(SSG
, SSG
, ISSG
, ISSG
, DSSG
, I64SSG
, I64SSG
);
774 case7(TRUNC
, TRUNC
,LAST
, LAST
, DTRUNC
, LAST
, LAST
);
776 case7(MOD
, LAST
, MOD
, UMOD
, LAST
, I64MOD
, U64MOD
);
777 case7(SHL
, LAST
, SHL
, SHL
, LAST
, U64SHL
, U64SHL
);
778 case7(IBFE
, LAST
, IBFE
, UBFE
, LAST
, LAST
, LAST
);
779 case7(IMSB
, LAST
, IMSB
, UMSB
, LAST
, LAST
, LAST
);
780 case7(IMUL_HI
, LAST
, IMUL_HI
, UMUL_HI
, LAST
, LAST
, LAST
);
781 case7(ISHR
, LAST
, ISHR
, USHR
, LAST
, I64SHR
, U64SHR
);
782 case7(ATOMIMAX
,LAST
, ATOMIMAX
,ATOMUMAX
,LAST
, LAST
, LAST
);
783 case7(ATOMIMIN
,LAST
, ATOMIMIN
,ATOMUMIN
,LAST
, LAST
, LAST
);
784 case7(ATOMUADD
,ATOMFADD
,ATOMUADD
,ATOMUADD
,LAST
, LAST
, LAST
);
786 casecomp(SEQ
, FSEQ
, USEQ
, USEQ
, DSEQ
, U64SEQ
, U64SEQ
);
787 casecomp(SNE
, FSNE
, USNE
, USNE
, DSNE
, U64SNE
, U64SNE
);
788 casecomp(SGE
, FSGE
, ISGE
, USGE
, DSGE
, I64SGE
, U64SGE
);
789 casecomp(SLT
, FSLT
, ISLT
, USLT
, DSLT
, I64SLT
, U64SLT
);
795 assert(op
!= TGSI_OPCODE_LAST
);
799 glsl_to_tgsi_instruction
*
800 glsl_to_tgsi_visitor::emit_dp(ir_instruction
*ir
,
801 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
,
804 static const enum tgsi_opcode dot_opcodes
[] = {
805 TGSI_OPCODE_DP2
, TGSI_OPCODE_DP3
, TGSI_OPCODE_DP4
808 return emit_asm(ir
, dot_opcodes
[elements
- 2], dst
, src0
, src1
);
812 * Emits TGSI scalar opcodes to produce unique answers across channels.
814 * Some TGSI opcodes are scalar-only, like ARB_fp/vp. The src X
815 * channel determines the result across all channels. So to do a vec4
816 * of this operation, we want to emit a scalar per source channel used
817 * to produce dest channels.
820 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, enum tgsi_opcode op
,
822 st_src_reg orig_src0
, st_src_reg orig_src1
)
825 int done_mask
= ~dst
.writemask
;
827 /* TGSI RCP is a scalar operation splatting results to all channels,
828 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
831 for (i
= 0; i
< 4; i
++) {
832 GLuint this_mask
= (1 << i
);
833 st_src_reg src0
= orig_src0
;
834 st_src_reg src1
= orig_src1
;
836 if (done_mask
& this_mask
)
839 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
840 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
841 for (j
= i
+ 1; j
< 4; j
++) {
842 /* If there is another enabled component in the destination that is
843 * derived from the same inputs, generate its value on this pass as
846 if (!(done_mask
& (1 << j
)) &&
847 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
848 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
849 this_mask
|= (1 << j
);
852 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
853 src0_swiz
, src0_swiz
);
854 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
855 src1_swiz
, src1_swiz
);
857 dst
.writemask
= this_mask
;
858 emit_asm(ir
, op
, dst
, src0
, src1
);
859 done_mask
|= this_mask
;
864 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, enum tgsi_opcode op
,
865 st_dst_reg dst
, st_src_reg src0
)
867 st_src_reg undef
= undef_src
;
869 undef
.swizzle
= SWIZZLE_XXXX
;
871 emit_scalar(ir
, op
, dst
, src0
, undef
);
875 glsl_to_tgsi_visitor::emit_arl(ir_instruction
*ir
,
876 st_dst_reg dst
, st_src_reg src0
)
878 enum tgsi_opcode op
= TGSI_OPCODE_ARL
;
880 if (src0
.type
== GLSL_TYPE_INT
|| src0
.type
== GLSL_TYPE_UINT
) {
881 if (!this->need_uarl
&& src0
.is_legal_tgsi_address_operand())
884 op
= TGSI_OPCODE_UARL
;
887 assert(dst
.file
== PROGRAM_ADDRESS
);
888 if (dst
.index
>= this->num_address_regs
)
889 this->num_address_regs
= dst
.index
+ 1;
891 emit_asm(NULL
, op
, dst
, src0
);
895 glsl_to_tgsi_visitor::add_constant(gl_register_file file
,
896 gl_constant_value values
[8], int size
,
898 uint16_t *swizzle_out
)
900 if (file
== PROGRAM_CONSTANT
) {
901 GLuint swizzle
= swizzle_out
? *swizzle_out
: 0;
902 int result
= _mesa_add_typed_unnamed_constant(this->prog
->Parameters
,
903 values
, size
, datatype
,
906 *swizzle_out
= swizzle
;
910 assert(file
== PROGRAM_IMMEDIATE
);
913 immediate_storage
*entry
;
914 int size32
= size
* ((datatype
== GL_DOUBLE
||
915 datatype
== GL_INT64_ARB
||
916 datatype
== GL_UNSIGNED_INT64_ARB
) ? 2 : 1);
919 /* Search immediate storage to see if we already have an identical
920 * immediate that we can use instead of adding a duplicate entry.
922 foreach_in_list(immediate_storage
, entry
, &this->immediates
) {
923 immediate_storage
*tmp
= entry
;
925 for (i
= 0; i
* 4 < size32
; i
++) {
926 int slot_size
= MIN2(size32
- (i
* 4), 4);
927 if (tmp
->type
!= datatype
|| tmp
->size32
!= slot_size
)
929 if (memcmp(tmp
->values
, &values
[i
* 4],
930 slot_size
* sizeof(gl_constant_value
)))
933 /* Everything matches, keep going until the full size is matched */
934 tmp
= (immediate_storage
*)tmp
->next
;
937 /* The full value matched */
944 for (i
= 0; i
* 4 < size32
; i
++) {
945 int slot_size
= MIN2(size32
- (i
* 4), 4);
946 /* Add this immediate to the list. */
947 entry
= new(mem_ctx
) immediate_storage(&values
[i
* 4],
948 slot_size
, datatype
);
949 this->immediates
.push_tail(entry
);
950 this->num_immediates
++;
956 glsl_to_tgsi_visitor::st_src_reg_for_float(float val
)
958 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_FLOAT
);
959 union gl_constant_value uval
;
962 src
.index
= add_constant(src
.file
, &uval
, 1, GL_FLOAT
, &src
.swizzle
);
968 glsl_to_tgsi_visitor::st_src_reg_for_double(double val
)
970 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_DOUBLE
);
971 union gl_constant_value uval
[2];
973 memcpy(uval
, &val
, sizeof(uval
));
974 src
.index
= add_constant(src
.file
, uval
, 1, GL_DOUBLE
, &src
.swizzle
);
975 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
980 glsl_to_tgsi_visitor::st_src_reg_for_int(int val
)
982 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_INT
);
983 union gl_constant_value uval
;
985 assert(native_integers
);
988 src
.index
= add_constant(src
.file
, &uval
, 1, GL_INT
, &src
.swizzle
);
994 glsl_to_tgsi_visitor::st_src_reg_for_int64(int64_t val
)
996 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_INT64
);
997 union gl_constant_value uval
[2];
999 memcpy(uval
, &val
, sizeof(uval
));
1000 src
.index
= add_constant(src
.file
, uval
, 1, GL_DOUBLE
, &src
.swizzle
);
1001 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
1007 glsl_to_tgsi_visitor::st_src_reg_for_type(enum glsl_base_type type
, int val
)
1009 if (native_integers
)
1010 return type
== GLSL_TYPE_FLOAT
? st_src_reg_for_float(val
) :
1011 st_src_reg_for_int(val
);
1013 return st_src_reg_for_float(val
);
1017 attrib_type_size(const struct glsl_type
*type
, bool is_vs_input
)
1019 return type
->count_attribute_slots(is_vs_input
);
1023 type_size(const struct glsl_type
*type
)
1025 return type
->count_attribute_slots(false);
1029 add_buffer_to_load_and_stores(glsl_to_tgsi_instruction
*inst
, st_src_reg
*buf
,
1030 exec_list
*instructions
, ir_constant
*access
)
1033 * emit_asm() might have actually split the op into pieces, e.g. for
1034 * double stores. We have to go back and fix up all the generated ops.
1036 enum tgsi_opcode op
= inst
->op
;
1038 inst
->resource
= *buf
;
1040 inst
->buffer_access
= access
->value
.u
[0];
1042 if (inst
== instructions
->get_head_raw())
1044 inst
= (glsl_to_tgsi_instruction
*)inst
->get_prev();
1046 if (inst
->op
== TGSI_OPCODE_UADD
) {
1047 if (inst
== instructions
->get_head_raw())
1049 inst
= (glsl_to_tgsi_instruction
*)inst
->get_prev();
1051 } while (inst
->op
== op
&& inst
->resource
.file
== PROGRAM_UNDEFINED
);
1055 * If the given GLSL type is an array or matrix or a structure containing
1056 * an array/matrix member, return true. Else return false.
1058 * This is used to determine which kind of temp storage (PROGRAM_TEMPORARY
1059 * or PROGRAM_ARRAY) should be used for variables of this type. Anytime
1060 * we have an array that might be indexed with a variable, we need to use
1061 * the later storage type.
1064 type_has_array_or_matrix(const glsl_type
*type
)
1066 if (type
->is_array() || type
->is_matrix())
1069 if (type
->is_struct()) {
1070 for (unsigned i
= 0; i
< type
->length
; i
++) {
1071 if (type_has_array_or_matrix(type
->fields
.structure
[i
].type
)) {
1082 * In the initial pass of codegen, we assign temporary numbers to
1083 * intermediate results. (not SSA -- variable assignments will reuse
1087 glsl_to_tgsi_visitor::get_temp(const glsl_type
*type
)
1091 src
.type
= native_integers
? type
->base_type
: GLSL_TYPE_FLOAT
;
1096 if (!options
->EmitNoIndirectTemp
&& type_has_array_or_matrix(type
)) {
1097 if (next_array
>= max_num_arrays
) {
1098 max_num_arrays
+= 32;
1099 array_sizes
= (unsigned*)
1100 realloc(array_sizes
, sizeof(array_sizes
[0]) * max_num_arrays
);
1103 src
.file
= PROGRAM_ARRAY
;
1105 src
.array_id
= next_array
+ 1;
1106 array_sizes
[next_array
] = type_size(type
);
1110 src
.file
= PROGRAM_TEMPORARY
;
1111 src
.index
= next_temp
;
1112 next_temp
+= type_size(type
);
1115 if (type
->is_array() || type
->is_struct()) {
1116 src
.swizzle
= SWIZZLE_NOOP
;
1118 src
.swizzle
= swizzle_for_size(type
->vector_elements
);
1125 glsl_to_tgsi_visitor::find_variable_storage(ir_variable
*var
)
1127 struct hash_entry
*entry
;
1129 entry
= _mesa_hash_table_search(this->variables
, var
);
1133 return (variable_storage
*)entry
->data
;
1137 glsl_to_tgsi_visitor::visit(ir_variable
*ir
)
1139 if (ir
->data
.mode
== ir_var_uniform
&& strncmp(ir
->name
, "gl_", 3) == 0) {
1141 const ir_state_slot
*const slots
= ir
->get_state_slots();
1142 assert(slots
!= NULL
);
1144 /* Check if this statevar's setup in the STATE file exactly
1145 * matches how we'll want to reference it as a
1146 * struct/array/whatever. If not, then we need to move it into
1147 * temporary storage and hope that it'll get copy-propagated
1150 for (i
= 0; i
< ir
->get_num_state_slots(); i
++) {
1151 if (slots
[i
].swizzle
!= SWIZZLE_XYZW
) {
1156 variable_storage
*storage
;
1158 if (i
== ir
->get_num_state_slots()) {
1159 /* We'll set the index later. */
1160 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_STATE_VAR
, -1);
1162 _mesa_hash_table_insert(this->variables
, ir
, storage
);
1166 /* The variable_storage constructor allocates slots based on the size
1167 * of the type. However, this had better match the number of state
1168 * elements that we're going to copy into the new temporary.
1170 assert((int) ir
->get_num_state_slots() == type_size(ir
->type
));
1172 dst
= st_dst_reg(get_temp(ir
->type
));
1174 storage
= new(mem_ctx
) variable_storage(ir
, dst
.file
, dst
.index
,
1177 _mesa_hash_table_insert(this->variables
, ir
, storage
);
1181 for (unsigned int i
= 0; i
< ir
->get_num_state_slots(); i
++) {
1182 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
1185 if (storage
->file
== PROGRAM_STATE_VAR
) {
1186 if (storage
->index
== -1) {
1187 storage
->index
= index
;
1189 assert(index
== storage
->index
+ (int)i
);
1192 /* We use GLSL_TYPE_FLOAT here regardless of the actual type of
1193 * the data being moved since MOV does not care about the type of
1194 * data it is moving, and we don't want to declare registers with
1195 * array or struct types.
1197 st_src_reg
src(PROGRAM_STATE_VAR
, index
, GLSL_TYPE_FLOAT
);
1198 src
.swizzle
= slots
[i
].swizzle
;
1199 emit_asm(ir
, TGSI_OPCODE_MOV
, dst
, src
);
1200 /* even a float takes up a whole vec4 reg in a struct/array. */
1205 if (storage
->file
== PROGRAM_TEMPORARY
&&
1206 dst
.index
!= storage
->index
+ (int) ir
->get_num_state_slots()) {
1207 fail_link(this->shader_program
,
1208 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
1209 ir
->name
, dst
.index
- storage
->index
,
1210 type_size(ir
->type
));
1216 glsl_to_tgsi_visitor::visit(ir_loop
*ir
)
1218 emit_asm(NULL
, TGSI_OPCODE_BGNLOOP
);
1220 visit_exec_list(&ir
->body_instructions
, this);
1222 emit_asm(NULL
, TGSI_OPCODE_ENDLOOP
);
1226 glsl_to_tgsi_visitor::visit(ir_loop_jump
*ir
)
1229 case ir_loop_jump::jump_break
:
1230 emit_asm(NULL
, TGSI_OPCODE_BRK
);
1232 case ir_loop_jump::jump_continue
:
1233 emit_asm(NULL
, TGSI_OPCODE_CONT
);
1240 glsl_to_tgsi_visitor::visit(ir_function_signature
*ir
)
1247 glsl_to_tgsi_visitor::visit(ir_function
*ir
)
1249 /* Ignore function bodies other than main() -- we shouldn't see calls to
1250 * them since they should all be inlined before we get to glsl_to_tgsi.
1252 if (strcmp(ir
->name
, "main") == 0) {
1253 const ir_function_signature
*sig
;
1256 sig
= ir
->matching_signature(NULL
, &empty
, false);
1260 foreach_in_list(ir_instruction
, ir
, &sig
->body
) {
1267 glsl_to_tgsi_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
1269 int nonmul_operand
= 1 - mul_operand
;
1271 st_dst_reg result_dst
;
1273 // there is no TGSI opcode for this
1274 if (ir
->type
->is_integer_64())
1277 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
1278 if (!expr
|| expr
->operation
!= ir_binop_mul
)
1281 expr
->operands
[0]->accept(this);
1283 expr
->operands
[1]->accept(this);
1285 ir
->operands
[nonmul_operand
]->accept(this);
1288 this->result
= get_temp(ir
->type
);
1289 result_dst
= st_dst_reg(this->result
);
1290 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1291 emit_asm(ir
, TGSI_OPCODE_MAD
, result_dst
, a
, b
, c
);
1297 * Emit MAD(a, -b, a) instead of AND(a, NOT(b))
1299 * The logic values are 1.0 for true and 0.0 for false. Logical-and is
1300 * implemented using multiplication, and logical-or is implemented using
1301 * addition. Logical-not can be implemented as (true - x), or (1.0 - x).
1302 * As result, the logical expression (a & !b) can be rewritten as:
1306 * - (a * 1) - (a * b)
1310 * This final expression can be implemented as a single MAD(a, -b, a)
1314 glsl_to_tgsi_visitor::try_emit_mad_for_and_not(ir_expression
*ir
,
1317 const int other_operand
= 1 - try_operand
;
1320 ir_expression
*expr
= ir
->operands
[try_operand
]->as_expression();
1321 if (!expr
|| expr
->operation
!= ir_unop_logic_not
)
1324 ir
->operands
[other_operand
]->accept(this);
1326 expr
->operands
[0]->accept(this);
1329 b
.negate
= ~b
.negate
;
1331 this->result
= get_temp(ir
->type
);
1332 emit_asm(ir
, TGSI_OPCODE_MAD
, st_dst_reg(this->result
), a
, b
, a
);
1338 glsl_to_tgsi_visitor::reladdr_to_temp(ir_instruction
*ir
,
1339 st_src_reg
*reg
, int *num_reladdr
)
1341 if (!reg
->reladdr
&& !reg
->reladdr2
)
1345 emit_arl(ir
, address_reg
, *reg
->reladdr
);
1347 emit_arl(ir
, address_reg2
, *reg
->reladdr2
);
1349 if (*num_reladdr
!= 1) {
1350 st_src_reg temp
= get_temp(glsl_type::get_instance(reg
->type
, 4, 1));
1352 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), *reg
);
1360 glsl_to_tgsi_visitor::visit(ir_expression
*ir
)
1362 st_src_reg op
[ARRAY_SIZE(ir
->operands
)];
1364 /* Quick peephole: Emit MAD(a, b, c) instead of ADD(MUL(a, b), c)
1366 if (!this->precise
&& ir
->operation
== ir_binop_add
) {
1367 if (try_emit_mad(ir
, 1))
1369 if (try_emit_mad(ir
, 0))
1373 /* Quick peephole: Emit OPCODE_MAD(-a, -b, a) instead of AND(a, NOT(b))
1375 if (!native_integers
&& ir
->operation
== ir_binop_logic_and
) {
1376 if (try_emit_mad_for_and_not(ir
, 1))
1378 if (try_emit_mad_for_and_not(ir
, 0))
1382 if (ir
->operation
== ir_quadop_vector
)
1383 assert(!"ir_quadop_vector should have been lowered");
1385 for (unsigned int operand
= 0; operand
< ir
->num_operands
; operand
++) {
1386 this->result
.file
= PROGRAM_UNDEFINED
;
1387 ir
->operands
[operand
]->accept(this);
1388 if (this->result
.file
== PROGRAM_UNDEFINED
) {
1389 printf("Failed to get tree for expression operand:\n");
1390 ir
->operands
[operand
]->print();
1394 op
[operand
] = this->result
;
1396 /* Matrix expression operands should have been broken down to vector
1397 * operations already.
1399 assert(!ir
->operands
[operand
]->type
->is_matrix());
1402 visit_expression(ir
, op
);
1405 /* The non-recursive part of the expression visitor lives in a separate
1406 * function and should be prevented from being inlined, to avoid a stack
1407 * explosion when deeply nested expressions are visited.
1410 glsl_to_tgsi_visitor::visit_expression(ir_expression
* ir
, st_src_reg
*op
)
1412 st_src_reg result_src
;
1413 st_dst_reg result_dst
;
1415 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1416 if (ir
->operands
[1] &&
1417 ir
->operation
!= ir_binop_interpolate_at_offset
&&
1418 ir
->operation
!= ir_binop_interpolate_at_sample
) {
1419 st_src_reg
*swz_op
= NULL
;
1420 if (vector_elements
> ir
->operands
[1]->type
->vector_elements
) {
1421 assert(ir
->operands
[1]->type
->vector_elements
== 1);
1423 } else if (vector_elements
< ir
->operands
[1]->type
->vector_elements
) {
1424 assert(ir
->operands
[0]->type
->vector_elements
== 1);
1428 uint16_t swizzle_x
= GET_SWZ(swz_op
->swizzle
, 0);
1429 swz_op
->swizzle
= MAKE_SWIZZLE4(swizzle_x
, swizzle_x
,
1430 swizzle_x
, swizzle_x
);
1432 vector_elements
= MAX2(vector_elements
,
1433 ir
->operands
[1]->type
->vector_elements
);
1435 if (ir
->operands
[2] &&
1436 ir
->operands
[2]->type
->vector_elements
!= vector_elements
) {
1437 /* This can happen with ir_triop_lrp, i.e. glsl mix */
1438 assert(ir
->operands
[2]->type
->vector_elements
== 1);
1439 uint16_t swizzle_x
= GET_SWZ(op
[2].swizzle
, 0);
1440 op
[2].swizzle
= MAKE_SWIZZLE4(swizzle_x
, swizzle_x
,
1441 swizzle_x
, swizzle_x
);
1444 this->result
.file
= PROGRAM_UNDEFINED
;
1446 /* Storage for our result. Ideally for an assignment we'd be using
1447 * the actual storage for the result here, instead.
1449 result_src
= get_temp(ir
->type
);
1450 /* convenience for the emit functions below. */
1451 result_dst
= st_dst_reg(result_src
);
1452 /* Limit writes to the channels that will be used by result_src later.
1453 * This does limit this temp's use as a temporary for multi-instruction
1456 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1458 switch (ir
->operation
) {
1459 case ir_unop_logic_not
:
1460 if (result_dst
.type
!= GLSL_TYPE_FLOAT
)
1461 emit_asm(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1463 /* Previously 'SEQ dst, src, 0.0' was used for this. However, many
1464 * older GPUs implement SEQ using multiple instructions (i915 uses two
1465 * SGE instructions and a MUL instruction). Since our logic values are
1466 * 0.0 and 1.0, 1-x also implements !x.
1468 op
[0].negate
= ~op
[0].negate
;
1469 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0],
1470 st_src_reg_for_float(1.0));
1474 if (result_dst
.type
== GLSL_TYPE_INT64
||
1475 result_dst
.type
== GLSL_TYPE_UINT64
)
1476 emit_asm(ir
, TGSI_OPCODE_I64NEG
, result_dst
, op
[0]);
1477 else if (result_dst
.type
== GLSL_TYPE_INT
||
1478 result_dst
.type
== GLSL_TYPE_UINT
)
1479 emit_asm(ir
, TGSI_OPCODE_INEG
, result_dst
, op
[0]);
1480 else if (result_dst
.type
== GLSL_TYPE_DOUBLE
)
1481 emit_asm(ir
, TGSI_OPCODE_DNEG
, result_dst
, op
[0]);
1483 op
[0].negate
= ~op
[0].negate
;
1487 case ir_unop_subroutine_to_int
:
1488 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
1491 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1492 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0].get_abs());
1493 else if (result_dst
.type
== GLSL_TYPE_DOUBLE
)
1494 emit_asm(ir
, TGSI_OPCODE_DABS
, result_dst
, op
[0]);
1495 else if (result_dst
.type
== GLSL_TYPE_INT64
||
1496 result_dst
.type
== GLSL_TYPE_UINT64
)
1497 emit_asm(ir
, TGSI_OPCODE_I64ABS
, result_dst
, op
[0]);
1499 emit_asm(ir
, TGSI_OPCODE_IABS
, result_dst
, op
[0]);
1502 emit_asm(ir
, TGSI_OPCODE_SSG
, result_dst
, op
[0]);
1505 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, op
[0]);
1509 emit_scalar(ir
, TGSI_OPCODE_EX2
, result_dst
, op
[0]);
1512 assert(!"not reached: should be handled by exp_to_exp2");
1515 assert(!"not reached: should be handled by log_to_log2");
1518 emit_scalar(ir
, TGSI_OPCODE_LG2
, result_dst
, op
[0]);
1521 emit_scalar(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1524 emit_scalar(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1526 case ir_unop_saturate
: {
1527 glsl_to_tgsi_instruction
*inst
;
1528 inst
= emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
1529 inst
->saturate
= true;
1534 case ir_unop_dFdx_coarse
:
1535 emit_asm(ir
, TGSI_OPCODE_DDX
, result_dst
, op
[0]);
1537 case ir_unop_dFdx_fine
:
1538 emit_asm(ir
, TGSI_OPCODE_DDX_FINE
, result_dst
, op
[0]);
1541 case ir_unop_dFdy_coarse
:
1542 case ir_unop_dFdy_fine
:
1544 /* The X component contains 1 or -1 depending on whether the framebuffer
1545 * is a FBO or the window system buffer, respectively.
1546 * It is then multiplied with the source operand of DDY.
1548 static const gl_state_index16 transform_y_state
[STATE_LENGTH
]
1549 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
};
1551 unsigned transform_y_index
=
1552 _mesa_add_state_reference(this->prog
->Parameters
,
1555 st_src_reg transform_y
= st_src_reg(PROGRAM_STATE_VAR
,
1557 glsl_type::vec4_type
);
1558 transform_y
.swizzle
= SWIZZLE_XXXX
;
1560 st_src_reg temp
= get_temp(glsl_type::vec4_type
);
1562 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(temp
), transform_y
, op
[0]);
1563 emit_asm(ir
, ir
->operation
== ir_unop_dFdy_fine
?
1564 TGSI_OPCODE_DDY_FINE
: TGSI_OPCODE_DDY
, result_dst
, temp
);
1568 case ir_unop_frexp_sig
:
1569 emit_asm(ir
, TGSI_OPCODE_DFRACEXP
, result_dst
, undef_dst
, op
[0]);
1572 case ir_unop_frexp_exp
:
1573 emit_asm(ir
, TGSI_OPCODE_DFRACEXP
, undef_dst
, result_dst
, op
[0]);
1576 case ir_unop_noise
: {
1577 /* At some point, a motivated person could add a better
1578 * implementation of noise. Currently not even the nvidia
1579 * binary drivers do anything more than this. In any case, the
1580 * place to do this is in the GL state tracker, not the poor
1583 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, st_src_reg_for_float(0.5));
1588 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1591 op
[1].negate
= ~op
[1].negate
;
1592 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1596 emit_asm(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1599 emit_asm(ir
, TGSI_OPCODE_DIV
, result_dst
, op
[0], op
[1]);
1602 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1603 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1605 emit_asm(ir
, TGSI_OPCODE_MOD
, result_dst
, op
[0], op
[1]);
1609 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[0], op
[1]);
1611 case ir_binop_gequal
:
1612 emit_asm(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[0], op
[1]);
1614 case ir_binop_equal
:
1615 emit_asm(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1617 case ir_binop_nequal
:
1618 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1620 case ir_binop_all_equal
:
1621 /* "==" operator producing a scalar boolean. */
1622 if (ir
->operands
[0]->type
->is_vector() ||
1623 ir
->operands
[1]->type
->is_vector()) {
1624 st_src_reg temp
= get_temp(native_integers
?
1625 glsl_type::uvec4_type
:
1626 glsl_type::vec4_type
);
1628 if (native_integers
) {
1629 st_dst_reg temp_dst
= st_dst_reg(temp
);
1630 st_src_reg temp1
= st_src_reg(temp
), temp2
= st_src_reg(temp
);
1632 if (ir
->operands
[0]->type
->is_boolean() &&
1633 ir
->operands
[1]->as_constant() &&
1634 ir
->operands
[1]->as_constant()->is_one()) {
1635 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), op
[0]);
1637 emit_asm(ir
, TGSI_OPCODE_SEQ
, st_dst_reg(temp
), op
[0], op
[1]);
1640 /* Emit 1-3 AND operations to combine the SEQ results. */
1641 switch (ir
->operands
[0]->type
->vector_elements
) {
1645 temp_dst
.writemask
= WRITEMASK_Y
;
1646 temp1
.swizzle
= SWIZZLE_YYYY
;
1647 temp2
.swizzle
= SWIZZLE_ZZZZ
;
1648 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1651 temp_dst
.writemask
= WRITEMASK_X
;
1652 temp1
.swizzle
= SWIZZLE_XXXX
;
1653 temp2
.swizzle
= SWIZZLE_YYYY
;
1654 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1655 temp_dst
.writemask
= WRITEMASK_Y
;
1656 temp1
.swizzle
= SWIZZLE_ZZZZ
;
1657 temp2
.swizzle
= SWIZZLE_WWWW
;
1658 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1661 temp1
.swizzle
= SWIZZLE_XXXX
;
1662 temp2
.swizzle
= SWIZZLE_YYYY
;
1663 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, temp1
, temp2
);
1665 emit_asm(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1667 /* After the dot-product, the value will be an integer on the
1668 * range [0,4]. Zero becomes 1.0, and positive values become zero.
1670 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1672 /* Negating the result of the dot-product gives values on the range
1673 * [-4, 0]. Zero becomes 1.0, and negative values become zero.
1674 * This is achieved using SGE.
1676 st_src_reg sge_src
= result_src
;
1677 sge_src
.negate
= ~sge_src
.negate
;
1678 emit_asm(ir
, TGSI_OPCODE_SGE
, result_dst
, sge_src
,
1679 st_src_reg_for_float(0.0));
1682 emit_asm(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1685 case ir_binop_any_nequal
:
1686 /* "!=" operator producing a scalar boolean. */
1687 if (ir
->operands
[0]->type
->is_vector() ||
1688 ir
->operands
[1]->type
->is_vector()) {
1689 st_src_reg temp
= get_temp(native_integers
?
1690 glsl_type::uvec4_type
:
1691 glsl_type::vec4_type
);
1692 if (ir
->operands
[0]->type
->is_boolean() &&
1693 ir
->operands
[1]->as_constant() &&
1694 ir
->operands
[1]->as_constant()->is_zero()) {
1695 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), op
[0]);
1697 emit_asm(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1700 if (native_integers
) {
1701 st_dst_reg temp_dst
= st_dst_reg(temp
);
1702 st_src_reg temp1
= st_src_reg(temp
), temp2
= st_src_reg(temp
);
1704 /* Emit 1-3 OR operations to combine the SNE results. */
1705 switch (ir
->operands
[0]->type
->vector_elements
) {
1709 temp_dst
.writemask
= WRITEMASK_Y
;
1710 temp1
.swizzle
= SWIZZLE_YYYY
;
1711 temp2
.swizzle
= SWIZZLE_ZZZZ
;
1712 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1715 temp_dst
.writemask
= WRITEMASK_X
;
1716 temp1
.swizzle
= SWIZZLE_XXXX
;
1717 temp2
.swizzle
= SWIZZLE_YYYY
;
1718 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1719 temp_dst
.writemask
= WRITEMASK_Y
;
1720 temp1
.swizzle
= SWIZZLE_ZZZZ
;
1721 temp2
.swizzle
= SWIZZLE_WWWW
;
1722 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1725 temp1
.swizzle
= SWIZZLE_XXXX
;
1726 temp2
.swizzle
= SWIZZLE_YYYY
;
1727 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, temp1
, temp2
);
1729 /* After the dot-product, the value will be an integer on the
1730 * range [0,4]. Zero stays zero, and positive values become 1.0.
1732 glsl_to_tgsi_instruction
*const dp
=
1733 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1734 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1735 /* The clamping to [0,1] can be done for free in the fragment
1736 * shader with a saturate.
1738 dp
->saturate
= true;
1740 /* Negating the result of the dot-product gives values on the
1741 * range [-4, 0]. Zero stays zero, and negative values become
1742 * 1.0. This achieved using SLT.
1744 st_src_reg slt_src
= result_src
;
1745 slt_src
.negate
= ~slt_src
.negate
;
1746 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
,
1747 st_src_reg_for_float(0.0));
1751 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1755 case ir_binop_logic_xor
:
1756 if (native_integers
)
1757 emit_asm(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0], op
[1]);
1759 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1762 case ir_binop_logic_or
: {
1763 if (native_integers
) {
1764 /* If integers are used as booleans, we can use an actual "or"
1767 assert(native_integers
);
1768 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0], op
[1]);
1770 /* After the addition, the value will be an integer on the
1771 * range [0,2]. Zero stays zero, and positive values become 1.0.
1773 glsl_to_tgsi_instruction
*add
=
1774 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1775 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1776 /* The clamping to [0,1] can be done for free in the fragment
1777 * shader with a saturate if floats are being used as boolean
1780 add
->saturate
= true;
1782 /* Negating the result of the addition gives values on the range
1783 * [-2, 0]. Zero stays zero, and negative values become 1.0
1784 * This is achieved using SLT.
1786 st_src_reg slt_src
= result_src
;
1787 slt_src
.negate
= ~slt_src
.negate
;
1788 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
,
1789 st_src_reg_for_float(0.0));
1795 case ir_binop_logic_and
:
1796 /* If native integers are disabled, the bool args are stored as float 0.0
1797 * or 1.0, so "mul" gives us "and". If they're enabled, just use the
1798 * actual AND opcode.
1800 if (native_integers
)
1801 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], op
[1]);
1803 emit_asm(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1807 assert(ir
->operands
[0]->type
->is_vector());
1808 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1809 emit_dp(ir
, result_dst
, op
[0], op
[1],
1810 ir
->operands
[0]->type
->vector_elements
);
1815 emit_scalar(ir
, TGSI_OPCODE_SQRT
, result_dst
, op
[0]);
1817 /* This is the only instruction sequence that makes the game "Risen"
1818 * render correctly. ABS is not required for the game, but since GLSL
1819 * declares negative values as "undefined", allowing us to do whatever
1820 * we want, I choose to use ABS to match DX9 and pre-GLSL RSQ
1823 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0].get_abs());
1824 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, result_src
);
1828 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1831 if (native_integers
) {
1832 emit_asm(ir
, TGSI_OPCODE_I2F
, result_dst
, op
[0]);
1835 /* fallthrough to next case otherwise */
1837 if (native_integers
) {
1838 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0],
1839 st_src_reg_for_float(1.0));
1842 /* fallthrough to next case otherwise */
1845 case ir_unop_i642u64
:
1846 case ir_unop_u642i64
:
1847 /* Converting between signed and unsigned integers is a no-op. */
1849 result_src
.type
= result_dst
.type
;
1852 if (native_integers
) {
1853 /* Booleans are stored as integers using ~0 for true and 0 for false.
1854 * GLSL requires that int(bool) return 1 for true and 0 for false.
1855 * This conversion is done with AND, but it could be done with NEG.
1857 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0],
1858 st_src_reg_for_int(1));
1860 /* Booleans and integers are both stored as floats when native
1861 * integers are disabled.
1867 if (native_integers
)
1868 emit_asm(ir
, TGSI_OPCODE_F2I
, result_dst
, op
[0]);
1870 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1873 if (native_integers
)
1874 emit_asm(ir
, TGSI_OPCODE_F2U
, result_dst
, op
[0]);
1876 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1878 case ir_unop_bitcast_f2i
:
1879 case ir_unop_bitcast_f2u
:
1880 /* Make sure we don't propagate the negate modifier to integer opcodes. */
1881 if (op
[0].negate
|| op
[0].abs
)
1882 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
1885 result_src
.type
= ir
->operation
== ir_unop_bitcast_f2i
? GLSL_TYPE_INT
:
1888 case ir_unop_bitcast_i2f
:
1889 case ir_unop_bitcast_u2f
:
1891 result_src
.type
= GLSL_TYPE_FLOAT
;
1894 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0],
1895 st_src_reg_for_float(0.0));
1898 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0],
1899 st_src_reg_for_double(0.0));
1902 if (native_integers
)
1903 emit_asm(ir
, TGSI_OPCODE_USNE
, result_dst
, op
[0],
1904 st_src_reg_for_int(0));
1906 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0],
1907 st_src_reg_for_float(0.0));
1909 case ir_unop_bitcast_u642d
:
1910 case ir_unop_bitcast_i642d
:
1912 result_src
.type
= GLSL_TYPE_DOUBLE
;
1914 case ir_unop_bitcast_d2i64
:
1916 result_src
.type
= GLSL_TYPE_INT64
;
1918 case ir_unop_bitcast_d2u64
:
1920 result_src
.type
= GLSL_TYPE_UINT64
;
1923 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1926 emit_asm(ir
, TGSI_OPCODE_CEIL
, result_dst
, op
[0]);
1929 emit_asm(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
1931 case ir_unop_round_even
:
1932 emit_asm(ir
, TGSI_OPCODE_ROUND
, result_dst
, op
[0]);
1935 emit_asm(ir
, TGSI_OPCODE_FRC
, result_dst
, op
[0]);
1939 emit_asm(ir
, TGSI_OPCODE_MIN
, result_dst
, op
[0], op
[1]);
1942 emit_asm(ir
, TGSI_OPCODE_MAX
, result_dst
, op
[0], op
[1]);
1945 emit_scalar(ir
, TGSI_OPCODE_POW
, result_dst
, op
[0], op
[1]);
1948 case ir_unop_bit_not
:
1949 if (native_integers
) {
1950 emit_asm(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1954 if (native_integers
) {
1955 emit_asm(ir
, TGSI_OPCODE_U2F
, result_dst
, op
[0]);
1958 case ir_binop_lshift
:
1959 case ir_binop_rshift
:
1960 if (native_integers
) {
1961 enum tgsi_opcode opcode
= ir
->operation
== ir_binop_lshift
1962 ? TGSI_OPCODE_SHL
: TGSI_OPCODE_ISHR
;
1965 if (glsl_base_type_is_64bit(op
[0].type
)) {
1966 /* GLSL shift operations have 32-bit shift counts, but TGSI uses
1969 count
= get_temp(glsl_type::u64vec(ir
->operands
[1]
1970 ->type
->components()));
1971 emit_asm(ir
, TGSI_OPCODE_U2I64
, st_dst_reg(count
), op
[1]);
1976 emit_asm(ir
, opcode
, result_dst
, op
[0], count
);
1979 case ir_binop_bit_and
:
1980 if (native_integers
) {
1981 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], op
[1]);
1984 case ir_binop_bit_xor
:
1985 if (native_integers
) {
1986 emit_asm(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0], op
[1]);
1989 case ir_binop_bit_or
:
1990 if (native_integers
) {
1991 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0], op
[1]);
1995 assert(!"GLSL 1.30 features unsupported");
1998 case ir_binop_ubo_load
: {
1999 if (ctx
->Const
.UseSTD430AsDefaultPacking
) {
2000 ir_rvalue
*block
= ir
->operands
[0];
2001 ir_rvalue
*offset
= ir
->operands
[1];
2002 ir_constant
*const_block
= block
->as_constant();
2004 st_src_reg
cbuf(PROGRAM_CONSTANT
,
2005 (const_block
? const_block
->value
.u
[0] + 1 : 1),
2006 ir
->type
->base_type
);
2008 cbuf
.has_index2
= true;
2011 block
->accept(this);
2012 cbuf
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2013 *cbuf
.reladdr
= this->result
;
2014 emit_arl(ir
, sampler_reladdr
, this->result
);
2017 /* Calculate the surface offset */
2018 offset
->accept(this);
2019 st_src_reg off
= this->result
;
2021 glsl_to_tgsi_instruction
*inst
=
2022 emit_asm(ir
, TGSI_OPCODE_LOAD
, result_dst
, off
);
2024 if (result_dst
.type
== GLSL_TYPE_BOOL
)
2025 emit_asm(ir
, TGSI_OPCODE_USNE
, result_dst
, st_src_reg(result_dst
),
2026 st_src_reg_for_int(0));
2028 add_buffer_to_load_and_stores(inst
, &cbuf
, &this->instructions
,
2031 ir_constant
*const_uniform_block
= ir
->operands
[0]->as_constant();
2032 ir_constant
*const_offset_ir
= ir
->operands
[1]->as_constant();
2033 unsigned const_offset
= const_offset_ir
?
2034 const_offset_ir
->value
.u
[0] : 0;
2035 unsigned const_block
= const_uniform_block
?
2036 const_uniform_block
->value
.u
[0] + 1 : 1;
2037 st_src_reg index_reg
= get_temp(glsl_type::uint_type
);
2040 cbuf
.type
= ir
->type
->base_type
;
2041 cbuf
.file
= PROGRAM_CONSTANT
;
2043 cbuf
.reladdr
= NULL
;
2046 cbuf
.index2D
= const_block
;
2048 assert(ir
->type
->is_vector() || ir
->type
->is_scalar());
2050 if (const_offset_ir
) {
2051 /* Constant index into constant buffer */
2052 cbuf
.reladdr
= NULL
;
2053 cbuf
.index
= const_offset
/ 16;
2055 ir_expression
*offset_expr
= ir
->operands
[1]->as_expression();
2056 st_src_reg offset
= op
[1];
2058 /* The OpenGL spec is written in such a way that accesses with
2059 * non-constant offset are almost always vec4-aligned. The only
2060 * exception to this are members of structs in arrays of structs:
2061 * each struct in an array of structs is at least vec4-aligned,
2062 * but single-element and [ui]vec2 members of the struct may be at
2063 * an offset that is not a multiple of 16 bytes.
2065 * Here, we extract that offset, relying on previous passes to
2066 * always generate offset expressions of the form
2067 * (+ expr constant_offset).
2069 * Note that the std430 layout, which allows more cases of
2070 * alignment less than vec4 in arrays, is not supported for
2071 * uniform blocks, so we do not have to deal with it here.
2073 if (offset_expr
&& offset_expr
->operation
== ir_binop_add
) {
2074 const_offset_ir
= offset_expr
->operands
[1]->as_constant();
2075 if (const_offset_ir
) {
2076 const_offset
= const_offset_ir
->value
.u
[0];
2077 cbuf
.index
= const_offset
/ 16;
2078 offset_expr
->operands
[0]->accept(this);
2079 offset
= this->result
;
2083 /* Relative/variable index into constant buffer */
2084 emit_asm(ir
, TGSI_OPCODE_USHR
, st_dst_reg(index_reg
), offset
,
2085 st_src_reg_for_int(4));
2086 cbuf
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2087 *cbuf
.reladdr
= index_reg
;
2090 if (const_uniform_block
) {
2091 /* Constant constant buffer */
2092 cbuf
.reladdr2
= NULL
;
2094 /* Relative/variable constant buffer */
2095 cbuf
.reladdr2
= ralloc(mem_ctx
, st_src_reg
);
2096 *cbuf
.reladdr2
= op
[0];
2098 cbuf
.has_index2
= true;
2100 cbuf
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
2101 if (glsl_base_type_is_64bit(cbuf
.type
))
2102 cbuf
.swizzle
+= MAKE_SWIZZLE4(const_offset
% 16 / 8,
2103 const_offset
% 16 / 8,
2104 const_offset
% 16 / 8,
2105 const_offset
% 16 / 8);
2107 cbuf
.swizzle
+= MAKE_SWIZZLE4(const_offset
% 16 / 4,
2108 const_offset
% 16 / 4,
2109 const_offset
% 16 / 4,
2110 const_offset
% 16 / 4);
2112 if (ir
->type
->is_boolean()) {
2113 emit_asm(ir
, TGSI_OPCODE_USNE
, result_dst
, cbuf
,
2114 st_src_reg_for_int(0));
2116 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, cbuf
);
2122 /* note: we have to reorder the three args here */
2123 emit_asm(ir
, TGSI_OPCODE_LRP
, result_dst
, op
[2], op
[1], op
[0]);
2126 if (this->ctx
->Const
.NativeIntegers
)
2127 emit_asm(ir
, TGSI_OPCODE_UCMP
, result_dst
, op
[0], op
[1], op
[2]);
2129 op
[0].negate
= ~op
[0].negate
;
2130 emit_asm(ir
, TGSI_OPCODE_CMP
, result_dst
, op
[0], op
[1], op
[2]);
2133 case ir_triop_bitfield_extract
:
2134 emit_asm(ir
, TGSI_OPCODE_IBFE
, result_dst
, op
[0], op
[1], op
[2]);
2136 case ir_quadop_bitfield_insert
:
2137 emit_asm(ir
, TGSI_OPCODE_BFI
, result_dst
, op
[0], op
[1], op
[2], op
[3]);
2139 case ir_unop_bitfield_reverse
:
2140 emit_asm(ir
, TGSI_OPCODE_BREV
, result_dst
, op
[0]);
2142 case ir_unop_bit_count
:
2143 emit_asm(ir
, TGSI_OPCODE_POPC
, result_dst
, op
[0]);
2145 case ir_unop_find_msb
:
2146 emit_asm(ir
, TGSI_OPCODE_IMSB
, result_dst
, op
[0]);
2148 case ir_unop_find_lsb
:
2149 emit_asm(ir
, TGSI_OPCODE_LSB
, result_dst
, op
[0]);
2151 case ir_binop_imul_high
:
2152 emit_asm(ir
, TGSI_OPCODE_IMUL_HI
, result_dst
, op
[0], op
[1]);
2155 /* In theory, MAD is incorrect here. */
2157 emit_asm(ir
, TGSI_OPCODE_FMA
, result_dst
, op
[0], op
[1], op
[2]);
2159 emit_asm(ir
, TGSI_OPCODE_MAD
, result_dst
, op
[0], op
[1], op
[2]);
2161 case ir_unop_interpolate_at_centroid
:
2162 emit_asm(ir
, TGSI_OPCODE_INTERP_CENTROID
, result_dst
, op
[0]);
2164 case ir_binop_interpolate_at_offset
: {
2165 /* The y coordinate needs to be flipped for the default fb */
2166 static const gl_state_index16 transform_y_state
[STATE_LENGTH
]
2167 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
};
2169 unsigned transform_y_index
=
2170 _mesa_add_state_reference(this->prog
->Parameters
,
2173 st_src_reg transform_y
= st_src_reg(PROGRAM_STATE_VAR
,
2175 glsl_type::vec4_type
);
2176 transform_y
.swizzle
= SWIZZLE_XXXX
;
2178 st_src_reg temp
= get_temp(glsl_type::vec2_type
);
2179 st_dst_reg temp_dst
= st_dst_reg(temp
);
2181 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[1]);
2182 temp_dst
.writemask
= WRITEMASK_Y
;
2183 emit_asm(ir
, TGSI_OPCODE_MUL
, temp_dst
, transform_y
, op
[1]);
2184 emit_asm(ir
, TGSI_OPCODE_INTERP_OFFSET
, result_dst
, op
[0], temp
);
2187 case ir_binop_interpolate_at_sample
:
2188 emit_asm(ir
, TGSI_OPCODE_INTERP_SAMPLE
, result_dst
, op
[0], op
[1]);
2192 emit_asm(ir
, TGSI_OPCODE_D2F
, result_dst
, op
[0]);
2195 emit_asm(ir
, TGSI_OPCODE_F2D
, result_dst
, op
[0]);
2198 emit_asm(ir
, TGSI_OPCODE_D2I
, result_dst
, op
[0]);
2201 emit_asm(ir
, TGSI_OPCODE_I2D
, result_dst
, op
[0]);
2204 emit_asm(ir
, TGSI_OPCODE_D2U
, result_dst
, op
[0]);
2207 emit_asm(ir
, TGSI_OPCODE_U2D
, result_dst
, op
[0]);
2209 case ir_unop_unpack_double_2x32
:
2210 case ir_unop_pack_double_2x32
:
2211 case ir_unop_unpack_int_2x32
:
2212 case ir_unop_pack_int_2x32
:
2213 case ir_unop_unpack_uint_2x32
:
2214 case ir_unop_pack_uint_2x32
:
2215 case ir_unop_unpack_sampler_2x32
:
2216 case ir_unop_pack_sampler_2x32
:
2217 case ir_unop_unpack_image_2x32
:
2218 case ir_unop_pack_image_2x32
:
2219 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
2222 case ir_binop_ldexp
:
2223 if (ir
->operands
[0]->type
->is_double()) {
2224 emit_asm(ir
, TGSI_OPCODE_DLDEXP
, result_dst
, op
[0], op
[1]);
2225 } else if (ir
->operands
[0]->type
->is_float()) {
2226 emit_asm(ir
, TGSI_OPCODE_LDEXP
, result_dst
, op
[0], op
[1]);
2228 assert(!"Invalid ldexp for non-double opcode in glsl_to_tgsi_visitor::visit()");
2232 case ir_unop_pack_half_2x16
:
2233 emit_asm(ir
, TGSI_OPCODE_PK2H
, result_dst
, op
[0]);
2235 case ir_unop_unpack_half_2x16
:
2236 emit_asm(ir
, TGSI_OPCODE_UP2H
, result_dst
, op
[0]);
2239 case ir_unop_get_buffer_size
: {
2240 ir_constant
*const_offset
= ir
->operands
[0]->as_constant();
2241 int buf_base
= ctx
->st
->has_hw_atomics
2242 ? 0 : ctx
->Const
.Program
[shader
->Stage
].MaxAtomicBuffers
;
2245 buf_base
+ (const_offset
? const_offset
->value
.u
[0] : 0),
2247 if (!const_offset
) {
2248 buffer
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2249 *buffer
.reladdr
= op
[0];
2250 emit_arl(ir
, sampler_reladdr
, op
[0]);
2252 emit_asm(ir
, TGSI_OPCODE_RESQ
, result_dst
)->resource
= buffer
;
2258 case ir_unop_b2i64
: {
2259 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2260 st_dst_reg temp_dst
= st_dst_reg(temp
);
2261 unsigned orig_swz
= op
[0].swizzle
;
2263 * To convert unsigned to 64-bit:
2264 * zero Y channel, copy X channel.
2266 temp_dst
.writemask
= WRITEMASK_Y
;
2267 if (vector_elements
> 1)
2268 temp_dst
.writemask
|= WRITEMASK_W
;
2269 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, st_src_reg_for_int(0));
2270 temp_dst
.writemask
= WRITEMASK_X
;
2271 if (vector_elements
> 1)
2272 temp_dst
.writemask
|= WRITEMASK_Z
;
2273 op
[0].swizzle
= MAKE_SWIZZLE4(GET_SWZ(orig_swz
, 0), GET_SWZ(orig_swz
, 0),
2274 GET_SWZ(orig_swz
, 1), GET_SWZ(orig_swz
, 1));
2275 if (ir
->operation
== ir_unop_u2i64
|| ir
->operation
== ir_unop_u2u64
)
2276 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2278 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, op
[0], st_src_reg_for_int(1));
2280 result_src
.type
= GLSL_TYPE_UINT64
;
2281 if (vector_elements
> 2) {
2282 /* Subtle: We rely on the fact that get_temp here returns the next
2283 * TGSI temporary register directly after the temp register used for
2284 * the first two components, so that the result gets picked up
2287 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2288 st_dst_reg temp_dst
= st_dst_reg(temp
);
2289 temp_dst
.writemask
= WRITEMASK_Y
;
2290 if (vector_elements
> 3)
2291 temp_dst
.writemask
|= WRITEMASK_W
;
2292 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, st_src_reg_for_int(0));
2294 temp_dst
.writemask
= WRITEMASK_X
;
2295 if (vector_elements
> 3)
2296 temp_dst
.writemask
|= WRITEMASK_Z
;
2297 op
[0].swizzle
= MAKE_SWIZZLE4(GET_SWZ(orig_swz
, 2),
2298 GET_SWZ(orig_swz
, 2),
2299 GET_SWZ(orig_swz
, 3),
2300 GET_SWZ(orig_swz
, 3));
2301 if (ir
->operation
== ir_unop_u2i64
|| ir
->operation
== ir_unop_u2u64
)
2302 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2304 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, op
[0],
2305 st_src_reg_for_int(1));
2312 case ir_unop_i642u
: {
2313 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2314 st_dst_reg temp_dst
= st_dst_reg(temp
);
2315 unsigned orig_swz
= op
[0].swizzle
;
2316 unsigned orig_idx
= op
[0].index
;
2318 temp_dst
.writemask
= WRITEMASK_X
;
2320 for (el
= 0; el
< vector_elements
; el
++) {
2321 unsigned swz
= GET_SWZ(orig_swz
, el
);
2323 op
[0].swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_Z
,
2324 SWIZZLE_Z
, SWIZZLE_Z
);
2326 op
[0].swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
,
2327 SWIZZLE_X
, SWIZZLE_X
);
2329 op
[0].index
= orig_idx
+ 1;
2330 op
[0].type
= GLSL_TYPE_UINT
;
2331 temp_dst
.writemask
= WRITEMASK_X
<< el
;
2332 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2335 if (ir
->operation
== ir_unop_u642u
|| ir
->operation
== ir_unop_i642u
)
2336 result_src
.type
= GLSL_TYPE_UINT
;
2338 result_src
.type
= GLSL_TYPE_INT
;
2342 emit_asm(ir
, TGSI_OPCODE_U64SNE
, result_dst
, op
[0],
2343 st_src_reg_for_int64(0));
2346 emit_asm(ir
, TGSI_OPCODE_I642F
, result_dst
, op
[0]);
2349 emit_asm(ir
, TGSI_OPCODE_U642F
, result_dst
, op
[0]);
2352 emit_asm(ir
, TGSI_OPCODE_I642D
, result_dst
, op
[0]);
2355 emit_asm(ir
, TGSI_OPCODE_U642D
, result_dst
, op
[0]);
2358 emit_asm(ir
, TGSI_OPCODE_I2I64
, result_dst
, op
[0]);
2361 emit_asm(ir
, TGSI_OPCODE_F2I64
, result_dst
, op
[0]);
2364 emit_asm(ir
, TGSI_OPCODE_D2I64
, result_dst
, op
[0]);
2367 emit_asm(ir
, TGSI_OPCODE_I2I64
, result_dst
, op
[0]);
2370 emit_asm(ir
, TGSI_OPCODE_F2U64
, result_dst
, op
[0]);
2373 emit_asm(ir
, TGSI_OPCODE_D2U64
, result_dst
, op
[0]);
2375 /* these might be needed */
2376 case ir_unop_pack_snorm_2x16
:
2377 case ir_unop_pack_unorm_2x16
:
2378 case ir_unop_pack_snorm_4x8
:
2379 case ir_unop_pack_unorm_4x8
:
2381 case ir_unop_unpack_snorm_2x16
:
2382 case ir_unop_unpack_unorm_2x16
:
2383 case ir_unop_unpack_snorm_4x8
:
2384 case ir_unop_unpack_unorm_4x8
:
2386 case ir_quadop_vector
:
2387 case ir_binop_vector_extract
:
2388 case ir_triop_vector_insert
:
2389 case ir_binop_carry
:
2390 case ir_binop_borrow
:
2391 case ir_unop_ssbo_unsized_array_length
:
2392 /* This operation is not supported, or should have already been handled.
2394 assert(!"Invalid ir opcode in glsl_to_tgsi_visitor::visit()");
2398 this->result
= result_src
;
2403 glsl_to_tgsi_visitor::visit(ir_swizzle
*ir
)
2409 /* Note that this is only swizzles in expressions, not those on the left
2410 * hand side of an assignment, which do write masking. See ir_assignment
2414 ir
->val
->accept(this);
2416 assert(src
.file
!= PROGRAM_UNDEFINED
);
2417 assert(ir
->type
->vector_elements
> 0);
2419 for (i
= 0; i
< 4; i
++) {
2420 if (i
< ir
->type
->vector_elements
) {
2423 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.x
);
2426 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.y
);
2429 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.z
);
2432 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.w
);
2436 /* If the type is smaller than a vec4, replicate the last
2439 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
2443 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2448 /* Test if the variable is an array. Note that geometry and
2449 * tessellation shader inputs are outputs are always arrays (except
2450 * for patch inputs), so only the array element type is considered.
2453 is_inout_array(unsigned stage
, ir_variable
*var
, bool *remove_array
)
2455 const glsl_type
*type
= var
->type
;
2457 *remove_array
= false;
2459 if ((stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_in
) ||
2460 (stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_out
))
2463 if (((stage
== MESA_SHADER_GEOMETRY
&& var
->data
.mode
== ir_var_shader_in
) ||
2464 (stage
== MESA_SHADER_TESS_EVAL
&& var
->data
.mode
== ir_var_shader_in
) ||
2465 stage
== MESA_SHADER_TESS_CTRL
) &&
2467 if (!var
->type
->is_array())
2468 return false; /* a system value probably */
2470 type
= var
->type
->fields
.array
;
2471 *remove_array
= true;
2474 return type
->is_array() || type
->is_matrix();
2478 st_translate_interp_loc(ir_variable
*var
)
2480 if (var
->data
.centroid
)
2481 return TGSI_INTERPOLATE_LOC_CENTROID
;
2482 else if (var
->data
.sample
)
2483 return TGSI_INTERPOLATE_LOC_SAMPLE
;
2485 return TGSI_INTERPOLATE_LOC_CENTER
;
2489 glsl_to_tgsi_visitor::visit(ir_dereference_variable
*ir
)
2491 variable_storage
*entry
;
2492 ir_variable
*var
= ir
->var
;
2495 if (handle_bound_deref(ir
->as_dereference()))
2498 entry
= find_variable_storage(ir
->var
);
2501 switch (var
->data
.mode
) {
2502 case ir_var_uniform
:
2503 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_UNIFORM
,
2504 var
->data
.param_index
);
2505 _mesa_hash_table_insert(this->variables
, var
, entry
);
2507 case ir_var_shader_in
: {
2508 /* The linker assigns locations for varyings and attributes,
2509 * including deprecated builtins (like gl_Color), user-assign
2510 * generic attributes (glBindVertexLocation), and
2511 * user-defined varyings.
2513 assert(var
->data
.location
!= -1);
2515 const glsl_type
*type_without_array
= var
->type
->without_array();
2516 struct inout_decl
*decl
= &inputs
[num_inputs
];
2517 unsigned component
= var
->data
.location_frac
;
2518 unsigned num_components
;
2521 if (type_without_array
->is_64bit())
2522 component
= component
/ 2;
2523 if (type_without_array
->vector_elements
)
2524 num_components
= type_without_array
->vector_elements
;
2528 decl
->mesa_index
= var
->data
.location
;
2529 decl
->interp
= (glsl_interp_mode
) var
->data
.interpolation
;
2530 decl
->interp_loc
= st_translate_interp_loc(var
);
2531 decl
->base_type
= type_without_array
->base_type
;
2532 decl
->usage_mask
= u_bit_consecutive(component
, num_components
);
2534 if (is_inout_array(shader
->Stage
, var
, &remove_array
)) {
2535 decl
->array_id
= num_input_arrays
+ 1;
2542 decl
->size
= type_size(var
->type
->fields
.array
);
2544 decl
->size
= type_size(var
->type
);
2546 entry
= new(mem_ctx
) variable_storage(var
,
2550 entry
->component
= component
;
2552 _mesa_hash_table_insert(this->variables
, var
, entry
);
2556 case ir_var_shader_out
: {
2557 assert(var
->data
.location
!= -1);
2559 const glsl_type
*type_without_array
= var
->type
->without_array();
2560 struct inout_decl
*decl
= &outputs
[num_outputs
];
2561 unsigned component
= var
->data
.location_frac
;
2562 unsigned num_components
;
2565 decl
->invariant
= var
->data
.invariant
;
2567 if (type_without_array
->is_64bit())
2568 component
= component
/ 2;
2569 if (type_without_array
->vector_elements
)
2570 num_components
= type_without_array
->vector_elements
;
2574 decl
->mesa_index
= var
->data
.location
+ FRAG_RESULT_MAX
* var
->data
.index
;
2575 decl
->base_type
= type_without_array
->base_type
;
2576 decl
->usage_mask
= u_bit_consecutive(component
, num_components
);
2577 if (var
->data
.stream
& (1u << 31)) {
2578 decl
->gs_out_streams
= var
->data
.stream
& ~(1u << 31);
2580 assert(var
->data
.stream
< 4);
2581 decl
->gs_out_streams
= 0;
2582 for (unsigned i
= 0; i
< num_components
; ++i
)
2583 decl
->gs_out_streams
|= var
->data
.stream
<< (2 * (component
+ i
));
2586 if (is_inout_array(shader
->Stage
, var
, &remove_array
)) {
2587 decl
->array_id
= num_output_arrays
+ 1;
2588 num_output_arrays
++;
2594 decl
->size
= type_size(var
->type
->fields
.array
);
2596 decl
->size
= type_size(var
->type
);
2598 if (var
->data
.fb_fetch_output
) {
2599 st_dst_reg dst
= st_dst_reg(get_temp(var
->type
));
2600 st_src_reg src
= st_src_reg(PROGRAM_OUTPUT
, decl
->mesa_index
,
2601 var
->type
, component
, decl
->array_id
);
2602 emit_asm(NULL
, TGSI_OPCODE_FBFETCH
, dst
, src
);
2603 entry
= new(mem_ctx
) variable_storage(var
, dst
.file
, dst
.index
,
2606 entry
= new(mem_ctx
) variable_storage(var
,
2611 entry
->component
= component
;
2613 _mesa_hash_table_insert(this->variables
, var
, entry
);
2617 case ir_var_system_value
:
2618 entry
= new(mem_ctx
) variable_storage(var
,
2619 PROGRAM_SYSTEM_VALUE
,
2620 var
->data
.location
);
2623 case ir_var_temporary
:
2624 st_src_reg src
= get_temp(var
->type
);
2626 entry
= new(mem_ctx
) variable_storage(var
, src
.file
, src
.index
,
2628 _mesa_hash_table_insert(this->variables
, var
, entry
);
2634 printf("Failed to make storage for %s\n", var
->name
);
2639 this->result
= st_src_reg(entry
->file
, entry
->index
, var
->type
,
2640 entry
->component
, entry
->array_id
);
2641 if (this->shader
->Stage
== MESA_SHADER_VERTEX
&&
2642 var
->data
.mode
== ir_var_shader_in
&&
2643 var
->type
->without_array()->is_double())
2644 this->result
.is_double_vertex_input
= true;
2645 if (!native_integers
)
2646 this->result
.type
= GLSL_TYPE_FLOAT
;
2650 shrink_array_declarations(struct inout_decl
*decls
, unsigned count
,
2651 GLbitfield64
* usage_mask
,
2652 GLbitfield64 double_usage_mask
,
2653 GLbitfield
* patch_usage_mask
)
2658 /* Fix array declarations by removing unused array elements at both ends
2659 * of the arrays. For example, mat4[3] where only mat[1] is used.
2661 for (i
= 0; i
< count
; i
++) {
2662 struct inout_decl
*decl
= &decls
[i
];
2663 if (!decl
->array_id
)
2666 /* Shrink the beginning. */
2667 for (j
= 0; j
< (int)decl
->size
; j
++) {
2668 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
) {
2669 if (*patch_usage_mask
&
2670 BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
))
2674 if (*usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
))
2676 if (double_usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
-1))
2685 /* Shrink the end. */
2686 for (j
= decl
->size
-1; j
>= 0; j
--) {
2687 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
) {
2688 if (*patch_usage_mask
&
2689 BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
))
2693 if (*usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
))
2695 if (double_usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
-1))
2702 /* When not all entries of an array are accessed, we mark them as used
2703 * here anyway, to ensure that the input/output mapping logic doesn't get
2706 * TODO This happens when an array isn't used via indirect access, which
2707 * some game ports do (at least eON-based). There is an optimization
2708 * opportunity here by replacing the array declaration with non-array
2709 * declarations of those slots that are actually used.
2711 for (j
= 1; j
< (int)decl
->size
; ++j
) {
2712 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
)
2713 *patch_usage_mask
|= BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
);
2715 *usage_mask
|= BITFIELD64_BIT(decl
->mesa_index
+ j
);
2722 mark_array_io(struct inout_decl
*decls
, unsigned count
,
2723 GLbitfield64
* usage_mask
,
2724 GLbitfield64 double_usage_mask
,
2725 GLbitfield
* patch_usage_mask
)
2730 /* Fix array declarations by removing unused array elements at both ends
2731 * of the arrays. For example, mat4[3] where only mat[1] is used.
2733 for (i
= 0; i
< count
; i
++) {
2734 struct inout_decl
*decl
= &decls
[i
];
2735 if (!decl
->array_id
)
2738 /* When not all entries of an array are accessed, we mark them as used
2739 * here anyway, to ensure that the input/output mapping logic doesn't get
2742 * TODO This happens when an array isn't used via indirect access, which
2743 * some game ports do (at least eON-based). There is an optimization
2744 * opportunity here by replacing the array declaration with non-array
2745 * declarations of those slots that are actually used.
2747 for (j
= 0; j
< (int)decl
->size
; ++j
) {
2748 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
)
2749 *patch_usage_mask
|= BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
);
2751 *usage_mask
|= BITFIELD64_BIT(decl
->mesa_index
+ j
);
2757 glsl_to_tgsi_visitor::visit(ir_dereference_array
*ir
)
2762 ir_variable
*var
= ir
->variable_referenced();
2764 if (handle_bound_deref(ir
->as_dereference()))
2767 /* We only need the logic provided by st_glsl_storage_type_size()
2768 * for arrays of structs. Indirect sampler and image indexing is handled
2771 int element_size
= ir
->type
->without_array()->is_struct() ?
2772 st_glsl_storage_type_size(ir
->type
, var
->data
.bindless
) :
2773 type_size(ir
->type
);
2775 index
= ir
->array_index
->constant_expression_value(ralloc_parent(ir
));
2777 ir
->array
->accept(this);
2780 if (!src
.has_index2
) {
2781 switch (this->prog
->Target
) {
2782 case GL_TESS_CONTROL_PROGRAM_NV
:
2783 is_2D
= (src
.file
== PROGRAM_INPUT
|| src
.file
== PROGRAM_OUTPUT
) &&
2784 !ir
->variable_referenced()->data
.patch
;
2786 case GL_TESS_EVALUATION_PROGRAM_NV
:
2787 is_2D
= src
.file
== PROGRAM_INPUT
&&
2788 !ir
->variable_referenced()->data
.patch
;
2790 case GL_GEOMETRY_PROGRAM_NV
:
2791 is_2D
= src
.file
== PROGRAM_INPUT
;
2801 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
2802 src
.file
== PROGRAM_INPUT
)
2803 element_size
= attrib_type_size(ir
->type
, true);
2805 src
.index2D
= index
->value
.i
[0];
2806 src
.has_index2
= true;
2808 src
.index
+= index
->value
.i
[0] * element_size
;
2810 /* Variable index array dereference. It eats the "vec4" of the
2811 * base of the array and an index that offsets the TGSI register
2814 ir
->array_index
->accept(this);
2816 st_src_reg index_reg
;
2818 if (element_size
== 1) {
2819 index_reg
= this->result
;
2821 index_reg
= get_temp(native_integers
?
2822 glsl_type::int_type
: glsl_type::float_type
);
2824 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(index_reg
),
2825 this->result
, st_src_reg_for_type(index_reg
.type
, element_size
));
2828 /* If there was already a relative address register involved, add the
2829 * new and the old together to get the new offset.
2831 if (!is_2D
&& src
.reladdr
!= NULL
) {
2832 st_src_reg accum_reg
= get_temp(native_integers
?
2833 glsl_type::int_type
: glsl_type::float_type
);
2835 emit_asm(ir
, TGSI_OPCODE_ADD
, st_dst_reg(accum_reg
),
2836 index_reg
, *src
.reladdr
);
2838 index_reg
= accum_reg
;
2842 src
.reladdr2
= ralloc(mem_ctx
, st_src_reg
);
2843 *src
.reladdr2
= index_reg
;
2845 src
.has_index2
= true;
2847 src
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2848 *src
.reladdr
= index_reg
;
2852 /* Change the register type to the element type of the array. */
2853 src
.type
= ir
->type
->base_type
;
2859 glsl_to_tgsi_visitor::visit(ir_dereference_record
*ir
)
2862 const glsl_type
*struct_type
= ir
->record
->type
;
2863 ir_variable
*var
= ir
->record
->variable_referenced();
2866 if (handle_bound_deref(ir
->as_dereference()))
2869 ir
->record
->accept(this);
2871 assert(ir
->field_idx
>= 0);
2873 for (i
= 0; i
< struct_type
->length
; i
++) {
2874 if (i
== (unsigned) ir
->field_idx
)
2876 const glsl_type
*member_type
= struct_type
->fields
.structure
[i
].type
;
2877 offset
+= st_glsl_storage_type_size(member_type
, var
->data
.bindless
);
2880 /* If the type is smaller than a vec4, replicate the last channel out. */
2881 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
2882 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
2884 this->result
.swizzle
= SWIZZLE_NOOP
;
2886 this->result
.index
+= offset
;
2887 this->result
.type
= ir
->type
->base_type
;
2891 * We want to be careful in assignment setup to hit the actual storage
2892 * instead of potentially using a temporary like we might with the
2893 * ir_dereference handler.
2896 get_assignment_lhs(ir_dereference
*ir
, glsl_to_tgsi_visitor
*v
, int *component
)
2898 /* The LHS must be a dereference. If the LHS is a variable indexed array
2899 * access of a vector, it must be separated into a series conditional moves
2900 * before reaching this point (see ir_vec_index_to_cond_assign).
2902 assert(ir
->as_dereference());
2903 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
2905 assert(!deref_array
->array
->type
->is_vector());
2908 /* Use the rvalue deref handler for the most part. We write swizzles using
2909 * the writemask, but we do extract the base component for enhanced layouts
2910 * from the source swizzle.
2913 *component
= GET_SWZ(v
->result
.swizzle
, 0);
2914 return st_dst_reg(v
->result
);
2918 * Process the condition of a conditional assignment
2920 * Examines the condition of a conditional assignment to generate the optimal
2921 * first operand of a \c CMP instruction. If the condition is a relational
2922 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
2923 * used as the source for the \c CMP instruction. Otherwise the comparison
2924 * is processed to a boolean result, and the boolean result is used as the
2925 * operand to the CMP instruction.
2928 glsl_to_tgsi_visitor::process_move_condition(ir_rvalue
*ir
)
2930 ir_rvalue
*src_ir
= ir
;
2932 bool switch_order
= false;
2934 ir_expression
*const expr
= ir
->as_expression();
2936 if (native_integers
) {
2937 if ((expr
!= NULL
) && (expr
->num_operands
== 2)) {
2938 enum glsl_base_type type
= expr
->operands
[0]->type
->base_type
;
2939 if (type
== GLSL_TYPE_INT
|| type
== GLSL_TYPE_UINT
||
2940 type
== GLSL_TYPE_BOOL
) {
2941 if (expr
->operation
== ir_binop_equal
) {
2942 if (expr
->operands
[0]->is_zero()) {
2943 src_ir
= expr
->operands
[1];
2944 switch_order
= true;
2946 else if (expr
->operands
[1]->is_zero()) {
2947 src_ir
= expr
->operands
[0];
2948 switch_order
= true;
2951 else if (expr
->operation
== ir_binop_nequal
) {
2952 if (expr
->operands
[0]->is_zero()) {
2953 src_ir
= expr
->operands
[1];
2955 else if (expr
->operands
[1]->is_zero()) {
2956 src_ir
= expr
->operands
[0];
2962 src_ir
->accept(this);
2963 return switch_order
;
2966 if ((expr
!= NULL
) && (expr
->num_operands
== 2)) {
2967 bool zero_on_left
= false;
2969 if (expr
->operands
[0]->is_zero()) {
2970 src_ir
= expr
->operands
[1];
2971 zero_on_left
= true;
2972 } else if (expr
->operands
[1]->is_zero()) {
2973 src_ir
= expr
->operands
[0];
2974 zero_on_left
= false;
2978 * (a < 0) T F F ( a < 0) T F F
2979 * (0 < a) F F T (-a < 0) F F T
2980 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
2981 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
2983 * Note that exchanging the order of 0 and 'a' in the comparison simply
2984 * means that the value of 'a' should be negated.
2987 switch (expr
->operation
) {
2989 switch_order
= false;
2990 negate
= zero_on_left
;
2993 case ir_binop_gequal
:
2994 switch_order
= true;
2995 negate
= zero_on_left
;
2999 /* This isn't the right kind of comparison afterall, so make sure
3000 * the whole condition is visited.
3008 src_ir
->accept(this);
3010 /* We use the TGSI_OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
3011 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
3012 * choose which value TGSI_OPCODE_CMP produces without an extra instruction
3013 * computing the condition.
3016 this->result
.negate
= ~this->result
.negate
;
3018 return switch_order
;
3022 glsl_to_tgsi_visitor::emit_block_mov(ir_assignment
*ir
, const struct glsl_type
*type
,
3023 st_dst_reg
*l
, st_src_reg
*r
,
3024 st_src_reg
*cond
, bool cond_swap
)
3026 if (type
->is_struct()) {
3027 for (unsigned int i
= 0; i
< type
->length
; i
++) {
3028 emit_block_mov(ir
, type
->fields
.structure
[i
].type
, l
, r
,
3034 if (type
->is_array()) {
3035 for (unsigned int i
= 0; i
< type
->length
; i
++) {
3036 emit_block_mov(ir
, type
->fields
.array
, l
, r
, cond
, cond_swap
);
3041 if (type
->is_matrix()) {
3042 const struct glsl_type
*vec_type
;
3044 vec_type
= glsl_type::get_instance(type
->is_double()
3045 ? GLSL_TYPE_DOUBLE
: GLSL_TYPE_FLOAT
,
3046 type
->vector_elements
, 1);
3048 for (int i
= 0; i
< type
->matrix_columns
; i
++) {
3049 emit_block_mov(ir
, vec_type
, l
, r
, cond
, cond_swap
);
3054 assert(type
->is_scalar() || type
->is_vector());
3056 l
->type
= type
->base_type
;
3057 r
->type
= type
->base_type
;
3059 st_src_reg l_src
= st_src_reg(*l
);
3061 if (l_src
.file
== PROGRAM_OUTPUT
&&
3062 this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
&&
3063 (l_src
.index
== FRAG_RESULT_DEPTH
||
3064 l_src
.index
== FRAG_RESULT_STENCIL
)) {
3065 /* This is a special case because the source swizzles will be shifted
3066 * later to account for the difference between GLSL (where they're
3067 * plain floats) and TGSI (where they're Z and Y components). */
3068 l_src
.swizzle
= SWIZZLE_XXXX
;
3071 if (native_integers
) {
3072 emit_asm(ir
, TGSI_OPCODE_UCMP
, *l
, *cond
,
3073 cond_swap
? l_src
: *r
,
3074 cond_swap
? *r
: l_src
);
3076 emit_asm(ir
, TGSI_OPCODE_CMP
, *l
, *cond
,
3077 cond_swap
? l_src
: *r
,
3078 cond_swap
? *r
: l_src
);
3081 emit_asm(ir
, TGSI_OPCODE_MOV
, *l
, *r
);
3085 if (type
->is_dual_slot()) {
3087 if (r
->is_double_vertex_input
== false)
3093 glsl_to_tgsi_visitor::visit(ir_assignment
*ir
)
3099 /* all generated instructions need to be flaged as precise */
3100 this->precise
= is_precise(ir
->lhs
->variable_referenced());
3101 ir
->rhs
->accept(this);
3104 l
= get_assignment_lhs(ir
->lhs
, this, &dst_component
);
3108 int first_enabled_chan
= 0;
3110 ir_variable
*variable
= ir
->lhs
->variable_referenced();
3112 if (shader
->Stage
== MESA_SHADER_FRAGMENT
&&
3113 variable
->data
.mode
== ir_var_shader_out
&&
3114 (variable
->data
.location
== FRAG_RESULT_DEPTH
||
3115 variable
->data
.location
== FRAG_RESULT_STENCIL
)) {
3116 assert(ir
->lhs
->type
->is_scalar());
3117 assert(ir
->write_mask
== WRITEMASK_X
);
3119 if (variable
->data
.location
== FRAG_RESULT_DEPTH
)
3120 l
.writemask
= WRITEMASK_Z
;
3122 assert(variable
->data
.location
== FRAG_RESULT_STENCIL
);
3123 l
.writemask
= WRITEMASK_Y
;
3125 } else if (ir
->write_mask
== 0) {
3126 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
3128 unsigned num_elements
=
3129 ir
->lhs
->type
->without_array()->vector_elements
;
3132 l
.writemask
= u_bit_consecutive(0, num_elements
);
3134 /* The type is a struct or an array of (array of) structs. */
3135 l
.writemask
= WRITEMASK_XYZW
;
3138 l
.writemask
= ir
->write_mask
;
3141 for (int i
= 0; i
< 4; i
++) {
3142 if (l
.writemask
& (1 << i
)) {
3143 first_enabled_chan
= GET_SWZ(r
.swizzle
, i
);
3148 l
.writemask
= l
.writemask
<< dst_component
;
3150 /* Swizzle a small RHS vector into the channels being written.
3152 * glsl ir treats write_mask as dictating how many channels are
3153 * present on the RHS while TGSI treats write_mask as just
3154 * showing which channels of the vec4 RHS get written.
3156 for (int i
= 0; i
< 4; i
++) {
3157 if (l
.writemask
& (1 << i
))
3158 swizzles
[i
] = GET_SWZ(r
.swizzle
, rhs_chan
++);
3160 swizzles
[i
] = first_enabled_chan
;
3162 r
.swizzle
= MAKE_SWIZZLE4(swizzles
[0], swizzles
[1],
3163 swizzles
[2], swizzles
[3]);
3166 assert(l
.file
!= PROGRAM_UNDEFINED
);
3167 assert(r
.file
!= PROGRAM_UNDEFINED
);
3169 if (ir
->condition
) {
3170 const bool switch_order
= this->process_move_condition(ir
->condition
);
3171 st_src_reg condition
= this->result
;
3173 emit_block_mov(ir
, ir
->lhs
->type
, &l
, &r
, &condition
, switch_order
);
3174 } else if (ir
->rhs
->as_expression() &&
3175 this->instructions
.get_tail() &&
3176 ir
->rhs
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->ir
&&
3177 !((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->is_64bit_expanded
&&
3178 type_size(ir
->lhs
->type
) == 1 &&
3179 l
.writemask
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->dst
[0].writemask
) {
3180 /* To avoid emitting an extra MOV when assigning an expression to a
3181 * variable, emit the last instruction of the expression again, but
3182 * replace the destination register with the target of the assignment.
3183 * Dead code elimination will remove the original instruction.
3185 glsl_to_tgsi_instruction
*inst
, *new_inst
;
3186 inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
3187 new_inst
= emit_asm(ir
, inst
->op
, l
, inst
->src
[0], inst
->src
[1], inst
->src
[2], inst
->src
[3]);
3188 new_inst
->saturate
= inst
->saturate
;
3189 new_inst
->resource
= inst
->resource
;
3190 inst
->dead_mask
= inst
->dst
[0].writemask
;
3192 emit_block_mov(ir
, ir
->rhs
->type
, &l
, &r
, NULL
, false);
3199 glsl_to_tgsi_visitor::visit(ir_constant
*ir
)
3202 GLdouble stack_vals
[4] = { 0 };
3203 gl_constant_value
*values
= (gl_constant_value
*) stack_vals
;
3204 GLenum gl_type
= GL_NONE
;
3205 unsigned int i
, elements
;
3206 static int in_array
= 0;
3207 gl_register_file file
= in_array
? PROGRAM_CONSTANT
: PROGRAM_IMMEDIATE
;
3209 /* Unfortunately, 4 floats is all we can get into
3210 * _mesa_add_typed_unnamed_constant. So, make a temp to store an
3211 * aggregate constant and move each constant value into it. If we
3212 * get lucky, copy propagation will eliminate the extra moves.
3214 if (ir
->type
->is_struct()) {
3215 st_src_reg temp_base
= get_temp(ir
->type
);
3216 st_dst_reg temp
= st_dst_reg(temp_base
);
3218 for (i
= 0; i
< ir
->type
->length
; i
++) {
3219 ir_constant
*const field_value
= ir
->get_record_field(i
);
3220 int size
= type_size(field_value
->type
);
3224 field_value
->accept(this);
3227 for (unsigned j
= 0; j
< (unsigned int)size
; j
++) {
3228 emit_asm(ir
, TGSI_OPCODE_MOV
, temp
, src
);
3234 this->result
= temp_base
;
3238 if (ir
->type
->is_array()) {
3239 st_src_reg temp_base
= get_temp(ir
->type
);
3240 st_dst_reg temp
= st_dst_reg(temp_base
);
3241 int size
= type_size(ir
->type
->fields
.array
);
3246 for (i
= 0; i
< ir
->type
->length
; i
++) {
3247 ir
->const_elements
[i
]->accept(this);
3249 for (int j
= 0; j
< size
; j
++) {
3250 emit_asm(ir
, TGSI_OPCODE_MOV
, temp
, src
);
3256 this->result
= temp_base
;
3261 if (ir
->type
->is_matrix()) {
3262 st_src_reg mat
= get_temp(ir
->type
);
3263 st_dst_reg mat_column
= st_dst_reg(mat
);
3265 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
3266 switch (ir
->type
->base_type
) {
3267 case GLSL_TYPE_FLOAT
:
3268 values
= (gl_constant_value
*)
3269 &ir
->value
.f
[i
* ir
->type
->vector_elements
];
3271 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
3272 src
.index
= add_constant(file
,
3274 ir
->type
->vector_elements
,
3277 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3279 case GLSL_TYPE_DOUBLE
:
3280 values
= (gl_constant_value
*)
3281 &ir
->value
.d
[i
* ir
->type
->vector_elements
];
3282 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
3283 src
.index
= add_constant(file
,
3285 ir
->type
->vector_elements
,
3288 if (ir
->type
->vector_elements
>= 2) {
3289 mat_column
.writemask
= WRITEMASK_XY
;
3290 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
3291 SWIZZLE_X
, SWIZZLE_Y
);
3292 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3294 mat_column
.writemask
= WRITEMASK_X
;
3295 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
,
3296 SWIZZLE_X
, SWIZZLE_X
);
3297 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3300 if (ir
->type
->vector_elements
> 2) {
3301 if (ir
->type
->vector_elements
== 4) {
3302 mat_column
.writemask
= WRITEMASK_ZW
;
3303 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
3304 SWIZZLE_X
, SWIZZLE_Y
);
3305 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3307 mat_column
.writemask
= WRITEMASK_Z
;
3308 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_Y
, SWIZZLE_Y
,
3309 SWIZZLE_Y
, SWIZZLE_Y
);
3310 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3311 mat_column
.writemask
= WRITEMASK_XYZW
;
3312 src
.swizzle
= SWIZZLE_XYZW
;
3318 unreachable("Illegal matrix constant type.\n");
3327 elements
= ir
->type
->vector_elements
;
3328 switch (ir
->type
->base_type
) {
3329 case GLSL_TYPE_FLOAT
:
3331 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3332 values
[i
].f
= ir
->value
.f
[i
];
3335 case GLSL_TYPE_DOUBLE
:
3336 gl_type
= GL_DOUBLE
;
3337 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3338 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(double));
3341 case GLSL_TYPE_INT64
:
3342 gl_type
= GL_INT64_ARB
;
3343 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3344 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(int64_t));
3347 case GLSL_TYPE_UINT64
:
3348 gl_type
= GL_UNSIGNED_INT64_ARB
;
3349 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3350 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(uint64_t));
3353 case GLSL_TYPE_UINT
:
3354 gl_type
= native_integers
? GL_UNSIGNED_INT
: GL_FLOAT
;
3355 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3356 if (native_integers
)
3357 values
[i
].u
= ir
->value
.u
[i
];
3359 values
[i
].f
= ir
->value
.u
[i
];
3363 gl_type
= native_integers
? GL_INT
: GL_FLOAT
;
3364 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3365 if (native_integers
)
3366 values
[i
].i
= ir
->value
.i
[i
];
3368 values
[i
].f
= ir
->value
.i
[i
];
3371 case GLSL_TYPE_BOOL
:
3372 gl_type
= native_integers
? GL_BOOL
: GL_FLOAT
;
3373 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3374 values
[i
].u
= ir
->value
.b
[i
] ? ctx
->Const
.UniformBooleanTrue
: 0;
3377 case GLSL_TYPE_SAMPLER
:
3378 case GLSL_TYPE_IMAGE
:
3379 gl_type
= GL_UNSIGNED_INT
;
3381 values
[0].u
= ir
->value
.u64
[0] & 0xffffffff;
3382 values
[1].u
= ir
->value
.u64
[0] >> 32;
3385 assert(!"Non-float/uint/int/bool/sampler/image constant");
3388 this->result
= st_src_reg(file
, -1, ir
->type
);
3389 this->result
.index
= add_constant(file
,
3393 &this->result
.swizzle
);
3397 glsl_to_tgsi_visitor::visit_atomic_counter_intrinsic(ir_call
*ir
)
3399 exec_node
*param
= ir
->actual_parameters
.get_head();
3400 ir_dereference
*deref
= static_cast<ir_dereference
*>(param
);
3401 ir_variable
*location
= deref
->variable_referenced();
3402 bool has_hw_atomics
= st_context(ctx
)->has_hw_atomics
;
3403 /* Calculate the surface offset */
3405 unsigned array_size
= 0, base
= 0;
3407 st_src_reg resource
;
3409 get_deref_offsets(deref
, &array_size
, &base
, &index
, &offset
, false);
3411 if (has_hw_atomics
) {
3412 variable_storage
*entry
= find_variable_storage(location
);
3413 st_src_reg
buffer(PROGRAM_HW_ATOMIC
, 0, GLSL_TYPE_ATOMIC_UINT
,
3414 location
->data
.binding
);
3417 entry
= new(mem_ctx
) variable_storage(location
, PROGRAM_HW_ATOMIC
,
3419 _mesa_hash_table_insert(this->variables
, location
, entry
);
3421 atomic_info
[num_atomics
].location
= location
->data
.location
;
3422 atomic_info
[num_atomics
].binding
= location
->data
.binding
;
3423 atomic_info
[num_atomics
].size
= location
->type
->arrays_of_arrays_size();
3424 if (atomic_info
[num_atomics
].size
== 0)
3425 atomic_info
[num_atomics
].size
= 1;
3426 atomic_info
[num_atomics
].array_id
= 0;
3430 if (offset
.file
!= PROGRAM_UNDEFINED
) {
3431 if (atomic_info
[entry
->index
].array_id
== 0) {
3432 num_atomic_arrays
++;
3433 atomic_info
[entry
->index
].array_id
= num_atomic_arrays
;
3435 buffer
.array_id
= atomic_info
[entry
->index
].array_id
;
3438 buffer
.index
= index
;
3439 buffer
.index
+= location
->data
.offset
/ ATOMIC_COUNTER_SIZE
;
3440 buffer
.has_index2
= true;
3442 if (offset
.file
!= PROGRAM_UNDEFINED
) {
3443 buffer
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
3444 *buffer
.reladdr
= offset
;
3445 emit_arl(ir
, sampler_reladdr
, offset
);
3447 offset
= st_src_reg_for_int(0);
3451 st_src_reg
buffer(PROGRAM_BUFFER
, location
->data
.binding
,
3452 GLSL_TYPE_ATOMIC_UINT
);
3454 if (offset
.file
!= PROGRAM_UNDEFINED
) {
3455 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(offset
),
3456 offset
, st_src_reg_for_int(ATOMIC_COUNTER_SIZE
));
3457 emit_asm(ir
, TGSI_OPCODE_ADD
, st_dst_reg(offset
),
3458 offset
, st_src_reg_for_int(location
->data
.offset
+ index
* ATOMIC_COUNTER_SIZE
));
3460 offset
= st_src_reg_for_int(location
->data
.offset
+ index
* ATOMIC_COUNTER_SIZE
);
3465 ir
->return_deref
->accept(this);
3466 st_dst_reg
dst(this->result
);
3467 dst
.writemask
= WRITEMASK_X
;
3469 glsl_to_tgsi_instruction
*inst
;
3471 if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_read
) {
3472 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, offset
);
3473 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_increment
) {
3474 inst
= emit_asm(ir
, TGSI_OPCODE_ATOMUADD
, dst
, offset
,
3475 st_src_reg_for_int(1));
3476 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_predecrement
) {
3477 inst
= emit_asm(ir
, TGSI_OPCODE_ATOMUADD
, dst
, offset
,
3478 st_src_reg_for_int(-1));
3479 emit_asm(ir
, TGSI_OPCODE_ADD
, dst
, this->result
, st_src_reg_for_int(-1));
3481 param
= param
->get_next();
3482 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3485 st_src_reg data
= this->result
, data2
= undef_src
;
3486 enum tgsi_opcode opcode
;
3487 switch (ir
->callee
->intrinsic_id
) {
3488 case ir_intrinsic_atomic_counter_add
:
3489 opcode
= TGSI_OPCODE_ATOMUADD
;
3491 case ir_intrinsic_atomic_counter_min
:
3492 opcode
= TGSI_OPCODE_ATOMIMIN
;
3494 case ir_intrinsic_atomic_counter_max
:
3495 opcode
= TGSI_OPCODE_ATOMIMAX
;
3497 case ir_intrinsic_atomic_counter_and
:
3498 opcode
= TGSI_OPCODE_ATOMAND
;
3500 case ir_intrinsic_atomic_counter_or
:
3501 opcode
= TGSI_OPCODE_ATOMOR
;
3503 case ir_intrinsic_atomic_counter_xor
:
3504 opcode
= TGSI_OPCODE_ATOMXOR
;
3506 case ir_intrinsic_atomic_counter_exchange
:
3507 opcode
= TGSI_OPCODE_ATOMXCHG
;
3509 case ir_intrinsic_atomic_counter_comp_swap
: {
3510 opcode
= TGSI_OPCODE_ATOMCAS
;
3511 param
= param
->get_next();
3512 val
= ((ir_instruction
*)param
)->as_rvalue();
3514 data2
= this->result
;
3518 assert(!"Unexpected intrinsic");
3522 inst
= emit_asm(ir
, opcode
, dst
, offset
, data
, data2
);
3525 inst
->resource
= resource
;
3529 glsl_to_tgsi_visitor::visit_ssbo_intrinsic(ir_call
*ir
)
3531 exec_node
*param
= ir
->actual_parameters
.get_head();
3533 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
3535 param
= param
->get_next();
3536 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
3538 ir_constant
*const_block
= block
->as_constant();
3539 int buf_base
= st_context(ctx
)->has_hw_atomics
3540 ? 0 : ctx
->Const
.Program
[shader
->Stage
].MaxAtomicBuffers
;
3543 buf_base
+ (const_block
? const_block
->value
.u
[0] : 0),
3547 block
->accept(this);
3548 buffer
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
3549 *buffer
.reladdr
= this->result
;
3550 emit_arl(ir
, sampler_reladdr
, this->result
);
3553 /* Calculate the surface offset */
3554 offset
->accept(this);
3555 st_src_reg off
= this->result
;
3557 st_dst_reg dst
= undef_dst
;
3558 if (ir
->return_deref
) {
3559 ir
->return_deref
->accept(this);
3560 dst
= st_dst_reg(this->result
);
3561 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3564 glsl_to_tgsi_instruction
*inst
;
3566 if (ir
->callee
->intrinsic_id
== ir_intrinsic_ssbo_load
) {
3567 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, off
);
3568 if (dst
.type
== GLSL_TYPE_BOOL
)
3569 emit_asm(ir
, TGSI_OPCODE_USNE
, dst
, st_src_reg(dst
),
3570 st_src_reg_for_int(0));
3571 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_ssbo_store
) {
3572 param
= param
->get_next();
3573 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3576 param
= param
->get_next();
3577 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
3579 dst
.writemask
= write_mask
->value
.u
[0];
3581 dst
.type
= this->result
.type
;
3582 inst
= emit_asm(ir
, TGSI_OPCODE_STORE
, dst
, off
, this->result
);
3584 param
= param
->get_next();
3585 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3588 st_src_reg data
= this->result
, data2
= undef_src
;
3589 enum tgsi_opcode opcode
;
3590 switch (ir
->callee
->intrinsic_id
) {
3591 case ir_intrinsic_ssbo_atomic_add
:
3592 opcode
= TGSI_OPCODE_ATOMUADD
;
3594 case ir_intrinsic_ssbo_atomic_min
:
3595 opcode
= TGSI_OPCODE_ATOMIMIN
;
3597 case ir_intrinsic_ssbo_atomic_max
:
3598 opcode
= TGSI_OPCODE_ATOMIMAX
;
3600 case ir_intrinsic_ssbo_atomic_and
:
3601 opcode
= TGSI_OPCODE_ATOMAND
;
3603 case ir_intrinsic_ssbo_atomic_or
:
3604 opcode
= TGSI_OPCODE_ATOMOR
;
3606 case ir_intrinsic_ssbo_atomic_xor
:
3607 opcode
= TGSI_OPCODE_ATOMXOR
;
3609 case ir_intrinsic_ssbo_atomic_exchange
:
3610 opcode
= TGSI_OPCODE_ATOMXCHG
;
3612 case ir_intrinsic_ssbo_atomic_comp_swap
:
3613 opcode
= TGSI_OPCODE_ATOMCAS
;
3614 param
= param
->get_next();
3615 val
= ((ir_instruction
*)param
)->as_rvalue();
3617 data2
= this->result
;
3620 assert(!"Unexpected intrinsic");
3624 inst
= emit_asm(ir
, opcode
, dst
, off
, data
, data2
);
3627 param
= param
->get_next();
3628 ir_constant
*access
= NULL
;
3629 if (!param
->is_tail_sentinel()) {
3630 access
= ((ir_instruction
*)param
)->as_constant();
3634 add_buffer_to_load_and_stores(inst
, &buffer
, &this->instructions
, access
);
3638 glsl_to_tgsi_visitor::visit_membar_intrinsic(ir_call
*ir
)
3640 switch (ir
->callee
->intrinsic_id
) {
3641 case ir_intrinsic_memory_barrier
:
3642 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3643 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
|
3644 TGSI_MEMBAR_ATOMIC_BUFFER
|
3645 TGSI_MEMBAR_SHADER_IMAGE
|
3646 TGSI_MEMBAR_SHARED
));
3648 case ir_intrinsic_memory_barrier_atomic_counter
:
3649 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3650 st_src_reg_for_int(TGSI_MEMBAR_ATOMIC_BUFFER
));
3652 case ir_intrinsic_memory_barrier_buffer
:
3653 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3654 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
));
3656 case ir_intrinsic_memory_barrier_image
:
3657 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3658 st_src_reg_for_int(TGSI_MEMBAR_SHADER_IMAGE
));
3660 case ir_intrinsic_memory_barrier_shared
:
3661 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3662 st_src_reg_for_int(TGSI_MEMBAR_SHARED
));
3664 case ir_intrinsic_group_memory_barrier
:
3665 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3666 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
|
3667 TGSI_MEMBAR_ATOMIC_BUFFER
|
3668 TGSI_MEMBAR_SHADER_IMAGE
|
3669 TGSI_MEMBAR_SHARED
|
3670 TGSI_MEMBAR_THREAD_GROUP
));
3673 assert(!"Unexpected memory barrier intrinsic");
3678 glsl_to_tgsi_visitor::visit_shared_intrinsic(ir_call
*ir
)
3680 exec_node
*param
= ir
->actual_parameters
.get_head();
3682 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
3684 st_src_reg
buffer(PROGRAM_MEMORY
, 0, GLSL_TYPE_UINT
);
3686 /* Calculate the surface offset */
3687 offset
->accept(this);
3688 st_src_reg off
= this->result
;
3690 st_dst_reg dst
= undef_dst
;
3691 if (ir
->return_deref
) {
3692 ir
->return_deref
->accept(this);
3693 dst
= st_dst_reg(this->result
);
3694 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3697 glsl_to_tgsi_instruction
*inst
;
3699 if (ir
->callee
->intrinsic_id
== ir_intrinsic_shared_load
) {
3700 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, off
);
3701 inst
->resource
= buffer
;
3702 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_shared_store
) {
3703 param
= param
->get_next();
3704 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3707 param
= param
->get_next();
3708 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
3710 dst
.writemask
= write_mask
->value
.u
[0];
3712 dst
.type
= this->result
.type
;
3713 inst
= emit_asm(ir
, TGSI_OPCODE_STORE
, dst
, off
, this->result
);
3714 inst
->resource
= buffer
;
3716 param
= param
->get_next();
3717 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3720 st_src_reg data
= this->result
, data2
= undef_src
;
3721 enum tgsi_opcode opcode
;
3722 switch (ir
->callee
->intrinsic_id
) {
3723 case ir_intrinsic_shared_atomic_add
:
3724 opcode
= TGSI_OPCODE_ATOMUADD
;
3726 case ir_intrinsic_shared_atomic_min
:
3727 opcode
= TGSI_OPCODE_ATOMIMIN
;
3729 case ir_intrinsic_shared_atomic_max
:
3730 opcode
= TGSI_OPCODE_ATOMIMAX
;
3732 case ir_intrinsic_shared_atomic_and
:
3733 opcode
= TGSI_OPCODE_ATOMAND
;
3735 case ir_intrinsic_shared_atomic_or
:
3736 opcode
= TGSI_OPCODE_ATOMOR
;
3738 case ir_intrinsic_shared_atomic_xor
:
3739 opcode
= TGSI_OPCODE_ATOMXOR
;
3741 case ir_intrinsic_shared_atomic_exchange
:
3742 opcode
= TGSI_OPCODE_ATOMXCHG
;
3744 case ir_intrinsic_shared_atomic_comp_swap
:
3745 opcode
= TGSI_OPCODE_ATOMCAS
;
3746 param
= param
->get_next();
3747 val
= ((ir_instruction
*)param
)->as_rvalue();
3749 data2
= this->result
;
3752 assert(!"Unexpected intrinsic");
3756 inst
= emit_asm(ir
, opcode
, dst
, off
, data
, data2
);
3757 inst
->resource
= buffer
;
3762 get_image_qualifiers(ir_dereference
*ir
, const glsl_type
**type
,
3763 bool *memory_coherent
, bool *memory_volatile
,
3764 bool *memory_restrict
, bool *memory_read_only
,
3765 unsigned *image_format
)
3768 switch (ir
->ir_type
) {
3769 case ir_type_dereference_record
: {
3770 ir_dereference_record
*deref_record
= ir
->as_dereference_record();
3771 const glsl_type
*struct_type
= deref_record
->record
->type
;
3772 int fild_idx
= deref_record
->field_idx
;
3774 *type
= struct_type
->fields
.structure
[fild_idx
].type
->without_array();
3776 struct_type
->fields
.structure
[fild_idx
].memory_coherent
;
3778 struct_type
->fields
.structure
[fild_idx
].memory_volatile
;
3780 struct_type
->fields
.structure
[fild_idx
].memory_restrict
;
3782 struct_type
->fields
.structure
[fild_idx
].memory_read_only
;
3784 struct_type
->fields
.structure
[fild_idx
].image_format
;
3788 case ir_type_dereference_array
: {
3789 ir_dereference_array
*deref_arr
= ir
->as_dereference_array();
3790 get_image_qualifiers((ir_dereference
*)deref_arr
->array
, type
,
3791 memory_coherent
, memory_volatile
, memory_restrict
,
3792 memory_read_only
, image_format
);
3796 case ir_type_dereference_variable
: {
3797 ir_variable
*var
= ir
->variable_referenced();
3799 *type
= var
->type
->without_array();
3800 *memory_coherent
= var
->data
.memory_coherent
;
3801 *memory_volatile
= var
->data
.memory_volatile
;
3802 *memory_restrict
= var
->data
.memory_restrict
;
3803 *memory_read_only
= var
->data
.memory_read_only
;
3804 *image_format
= var
->data
.image_format
;
3814 glsl_to_tgsi_visitor::visit_image_intrinsic(ir_call
*ir
)
3816 exec_node
*param
= ir
->actual_parameters
.get_head();
3818 ir_dereference
*img
= (ir_dereference
*)param
;
3819 const ir_variable
*imgvar
= img
->variable_referenced();
3820 unsigned sampler_array_size
= 1, sampler_base
= 0;
3821 bool memory_coherent
= false, memory_volatile
= false,
3822 memory_restrict
= false, memory_read_only
= false;
3823 unsigned image_format
= 0;
3824 const glsl_type
*type
= NULL
;
3826 get_image_qualifiers(img
, &type
, &memory_coherent
, &memory_volatile
,
3827 &memory_restrict
, &memory_read_only
, &image_format
);
3830 st_src_reg
image(PROGRAM_IMAGE
, 0, GLSL_TYPE_UINT
);
3832 get_deref_offsets(img
, &sampler_array_size
, &sampler_base
,
3833 &index
, &reladdr
, !imgvar
->contains_bindless());
3835 image
.index
= index
;
3836 if (reladdr
.file
!= PROGRAM_UNDEFINED
) {
3837 image
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
3838 *image
.reladdr
= reladdr
;
3839 emit_arl(ir
, sampler_reladdr
, reladdr
);
3842 st_dst_reg dst
= undef_dst
;
3843 if (ir
->return_deref
) {
3844 ir
->return_deref
->accept(this);
3845 dst
= st_dst_reg(this->result
);
3846 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3849 glsl_to_tgsi_instruction
*inst
;
3851 st_src_reg bindless
;
3852 if (imgvar
->contains_bindless()) {
3854 bindless
= this->result
;
3857 if (ir
->callee
->intrinsic_id
== ir_intrinsic_image_size
) {
3858 dst
.writemask
= WRITEMASK_XYZ
;
3859 inst
= emit_asm(ir
, TGSI_OPCODE_RESQ
, dst
);
3860 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_image_samples
) {
3861 st_src_reg res
= get_temp(glsl_type::ivec4_type
);
3862 st_dst_reg dstres
= st_dst_reg(res
);
3863 dstres
.writemask
= WRITEMASK_W
;
3864 inst
= emit_asm(ir
, TGSI_OPCODE_RESQ
, dstres
);
3865 res
.swizzle
= SWIZZLE_WWWW
;
3866 emit_asm(ir
, TGSI_OPCODE_MOV
, dst
, res
);
3868 st_src_reg arg1
= undef_src
, arg2
= undef_src
;
3870 st_dst_reg coord_dst
;
3871 coord
= get_temp(glsl_type::ivec4_type
);
3872 coord_dst
= st_dst_reg(coord
);
3873 coord_dst
.writemask
= (1 << type
->coordinate_components()) - 1;
3874 param
= param
->get_next();
3875 ((ir_dereference
*)param
)->accept(this);
3876 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
3877 coord
.swizzle
= SWIZZLE_XXXX
;
3878 switch (type
->coordinate_components()) {
3879 case 4: assert(!"unexpected coord count");
3881 case 3: coord
.swizzle
|= SWIZZLE_Z
<< 6;
3883 case 2: coord
.swizzle
|= SWIZZLE_Y
<< 3;
3886 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
3887 param
= param
->get_next();
3888 ((ir_dereference
*)param
)->accept(this);
3889 st_src_reg sample
= this->result
;
3890 sample
.swizzle
= SWIZZLE_XXXX
;
3891 coord_dst
.writemask
= WRITEMASK_W
;
3892 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, sample
);
3893 coord
.swizzle
|= SWIZZLE_W
<< 9;
3896 param
= param
->get_next();
3897 if (!param
->is_tail_sentinel()) {
3898 ((ir_dereference
*)param
)->accept(this);
3899 arg1
= this->result
;
3900 param
= param
->get_next();
3903 if (!param
->is_tail_sentinel()) {
3904 ((ir_dereference
*)param
)->accept(this);
3905 arg2
= this->result
;
3906 param
= param
->get_next();
3909 assert(param
->is_tail_sentinel());
3911 enum tgsi_opcode opcode
;
3912 switch (ir
->callee
->intrinsic_id
) {
3913 case ir_intrinsic_image_load
:
3914 opcode
= TGSI_OPCODE_LOAD
;
3916 case ir_intrinsic_image_store
:
3917 opcode
= TGSI_OPCODE_STORE
;
3919 case ir_intrinsic_image_atomic_add
:
3920 opcode
= TGSI_OPCODE_ATOMUADD
;
3922 case ir_intrinsic_image_atomic_min
:
3923 opcode
= TGSI_OPCODE_ATOMIMIN
;
3925 case ir_intrinsic_image_atomic_max
:
3926 opcode
= TGSI_OPCODE_ATOMIMAX
;
3928 case ir_intrinsic_image_atomic_and
:
3929 opcode
= TGSI_OPCODE_ATOMAND
;
3931 case ir_intrinsic_image_atomic_or
:
3932 opcode
= TGSI_OPCODE_ATOMOR
;
3934 case ir_intrinsic_image_atomic_xor
:
3935 opcode
= TGSI_OPCODE_ATOMXOR
;
3937 case ir_intrinsic_image_atomic_exchange
:
3938 opcode
= TGSI_OPCODE_ATOMXCHG
;
3940 case ir_intrinsic_image_atomic_comp_swap
:
3941 opcode
= TGSI_OPCODE_ATOMCAS
;
3943 case ir_intrinsic_image_atomic_inc_wrap
: {
3944 /* There's a bit of disagreement between GLSL and the hardware. The
3945 * hardware wants to wrap after the given wrap value, while GLSL
3946 * wants to wrap at the value. Subtract 1 to make up the difference.
3948 st_src_reg wrap
= get_temp(glsl_type::uint_type
);
3949 emit_asm(ir
, TGSI_OPCODE_ADD
, st_dst_reg(wrap
),
3950 arg1
, st_src_reg_for_int(-1));
3952 opcode
= TGSI_OPCODE_ATOMINC_WRAP
;
3955 case ir_intrinsic_image_atomic_dec_wrap
:
3956 opcode
= TGSI_OPCODE_ATOMDEC_WRAP
;
3959 assert(!"Unexpected intrinsic");
3963 inst
= emit_asm(ir
, opcode
, dst
, coord
, arg1
, arg2
);
3964 if (opcode
== TGSI_OPCODE_STORE
)
3965 inst
->dst
[0].writemask
= WRITEMASK_XYZW
;
3968 if (imgvar
->contains_bindless()) {
3969 inst
->resource
= bindless
;
3970 inst
->resource
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
3971 SWIZZLE_X
, SWIZZLE_Y
);
3973 inst
->resource
= image
;
3974 inst
->sampler_array_size
= sampler_array_size
;
3975 inst
->sampler_base
= sampler_base
;
3978 inst
->tex_target
= type
->sampler_index();
3979 inst
->image_format
= st_mesa_format_to_pipe_format(st_context(ctx
),
3980 _mesa_get_shader_image_format(image_format
));
3981 inst
->read_only
= memory_read_only
;
3983 if (memory_coherent
)
3984 inst
->buffer_access
|= TGSI_MEMORY_COHERENT
;
3985 if (memory_restrict
)
3986 inst
->buffer_access
|= TGSI_MEMORY_RESTRICT
;
3987 if (memory_volatile
)
3988 inst
->buffer_access
|= TGSI_MEMORY_VOLATILE
;
3992 glsl_to_tgsi_visitor::visit_generic_intrinsic(ir_call
*ir
, enum tgsi_opcode op
)
3994 ir
->return_deref
->accept(this);
3995 st_dst_reg dst
= st_dst_reg(this->result
);
3997 dst
.writemask
= u_bit_consecutive(0, ir
->return_deref
->var
->type
->vector_elements
);
3999 st_src_reg src
[4] = { undef_src
, undef_src
, undef_src
, undef_src
};
4000 unsigned num_src
= 0;
4001 foreach_in_list(ir_rvalue
, param
, &ir
->actual_parameters
) {
4002 assert(num_src
< ARRAY_SIZE(src
));
4004 this->result
.file
= PROGRAM_UNDEFINED
;
4005 param
->accept(this);
4006 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
4008 src
[num_src
] = this->result
;
4012 emit_asm(ir
, op
, dst
, src
[0], src
[1], src
[2], src
[3]);
4016 glsl_to_tgsi_visitor::visit(ir_call
*ir
)
4018 ir_function_signature
*sig
= ir
->callee
;
4020 /* Filter out intrinsics */
4021 switch (sig
->intrinsic_id
) {
4022 case ir_intrinsic_atomic_counter_read
:
4023 case ir_intrinsic_atomic_counter_increment
:
4024 case ir_intrinsic_atomic_counter_predecrement
:
4025 case ir_intrinsic_atomic_counter_add
:
4026 case ir_intrinsic_atomic_counter_min
:
4027 case ir_intrinsic_atomic_counter_max
:
4028 case ir_intrinsic_atomic_counter_and
:
4029 case ir_intrinsic_atomic_counter_or
:
4030 case ir_intrinsic_atomic_counter_xor
:
4031 case ir_intrinsic_atomic_counter_exchange
:
4032 case ir_intrinsic_atomic_counter_comp_swap
:
4033 visit_atomic_counter_intrinsic(ir
);
4036 case ir_intrinsic_ssbo_load
:
4037 case ir_intrinsic_ssbo_store
:
4038 case ir_intrinsic_ssbo_atomic_add
:
4039 case ir_intrinsic_ssbo_atomic_min
:
4040 case ir_intrinsic_ssbo_atomic_max
:
4041 case ir_intrinsic_ssbo_atomic_and
:
4042 case ir_intrinsic_ssbo_atomic_or
:
4043 case ir_intrinsic_ssbo_atomic_xor
:
4044 case ir_intrinsic_ssbo_atomic_exchange
:
4045 case ir_intrinsic_ssbo_atomic_comp_swap
:
4046 visit_ssbo_intrinsic(ir
);
4049 case ir_intrinsic_memory_barrier
:
4050 case ir_intrinsic_memory_barrier_atomic_counter
:
4051 case ir_intrinsic_memory_barrier_buffer
:
4052 case ir_intrinsic_memory_barrier_image
:
4053 case ir_intrinsic_memory_barrier_shared
:
4054 case ir_intrinsic_group_memory_barrier
:
4055 visit_membar_intrinsic(ir
);
4058 case ir_intrinsic_shared_load
:
4059 case ir_intrinsic_shared_store
:
4060 case ir_intrinsic_shared_atomic_add
:
4061 case ir_intrinsic_shared_atomic_min
:
4062 case ir_intrinsic_shared_atomic_max
:
4063 case ir_intrinsic_shared_atomic_and
:
4064 case ir_intrinsic_shared_atomic_or
:
4065 case ir_intrinsic_shared_atomic_xor
:
4066 case ir_intrinsic_shared_atomic_exchange
:
4067 case ir_intrinsic_shared_atomic_comp_swap
:
4068 visit_shared_intrinsic(ir
);
4071 case ir_intrinsic_image_load
:
4072 case ir_intrinsic_image_store
:
4073 case ir_intrinsic_image_atomic_add
:
4074 case ir_intrinsic_image_atomic_min
:
4075 case ir_intrinsic_image_atomic_max
:
4076 case ir_intrinsic_image_atomic_and
:
4077 case ir_intrinsic_image_atomic_or
:
4078 case ir_intrinsic_image_atomic_xor
:
4079 case ir_intrinsic_image_atomic_exchange
:
4080 case ir_intrinsic_image_atomic_comp_swap
:
4081 case ir_intrinsic_image_size
:
4082 case ir_intrinsic_image_samples
:
4083 case ir_intrinsic_image_atomic_inc_wrap
:
4084 case ir_intrinsic_image_atomic_dec_wrap
:
4085 visit_image_intrinsic(ir
);
4088 case ir_intrinsic_shader_clock
:
4089 visit_generic_intrinsic(ir
, TGSI_OPCODE_CLOCK
);
4092 case ir_intrinsic_vote_all
:
4093 visit_generic_intrinsic(ir
, TGSI_OPCODE_VOTE_ALL
);
4095 case ir_intrinsic_vote_any
:
4096 visit_generic_intrinsic(ir
, TGSI_OPCODE_VOTE_ANY
);
4098 case ir_intrinsic_vote_eq
:
4099 visit_generic_intrinsic(ir
, TGSI_OPCODE_VOTE_EQ
);
4101 case ir_intrinsic_ballot
:
4102 visit_generic_intrinsic(ir
, TGSI_OPCODE_BALLOT
);
4104 case ir_intrinsic_read_first_invocation
:
4105 visit_generic_intrinsic(ir
, TGSI_OPCODE_READ_FIRST
);
4107 case ir_intrinsic_read_invocation
:
4108 visit_generic_intrinsic(ir
, TGSI_OPCODE_READ_INVOC
);
4111 case ir_intrinsic_helper_invocation
:
4112 visit_generic_intrinsic(ir
, TGSI_OPCODE_READ_HELPER
);
4115 case ir_intrinsic_invalid
:
4116 case ir_intrinsic_generic_load
:
4117 case ir_intrinsic_generic_store
:
4118 case ir_intrinsic_generic_atomic_add
:
4119 case ir_intrinsic_generic_atomic_and
:
4120 case ir_intrinsic_generic_atomic_or
:
4121 case ir_intrinsic_generic_atomic_xor
:
4122 case ir_intrinsic_generic_atomic_min
:
4123 case ir_intrinsic_generic_atomic_max
:
4124 case ir_intrinsic_generic_atomic_exchange
:
4125 case ir_intrinsic_generic_atomic_comp_swap
:
4126 case ir_intrinsic_begin_invocation_interlock
:
4127 case ir_intrinsic_end_invocation_interlock
:
4128 unreachable("Invalid intrinsic");
4133 glsl_to_tgsi_visitor::calc_deref_offsets(ir_dereference
*tail
,
4134 unsigned *array_elements
,
4136 st_src_reg
*indirect
,
4139 switch (tail
->ir_type
) {
4140 case ir_type_dereference_record
: {
4141 ir_dereference_record
*deref_record
= tail
->as_dereference_record();
4142 const glsl_type
*struct_type
= deref_record
->record
->type
;
4143 int field_index
= deref_record
->field_idx
;
4145 calc_deref_offsets(deref_record
->record
->as_dereference(), array_elements
, index
, indirect
, location
);
4147 assert(field_index
>= 0);
4148 *location
+= struct_type
->struct_location_offset(field_index
);
4152 case ir_type_dereference_array
: {
4153 ir_dereference_array
*deref_arr
= tail
->as_dereference_array();
4155 void *mem_ctx
= ralloc_parent(deref_arr
);
4156 ir_constant
*array_index
=
4157 deref_arr
->array_index
->constant_expression_value(mem_ctx
);
4160 st_src_reg temp_reg
;
4161 st_dst_reg temp_dst
;
4163 temp_reg
= get_temp(glsl_type::uint_type
);
4164 temp_dst
= st_dst_reg(temp_reg
);
4165 temp_dst
.writemask
= 1;
4167 deref_arr
->array_index
->accept(this);
4168 if (*array_elements
!= 1)
4169 emit_asm(NULL
, TGSI_OPCODE_MUL
, temp_dst
, this->result
, st_src_reg_for_int(*array_elements
));
4171 emit_asm(NULL
, TGSI_OPCODE_MOV
, temp_dst
, this->result
);
4173 if (indirect
->file
== PROGRAM_UNDEFINED
)
4174 *indirect
= temp_reg
;
4176 temp_dst
= st_dst_reg(*indirect
);
4177 temp_dst
.writemask
= 1;
4178 emit_asm(NULL
, TGSI_OPCODE_ADD
, temp_dst
, *indirect
, temp_reg
);
4181 *index
+= array_index
->value
.u
[0] * *array_elements
;
4183 *array_elements
*= deref_arr
->array
->type
->length
;
4185 calc_deref_offsets(deref_arr
->array
->as_dereference(), array_elements
, index
, indirect
, location
);
4194 glsl_to_tgsi_visitor::get_deref_offsets(ir_dereference
*ir
,
4195 unsigned *array_size
,
4198 st_src_reg
*reladdr
,
4201 GLuint shader
= _mesa_program_enum_to_shader_stage(this->prog
->Target
);
4202 unsigned location
= 0;
4203 ir_variable
*var
= ir
->variable_referenced();
4211 location
= var
->data
.location
;
4212 calc_deref_offsets(ir
, array_size
, index
, reladdr
, &location
);
4215 * If we end up with no indirect then adjust the base to the index,
4216 * and set the array size to 1.
4218 if (reladdr
->file
== PROGRAM_UNDEFINED
) {
4224 assert(location
!= 0xffffffff);
4225 *base
+= this->shader_program
->data
->UniformStorage
[location
].opaque
[shader
].index
;
4226 *index
+= this->shader_program
->data
->UniformStorage
[location
].opaque
[shader
].index
;
4231 glsl_to_tgsi_visitor::canonicalize_gather_offset(st_src_reg offset
)
4233 if (offset
.reladdr
|| offset
.reladdr2
||
4234 offset
.has_index2
||
4235 offset
.file
== PROGRAM_UNIFORM
||
4236 offset
.file
== PROGRAM_CONSTANT
||
4237 offset
.file
== PROGRAM_STATE_VAR
) {
4238 st_src_reg tmp
= get_temp(glsl_type::ivec2_type
);
4239 st_dst_reg tmp_dst
= st_dst_reg(tmp
);
4240 tmp_dst
.writemask
= WRITEMASK_XY
;
4241 emit_asm(NULL
, TGSI_OPCODE_MOV
, tmp_dst
, offset
);
4249 glsl_to_tgsi_visitor::handle_bound_deref(ir_dereference
*ir
)
4251 ir_variable
*var
= ir
->variable_referenced();
4253 if (!var
|| var
->data
.mode
!= ir_var_uniform
|| var
->data
.bindless
||
4254 !(ir
->type
->is_image() || ir
->type
->is_sampler()))
4257 /* Convert from bound sampler/image to bindless handle. */
4258 bool is_image
= ir
->type
->is_image();
4259 st_src_reg
resource(is_image
? PROGRAM_IMAGE
: PROGRAM_SAMPLER
, 0, GLSL_TYPE_UINT
);
4261 unsigned array_size
= 1, base
= 0;
4263 get_deref_offsets(ir
, &array_size
, &base
, &index
, &reladdr
, true);
4265 resource
.index
= index
;
4266 if (reladdr
.file
!= PROGRAM_UNDEFINED
) {
4267 resource
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
4268 *resource
.reladdr
= reladdr
;
4269 emit_arl(ir
, sampler_reladdr
, reladdr
);
4272 this->result
= get_temp(glsl_type::uvec2_type
);
4273 st_dst_reg
dst(this->result
);
4274 dst
.writemask
= WRITEMASK_XY
;
4276 glsl_to_tgsi_instruction
*inst
= emit_asm(
4277 ir
, is_image
? TGSI_OPCODE_IMG2HND
: TGSI_OPCODE_SAMP2HND
, dst
);
4279 inst
->tex_target
= ir
->type
->sampler_index();
4280 inst
->resource
= resource
;
4281 inst
->sampler_array_size
= array_size
;
4282 inst
->sampler_base
= base
;
4288 glsl_to_tgsi_visitor::visit(ir_texture
*ir
)
4290 st_src_reg result_src
, coord
, cube_sc
, lod_info
, projector
, dx
, dy
;
4291 st_src_reg offset
[MAX_GLSL_TEXTURE_OFFSET
], sample_index
, component
;
4292 st_src_reg levels_src
, reladdr
;
4293 st_dst_reg result_dst
, coord_dst
, cube_sc_dst
;
4294 glsl_to_tgsi_instruction
*inst
= NULL
;
4295 enum tgsi_opcode opcode
= TGSI_OPCODE_NOP
;
4296 const glsl_type
*sampler_type
= ir
->sampler
->type
;
4297 unsigned sampler_array_size
= 1, sampler_base
= 0;
4298 bool is_cube_array
= false, is_cube_shadow
= false;
4299 ir_variable
*var
= ir
->sampler
->variable_referenced();
4302 /* if we are a cube array sampler or a cube shadow */
4303 if (sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
) {
4304 is_cube_array
= sampler_type
->sampler_array
;
4305 is_cube_shadow
= sampler_type
->sampler_shadow
;
4308 if (ir
->coordinate
) {
4309 ir
->coordinate
->accept(this);
4311 /* Put our coords in a temp. We'll need to modify them for shadow,
4312 * projection, or LOD, so the only case we'd use it as-is is if
4313 * we're doing plain old texturing. The optimization passes on
4314 * glsl_to_tgsi_visitor should handle cleaning up our mess in that case.
4316 coord
= get_temp(glsl_type::vec4_type
);
4317 coord_dst
= st_dst_reg(coord
);
4318 coord_dst
.writemask
= (1 << ir
->coordinate
->type
->vector_elements
) - 1;
4319 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
4322 if (ir
->projector
) {
4323 ir
->projector
->accept(this);
4324 projector
= this->result
;
4327 /* Storage for our result. Ideally for an assignment we'd be using
4328 * the actual storage for the result here, instead.
4330 result_src
= get_temp(ir
->type
);
4331 result_dst
= st_dst_reg(result_src
);
4332 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
4336 opcode
= (is_cube_array
&& ir
->shadow_comparator
) ? TGSI_OPCODE_TEX2
: TGSI_OPCODE_TEX
;
4338 ir
->offset
->accept(this);
4339 offset
[0] = this->result
;
4343 if (is_cube_array
|| is_cube_shadow
) {
4344 opcode
= TGSI_OPCODE_TXB2
;
4347 opcode
= TGSI_OPCODE_TXB
;
4349 ir
->lod_info
.bias
->accept(this);
4350 lod_info
= this->result
;
4352 ir
->offset
->accept(this);
4353 offset
[0] = this->result
;
4357 if (this->has_tex_txf_lz
&& ir
->lod_info
.lod
->is_zero()) {
4358 opcode
= TGSI_OPCODE_TEX_LZ
;
4360 opcode
= is_cube_array
? TGSI_OPCODE_TXL2
: TGSI_OPCODE_TXL
;
4361 ir
->lod_info
.lod
->accept(this);
4362 lod_info
= this->result
;
4365 ir
->offset
->accept(this);
4366 offset
[0] = this->result
;
4370 opcode
= TGSI_OPCODE_TXD
;
4371 ir
->lod_info
.grad
.dPdx
->accept(this);
4373 ir
->lod_info
.grad
.dPdy
->accept(this);
4376 ir
->offset
->accept(this);
4377 offset
[0] = this->result
;
4381 opcode
= TGSI_OPCODE_TXQ
;
4382 ir
->lod_info
.lod
->accept(this);
4383 lod_info
= this->result
;
4385 case ir_query_levels
:
4386 opcode
= TGSI_OPCODE_TXQ
;
4387 lod_info
= undef_src
;
4388 levels_src
= get_temp(ir
->type
);
4391 if (this->has_tex_txf_lz
&& ir
->lod_info
.lod
->is_zero()) {
4392 opcode
= TGSI_OPCODE_TXF_LZ
;
4394 opcode
= TGSI_OPCODE_TXF
;
4395 ir
->lod_info
.lod
->accept(this);
4396 lod_info
= this->result
;
4399 ir
->offset
->accept(this);
4400 offset
[0] = this->result
;
4404 opcode
= TGSI_OPCODE_TXF
;
4405 ir
->lod_info
.sample_index
->accept(this);
4406 sample_index
= this->result
;
4409 opcode
= TGSI_OPCODE_TG4
;
4410 ir
->lod_info
.component
->accept(this);
4411 component
= this->result
;
4413 ir
->offset
->accept(this);
4414 if (ir
->offset
->type
->is_array()) {
4415 const glsl_type
*elt_type
= ir
->offset
->type
->fields
.array
;
4416 for (i
= 0; i
< ir
->offset
->type
->length
; i
++) {
4417 offset
[i
] = this->result
;
4418 offset
[i
].index
+= i
* type_size(elt_type
);
4419 offset
[i
].type
= elt_type
->base_type
;
4420 offset
[i
].swizzle
= swizzle_for_size(elt_type
->vector_elements
);
4421 offset
[i
] = canonicalize_gather_offset(offset
[i
]);
4424 offset
[0] = canonicalize_gather_offset(this->result
);
4429 opcode
= TGSI_OPCODE_LODQ
;
4431 case ir_texture_samples
:
4432 opcode
= TGSI_OPCODE_TXQS
;
4434 case ir_samples_identical
:
4435 unreachable("Unexpected ir_samples_identical opcode");
4438 if (ir
->projector
) {
4439 if (opcode
== TGSI_OPCODE_TEX
) {
4440 /* Slot the projector in as the last component of the coord. */
4441 coord_dst
.writemask
= WRITEMASK_W
;
4442 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, projector
);
4443 coord_dst
.writemask
= WRITEMASK_XYZW
;
4444 opcode
= TGSI_OPCODE_TXP
;
4446 st_src_reg coord_w
= coord
;
4447 coord_w
.swizzle
= SWIZZLE_WWWW
;
4449 /* For the other TEX opcodes there's no projective version
4450 * since the last slot is taken up by LOD info. Do the
4451 * projective divide now.
4453 coord_dst
.writemask
= WRITEMASK_W
;
4454 emit_asm(ir
, TGSI_OPCODE_RCP
, coord_dst
, projector
);
4456 /* In the case where we have to project the coordinates "by hand,"
4457 * the shadow comparator value must also be projected.
4459 st_src_reg tmp_src
= coord
;
4460 if (ir
->shadow_comparator
) {
4461 /* Slot the shadow value in as the second to last component of the
4464 ir
->shadow_comparator
->accept(this);
4466 tmp_src
= get_temp(glsl_type::vec4_type
);
4467 st_dst_reg tmp_dst
= st_dst_reg(tmp_src
);
4469 /* Projective division not allowed for array samplers. */
4470 assert(!sampler_type
->sampler_array
);
4472 tmp_dst
.writemask
= WRITEMASK_Z
;
4473 emit_asm(ir
, TGSI_OPCODE_MOV
, tmp_dst
, this->result
);
4475 tmp_dst
.writemask
= WRITEMASK_XY
;
4476 emit_asm(ir
, TGSI_OPCODE_MOV
, tmp_dst
, coord
);
4479 coord_dst
.writemask
= WRITEMASK_XYZ
;
4480 emit_asm(ir
, TGSI_OPCODE_MUL
, coord_dst
, tmp_src
, coord_w
);
4482 coord_dst
.writemask
= WRITEMASK_XYZW
;
4483 coord
.swizzle
= SWIZZLE_XYZW
;
4487 /* If projection is done and the opcode is not TGSI_OPCODE_TXP, then the
4488 * shadow comparator was put in the correct place (and projected) by the
4489 * code, above, that handles by-hand projection.
4491 if (ir
->shadow_comparator
&& (!ir
->projector
|| opcode
== TGSI_OPCODE_TXP
)) {
4492 /* Slot the shadow value in as the second to last component of the
4495 ir
->shadow_comparator
->accept(this);
4497 if (is_cube_array
) {
4498 cube_sc
= get_temp(glsl_type::float_type
);
4499 cube_sc_dst
= st_dst_reg(cube_sc
);
4500 cube_sc_dst
.writemask
= WRITEMASK_X
;
4501 emit_asm(ir
, TGSI_OPCODE_MOV
, cube_sc_dst
, this->result
);
4502 cube_sc_dst
.writemask
= WRITEMASK_X
;
4505 if ((sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_2D
&&
4506 sampler_type
->sampler_array
) ||
4507 sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
) {
4508 coord_dst
.writemask
= WRITEMASK_W
;
4510 coord_dst
.writemask
= WRITEMASK_Z
;
4512 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
4513 coord_dst
.writemask
= WRITEMASK_XYZW
;
4517 if (ir
->op
== ir_txf_ms
) {
4518 coord_dst
.writemask
= WRITEMASK_W
;
4519 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, sample_index
);
4520 coord_dst
.writemask
= WRITEMASK_XYZW
;
4521 } else if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXB
||
4522 opcode
== TGSI_OPCODE_TXF
) {
4523 /* TGSI stores LOD or LOD bias in the last channel of the coords. */
4524 coord_dst
.writemask
= WRITEMASK_W
;
4525 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, lod_info
);
4526 coord_dst
.writemask
= WRITEMASK_XYZW
;
4529 st_src_reg
sampler(PROGRAM_SAMPLER
, 0, GLSL_TYPE_UINT
);
4532 get_deref_offsets(ir
->sampler
, &sampler_array_size
, &sampler_base
,
4533 &index
, &reladdr
, !var
->contains_bindless());
4535 sampler
.index
= index
;
4536 if (reladdr
.file
!= PROGRAM_UNDEFINED
) {
4537 sampler
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
4538 *sampler
.reladdr
= reladdr
;
4539 emit_arl(ir
, sampler_reladdr
, reladdr
);
4542 st_src_reg bindless
;
4543 if (var
->contains_bindless()) {
4544 ir
->sampler
->accept(this);
4545 bindless
= this->result
;
4548 if (opcode
== TGSI_OPCODE_TXD
)
4549 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, dx
, dy
);
4550 else if (opcode
== TGSI_OPCODE_TXQ
) {
4551 if (ir
->op
== ir_query_levels
) {
4552 /* the level is stored in W */
4553 inst
= emit_asm(ir
, opcode
, st_dst_reg(levels_src
), lod_info
);
4554 result_dst
.writemask
= WRITEMASK_X
;
4555 levels_src
.swizzle
= SWIZZLE_WWWW
;
4556 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, levels_src
);
4558 inst
= emit_asm(ir
, opcode
, result_dst
, lod_info
);
4559 } else if (opcode
== TGSI_OPCODE_TXQS
) {
4560 inst
= emit_asm(ir
, opcode
, result_dst
);
4561 } else if (opcode
== TGSI_OPCODE_TXL2
|| opcode
== TGSI_OPCODE_TXB2
) {
4562 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, lod_info
);
4563 } else if (opcode
== TGSI_OPCODE_TEX2
) {
4564 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, cube_sc
);
4565 } else if (opcode
== TGSI_OPCODE_TG4
) {
4566 if (is_cube_array
&& ir
->shadow_comparator
) {
4567 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, cube_sc
);
4569 if (this->tg4_component_in_swizzle
) {
4570 inst
= emit_asm(ir
, opcode
, result_dst
, coord
);
4572 foreach_in_list(immediate_storage
, entry
, &this->immediates
) {
4573 if (component
.index
== idx
) {
4574 gl_constant_value value
= entry
->values
[component
.swizzle
];
4575 inst
->gather_component
= value
.i
;
4581 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, component
);
4585 inst
= emit_asm(ir
, opcode
, result_dst
, coord
);
4587 if (ir
->shadow_comparator
)
4588 inst
->tex_shadow
= GL_TRUE
;
4590 if (var
->contains_bindless()) {
4591 inst
->resource
= bindless
;
4592 inst
->resource
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
4593 SWIZZLE_X
, SWIZZLE_Y
);
4595 inst
->resource
= sampler
;
4596 inst
->sampler_array_size
= sampler_array_size
;
4597 inst
->sampler_base
= sampler_base
;
4601 if (!inst
->tex_offsets
)
4602 inst
->tex_offsets
= rzalloc_array(inst
, st_src_reg
,
4603 MAX_GLSL_TEXTURE_OFFSET
);
4605 for (i
= 0; i
< MAX_GLSL_TEXTURE_OFFSET
&&
4606 offset
[i
].file
!= PROGRAM_UNDEFINED
; i
++)
4607 inst
->tex_offsets
[i
] = offset
[i
];
4608 inst
->tex_offset_num_offset
= i
;
4611 inst
->tex_target
= sampler_type
->sampler_index();
4612 inst
->tex_type
= ir
->type
->base_type
;
4614 this->result
= result_src
;
4618 glsl_to_tgsi_visitor::visit(ir_return
*ir
)
4620 assert(!ir
->get_value());
4622 emit_asm(ir
, TGSI_OPCODE_RET
);
4626 glsl_to_tgsi_visitor::visit(ir_discard
*ir
)
4628 if (ir
->condition
) {
4629 ir
->condition
->accept(this);
4630 st_src_reg condition
= this->result
;
4632 /* Convert the bool condition to a float so we can negate. */
4633 if (native_integers
) {
4634 st_src_reg temp
= get_temp(ir
->condition
->type
);
4635 emit_asm(ir
, TGSI_OPCODE_AND
, st_dst_reg(temp
),
4636 condition
, st_src_reg_for_float(1.0));
4640 condition
.negate
= ~condition
.negate
;
4641 emit_asm(ir
, TGSI_OPCODE_KILL_IF
, undef_dst
, condition
);
4643 /* unconditional kil */
4644 emit_asm(ir
, TGSI_OPCODE_KILL
);
4649 glsl_to_tgsi_visitor::visit(ir_demote
*ir
)
4651 emit_asm(ir
, TGSI_OPCODE_DEMOTE
);
4655 glsl_to_tgsi_visitor::visit(ir_if
*ir
)
4657 enum tgsi_opcode if_opcode
;
4658 glsl_to_tgsi_instruction
*if_inst
;
4660 ir
->condition
->accept(this);
4661 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
4663 if_opcode
= native_integers
? TGSI_OPCODE_UIF
: TGSI_OPCODE_IF
;
4665 if_inst
= emit_asm(ir
->condition
, if_opcode
, undef_dst
, this->result
);
4667 this->instructions
.push_tail(if_inst
);
4669 visit_exec_list(&ir
->then_instructions
, this);
4671 if (!ir
->else_instructions
.is_empty()) {
4672 emit_asm(ir
->condition
, TGSI_OPCODE_ELSE
);
4673 visit_exec_list(&ir
->else_instructions
, this);
4676 if_inst
= emit_asm(ir
->condition
, TGSI_OPCODE_ENDIF
);
4681 glsl_to_tgsi_visitor::visit(ir_emit_vertex
*ir
)
4683 assert(this->prog
->Target
== GL_GEOMETRY_PROGRAM_NV
);
4685 ir
->stream
->accept(this);
4686 emit_asm(ir
, TGSI_OPCODE_EMIT
, undef_dst
, this->result
);
4690 glsl_to_tgsi_visitor::visit(ir_end_primitive
*ir
)
4692 assert(this->prog
->Target
== GL_GEOMETRY_PROGRAM_NV
);
4694 ir
->stream
->accept(this);
4695 emit_asm(ir
, TGSI_OPCODE_ENDPRIM
, undef_dst
, this->result
);
4699 glsl_to_tgsi_visitor::visit(ir_barrier
*ir
)
4701 assert(this->prog
->Target
== GL_TESS_CONTROL_PROGRAM_NV
||
4702 this->prog
->Target
== GL_COMPUTE_PROGRAM_NV
);
4704 emit_asm(ir
, TGSI_OPCODE_BARRIER
);
4707 glsl_to_tgsi_visitor::glsl_to_tgsi_visitor()
4709 STATIC_ASSERT(sizeof(samplers_used
) * 8 >= PIPE_MAX_SAMPLERS
);
4711 result
.file
= PROGRAM_UNDEFINED
;
4718 num_input_arrays
= 0;
4719 num_output_arrays
= 0;
4721 num_atomic_arrays
= 0;
4723 num_address_regs
= 0;
4726 indirect_addr_consts
= false;
4727 wpos_transform_const
= -1;
4728 native_integers
= false;
4729 mem_ctx
= ralloc_context(NULL
);
4734 tg4_component_in_swizzle
= false;
4735 shader_program
= NULL
;
4740 use_shared_memory
= false;
4741 has_tex_txf_lz
= false;
4745 static void var_destroy(struct hash_entry
*entry
)
4747 variable_storage
*storage
= (variable_storage
*)entry
->data
;
4752 glsl_to_tgsi_visitor::~glsl_to_tgsi_visitor()
4754 _mesa_hash_table_destroy(variables
, var_destroy
);
4756 ralloc_free(mem_ctx
);
4759 extern "C" void free_glsl_to_tgsi_visitor(glsl_to_tgsi_visitor
*v
)
4766 * Count resources used by the given gpu program (number of texture
4770 count_resources(glsl_to_tgsi_visitor
*v
, gl_program
*prog
)
4772 v
->samplers_used
= 0;
4774 prog
->info
.textures_used_by_txf
= 0;
4776 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &v
->instructions
) {
4777 if (inst
->info
->is_tex
) {
4778 for (int i
= 0; i
< inst
->sampler_array_size
; i
++) {
4779 unsigned idx
= inst
->sampler_base
+ i
;
4780 v
->samplers_used
|= 1u << idx
;
4782 debug_assert(idx
< (int)ARRAY_SIZE(v
->sampler_types
));
4783 v
->sampler_types
[idx
] = inst
->tex_type
;
4784 v
->sampler_targets
[idx
] =
4785 st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
4787 if (inst
->op
== TGSI_OPCODE_TXF
|| inst
->op
== TGSI_OPCODE_TXF_LZ
) {
4788 prog
->info
.textures_used_by_txf
|= 1u << idx
;
4793 if (inst
->tex_target
== TEXTURE_EXTERNAL_INDEX
)
4794 prog
->ExternalSamplersUsed
|= 1 << inst
->resource
.index
;
4796 if (inst
->resource
.file
!= PROGRAM_UNDEFINED
&& (
4797 is_resource_instruction(inst
->op
) ||
4798 inst
->op
== TGSI_OPCODE_STORE
)) {
4799 if (inst
->resource
.file
== PROGRAM_MEMORY
) {
4800 v
->use_shared_memory
= true;
4801 } else if (inst
->resource
.file
== PROGRAM_IMAGE
) {
4802 for (int i
= 0; i
< inst
->sampler_array_size
; i
++) {
4803 unsigned idx
= inst
->sampler_base
+ i
;
4804 v
->images_used
|= 1 << idx
;
4805 v
->image_targets
[idx
] =
4806 st_translate_texture_target(inst
->tex_target
, false);
4807 v
->image_formats
[idx
] = inst
->image_format
;
4808 v
->image_wr
[idx
] = !inst
->read_only
;
4813 prog
->SamplersUsed
= v
->samplers_used
;
4815 if (v
->shader_program
!= NULL
)
4816 _mesa_update_shader_textures_used(v
->shader_program
, prog
);
4820 * Returns the mask of channels (bitmask of WRITEMASK_X,Y,Z,W) which
4821 * are read from the given src in this instruction
4824 get_src_arg_mask(st_dst_reg dst
, st_src_reg src
)
4826 int read_mask
= 0, comp
;
4828 /* Now, given the src swizzle and the written channels, find which
4829 * components are actually read
4831 for (comp
= 0; comp
< 4; ++comp
) {
4832 const unsigned coord
= GET_SWZ(src
.swizzle
, comp
);
4834 if (dst
.writemask
& (1 << comp
) && coord
<= SWIZZLE_W
)
4835 read_mask
|= 1 << coord
;
4842 * This pass replaces CMP T0, T1 T2 T0 with MOV T0, T2 when the CMP
4843 * instruction is the first instruction to write to register T0. There are
4844 * several lowering passes done in GLSL IR (e.g. branches and
4845 * relative addressing) that create a large number of conditional assignments
4846 * that ir_to_mesa converts to CMP instructions like the one mentioned above.
4848 * Here is why this conversion is safe:
4849 * CMP T0, T1 T2 T0 can be expanded to:
4855 * If (T1 < 0.0) evaluates to true then our replacement MOV T0, T2 is the same
4856 * as the original program. If (T1 < 0.0) evaluates to false, executing
4857 * MOV T0, T0 will store a garbage value in T0 since T0 is uninitialized.
4858 * Therefore, it doesn't matter that we are replacing MOV T0, T0 with MOV T0, T2
4859 * because any instruction that was going to read from T0 after this was going
4860 * to read a garbage value anyway.
4863 glsl_to_tgsi_visitor::simplify_cmp(void)
4865 int tempWritesSize
= 0;
4866 unsigned *tempWrites
= NULL
;
4867 unsigned outputWrites
[VARYING_SLOT_TESS_MAX
];
4869 memset(outputWrites
, 0, sizeof(outputWrites
));
4871 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4872 unsigned prevWriteMask
= 0;
4874 /* Give up if we encounter relative addressing or flow control. */
4875 if (inst
->dst
[0].reladdr
|| inst
->dst
[0].reladdr2
||
4876 inst
->dst
[1].reladdr
|| inst
->dst
[1].reladdr2
||
4877 inst
->info
->is_branch
||
4878 inst
->op
== TGSI_OPCODE_CONT
||
4879 inst
->op
== TGSI_OPCODE_END
||
4880 inst
->op
== TGSI_OPCODE_RET
) {
4884 if (inst
->dst
[0].file
== PROGRAM_OUTPUT
) {
4885 assert(inst
->dst
[0].index
< (signed)ARRAY_SIZE(outputWrites
));
4886 prevWriteMask
= outputWrites
[inst
->dst
[0].index
];
4887 outputWrites
[inst
->dst
[0].index
] |= inst
->dst
[0].writemask
;
4888 } else if (inst
->dst
[0].file
== PROGRAM_TEMPORARY
) {
4889 if (inst
->dst
[0].index
>= tempWritesSize
) {
4890 const int inc
= 4096;
4892 tempWrites
= (unsigned*)
4894 (tempWritesSize
+ inc
) * sizeof(unsigned));
4898 memset(tempWrites
+ tempWritesSize
, 0, inc
* sizeof(unsigned));
4899 tempWritesSize
+= inc
;
4902 prevWriteMask
= tempWrites
[inst
->dst
[0].index
];
4903 tempWrites
[inst
->dst
[0].index
] |= inst
->dst
[0].writemask
;
4907 /* For a CMP to be considered a conditional write, the destination
4908 * register and source register two must be the same. */
4909 if (inst
->op
== TGSI_OPCODE_CMP
4910 && !(inst
->dst
[0].writemask
& prevWriteMask
)
4911 && inst
->src
[2].file
== inst
->dst
[0].file
4912 && inst
->src
[2].index
== inst
->dst
[0].index
4913 && inst
->dst
[0].writemask
==
4914 get_src_arg_mask(inst
->dst
[0], inst
->src
[2])) {
4916 inst
->op
= TGSI_OPCODE_MOV
;
4917 inst
->info
= tgsi_get_opcode_info(inst
->op
);
4918 inst
->src
[0] = inst
->src
[1];
4926 rename_temp_handle_src(struct rename_reg_pair
*renames
, st_src_reg
*src
)
4928 if (src
&& src
->file
== PROGRAM_TEMPORARY
) {
4929 int old_idx
= src
->index
;
4930 if (renames
[old_idx
].valid
)
4931 src
->index
= renames
[old_idx
].new_reg
;
4935 /* Replaces all references to a temporary register index with another index. */
4937 glsl_to_tgsi_visitor::rename_temp_registers(struct rename_reg_pair
*renames
)
4939 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4941 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
4942 rename_temp_handle_src(renames
, &inst
->src
[j
]);
4943 rename_temp_handle_src(renames
, inst
->src
[j
].reladdr
);
4944 rename_temp_handle_src(renames
, inst
->src
[j
].reladdr2
);
4947 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
4948 rename_temp_handle_src(renames
, &inst
->tex_offsets
[j
]);
4949 rename_temp_handle_src(renames
, inst
->tex_offsets
[j
].reladdr
);
4950 rename_temp_handle_src(renames
, inst
->tex_offsets
[j
].reladdr2
);
4953 rename_temp_handle_src(renames
, &inst
->resource
);
4954 rename_temp_handle_src(renames
, inst
->resource
.reladdr
);
4955 rename_temp_handle_src(renames
, inst
->resource
.reladdr2
);
4957 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4958 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
) {
4959 int old_idx
= inst
->dst
[j
].index
;
4960 if (renames
[old_idx
].valid
)
4961 inst
->dst
[j
].index
= renames
[old_idx
].new_reg
;
4963 rename_temp_handle_src(renames
, inst
->dst
[j
].reladdr
);
4964 rename_temp_handle_src(renames
, inst
->dst
[j
].reladdr2
);
4970 glsl_to_tgsi_visitor::get_first_temp_write(int *first_writes
)
4972 int depth
= 0; /* loop depth */
4973 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
4976 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4977 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4978 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
) {
4979 if (first_writes
[inst
->dst
[j
].index
] == -1)
4980 first_writes
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4984 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
4987 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
4997 glsl_to_tgsi_visitor::get_first_temp_read(int *first_reads
)
4999 int depth
= 0; /* loop depth */
5000 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
5003 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5004 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
5005 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
) {
5006 if (first_reads
[inst
->src
[j
].index
] == -1)
5007 first_reads
[inst
->src
[j
].index
] = (depth
== 0) ? i
: loop_start
;
5010 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
5011 if (inst
->tex_offsets
[j
].file
== PROGRAM_TEMPORARY
) {
5012 if (first_reads
[inst
->tex_offsets
[j
].index
] == -1)
5013 first_reads
[inst
->tex_offsets
[j
].index
] = (depth
== 0) ? i
: loop_start
;
5016 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
5019 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
5029 glsl_to_tgsi_visitor::get_last_temp_read_first_temp_write(int *last_reads
, int *first_writes
)
5031 int depth
= 0; /* loop depth */
5032 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
5035 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5036 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
5037 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
)
5038 last_reads
[inst
->src
[j
].index
] = (depth
== 0) ? i
: -2;
5040 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
5041 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
) {
5042 if (first_writes
[inst
->dst
[j
].index
] == -1)
5043 first_writes
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: loop_start
;
5044 last_reads
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: -2;
5047 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
5048 if (inst
->tex_offsets
[j
].file
== PROGRAM_TEMPORARY
)
5049 last_reads
[inst
->tex_offsets
[j
].index
] = (depth
== 0) ? i
: -2;
5051 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
5054 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
5057 for (k
= 0; k
< this->next_temp
; k
++) {
5058 if (last_reads
[k
] == -2) {
5070 glsl_to_tgsi_visitor::get_last_temp_write(int *last_writes
)
5072 int depth
= 0; /* loop depth */
5076 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5077 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
5078 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
)
5079 last_writes
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: -2;
5082 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
5084 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
5086 for (k
= 0; k
< this->next_temp
; k
++) {
5087 if (last_writes
[k
] == -2) {
5098 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
5099 * channels for copy propagation and updates following instructions to
5100 * use the original versions.
5102 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
5103 * will occur. As an example, a TXP production before this pass:
5105 * 0: MOV TEMP[1], INPUT[4].xyyy;
5106 * 1: MOV TEMP[1].w, INPUT[4].wwww;
5107 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
5111 * 0: MOV TEMP[1], INPUT[4].xyyy;
5112 * 1: MOV TEMP[1].w, INPUT[4].wwww;
5113 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5115 * which allows for dead code elimination on TEMP[1]'s writes.
5118 glsl_to_tgsi_visitor::copy_propagate(void)
5120 glsl_to_tgsi_instruction
**acp
= rzalloc_array(mem_ctx
,
5121 glsl_to_tgsi_instruction
*,
5122 this->next_temp
* 4);
5123 int *acp_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
5126 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5127 assert(inst
->dst
[0].file
!= PROGRAM_TEMPORARY
5128 || inst
->dst
[0].index
< this->next_temp
);
5130 /* First, do any copy propagation possible into the src regs. */
5131 for (int r
= 0; r
< 3; r
++) {
5132 glsl_to_tgsi_instruction
*first
= NULL
;
5134 int acp_base
= inst
->src
[r
].index
* 4;
5136 if (inst
->src
[r
].file
!= PROGRAM_TEMPORARY
||
5137 inst
->src
[r
].reladdr
||
5138 inst
->src
[r
].reladdr2
)
5141 /* See if we can find entries in the ACP consisting of MOVs
5142 * from the same src register for all the swizzled channels
5143 * of this src register reference.
5145 for (int i
= 0; i
< 4; i
++) {
5146 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
5147 glsl_to_tgsi_instruction
*copy_chan
= acp
[acp_base
+ src_chan
];
5154 assert(acp_level
[acp_base
+ src_chan
] <= level
);
5159 if (first
->src
[0].file
!= copy_chan
->src
[0].file
||
5160 first
->src
[0].index
!= copy_chan
->src
[0].index
||
5161 first
->src
[0].double_reg2
!= copy_chan
->src
[0].double_reg2
||
5162 first
->src
[0].index2D
!= copy_chan
->src
[0].index2D
) {
5170 /* We've now validated that we can copy-propagate to
5171 * replace this src register reference. Do it.
5173 inst
->src
[r
].file
= first
->src
[0].file
;
5174 inst
->src
[r
].index
= first
->src
[0].index
;
5175 inst
->src
[r
].index2D
= first
->src
[0].index2D
;
5176 inst
->src
[r
].has_index2
= first
->src
[0].has_index2
;
5177 inst
->src
[r
].double_reg2
= first
->src
[0].double_reg2
;
5178 inst
->src
[r
].array_id
= first
->src
[0].array_id
;
5181 for (int i
= 0; i
< 4; i
++) {
5182 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
5183 glsl_to_tgsi_instruction
*copy_inst
= acp
[acp_base
+ src_chan
];
5184 swizzle
|= (GET_SWZ(copy_inst
->src
[0].swizzle
, src_chan
) << (3 * i
));
5186 inst
->src
[r
].swizzle
= swizzle
;
5191 case TGSI_OPCODE_BGNLOOP
:
5192 case TGSI_OPCODE_ENDLOOP
:
5193 /* End of a basic block, clear the ACP entirely. */
5194 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
5197 case TGSI_OPCODE_IF
:
5198 case TGSI_OPCODE_UIF
:
5202 case TGSI_OPCODE_ENDIF
:
5203 case TGSI_OPCODE_ELSE
:
5204 /* Clear all channels written inside the block from the ACP, but
5205 * leaving those that were not touched.
5207 for (int r
= 0; r
< this->next_temp
; r
++) {
5208 for (int c
= 0; c
< 4; c
++) {
5209 if (!acp
[4 * r
+ c
])
5212 if (acp_level
[4 * r
+ c
] >= level
)
5213 acp
[4 * r
+ c
] = NULL
;
5216 if (inst
->op
== TGSI_OPCODE_ENDIF
)
5221 /* Continuing the block, clear any written channels from
5224 for (int d
= 0; d
< 2; d
++) {
5225 if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
&& inst
->dst
[d
].reladdr
) {
5226 /* Any temporary might be written, so no copy propagation
5227 * across this instruction.
5229 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
5230 } else if (inst
->dst
[d
].file
== PROGRAM_OUTPUT
&&
5231 inst
->dst
[d
].reladdr
) {
5232 /* Any output might be written, so no copy propagation
5233 * from outputs across this instruction.
5235 for (int r
= 0; r
< this->next_temp
; r
++) {
5236 for (int c
= 0; c
< 4; c
++) {
5237 if (!acp
[4 * r
+ c
])
5240 if (acp
[4 * r
+ c
]->src
[0].file
== PROGRAM_OUTPUT
)
5241 acp
[4 * r
+ c
] = NULL
;
5244 } else if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
||
5245 inst
->dst
[d
].file
== PROGRAM_OUTPUT
) {
5246 /* Clear where it's used as dst. */
5247 if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
) {
5248 for (int c
= 0; c
< 4; c
++) {
5249 if (inst
->dst
[d
].writemask
& (1 << c
))
5250 acp
[4 * inst
->dst
[d
].index
+ c
] = NULL
;
5254 /* Clear where it's used as src. */
5255 for (int r
= 0; r
< this->next_temp
; r
++) {
5256 for (int c
= 0; c
< 4; c
++) {
5257 if (!acp
[4 * r
+ c
])
5260 int src_chan
= GET_SWZ(acp
[4 * r
+ c
]->src
[0].swizzle
, c
);
5262 if (acp
[4 * r
+ c
]->src
[0].file
== inst
->dst
[d
].file
&&
5263 acp
[4 * r
+ c
]->src
[0].index
== inst
->dst
[d
].index
&&
5264 inst
->dst
[d
].writemask
& (1 << src_chan
)) {
5265 acp
[4 * r
+ c
] = NULL
;
5274 /* If this is a copy, add it to the ACP. */
5275 if (inst
->op
== TGSI_OPCODE_MOV
&&
5276 inst
->dst
[0].file
== PROGRAM_TEMPORARY
&&
5277 !(inst
->dst
[0].file
== inst
->src
[0].file
&&
5278 inst
->dst
[0].index
== inst
->src
[0].index
) &&
5279 !inst
->dst
[0].reladdr
&&
5280 !inst
->dst
[0].reladdr2
&&
5282 inst
->src
[0].file
!= PROGRAM_ARRAY
&&
5283 (inst
->src
[0].file
!= PROGRAM_OUTPUT
||
5284 this->shader
->Stage
!= MESA_SHADER_TESS_CTRL
) &&
5285 !inst
->src
[0].reladdr
&&
5286 !inst
->src
[0].reladdr2
&&
5287 !inst
->src
[0].negate
&&
5288 !inst
->src
[0].abs
) {
5289 for (int i
= 0; i
< 4; i
++) {
5290 if (inst
->dst
[0].writemask
& (1 << i
)) {
5291 acp
[4 * inst
->dst
[0].index
+ i
] = inst
;
5292 acp_level
[4 * inst
->dst
[0].index
+ i
] = level
;
5298 ralloc_free(acp_level
);
5303 dead_code_handle_reladdr(glsl_to_tgsi_instruction
**writes
, st_src_reg
*reladdr
)
5305 if (reladdr
&& reladdr
->file
== PROGRAM_TEMPORARY
) {
5306 /* Clear where it's used as src. */
5307 int swz
= GET_SWZ(reladdr
->swizzle
, 0);
5308 writes
[4 * reladdr
->index
+ swz
] = NULL
;
5313 * On a basic block basis, tracks available PROGRAM_TEMPORARY registers for dead
5316 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
5317 * will occur. As an example, a TXP production after copy propagation but
5320 * 0: MOV TEMP[1], INPUT[4].xyyy;
5321 * 1: MOV TEMP[1].w, INPUT[4].wwww;
5322 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5324 * and after this pass:
5326 * 0: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5329 glsl_to_tgsi_visitor::eliminate_dead_code(void)
5331 glsl_to_tgsi_instruction
**writes
= rzalloc_array(mem_ctx
,
5332 glsl_to_tgsi_instruction
*,
5333 this->next_temp
* 4);
5334 int *write_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
5338 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5339 assert(inst
->dst
[0].file
!= PROGRAM_TEMPORARY
5340 || inst
->dst
[0].index
< this->next_temp
);
5343 case TGSI_OPCODE_BGNLOOP
:
5344 case TGSI_OPCODE_ENDLOOP
:
5345 case TGSI_OPCODE_CONT
:
5346 case TGSI_OPCODE_BRK
:
5347 /* End of a basic block, clear the write array entirely.
5349 * This keeps us from killing dead code when the writes are
5350 * on either side of a loop, even when the register isn't touched
5351 * inside the loop. However, glsl_to_tgsi_visitor doesn't seem to emit
5352 * dead code of this type, so it shouldn't make a difference as long as
5353 * the dead code elimination pass in the GLSL compiler does its job.
5355 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5358 case TGSI_OPCODE_ENDIF
:
5359 case TGSI_OPCODE_ELSE
:
5360 /* Promote the recorded level of all channels written inside the
5361 * preceding if or else block to the level above the if/else block.
5363 for (int r
= 0; r
< this->next_temp
; r
++) {
5364 for (int c
= 0; c
< 4; c
++) {
5365 if (!writes
[4 * r
+ c
])
5368 if (write_level
[4 * r
+ c
] == level
)
5369 write_level
[4 * r
+ c
] = level
-1;
5372 if (inst
->op
== TGSI_OPCODE_ENDIF
)
5376 case TGSI_OPCODE_IF
:
5377 case TGSI_OPCODE_UIF
:
5379 /* fallthrough to default case to mark the condition as read */
5381 /* Continuing the block, clear any channels from the write array that
5382 * are read by this instruction.
5384 for (unsigned i
= 0; i
< ARRAY_SIZE(inst
->src
); i
++) {
5385 if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
&& inst
->src
[i
].reladdr
){
5386 /* Any temporary might be read, so no dead code elimination
5387 * across this instruction.
5389 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5390 } else if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
) {
5391 /* Clear where it's used as src. */
5392 int src_chans
= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 0);
5393 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 1);
5394 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 2);
5395 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 3);
5397 for (int c
= 0; c
< 4; c
++) {
5398 if (src_chans
& (1 << c
))
5399 writes
[4 * inst
->src
[i
].index
+ c
] = NULL
;
5402 dead_code_handle_reladdr(writes
, inst
->src
[i
].reladdr
);
5403 dead_code_handle_reladdr(writes
, inst
->src
[i
].reladdr2
);
5405 for (unsigned i
= 0; i
< inst
->tex_offset_num_offset
; i
++) {
5406 if (inst
->tex_offsets
[i
].file
== PROGRAM_TEMPORARY
&& inst
->tex_offsets
[i
].reladdr
){
5407 /* Any temporary might be read, so no dead code elimination
5408 * across this instruction.
5410 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5411 } else if (inst
->tex_offsets
[i
].file
== PROGRAM_TEMPORARY
) {
5412 /* Clear where it's used as src. */
5413 int src_chans
= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 0);
5414 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 1);
5415 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 2);
5416 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 3);
5418 for (int c
= 0; c
< 4; c
++) {
5419 if (src_chans
& (1 << c
))
5420 writes
[4 * inst
->tex_offsets
[i
].index
+ c
] = NULL
;
5423 dead_code_handle_reladdr(writes
, inst
->tex_offsets
[i
].reladdr
);
5424 dead_code_handle_reladdr(writes
, inst
->tex_offsets
[i
].reladdr2
);
5427 if (inst
->resource
.file
== PROGRAM_TEMPORARY
) {
5430 src_chans
= 1 << GET_SWZ(inst
->resource
.swizzle
, 0);
5431 src_chans
|= 1 << GET_SWZ(inst
->resource
.swizzle
, 1);
5432 src_chans
|= 1 << GET_SWZ(inst
->resource
.swizzle
, 2);
5433 src_chans
|= 1 << GET_SWZ(inst
->resource
.swizzle
, 3);
5435 for (int c
= 0; c
< 4; c
++) {
5436 if (src_chans
& (1 << c
))
5437 writes
[4 * inst
->resource
.index
+ c
] = NULL
;
5440 dead_code_handle_reladdr(writes
, inst
->resource
.reladdr
);
5441 dead_code_handle_reladdr(writes
, inst
->resource
.reladdr2
);
5443 for (unsigned i
= 0; i
< ARRAY_SIZE(inst
->dst
); i
++) {
5444 dead_code_handle_reladdr(writes
, inst
->dst
[i
].reladdr
);
5445 dead_code_handle_reladdr(writes
, inst
->dst
[i
].reladdr2
);
5450 /* If this instruction writes to a temporary, add it to the write array.
5451 * If there is already an instruction in the write array for one or more
5452 * of the channels, flag that channel write as dead.
5454 for (unsigned i
= 0; i
< ARRAY_SIZE(inst
->dst
); i
++) {
5455 if (inst
->dst
[i
].file
== PROGRAM_TEMPORARY
&&
5456 !inst
->dst
[i
].reladdr
) {
5457 for (int c
= 0; c
< 4; c
++) {
5458 if (inst
->dst
[i
].writemask
& (1 << c
)) {
5459 if (writes
[4 * inst
->dst
[i
].index
+ c
]) {
5460 if (write_level
[4 * inst
->dst
[i
].index
+ c
] < level
)
5463 writes
[4 * inst
->dst
[i
].index
+ c
]->dead_mask
|= (1 << c
);
5465 writes
[4 * inst
->dst
[i
].index
+ c
] = inst
;
5466 write_level
[4 * inst
->dst
[i
].index
+ c
] = level
;
5473 /* Anything still in the write array at this point is dead code. */
5474 for (int r
= 0; r
< this->next_temp
; r
++) {
5475 for (int c
= 0; c
< 4; c
++) {
5476 glsl_to_tgsi_instruction
*inst
= writes
[4 * r
+ c
];
5478 inst
->dead_mask
|= (1 << c
);
5482 /* Now actually remove the instructions that are completely dead and update
5483 * the writemask of other instructions with dead channels.
5485 foreach_in_list_safe(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5486 if (!inst
->dead_mask
|| !inst
->dst
[0].writemask
)
5488 /* No amount of dead masks should remove memory stores */
5489 if (inst
->info
->is_store
)
5492 if ((inst
->dst
[0].writemask
& ~inst
->dead_mask
) == 0) {
5497 if (glsl_base_type_is_64bit(inst
->dst
[0].type
)) {
5498 if (inst
->dead_mask
== WRITEMASK_XY
||
5499 inst
->dead_mask
== WRITEMASK_ZW
)
5500 inst
->dst
[0].writemask
&= ~(inst
->dead_mask
);
5502 inst
->dst
[0].writemask
&= ~(inst
->dead_mask
);
5506 ralloc_free(write_level
);
5507 ralloc_free(writes
);
5512 /* merge DFRACEXP instructions into one. */
5514 glsl_to_tgsi_visitor::merge_two_dsts(void)
5516 /* We never delete inst, but we may delete its successor. */
5517 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5518 glsl_to_tgsi_instruction
*inst2
;
5521 if (num_inst_dst_regs(inst
) != 2)
5524 if (inst
->dst
[0].file
!= PROGRAM_UNDEFINED
&&
5525 inst
->dst
[1].file
!= PROGRAM_UNDEFINED
)
5528 assert(inst
->dst
[0].file
!= PROGRAM_UNDEFINED
||
5529 inst
->dst
[1].file
!= PROGRAM_UNDEFINED
);
5531 if (inst
->dst
[0].file
== PROGRAM_UNDEFINED
)
5536 inst2
= (glsl_to_tgsi_instruction
*) inst
->next
;
5537 while (!inst2
->is_tail_sentinel()) {
5538 if (inst
->op
== inst2
->op
&&
5539 inst2
->dst
[defined
].file
== PROGRAM_UNDEFINED
&&
5540 inst
->src
[0].file
== inst2
->src
[0].file
&&
5541 inst
->src
[0].index
== inst2
->src
[0].index
&&
5542 inst
->src
[0].type
== inst2
->src
[0].type
&&
5543 inst
->src
[0].swizzle
== inst2
->src
[0].swizzle
)
5545 inst2
= (glsl_to_tgsi_instruction
*) inst2
->next
;
5548 if (inst2
->is_tail_sentinel()) {
5549 /* Undefined destinations are not allowed, substitute with an unused
5550 * temporary register.
5552 st_src_reg tmp
= get_temp(glsl_type::vec4_type
);
5553 inst
->dst
[defined
^ 1] = st_dst_reg(tmp
);
5554 inst
->dst
[defined
^ 1].writemask
= 0;
5558 inst
->dst
[defined
^ 1] = inst2
->dst
[defined
^ 1];
5564 template <typename st_reg
>
5565 void test_indirect_access(const st_reg
& reg
, bool *has_indirect_access
)
5567 if (reg
.file
== PROGRAM_ARRAY
) {
5568 if (reg
.reladdr
|| reg
.reladdr2
|| reg
.has_index2
) {
5569 has_indirect_access
[reg
.array_id
] = true;
5571 test_indirect_access(*reg
.reladdr
, has_indirect_access
);
5573 test_indirect_access(*reg
.reladdr2
, has_indirect_access
);
5578 template <typename st_reg
>
5579 void remap_array(st_reg
& reg
, const int *array_remap_info
,
5580 const bool *has_indirect_access
)
5582 if (reg
.file
== PROGRAM_ARRAY
) {
5583 if (!has_indirect_access
[reg
.array_id
]) {
5584 reg
.file
= PROGRAM_TEMPORARY
;
5585 reg
.index
= reg
.index
+ array_remap_info
[reg
.array_id
];
5588 reg
.array_id
= array_remap_info
[reg
.array_id
];
5592 remap_array(*reg
.reladdr
, array_remap_info
, has_indirect_access
);
5595 remap_array(*reg
.reladdr2
, array_remap_info
, has_indirect_access
);
5599 /* One-dimensional arrays whose elements are only accessed directly are
5600 * replaced by an according set of temporary registers that then can become
5601 * subject to further optimization steps like copy propagation and
5605 glsl_to_tgsi_visitor::split_arrays(void)
5610 bool *has_indirect_access
= rzalloc_array(mem_ctx
, bool, next_array
+ 1);
5612 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5613 for (unsigned j
= 0; j
< num_inst_src_regs(inst
); j
++)
5614 test_indirect_access(inst
->src
[j
], has_indirect_access
);
5616 for (unsigned j
= 0; j
< inst
->tex_offset_num_offset
; j
++)
5617 test_indirect_access(inst
->tex_offsets
[j
], has_indirect_access
);
5619 for (unsigned j
= 0; j
< num_inst_dst_regs(inst
); j
++)
5620 test_indirect_access(inst
->dst
[j
], has_indirect_access
);
5622 test_indirect_access(inst
->resource
, has_indirect_access
);
5625 unsigned array_offset
= 0;
5626 unsigned n_remaining_arrays
= 0;
5628 /* Double use: For arrays that get split this value will contain
5629 * the base index of the temporary registers this array is replaced
5630 * with. For arrays that remain it contains the new array ID.
5632 int *array_remap_info
= rzalloc_array(has_indirect_access
, int,
5635 for (unsigned i
= 1; i
<= next_array
; ++i
) {
5636 if (!has_indirect_access
[i
]) {
5637 array_remap_info
[i
] = this->next_temp
+ array_offset
;
5638 array_offset
+= array_sizes
[i
- 1];
5640 array_sizes
[n_remaining_arrays
] = array_sizes
[i
-1];
5641 array_remap_info
[i
] = ++n_remaining_arrays
;
5645 if (next_array
!= n_remaining_arrays
) {
5646 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5647 for (unsigned j
= 0; j
< num_inst_src_regs(inst
); j
++)
5648 remap_array(inst
->src
[j
], array_remap_info
, has_indirect_access
);
5650 for (unsigned j
= 0; j
< inst
->tex_offset_num_offset
; j
++)
5651 remap_array(inst
->tex_offsets
[j
], array_remap_info
, has_indirect_access
);
5653 for (unsigned j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
5654 remap_array(inst
->dst
[j
], array_remap_info
, has_indirect_access
);
5656 remap_array(inst
->resource
, array_remap_info
, has_indirect_access
);
5660 ralloc_free(has_indirect_access
);
5661 this->next_temp
+= array_offset
;
5662 next_array
= n_remaining_arrays
;
5665 /* Merges temporary registers together where possible to reduce the number of
5666 * registers needed to run a program.
5668 * Produces optimal code only after copy propagation and dead code elimination
5671 glsl_to_tgsi_visitor::merge_registers(void)
5673 class array_live_range
*arr_live_ranges
= NULL
;
5675 struct register_live_range
*reg_live_ranges
=
5676 rzalloc_array(mem_ctx
, struct register_live_range
, this->next_temp
);
5678 if (this->next_array
> 0) {
5679 arr_live_ranges
= new array_live_range
[this->next_array
];
5680 for (unsigned i
= 0; i
< this->next_array
; ++i
)
5681 arr_live_ranges
[i
] = array_live_range(i
+1, this->array_sizes
[i
]);
5685 if (get_temp_registers_required_live_ranges(reg_live_ranges
, &this->instructions
,
5686 this->next_temp
, reg_live_ranges
,
5687 this->next_array
, arr_live_ranges
)) {
5688 struct rename_reg_pair
*renames
=
5689 rzalloc_array(reg_live_ranges
, struct rename_reg_pair
, this->next_temp
);
5690 get_temp_registers_remapping(reg_live_ranges
, this->next_temp
,
5691 reg_live_ranges
, renames
);
5692 rename_temp_registers(renames
);
5694 this->next_array
= merge_arrays(this->next_array
, this->array_sizes
,
5695 &this->instructions
, arr_live_ranges
);
5698 if (arr_live_ranges
)
5699 delete[] arr_live_ranges
;
5701 ralloc_free(reg_live_ranges
);
5704 /* Reassign indices to temporary registers by reusing unused indices created
5705 * by optimization passes. */
5707 glsl_to_tgsi_visitor::renumber_registers(void)
5711 int *first_writes
= ralloc_array(mem_ctx
, int, this->next_temp
);
5712 struct rename_reg_pair
*renames
= rzalloc_array(mem_ctx
, struct rename_reg_pair
, this->next_temp
);
5714 for (i
= 0; i
< this->next_temp
; i
++) {
5715 first_writes
[i
] = -1;
5717 get_first_temp_write(first_writes
);
5719 for (i
= 0; i
< this->next_temp
; i
++) {
5720 if (first_writes
[i
] < 0) continue;
5721 if (i
!= new_index
) {
5722 renames
[i
].new_reg
= new_index
;
5723 renames
[i
].valid
= true;
5728 rename_temp_registers(renames
);
5729 this->next_temp
= new_index
;
5730 ralloc_free(renames
);
5731 ralloc_free(first_writes
);
5735 void glsl_to_tgsi_visitor::print_stats()
5737 int narray_registers
= 0;
5738 for (unsigned i
= 0; i
< this->next_array
; ++i
)
5739 narray_registers
+= this->array_sizes
[i
];
5741 int ninstructions
= 0;
5742 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &instructions
) {
5746 simple_mtx_lock(&print_stats_mutex
);
5747 stats_log
<< next_array
<< ", "
5748 << next_temp
<< ", "
5749 << narray_registers
<< ", "
5750 << next_temp
+ narray_registers
<< ", "
5751 << ninstructions
<< "\n";
5752 simple_mtx_unlock(&print_stats_mutex
);
5755 /* ------------------------- TGSI conversion stuff -------------------------- */
5758 * Intermediate state used during shader translation.
5760 struct st_translate
{
5761 struct ureg_program
*ureg
;
5763 unsigned temps_size
;
5764 struct ureg_dst
*temps
;
5766 struct ureg_dst
*arrays
;
5767 unsigned num_temp_arrays
;
5768 struct ureg_src
*constants
;
5770 struct ureg_src
*immediates
;
5772 struct ureg_dst outputs
[PIPE_MAX_SHADER_OUTPUTS
];
5773 struct ureg_src inputs
[PIPE_MAX_SHADER_INPUTS
];
5774 struct ureg_dst address
[3];
5775 struct ureg_src samplers
[PIPE_MAX_SAMPLERS
];
5776 struct ureg_src buffers
[PIPE_MAX_SHADER_BUFFERS
];
5777 struct ureg_src images
[PIPE_MAX_SHADER_IMAGES
];
5778 struct ureg_src systemValues
[SYSTEM_VALUE_MAX
];
5779 struct ureg_src hw_atomics
[PIPE_MAX_HW_ATOMIC_BUFFERS
];
5780 struct ureg_src shared_memory
;
5781 unsigned *array_sizes
;
5782 struct inout_decl
*input_decls
;
5783 unsigned num_input_decls
;
5784 struct inout_decl
*output_decls
;
5785 unsigned num_output_decls
;
5787 const ubyte
*inputMapping
;
5788 const ubyte
*outputMapping
;
5790 enum pipe_shader_type procType
; /**< PIPE_SHADER_VERTEX/FRAGMENT */
5792 bool tg4_component_in_swizzle
;
5795 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
5797 _mesa_sysval_to_semantic(unsigned sysval
)
5801 case SYSTEM_VALUE_VERTEX_ID
:
5802 return TGSI_SEMANTIC_VERTEXID
;
5803 case SYSTEM_VALUE_INSTANCE_ID
:
5804 return TGSI_SEMANTIC_INSTANCEID
;
5805 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
5806 return TGSI_SEMANTIC_VERTEXID_NOBASE
;
5807 case SYSTEM_VALUE_BASE_VERTEX
:
5808 return TGSI_SEMANTIC_BASEVERTEX
;
5809 case SYSTEM_VALUE_BASE_INSTANCE
:
5810 return TGSI_SEMANTIC_BASEINSTANCE
;
5811 case SYSTEM_VALUE_DRAW_ID
:
5812 return TGSI_SEMANTIC_DRAWID
;
5814 /* Geometry shader */
5815 case SYSTEM_VALUE_INVOCATION_ID
:
5816 return TGSI_SEMANTIC_INVOCATIONID
;
5818 /* Fragment shader */
5819 case SYSTEM_VALUE_FRAG_COORD
:
5820 return TGSI_SEMANTIC_POSITION
;
5821 case SYSTEM_VALUE_POINT_COORD
:
5822 return TGSI_SEMANTIC_PCOORD
;
5823 case SYSTEM_VALUE_FRONT_FACE
:
5824 return TGSI_SEMANTIC_FACE
;
5825 case SYSTEM_VALUE_SAMPLE_ID
:
5826 return TGSI_SEMANTIC_SAMPLEID
;
5827 case SYSTEM_VALUE_SAMPLE_POS
:
5828 return TGSI_SEMANTIC_SAMPLEPOS
;
5829 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
5830 return TGSI_SEMANTIC_SAMPLEMASK
;
5831 case SYSTEM_VALUE_HELPER_INVOCATION
:
5832 return TGSI_SEMANTIC_HELPER_INVOCATION
;
5834 /* Tessellation shader */
5835 case SYSTEM_VALUE_TESS_COORD
:
5836 return TGSI_SEMANTIC_TESSCOORD
;
5837 case SYSTEM_VALUE_VERTICES_IN
:
5838 return TGSI_SEMANTIC_VERTICESIN
;
5839 case SYSTEM_VALUE_PRIMITIVE_ID
:
5840 return TGSI_SEMANTIC_PRIMID
;
5841 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
5842 return TGSI_SEMANTIC_TESSOUTER
;
5843 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
5844 return TGSI_SEMANTIC_TESSINNER
;
5846 /* Compute shader */
5847 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
5848 return TGSI_SEMANTIC_THREAD_ID
;
5849 case SYSTEM_VALUE_WORK_GROUP_ID
:
5850 return TGSI_SEMANTIC_BLOCK_ID
;
5851 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
5852 return TGSI_SEMANTIC_GRID_SIZE
;
5853 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
5854 return TGSI_SEMANTIC_BLOCK_SIZE
;
5856 /* ARB_shader_ballot */
5857 case SYSTEM_VALUE_SUBGROUP_SIZE
:
5858 return TGSI_SEMANTIC_SUBGROUP_SIZE
;
5859 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
5860 return TGSI_SEMANTIC_SUBGROUP_INVOCATION
;
5861 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
5862 return TGSI_SEMANTIC_SUBGROUP_EQ_MASK
;
5863 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
5864 return TGSI_SEMANTIC_SUBGROUP_GE_MASK
;
5865 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
5866 return TGSI_SEMANTIC_SUBGROUP_GT_MASK
;
5867 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
5868 return TGSI_SEMANTIC_SUBGROUP_LE_MASK
;
5869 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
5870 return TGSI_SEMANTIC_SUBGROUP_LT_MASK
;
5873 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
5874 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
5875 case SYSTEM_VALUE_VERTEX_CNT
:
5876 case SYSTEM_VALUE_BARYCENTRIC_PIXEL
:
5877 case SYSTEM_VALUE_BARYCENTRIC_SAMPLE
:
5878 case SYSTEM_VALUE_BARYCENTRIC_CENTROID
:
5879 case SYSTEM_VALUE_BARYCENTRIC_SIZE
:
5881 assert(!"Unexpected SYSTEM_VALUE_ enum");
5882 return TGSI_SEMANTIC_COUNT
;
5887 * Map a glsl_to_tgsi constant/immediate to a TGSI immediate.
5889 static struct ureg_src
5890 emit_immediate(struct st_translate
*t
,
5891 gl_constant_value values
[4],
5892 GLenum type
, int size
)
5894 struct ureg_program
*ureg
= t
->ureg
;
5898 return ureg_DECL_immediate(ureg
, &values
[0].f
, size
);
5900 return ureg_DECL_immediate_f64(ureg
, (double *)&values
[0].f
, size
);
5902 return ureg_DECL_immediate_int64(ureg
, (int64_t *)&values
[0].f
, size
);
5903 case GL_UNSIGNED_INT64_ARB
:
5904 return ureg_DECL_immediate_uint64(ureg
, (uint64_t *)&values
[0].f
, size
);
5906 return ureg_DECL_immediate_int(ureg
, &values
[0].i
, size
);
5907 case GL_UNSIGNED_INT
:
5909 return ureg_DECL_immediate_uint(ureg
, &values
[0].u
, size
);
5911 assert(!"should not get here - type must be float, int, uint, or bool");
5912 return ureg_src_undef();
5917 * Map a glsl_to_tgsi dst register to a TGSI ureg_dst register.
5919 static struct ureg_dst
5920 dst_register(struct st_translate
*t
, gl_register_file file
, unsigned index
,
5926 case PROGRAM_UNDEFINED
:
5927 return ureg_dst_undef();
5929 case PROGRAM_TEMPORARY
:
5930 /* Allocate space for temporaries on demand. */
5931 if (index
>= t
->temps_size
) {
5932 const int inc
= align(index
- t
->temps_size
+ 1, 4096);
5934 t
->temps
= (struct ureg_dst
*)
5936 (t
->temps_size
+ inc
) * sizeof(struct ureg_dst
));
5938 return ureg_dst_undef();
5940 memset(t
->temps
+ t
->temps_size
, 0, inc
* sizeof(struct ureg_dst
));
5941 t
->temps_size
+= inc
;
5944 if (ureg_dst_is_undef(t
->temps
[index
]))
5945 t
->temps
[index
] = ureg_DECL_local_temporary(t
->ureg
);
5947 return t
->temps
[index
];
5950 assert(array_id
&& array_id
<= t
->num_temp_arrays
);
5951 array
= array_id
- 1;
5953 if (ureg_dst_is_undef(t
->arrays
[array
]))
5954 t
->arrays
[array
] = ureg_DECL_array_temporary(
5955 t
->ureg
, t
->array_sizes
[array
], TRUE
);
5957 return ureg_dst_array_offset(t
->arrays
[array
], index
);
5959 case PROGRAM_OUTPUT
:
5961 if (t
->procType
== PIPE_SHADER_FRAGMENT
)
5962 assert(index
< 2 * FRAG_RESULT_MAX
);
5963 else if (t
->procType
== PIPE_SHADER_TESS_CTRL
||
5964 t
->procType
== PIPE_SHADER_TESS_EVAL
)
5965 assert(index
< VARYING_SLOT_TESS_MAX
);
5967 assert(index
< VARYING_SLOT_MAX
);
5969 assert(t
->outputMapping
[index
] < ARRAY_SIZE(t
->outputs
));
5970 assert(t
->outputs
[t
->outputMapping
[index
]].File
!= TGSI_FILE_NULL
);
5971 return t
->outputs
[t
->outputMapping
[index
]];
5974 struct inout_decl
*decl
=
5975 find_inout_array(t
->output_decls
,
5976 t
->num_output_decls
, array_id
);
5977 unsigned mesa_index
= decl
->mesa_index
;
5978 int slot
= t
->outputMapping
[mesa_index
];
5980 assert(slot
!= -1 && t
->outputs
[slot
].File
== TGSI_FILE_OUTPUT
);
5982 struct ureg_dst dst
= t
->outputs
[slot
];
5983 dst
.ArrayID
= array_id
;
5984 return ureg_dst_array_offset(dst
, index
- mesa_index
);
5987 case PROGRAM_ADDRESS
:
5988 return t
->address
[index
];
5991 assert(!"unknown dst register file");
5992 return ureg_dst_undef();
5996 static struct ureg_src
5997 translate_src(struct st_translate
*t
, const st_src_reg
*src_reg
);
5999 static struct ureg_src
6000 translate_addr(struct st_translate
*t
, const st_src_reg
*reladdr
,
6001 unsigned addr_index
)
6003 if (t
->need_uarl
|| !reladdr
->is_legal_tgsi_address_operand())
6004 return ureg_src(t
->address
[addr_index
]);
6006 return translate_src(t
, reladdr
);
6010 * Create a TGSI ureg_dst register from an st_dst_reg.
6012 static struct ureg_dst
6013 translate_dst(struct st_translate
*t
,
6014 const st_dst_reg
*dst_reg
,
6017 struct ureg_dst dst
= dst_register(t
, dst_reg
->file
, dst_reg
->index
,
6020 if (dst
.File
== TGSI_FILE_NULL
)
6023 dst
= ureg_writemask(dst
, dst_reg
->writemask
);
6026 dst
= ureg_saturate(dst
);
6028 if (dst_reg
->reladdr
!= NULL
) {
6029 assert(dst_reg
->file
!= PROGRAM_TEMPORARY
);
6030 dst
= ureg_dst_indirect(dst
, translate_addr(t
, dst_reg
->reladdr
, 0));
6033 if (dst_reg
->has_index2
) {
6034 if (dst_reg
->reladdr2
)
6035 dst
= ureg_dst_dimension_indirect(dst
,
6036 translate_addr(t
, dst_reg
->reladdr2
, 1),
6039 dst
= ureg_dst_dimension(dst
, dst_reg
->index2D
);
6046 * Create a TGSI ureg_src register from an st_src_reg.
6048 static struct ureg_src
6049 translate_src(struct st_translate
*t
, const st_src_reg
*src_reg
)
6051 struct ureg_src src
;
6052 int index
= src_reg
->index
;
6053 int double_reg2
= src_reg
->double_reg2
? 1 : 0;
6055 switch (src_reg
->file
) {
6056 case PROGRAM_UNDEFINED
:
6057 src
= ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
6060 case PROGRAM_TEMPORARY
:
6062 src
= ureg_src(dst_register(t
, src_reg
->file
, src_reg
->index
,
6063 src_reg
->array_id
));
6066 case PROGRAM_OUTPUT
: {
6067 struct ureg_dst dst
= dst_register(t
, src_reg
->file
, src_reg
->index
,
6069 assert(dst
.WriteMask
!= 0);
6070 unsigned shift
= ffs(dst
.WriteMask
) - 1;
6071 src
= ureg_swizzle(ureg_src(dst
),
6075 MIN2(shift
+ 3, 3));
6079 case PROGRAM_UNIFORM
:
6080 assert(src_reg
->index
>= 0);
6081 src
= src_reg
->index
< t
->num_constants
?
6082 t
->constants
[src_reg
->index
] : ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
6084 case PROGRAM_STATE_VAR
:
6085 case PROGRAM_CONSTANT
: /* ie, immediate */
6086 if (src_reg
->has_index2
)
6087 src
= ureg_src_register(TGSI_FILE_CONSTANT
, src_reg
->index
);
6089 src
= src_reg
->index
>= 0 && src_reg
->index
< t
->num_constants
?
6090 t
->constants
[src_reg
->index
] : ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
6093 case PROGRAM_IMMEDIATE
:
6094 assert(src_reg
->index
>= 0 && src_reg
->index
< t
->num_immediates
);
6095 src
= t
->immediates
[src_reg
->index
];
6099 /* GLSL inputs are 64-bit containers, so we have to
6100 * map back to the original index and add the offset after
6102 index
-= double_reg2
;
6103 if (!src_reg
->array_id
) {
6104 assert(t
->inputMapping
[index
] < ARRAY_SIZE(t
->inputs
));
6105 assert(t
->inputs
[t
->inputMapping
[index
]].File
!= TGSI_FILE_NULL
);
6106 src
= t
->inputs
[t
->inputMapping
[index
] + double_reg2
];
6109 struct inout_decl
*decl
= find_inout_array(t
->input_decls
,
6112 unsigned mesa_index
= decl
->mesa_index
;
6113 int slot
= t
->inputMapping
[mesa_index
];
6115 assert(slot
!= -1 && t
->inputs
[slot
].File
== TGSI_FILE_INPUT
);
6117 src
= t
->inputs
[slot
];
6118 src
.ArrayID
= src_reg
->array_id
;
6119 src
= ureg_src_array_offset(src
, index
+ double_reg2
- mesa_index
);
6123 case PROGRAM_ADDRESS
:
6124 src
= ureg_src(t
->address
[src_reg
->index
]);
6127 case PROGRAM_SYSTEM_VALUE
:
6128 assert(src_reg
->index
< (int) ARRAY_SIZE(t
->systemValues
));
6129 src
= t
->systemValues
[src_reg
->index
];
6132 case PROGRAM_HW_ATOMIC
:
6133 src
= ureg_src_array_register(TGSI_FILE_HW_ATOMIC
, src_reg
->index
,
6138 assert(!"unknown src register file");
6139 return ureg_src_undef();
6142 if (src_reg
->has_index2
) {
6143 /* 2D indexes occur with geometry shader inputs (attrib, vertex)
6144 * and UBO constant buffers (buffer, position).
6146 if (src_reg
->reladdr2
)
6147 src
= ureg_src_dimension_indirect(src
,
6148 translate_addr(t
, src_reg
->reladdr2
, 1),
6151 src
= ureg_src_dimension(src
, src_reg
->index2D
);
6154 src
= ureg_swizzle(src
,
6155 GET_SWZ(src_reg
->swizzle
, 0) & 0x3,
6156 GET_SWZ(src_reg
->swizzle
, 1) & 0x3,
6157 GET_SWZ(src_reg
->swizzle
, 2) & 0x3,
6158 GET_SWZ(src_reg
->swizzle
, 3) & 0x3);
6161 src
= ureg_abs(src
);
6163 if ((src_reg
->negate
& 0xf) == NEGATE_XYZW
)
6164 src
= ureg_negate(src
);
6166 if (src_reg
->reladdr
!= NULL
) {
6167 assert(src_reg
->file
!= PROGRAM_TEMPORARY
);
6168 src
= ureg_src_indirect(src
, translate_addr(t
, src_reg
->reladdr
, 0));
6174 static struct tgsi_texture_offset
6175 translate_tex_offset(struct st_translate
*t
,
6176 const st_src_reg
*in_offset
)
6178 struct tgsi_texture_offset offset
;
6179 struct ureg_src src
= translate_src(t
, in_offset
);
6181 offset
.File
= src
.File
;
6182 offset
.Index
= src
.Index
;
6183 offset
.SwizzleX
= src
.SwizzleX
;
6184 offset
.SwizzleY
= src
.SwizzleY
;
6185 offset
.SwizzleZ
= src
.SwizzleZ
;
6188 assert(!src
.Indirect
);
6189 assert(!src
.DimIndirect
);
6190 assert(!src
.Dimension
);
6191 assert(!src
.Absolute
); /* those shouldn't be used with integers anyway */
6192 assert(!src
.Negate
);
6198 compile_tgsi_instruction(struct st_translate
*t
,
6199 const glsl_to_tgsi_instruction
*inst
)
6201 struct ureg_program
*ureg
= t
->ureg
;
6203 struct ureg_dst dst
[2];
6204 struct ureg_src src
[4];
6205 struct tgsi_texture_offset texoffsets
[MAX_GLSL_TEXTURE_OFFSET
];
6209 enum tgsi_texture_type tex_target
= TGSI_TEXTURE_BUFFER
;
6211 num_dst
= num_inst_dst_regs(inst
);
6212 num_src
= num_inst_src_regs(inst
);
6214 for (i
= 0; i
< num_dst
; i
++)
6215 dst
[i
] = translate_dst(t
,
6219 for (i
= 0; i
< num_src
; i
++)
6220 src
[i
] = translate_src(t
, &inst
->src
[i
]);
6223 case TGSI_OPCODE_BGNLOOP
:
6224 case TGSI_OPCODE_ELSE
:
6225 case TGSI_OPCODE_ENDLOOP
:
6226 case TGSI_OPCODE_IF
:
6227 case TGSI_OPCODE_UIF
:
6228 assert(num_dst
== 0);
6229 ureg_insn(ureg
, inst
->op
, NULL
, 0, src
, num_src
, inst
->precise
);
6232 case TGSI_OPCODE_TEX
:
6233 case TGSI_OPCODE_TEX_LZ
:
6234 case TGSI_OPCODE_TXB
:
6235 case TGSI_OPCODE_TXD
:
6236 case TGSI_OPCODE_TXL
:
6237 case TGSI_OPCODE_TXP
:
6238 case TGSI_OPCODE_TXQ
:
6239 case TGSI_OPCODE_TXQS
:
6240 case TGSI_OPCODE_TXF
:
6241 case TGSI_OPCODE_TXF_LZ
:
6242 case TGSI_OPCODE_TEX2
:
6243 case TGSI_OPCODE_TXB2
:
6244 case TGSI_OPCODE_TXL2
:
6245 case TGSI_OPCODE_TG4
:
6246 case TGSI_OPCODE_LODQ
:
6247 case TGSI_OPCODE_SAMP2HND
:
6248 if (inst
->resource
.file
== PROGRAM_SAMPLER
) {
6249 src
[num_src
] = t
->samplers
[inst
->resource
.index
];
6250 if (t
->tg4_component_in_swizzle
&& inst
->op
== TGSI_OPCODE_TG4
)
6251 src
[num_src
].SwizzleX
= inst
->gather_component
;
6253 /* Bindless samplers. */
6254 src
[num_src
] = translate_src(t
, &inst
->resource
);
6256 assert(src
[num_src
].File
!= TGSI_FILE_NULL
);
6257 if (inst
->resource
.reladdr
)
6259 ureg_src_indirect(src
[num_src
],
6260 translate_addr(t
, inst
->resource
.reladdr
, 2));
6262 for (i
= 0; i
< (int)inst
->tex_offset_num_offset
; i
++) {
6263 texoffsets
[i
] = translate_tex_offset(t
, &inst
->tex_offsets
[i
]);
6265 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
6271 st_translate_texture_type(inst
->tex_type
),
6272 texoffsets
, inst
->tex_offset_num_offset
,
6276 case TGSI_OPCODE_RESQ
:
6277 case TGSI_OPCODE_LOAD
:
6278 case TGSI_OPCODE_ATOMUADD
:
6279 case TGSI_OPCODE_ATOMXCHG
:
6280 case TGSI_OPCODE_ATOMCAS
:
6281 case TGSI_OPCODE_ATOMAND
:
6282 case TGSI_OPCODE_ATOMOR
:
6283 case TGSI_OPCODE_ATOMXOR
:
6284 case TGSI_OPCODE_ATOMUMIN
:
6285 case TGSI_OPCODE_ATOMUMAX
:
6286 case TGSI_OPCODE_ATOMIMIN
:
6287 case TGSI_OPCODE_ATOMIMAX
:
6288 case TGSI_OPCODE_ATOMFADD
:
6289 case TGSI_OPCODE_IMG2HND
:
6290 case TGSI_OPCODE_ATOMINC_WRAP
:
6291 case TGSI_OPCODE_ATOMDEC_WRAP
:
6292 for (i
= num_src
- 1; i
>= 0; i
--)
6293 src
[i
+ 1] = src
[i
];
6295 if (inst
->resource
.file
== PROGRAM_MEMORY
) {
6296 src
[0] = t
->shared_memory
;
6297 } else if (inst
->resource
.file
== PROGRAM_BUFFER
) {
6298 src
[0] = t
->buffers
[inst
->resource
.index
];
6299 } else if (inst
->resource
.file
== PROGRAM_HW_ATOMIC
) {
6300 src
[0] = translate_src(t
, &inst
->resource
);
6301 } else if (inst
->resource
.file
== PROGRAM_CONSTANT
) {
6302 assert(inst
->resource
.has_index2
);
6303 src
[0] = ureg_src_register(TGSI_FILE_CONSTBUF
, inst
->resource
.index
);
6305 assert(inst
->resource
.file
!= PROGRAM_UNDEFINED
);
6306 if (inst
->resource
.file
== PROGRAM_IMAGE
) {
6307 src
[0] = t
->images
[inst
->resource
.index
];
6309 /* Bindless images. */
6310 src
[0] = translate_src(t
, &inst
->resource
);
6312 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
6314 if (inst
->resource
.reladdr
)
6315 src
[0] = ureg_src_indirect(src
[0],
6316 translate_addr(t
, inst
->resource
.reladdr
, 2));
6317 assert(src
[0].File
!= TGSI_FILE_NULL
);
6318 ureg_memory_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
,
6319 inst
->buffer_access
,
6320 tex_target
, inst
->image_format
);
6323 case TGSI_OPCODE_STORE
:
6324 if (inst
->resource
.file
== PROGRAM_MEMORY
) {
6325 dst
[0] = ureg_dst(t
->shared_memory
);
6326 } else if (inst
->resource
.file
== PROGRAM_BUFFER
) {
6327 dst
[0] = ureg_dst(t
->buffers
[inst
->resource
.index
]);
6329 if (inst
->resource
.file
== PROGRAM_IMAGE
) {
6330 dst
[0] = ureg_dst(t
->images
[inst
->resource
.index
]);
6332 /* Bindless images. */
6333 dst
[0] = ureg_dst(translate_src(t
, &inst
->resource
));
6335 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
6337 dst
[0] = ureg_writemask(dst
[0], inst
->dst
[0].writemask
);
6338 if (inst
->resource
.reladdr
)
6339 dst
[0] = ureg_dst_indirect(dst
[0],
6340 translate_addr(t
, inst
->resource
.reladdr
, 2));
6341 assert(dst
[0].File
!= TGSI_FILE_NULL
);
6342 ureg_memory_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
,
6343 inst
->buffer_access
,
6344 tex_target
, inst
->image_format
);
6351 src
, num_src
, inst
->precise
);
6356 /* Invert SamplePos.y when rendering to the default framebuffer. */
6358 emit_samplepos_adjustment(struct st_translate
*t
, int wpos_y_transform
)
6360 struct ureg_program
*ureg
= t
->ureg
;
6362 assert(wpos_y_transform
>= 0);
6363 struct ureg_src trans_const
= ureg_DECL_constant(ureg
, wpos_y_transform
);
6364 struct ureg_src samplepos_sysval
= t
->systemValues
[SYSTEM_VALUE_SAMPLE_POS
];
6365 struct ureg_dst samplepos_flipped
= ureg_DECL_temporary(ureg
);
6366 struct ureg_dst is_fbo
= ureg_DECL_temporary(ureg
);
6368 ureg_ADD(ureg
, ureg_writemask(samplepos_flipped
, TGSI_WRITEMASK_Y
),
6369 ureg_imm1f(ureg
, 1), ureg_negate(samplepos_sysval
));
6371 /* If trans.x == 1, use samplepos.y, else use 1 - samplepos.y. */
6372 ureg_FSEQ(ureg
, ureg_writemask(is_fbo
, TGSI_WRITEMASK_Y
),
6373 ureg_scalar(trans_const
, TGSI_SWIZZLE_X
), ureg_imm1f(ureg
, 1));
6374 ureg_UCMP(ureg
, ureg_writemask(samplepos_flipped
, TGSI_WRITEMASK_Y
),
6375 ureg_src(is_fbo
), samplepos_sysval
, ureg_src(samplepos_flipped
));
6376 ureg_MOV(ureg
, ureg_writemask(samplepos_flipped
, TGSI_WRITEMASK_X
),
6379 /* Use the result in place of the system value. */
6380 t
->systemValues
[SYSTEM_VALUE_SAMPLE_POS
] = ureg_src(samplepos_flipped
);
6385 * Emit the TGSI instructions for inverting and adjusting WPOS.
6386 * This code is unavoidable because it also depends on whether
6387 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
6390 emit_wpos_adjustment(struct gl_context
*ctx
,
6391 struct st_translate
*t
,
6392 int wpos_transform_const
,
6394 GLfloat adjX
, GLfloat adjY
[2])
6396 struct ureg_program
*ureg
= t
->ureg
;
6398 assert(wpos_transform_const
>= 0);
6400 /* Fragment program uses fragment position input.
6401 * Need to replace instances of INPUT[WPOS] with temp T
6402 * where T = INPUT[WPOS] is inverted by Y.
6404 struct ureg_src wpostrans
= ureg_DECL_constant(ureg
, wpos_transform_const
);
6405 struct ureg_dst wpos_temp
= ureg_DECL_temporary(ureg
);
6406 struct ureg_src
*wpos
=
6407 ctx
->Const
.GLSLFragCoordIsSysVal
?
6408 &t
->systemValues
[SYSTEM_VALUE_FRAG_COORD
] :
6409 &t
->inputs
[t
->inputMapping
[VARYING_SLOT_POS
]];
6410 struct ureg_src wpos_input
= *wpos
;
6412 /* First, apply the coordinate shift: */
6413 if (adjX
|| adjY
[0] || adjY
[1]) {
6414 if (adjY
[0] != adjY
[1]) {
6415 /* Adjust the y coordinate by adjY[1] or adjY[0] respectively
6416 * depending on whether inversion is actually going to be applied
6417 * or not, which is determined by testing against the inversion
6418 * state variable used below, which will be either +1 or -1.
6420 struct ureg_dst adj_temp
= ureg_DECL_local_temporary(ureg
);
6422 ureg_CMP(ureg
, adj_temp
,
6423 ureg_scalar(wpostrans
, invert
? 2 : 0),
6424 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
),
6425 ureg_imm4f(ureg
, adjX
, adjY
[1], 0.0f
, 0.0f
));
6426 ureg_ADD(ureg
, wpos_temp
, wpos_input
, ureg_src(adj_temp
));
6428 ureg_ADD(ureg
, wpos_temp
, wpos_input
,
6429 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
));
6431 wpos_input
= ureg_src(wpos_temp
);
6433 /* MOV wpos_temp, input[wpos]
6435 ureg_MOV(ureg
, wpos_temp
, wpos_input
);
6438 /* Now the conditional y flip: STATE_FB_WPOS_Y_TRANSFORM.xy/zw will be
6439 * inversion/identity, or the other way around if we're drawing to an FBO.
6442 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
6445 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
6447 ureg_scalar(wpostrans
, 0),
6448 ureg_scalar(wpostrans
, 1));
6450 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
6453 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
6455 ureg_scalar(wpostrans
, 2),
6456 ureg_scalar(wpostrans
, 3));
6459 /* Use wpos_temp as position input from here on:
6461 *wpos
= ureg_src(wpos_temp
);
6466 * Emit fragment position/ooordinate code.
6469 emit_wpos(struct st_context
*st
,
6470 struct st_translate
*t
,
6471 const struct gl_program
*program
,
6472 struct ureg_program
*ureg
,
6473 int wpos_transform_const
)
6475 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
6476 GLfloat adjX
= 0.0f
;
6477 GLfloat adjY
[2] = { 0.0f
, 0.0f
};
6478 boolean invert
= FALSE
;
6480 /* Query the pixel center conventions supported by the pipe driver and set
6481 * adjX, adjY to help out if it cannot handle the requested one internally.
6483 * The bias of the y-coordinate depends on whether y-inversion takes place
6484 * (adjY[1]) or not (adjY[0]), which is in turn dependent on whether we are
6485 * drawing to an FBO (causes additional inversion), and whether the pipe
6486 * driver origin and the requested origin differ (the latter condition is
6487 * stored in the 'invert' variable).
6489 * For height = 100 (i = integer, h = half-integer, l = lower, u = upper):
6491 * center shift only:
6496 * l,i -> u,i: ( 0.0 + 1.0) * -1 + 100 = 99
6497 * l,h -> u,h: ( 0.5 + 0.0) * -1 + 100 = 99.5
6498 * u,i -> l,i: (99.0 + 1.0) * -1 + 100 = 0
6499 * u,h -> l,h: (99.5 + 0.0) * -1 + 100 = 0.5
6501 * inversion and center shift:
6502 * l,i -> u,h: ( 0.0 + 0.5) * -1 + 100 = 99.5
6503 * l,h -> u,i: ( 0.5 + 0.5) * -1 + 100 = 99
6504 * u,i -> l,h: (99.0 + 0.5) * -1 + 100 = 0.5
6505 * u,h -> l,i: (99.5 + 0.5) * -1 + 100 = 0
6507 if (program
->info
.fs
.origin_upper_left
) {
6508 /* Fragment shader wants origin in upper-left */
6509 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
)) {
6510 /* the driver supports upper-left origin */
6512 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
)) {
6513 /* the driver supports lower-left origin, need to invert Y */
6514 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_ORIGIN
,
6515 TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
6522 /* Fragment shader wants origin in lower-left */
6523 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
))
6524 /* the driver supports lower-left origin */
6525 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_ORIGIN
,
6526 TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
6527 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
))
6528 /* the driver supports upper-left origin, need to invert Y */
6534 if (program
->info
.fs
.pixel_center_integer
) {
6535 /* Fragment shader wants pixel center integer */
6536 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
6537 /* the driver supports pixel center integer */
6539 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
,
6540 TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
6542 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
6543 /* the driver supports pixel center half integer, need to bias X,Y */
6552 /* Fragment shader wants pixel center half integer */
6553 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
6554 /* the driver supports pixel center half integer */
6556 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
6557 /* the driver supports pixel center integer, need to bias X,Y */
6558 adjX
= adjY
[0] = adjY
[1] = 0.5f
;
6559 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
,
6560 TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
6566 /* we invert after adjustment so that we avoid the MOV to temporary,
6567 * and reuse the adjustment ADD instead */
6568 emit_wpos_adjustment(st
->ctx
, t
, wpos_transform_const
, invert
, adjX
, adjY
);
6572 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
6573 * TGSI uses +1 for front, -1 for back.
6574 * This function converts the TGSI value to the GL value. Simply clamping/
6575 * saturating the value to [0,1] does the job.
6578 emit_face_var(struct gl_context
*ctx
, struct st_translate
*t
)
6580 struct ureg_program
*ureg
= t
->ureg
;
6581 struct ureg_dst face_temp
= ureg_DECL_temporary(ureg
);
6582 struct ureg_src face_input
= t
->inputs
[t
->inputMapping
[VARYING_SLOT_FACE
]];
6584 if (ctx
->Const
.NativeIntegers
) {
6585 ureg_FSGE(ureg
, face_temp
, face_input
, ureg_imm1f(ureg
, 0));
6588 /* MOV_SAT face_temp, input[face] */
6589 ureg_MOV(ureg
, ureg_saturate(face_temp
), face_input
);
6592 /* Use face_temp as face input from here on: */
6593 t
->inputs
[t
->inputMapping
[VARYING_SLOT_FACE
]] = ureg_src(face_temp
);
6597 emit_compute_block_size(const struct gl_program
*prog
,
6598 struct ureg_program
*ureg
) {
6599 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
,
6600 prog
->info
.cs
.local_size
[0]);
6601 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
,
6602 prog
->info
.cs
.local_size
[1]);
6603 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
,
6604 prog
->info
.cs
.local_size
[2]);
6607 struct sort_inout_decls
{
6608 bool operator()(const struct inout_decl
&a
, const struct inout_decl
&b
) const {
6609 return mapping
[a
.mesa_index
] < mapping
[b
.mesa_index
];
6612 const ubyte
*mapping
;
6615 /* Sort the given array of decls by the corresponding slot (TGSI file index).
6617 * This is for the benefit of older drivers which are broken when the
6618 * declarations aren't sorted in this way.
6621 sort_inout_decls_by_slot(struct inout_decl
*decls
,
6623 const ubyte mapping
[])
6625 sort_inout_decls sorter
;
6626 sorter
.mapping
= mapping
;
6627 std::sort(decls
, decls
+ count
, sorter
);
6630 static enum tgsi_interpolate_mode
6631 st_translate_interp(enum glsl_interp_mode glsl_qual
, GLuint varying
)
6633 switch (glsl_qual
) {
6634 case INTERP_MODE_NONE
:
6635 if (varying
== VARYING_SLOT_COL0
|| varying
== VARYING_SLOT_COL1
)
6636 return TGSI_INTERPOLATE_COLOR
;
6637 return TGSI_INTERPOLATE_PERSPECTIVE
;
6638 case INTERP_MODE_SMOOTH
:
6639 return TGSI_INTERPOLATE_PERSPECTIVE
;
6640 case INTERP_MODE_FLAT
:
6641 return TGSI_INTERPOLATE_CONSTANT
;
6642 case INTERP_MODE_NOPERSPECTIVE
:
6643 return TGSI_INTERPOLATE_LINEAR
;
6645 assert(0 && "unexpected interp mode in st_translate_interp()");
6646 return TGSI_INTERPOLATE_PERSPECTIVE
;
6651 * Translate intermediate IR (glsl_to_tgsi_instruction) to TGSI format.
6652 * \param program the program to translate
6653 * \param numInputs number of input registers used
6654 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
6656 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
6657 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
6659 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
6660 * \param numOutputs number of output registers used
6661 * \param outputMapping maps Mesa fragment program outputs to TGSI
6663 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
6664 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
6667 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
6669 extern "C" enum pipe_error
6670 st_translate_program(
6671 struct gl_context
*ctx
,
6672 enum pipe_shader_type procType
,
6673 struct ureg_program
*ureg
,
6674 glsl_to_tgsi_visitor
*program
,
6675 const struct gl_program
*proginfo
,
6677 const ubyte inputMapping
[],
6678 const ubyte inputSlotToAttr
[],
6679 const ubyte inputSemanticName
[],
6680 const ubyte inputSemanticIndex
[],
6681 const ubyte interpMode
[],
6683 const ubyte outputMapping
[],
6684 const ubyte outputSemanticName
[],
6685 const ubyte outputSemanticIndex
[])
6687 struct pipe_screen
*screen
= st_context(ctx
)->pipe
->screen
;
6688 struct st_translate
*t
;
6690 struct gl_program_constants
*frag_const
=
6691 &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
6692 enum pipe_error ret
= PIPE_OK
;
6694 assert(numInputs
<= ARRAY_SIZE(t
->inputs
));
6695 assert(numOutputs
<= ARRAY_SIZE(t
->outputs
));
6697 ASSERT_BITFIELD_SIZE(st_src_reg
, type
, GLSL_TYPE_ERROR
);
6698 ASSERT_BITFIELD_SIZE(st_dst_reg
, type
, GLSL_TYPE_ERROR
);
6699 ASSERT_BITFIELD_SIZE(glsl_to_tgsi_instruction
, tex_type
, GLSL_TYPE_ERROR
);
6700 ASSERT_BITFIELD_SIZE(glsl_to_tgsi_instruction
, image_format
, PIPE_FORMAT_COUNT
);
6701 ASSERT_BITFIELD_SIZE(glsl_to_tgsi_instruction
, tex_target
,
6702 (gl_texture_index
) (NUM_TEXTURE_TARGETS
- 1));
6703 ASSERT_BITFIELD_SIZE(glsl_to_tgsi_instruction
, image_format
,
6704 (enum pipe_format
) (PIPE_FORMAT_COUNT
- 1));
6705 ASSERT_BITFIELD_SIZE(glsl_to_tgsi_instruction
, op
,
6706 (enum tgsi_opcode
) (TGSI_OPCODE_LAST
- 1));
6708 t
= CALLOC_STRUCT(st_translate
);
6710 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
6714 t
->procType
= procType
;
6715 t
->need_uarl
= !screen
->get_param(screen
, PIPE_CAP_TGSI_ANY_REG_AS_ADDRESS
);
6716 t
->tg4_component_in_swizzle
= screen
->get_param(screen
, PIPE_CAP_TGSI_TG4_COMPONENT_IN_SWIZZLE
);
6717 t
->inputMapping
= inputMapping
;
6718 t
->outputMapping
= outputMapping
;
6720 t
->num_temp_arrays
= program
->next_array
;
6721 if (t
->num_temp_arrays
)
6722 t
->arrays
= (struct ureg_dst
*)
6723 calloc(t
->num_temp_arrays
, sizeof(t
->arrays
[0]));
6726 * Declare input attributes.
6729 case PIPE_SHADER_FRAGMENT
:
6730 case PIPE_SHADER_GEOMETRY
:
6731 case PIPE_SHADER_TESS_EVAL
:
6732 case PIPE_SHADER_TESS_CTRL
:
6733 sort_inout_decls_by_slot(program
->inputs
, program
->num_inputs
, inputMapping
);
6735 for (i
= 0; i
< program
->num_inputs
; ++i
) {
6736 struct inout_decl
*decl
= &program
->inputs
[i
];
6737 unsigned slot
= inputMapping
[decl
->mesa_index
];
6738 struct ureg_src src
;
6739 ubyte tgsi_usage_mask
= decl
->usage_mask
;
6741 if (glsl_base_type_is_64bit(decl
->base_type
)) {
6742 if (tgsi_usage_mask
== 1)
6743 tgsi_usage_mask
= TGSI_WRITEMASK_XY
;
6744 else if (tgsi_usage_mask
== 2)
6745 tgsi_usage_mask
= TGSI_WRITEMASK_ZW
;
6747 tgsi_usage_mask
= TGSI_WRITEMASK_XYZW
;
6750 enum tgsi_interpolate_mode interp_mode
= TGSI_INTERPOLATE_CONSTANT
;
6751 enum tgsi_interpolate_loc interp_location
= TGSI_INTERPOLATE_LOC_CENTER
;
6752 if (procType
== PIPE_SHADER_FRAGMENT
) {
6754 interp_mode
= interpMode
[slot
] != TGSI_INTERPOLATE_COUNT
?
6755 (enum tgsi_interpolate_mode
) interpMode
[slot
] :
6756 st_translate_interp(decl
->interp
, inputSlotToAttr
[slot
]);
6758 interp_location
= (enum tgsi_interpolate_loc
) decl
->interp_loc
;
6761 src
= ureg_DECL_fs_input_cyl_centroid_layout(ureg
,
6762 (enum tgsi_semantic
) inputSemanticName
[slot
],
6763 inputSemanticIndex
[slot
],
6764 interp_mode
, 0, interp_location
, slot
, tgsi_usage_mask
,
6765 decl
->array_id
, decl
->size
);
6767 for (unsigned j
= 0; j
< decl
->size
; ++j
) {
6768 if (t
->inputs
[slot
+ j
].File
!= TGSI_FILE_INPUT
) {
6769 /* The ArrayID is set up in dst_register */
6770 t
->inputs
[slot
+ j
] = src
;
6771 t
->inputs
[slot
+ j
].ArrayID
= 0;
6772 t
->inputs
[slot
+ j
].Index
+= j
;
6777 case PIPE_SHADER_VERTEX
:
6778 for (i
= 0; i
< numInputs
; i
++) {
6779 t
->inputs
[i
] = ureg_DECL_vs_input(ureg
, i
);
6782 case PIPE_SHADER_COMPUTE
:
6789 * Declare output attributes.
6792 case PIPE_SHADER_FRAGMENT
:
6793 case PIPE_SHADER_COMPUTE
:
6795 case PIPE_SHADER_GEOMETRY
:
6796 case PIPE_SHADER_TESS_EVAL
:
6797 case PIPE_SHADER_TESS_CTRL
:
6798 case PIPE_SHADER_VERTEX
:
6799 sort_inout_decls_by_slot(program
->outputs
, program
->num_outputs
, outputMapping
);
6801 for (i
= 0; i
< program
->num_outputs
; ++i
) {
6802 struct inout_decl
*decl
= &program
->outputs
[i
];
6803 unsigned slot
= outputMapping
[decl
->mesa_index
];
6804 struct ureg_dst dst
;
6805 ubyte tgsi_usage_mask
= decl
->usage_mask
;
6807 if (glsl_base_type_is_64bit(decl
->base_type
)) {
6808 if (tgsi_usage_mask
== 1)
6809 tgsi_usage_mask
= TGSI_WRITEMASK_XY
;
6810 else if (tgsi_usage_mask
== 2)
6811 tgsi_usage_mask
= TGSI_WRITEMASK_ZW
;
6813 tgsi_usage_mask
= TGSI_WRITEMASK_XYZW
;
6816 dst
= ureg_DECL_output_layout(ureg
,
6817 (enum tgsi_semantic
) outputSemanticName
[slot
],
6818 outputSemanticIndex
[slot
],
6819 decl
->gs_out_streams
,
6820 slot
, tgsi_usage_mask
, decl
->array_id
, decl
->size
, decl
->invariant
);
6821 dst
.Invariant
= decl
->invariant
;
6822 for (unsigned j
= 0; j
< decl
->size
; ++j
) {
6823 if (t
->outputs
[slot
+ j
].File
!= TGSI_FILE_OUTPUT
) {
6824 /* The ArrayID is set up in dst_register */
6825 t
->outputs
[slot
+ j
] = dst
;
6826 t
->outputs
[slot
+ j
].ArrayID
= 0;
6827 t
->outputs
[slot
+ j
].Index
+= j
;
6828 t
->outputs
[slot
+ j
].Invariant
= decl
->invariant
;
6837 if (procType
== PIPE_SHADER_FRAGMENT
) {
6838 if (program
->shader
->Program
->info
.fs
.early_fragment_tests
||
6839 program
->shader
->Program
->info
.fs
.post_depth_coverage
) {
6840 ureg_property(ureg
, TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL
, 1);
6842 if (program
->shader
->Program
->info
.fs
.post_depth_coverage
)
6843 ureg_property(ureg
, TGSI_PROPERTY_FS_POST_DEPTH_COVERAGE
, 1);
6846 if (proginfo
->info
.inputs_read
& VARYING_BIT_POS
) {
6847 /* Must do this after setting up t->inputs. */
6848 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
,
6849 program
->wpos_transform_const
);
6852 if (proginfo
->info
.inputs_read
& VARYING_BIT_FACE
)
6853 emit_face_var(ctx
, t
);
6855 for (i
= 0; i
< numOutputs
; i
++) {
6856 switch (outputSemanticName
[i
]) {
6857 case TGSI_SEMANTIC_POSITION
:
6858 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6859 TGSI_SEMANTIC_POSITION
, /* Z/Depth */
6860 outputSemanticIndex
[i
]);
6861 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Z
);
6863 case TGSI_SEMANTIC_STENCIL
:
6864 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6865 TGSI_SEMANTIC_STENCIL
, /* Stencil */
6866 outputSemanticIndex
[i
]);
6867 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Y
);
6869 case TGSI_SEMANTIC_COLOR
:
6870 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6871 TGSI_SEMANTIC_COLOR
,
6872 outputSemanticIndex
[i
]);
6874 case TGSI_SEMANTIC_SAMPLEMASK
:
6875 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6876 TGSI_SEMANTIC_SAMPLEMASK
,
6877 outputSemanticIndex
[i
]);
6878 /* TODO: If we ever support more than 32 samples, this will have
6879 * to become an array.
6881 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_X
);
6884 assert(!"fragment shader outputs must be POSITION/STENCIL/COLOR");
6885 ret
= PIPE_ERROR_BAD_INPUT
;
6890 else if (procType
== PIPE_SHADER_VERTEX
) {
6891 for (i
= 0; i
< numOutputs
; i
++) {
6892 if (outputSemanticName
[i
] == TGSI_SEMANTIC_FOG
) {
6893 /* force register to contain a fog coordinate in the form (F, 0, 0, 1). */
6895 ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_YZW
),
6896 ureg_imm4f(ureg
, 0.0f
, 0.0f
, 0.0f
, 1.0f
));
6897 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_X
);
6902 if (procType
== PIPE_SHADER_COMPUTE
) {
6903 emit_compute_block_size(proginfo
, ureg
);
6906 /* Declare address register.
6908 if (program
->num_address_regs
> 0) {
6909 assert(program
->num_address_regs
<= 3);
6910 for (int i
= 0; i
< program
->num_address_regs
; i
++)
6911 t
->address
[i
] = ureg_DECL_address(ureg
);
6914 /* Declare misc input registers
6917 GLbitfield64 sysInputs
= proginfo
->info
.system_values_read
;
6919 for (i
= 0; sysInputs
; i
++) {
6920 if (sysInputs
& (1ull << i
)) {
6921 enum tgsi_semantic semName
= _mesa_sysval_to_semantic(i
);
6923 t
->systemValues
[i
] = ureg_DECL_system_value(ureg
, semName
, 0);
6925 if (semName
== TGSI_SEMANTIC_INSTANCEID
||
6926 semName
== TGSI_SEMANTIC_VERTEXID
) {
6927 /* From Gallium perspective, these system values are always
6928 * integer, and require native integer support. However, if
6929 * native integer is supported on the vertex stage but not the
6930 * pixel stage (e.g, i915g + draw), Mesa will generate IR that
6931 * assumes these system values are floats. To resolve the
6932 * inconsistency, we insert a U2F.
6934 struct st_context
*st
= st_context(ctx
);
6935 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
6936 assert(procType
== PIPE_SHADER_VERTEX
);
6937 assert(pscreen
->get_shader_param(pscreen
, PIPE_SHADER_VERTEX
, PIPE_SHADER_CAP_INTEGERS
));
6939 if (!ctx
->Const
.NativeIntegers
) {
6940 struct ureg_dst temp
= ureg_DECL_local_temporary(t
->ureg
);
6941 ureg_U2F(t
->ureg
, ureg_writemask(temp
, TGSI_WRITEMASK_X
),
6942 t
->systemValues
[i
]);
6943 t
->systemValues
[i
] = ureg_scalar(ureg_src(temp
), 0);
6947 if (procType
== PIPE_SHADER_FRAGMENT
&&
6948 semName
== TGSI_SEMANTIC_POSITION
)
6949 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
,
6950 program
->wpos_transform_const
);
6952 if (procType
== PIPE_SHADER_FRAGMENT
&&
6953 semName
== TGSI_SEMANTIC_SAMPLEPOS
)
6954 emit_samplepos_adjustment(t
, program
->wpos_transform_const
);
6956 sysInputs
&= ~(1ull << i
);
6961 t
->array_sizes
= program
->array_sizes
;
6962 t
->input_decls
= program
->inputs
;
6963 t
->num_input_decls
= program
->num_inputs
;
6964 t
->output_decls
= program
->outputs
;
6965 t
->num_output_decls
= program
->num_outputs
;
6967 /* Emit constants and uniforms. TGSI uses a single index space for these,
6968 * so we put all the translated regs in t->constants.
6970 if (proginfo
->Parameters
) {
6971 t
->constants
= (struct ureg_src
*)
6972 calloc(proginfo
->Parameters
->NumParameters
, sizeof(t
->constants
[0]));
6973 if (t
->constants
== NULL
) {
6974 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
6977 t
->num_constants
= proginfo
->Parameters
->NumParameters
;
6979 for (i
= 0; i
< proginfo
->Parameters
->NumParameters
; i
++) {
6980 unsigned pvo
= proginfo
->Parameters
->ParameterValueOffset
[i
];
6982 switch (proginfo
->Parameters
->Parameters
[i
].Type
) {
6983 case PROGRAM_STATE_VAR
:
6984 case PROGRAM_UNIFORM
:
6985 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
6988 /* Emit immediates for PROGRAM_CONSTANT only when there's no indirect
6989 * addressing of the const buffer.
6990 * FIXME: Be smarter and recognize param arrays:
6991 * indirect addressing is only valid within the referenced
6994 case PROGRAM_CONSTANT
:
6995 if (program
->indirect_addr_consts
)
6996 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
6998 t
->constants
[i
] = emit_immediate(t
,
6999 proginfo
->Parameters
->ParameterValues
+ pvo
,
7000 proginfo
->Parameters
->Parameters
[i
].DataType
,
7009 for (i
= 0; i
< proginfo
->info
.num_ubos
; i
++) {
7010 unsigned size
= proginfo
->sh
.UniformBlocks
[i
]->UniformBufferSize
;
7011 unsigned num_const_vecs
= (size
+ 15) / 16;
7012 unsigned first
, last
;
7013 assert(num_const_vecs
> 0);
7015 last
= num_const_vecs
> 0 ? num_const_vecs
- 1 : 0;
7016 ureg_DECL_constant2D(t
->ureg
, first
, last
, i
+ 1);
7019 /* Emit immediate values.
7021 t
->immediates
= (struct ureg_src
*)
7022 calloc(program
->num_immediates
, sizeof(struct ureg_src
));
7023 if (t
->immediates
== NULL
) {
7024 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
7027 t
->num_immediates
= program
->num_immediates
;
7030 foreach_in_list(immediate_storage
, imm
, &program
->immediates
) {
7031 assert(i
< program
->num_immediates
);
7032 t
->immediates
[i
++] = emit_immediate(t
, imm
->values
, imm
->type
, imm
->size32
);
7034 assert(i
== program
->num_immediates
);
7036 /* texture samplers */
7037 for (i
= 0; i
< frag_const
->MaxTextureImageUnits
; i
++) {
7038 if (program
->samplers_used
& (1u << i
)) {
7039 enum tgsi_return_type type
=
7040 st_translate_texture_type(program
->sampler_types
[i
]);
7042 t
->samplers
[i
] = ureg_DECL_sampler(ureg
, i
);
7044 ureg_DECL_sampler_view(ureg
, i
, program
->sampler_targets
[i
],
7045 type
, type
, type
, type
);
7049 /* Declare atomic and shader storage buffers. */
7051 struct gl_program
*prog
= program
->prog
;
7053 if (!st_context(ctx
)->has_hw_atomics
) {
7054 for (i
= 0; i
< prog
->info
.num_abos
; i
++) {
7055 unsigned index
= prog
->sh
.AtomicBuffers
[i
]->Binding
;
7056 assert(index
< frag_const
->MaxAtomicBuffers
);
7057 t
->buffers
[index
] = ureg_DECL_buffer(ureg
, index
, true);
7060 for (i
= 0; i
< program
->num_atomics
; i
++) {
7061 struct hwatomic_decl
*ainfo
= &program
->atomic_info
[i
];
7062 gl_uniform_storage
*uni_storage
= &prog
->sh
.data
->UniformStorage
[ainfo
->location
];
7063 int base
= uni_storage
->offset
/ ATOMIC_COUNTER_SIZE
;
7064 ureg_DECL_hw_atomic(ureg
, base
, base
+ ainfo
->size
- 1, ainfo
->binding
,
7069 assert(prog
->info
.num_ssbos
<= frag_const
->MaxShaderStorageBlocks
);
7070 for (i
= 0; i
< prog
->info
.num_ssbos
; i
++) {
7072 if (!st_context(ctx
)->has_hw_atomics
)
7073 index
+= frag_const
->MaxAtomicBuffers
;
7075 t
->buffers
[index
] = ureg_DECL_buffer(ureg
, index
, false);
7079 if (program
->use_shared_memory
)
7080 t
->shared_memory
= ureg_DECL_memory(ureg
, TGSI_MEMORY_TYPE_SHARED
);
7082 for (i
= 0; i
< program
->shader
->Program
->info
.num_images
; i
++) {
7083 if (program
->images_used
& (1 << i
)) {
7084 t
->images
[i
] = ureg_DECL_image(ureg
, i
,
7085 program
->image_targets
[i
],
7086 program
->image_formats
[i
],
7087 program
->image_wr
[i
],
7092 /* Emit each instruction in turn:
7094 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &program
->instructions
)
7095 compile_tgsi_instruction(t
, inst
);
7097 /* Set the next shader stage hint for VS and TES. */
7099 case PIPE_SHADER_VERTEX
:
7100 case PIPE_SHADER_TESS_EVAL
:
7101 if (program
->shader_program
->SeparateShader
)
7104 for (i
= program
->shader
->Stage
+1; i
<= MESA_SHADER_FRAGMENT
; i
++) {
7105 if (program
->shader_program
->_LinkedShaders
[i
]) {
7106 ureg_set_next_shader_processor(
7107 ureg
, pipe_shader_type_from_mesa((gl_shader_stage
)i
));
7113 ; /* nothing - silence compiler warning */
7121 t
->num_constants
= 0;
7122 free(t
->immediates
);
7123 t
->num_immediates
= 0;
7129 /* ----------------------------- End TGSI code ------------------------------ */
7133 * Convert a shader's GLSL IR into a Mesa gl_program, although without
7134 * generating Mesa IR.
7136 static struct gl_program
*
7137 get_mesa_program_tgsi(struct gl_context
*ctx
,
7138 struct gl_shader_program
*shader_program
,
7139 struct gl_linked_shader
*shader
)
7141 glsl_to_tgsi_visitor
* v
;
7142 struct gl_program
*prog
;
7143 struct gl_shader_compiler_options
*options
=
7144 &ctx
->Const
.ShaderCompilerOptions
[shader
->Stage
];
7145 struct pipe_screen
*pscreen
= ctx
->st
->pipe
->screen
;
7146 enum pipe_shader_type ptarget
= pipe_shader_type_from_mesa(shader
->Stage
);
7147 unsigned skip_merge_registers
;
7149 validate_ir_tree(shader
->ir
);
7151 prog
= shader
->Program
;
7153 prog
->Parameters
= _mesa_new_parameter_list();
7154 v
= new glsl_to_tgsi_visitor();
7157 v
->shader_program
= shader_program
;
7159 v
->options
= options
;
7160 v
->native_integers
= ctx
->Const
.NativeIntegers
;
7162 v
->have_sqrt
= pscreen
->get_shader_param(pscreen
, ptarget
,
7163 PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED
);
7164 v
->have_fma
= pscreen
->get_shader_param(pscreen
, ptarget
,
7165 PIPE_SHADER_CAP_TGSI_FMA_SUPPORTED
);
7166 v
->has_tex_txf_lz
= pscreen
->get_param(pscreen
,
7167 PIPE_CAP_TGSI_TEX_TXF_LZ
);
7168 v
->need_uarl
= !pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_ANY_REG_AS_ADDRESS
);
7170 v
->tg4_component_in_swizzle
= pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_TG4_COMPONENT_IN_SWIZZLE
);
7171 v
->variables
= _mesa_hash_table_create(v
->mem_ctx
, _mesa_hash_pointer
,
7172 _mesa_key_pointer_equal
);
7173 skip_merge_registers
=
7174 pscreen
->get_shader_param(pscreen
, ptarget
,
7175 PIPE_SHADER_CAP_TGSI_SKIP_MERGE_REGISTERS
);
7177 _mesa_generate_parameters_list_for_uniforms(ctx
, shader_program
, shader
,
7180 /* Remove reads from output registers. */
7181 if (!pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_CAN_READ_OUTPUTS
))
7182 lower_output_reads(shader
->Stage
, shader
->ir
);
7184 /* Emit intermediate IR for main(). */
7185 visit_exec_list(shader
->ir
, v
);
7188 /* Print out some information (for debugging purposes) used by the
7189 * optimization passes. */
7192 int *first_writes
= ralloc_array(v
->mem_ctx
, int, v
->next_temp
);
7193 int *first_reads
= ralloc_array(v
->mem_ctx
, int, v
->next_temp
);
7194 int *last_writes
= ralloc_array(v
->mem_ctx
, int, v
->next_temp
);
7195 int *last_reads
= ralloc_array(v
->mem_ctx
, int, v
->next_temp
);
7197 for (i
= 0; i
< v
->next_temp
; i
++) {
7198 first_writes
[i
] = -1;
7199 first_reads
[i
] = -1;
7200 last_writes
[i
] = -1;
7203 v
->get_first_temp_read(first_reads
);
7204 v
->get_last_temp_read_first_temp_write(last_reads
, first_writes
);
7205 v
->get_last_temp_write(last_writes
);
7206 for (i
= 0; i
< v
->next_temp
; i
++)
7207 printf("Temp %d: FR=%3d FW=%3d LR=%3d LW=%3d\n", i
, first_reads
[i
],
7211 ralloc_free(first_writes
);
7212 ralloc_free(first_reads
);
7213 ralloc_free(last_writes
);
7214 ralloc_free(last_reads
);
7218 /* Perform optimizations on the instructions in the glsl_to_tgsi_visitor. */
7220 v
->copy_propagate();
7222 while (v
->eliminate_dead_code());
7224 v
->merge_two_dsts();
7226 if (!skip_merge_registers
) {
7228 v
->copy_propagate();
7229 while (v
->eliminate_dead_code());
7231 v
->merge_registers();
7232 v
->copy_propagate();
7233 while (v
->eliminate_dead_code());
7236 v
->renumber_registers();
7238 /* Write the END instruction. */
7239 v
->emit_asm(NULL
, TGSI_OPCODE_END
);
7241 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
7243 _mesa_log("GLSL IR for linked %s program %d:\n",
7244 _mesa_shader_stage_to_string(shader
->Stage
),
7245 shader_program
->Name
);
7246 _mesa_print_ir(_mesa_get_log_file(), shader
->ir
, NULL
);
7250 do_set_program_inouts(shader
->ir
, prog
, shader
->Stage
);
7252 _mesa_copy_linked_program_data(shader_program
, shader
);
7254 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_SKIP_SHRINK_IO_ARRAYS
)) {
7255 mark_array_io(v
->inputs
, v
->num_inputs
,
7256 &prog
->info
.inputs_read
,
7257 prog
->DualSlotInputs
,
7258 &prog
->info
.patch_inputs_read
);
7260 mark_array_io(v
->outputs
, v
->num_outputs
,
7261 &prog
->info
.outputs_written
, 0ULL,
7262 &prog
->info
.patch_outputs_written
);
7264 shrink_array_declarations(v
->inputs
, v
->num_inputs
,
7265 &prog
->info
.inputs_read
,
7266 prog
->DualSlotInputs
,
7267 &prog
->info
.patch_inputs_read
);
7268 shrink_array_declarations(v
->outputs
, v
->num_outputs
,
7269 &prog
->info
.outputs_written
, 0ULL,
7270 &prog
->info
.patch_outputs_written
);
7273 count_resources(v
, prog
);
7275 /* The GLSL IR won't be needed anymore. */
7276 ralloc_free(shader
->ir
);
7279 /* This must be done before the uniform storage is associated. */
7280 if (shader
->Stage
== MESA_SHADER_FRAGMENT
&&
7281 (prog
->info
.inputs_read
& VARYING_BIT_POS
||
7282 prog
->info
.system_values_read
& (1ull << SYSTEM_VALUE_FRAG_COORD
) ||
7283 prog
->info
.system_values_read
& (1ull << SYSTEM_VALUE_SAMPLE_POS
))) {
7284 static const gl_state_index16 wposTransformState
[STATE_LENGTH
] = {
7285 STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
7288 v
->wpos_transform_const
= _mesa_add_state_reference(prog
->Parameters
,
7289 wposTransformState
);
7292 /* Avoid reallocation of the program parameter list, because the uniform
7293 * storage is only associated with the original parameter list.
7294 * This should be enough for Bitmap and DrawPixels constants.
7296 _mesa_reserve_parameter_storage(prog
->Parameters
, 8);
7298 /* This has to be done last. Any operation the can cause
7299 * prog->ParameterValues to get reallocated (e.g., anything that adds a
7300 * program constant) has to happen before creating this linkage.
7302 _mesa_associate_uniform_storage(ctx
, shader_program
, prog
);
7303 if (!shader_program
->data
->LinkStatus
) {
7304 free_glsl_to_tgsi_visitor(v
);
7305 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
7309 struct st_vertex_program
*stvp
;
7310 struct st_fragment_program
*stfp
;
7311 struct st_common_program
*stp
;
7312 struct st_compute_program
*stcp
;
7314 switch (shader
->Stage
) {
7315 case MESA_SHADER_VERTEX
:
7316 stvp
= (struct st_vertex_program
*)prog
;
7317 stvp
->glsl_to_tgsi
= v
;
7319 case MESA_SHADER_FRAGMENT
:
7320 stfp
= (struct st_fragment_program
*)prog
;
7321 stfp
->glsl_to_tgsi
= v
;
7323 case MESA_SHADER_TESS_CTRL
:
7324 case MESA_SHADER_TESS_EVAL
:
7325 case MESA_SHADER_GEOMETRY
:
7326 stp
= st_common_program(prog
);
7327 stp
->glsl_to_tgsi
= v
;
7329 case MESA_SHADER_COMPUTE
:
7330 stcp
= (struct st_compute_program
*)prog
;
7331 stcp
->glsl_to_tgsi
= v
;
7334 assert(!"should not be reached");
7338 PRINT_STATS(v
->print_stats());
7343 /* See if there are unsupported control flow statements. */
7344 class ir_control_flow_info_visitor
: public ir_hierarchical_visitor
{
7346 const struct gl_shader_compiler_options
*options
;
7348 ir_control_flow_info_visitor(const struct gl_shader_compiler_options
*options
)
7354 virtual ir_visitor_status
visit_enter(ir_function
*ir
)
7356 /* Other functions are skipped (same as glsl_to_tgsi). */
7357 if (strcmp(ir
->name
, "main") == 0)
7358 return visit_continue
;
7360 return visit_continue_with_parent
;
7363 virtual ir_visitor_status
visit_enter(ir_call
*ir
)
7365 if (!ir
->callee
->is_intrinsic()) {
7366 unsupported
= true; /* it's a function call */
7369 return visit_continue
;
7372 virtual ir_visitor_status
visit_enter(ir_return
*ir
)
7374 if (options
->EmitNoMainReturn
) {
7378 return visit_continue
;
7385 has_unsupported_control_flow(exec_list
*ir
,
7386 const struct gl_shader_compiler_options
*options
)
7388 ir_control_flow_info_visitor
visitor(options
);
7389 visit_list_elements(&visitor
, ir
);
7390 return visitor
.unsupported
;
7395 * This actually involves converting GLSL IR into an intermediate TGSI-like IR
7396 * with code lowering and other optimizations.
7399 st_link_tgsi(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
7401 struct pipe_screen
*pscreen
= ctx
->st
->pipe
->screen
;
7403 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
7404 struct gl_linked_shader
*shader
= prog
->_LinkedShaders
[i
];
7408 exec_list
*ir
= shader
->ir
;
7409 gl_shader_stage stage
= shader
->Stage
;
7410 enum pipe_shader_type ptarget
= pipe_shader_type_from_mesa(stage
);
7411 const struct gl_shader_compiler_options
*options
=
7412 &ctx
->Const
.ShaderCompilerOptions
[stage
];
7414 unsigned if_threshold
= pscreen
->get_shader_param(pscreen
, ptarget
,
7415 PIPE_SHADER_CAP_LOWER_IF_THRESHOLD
);
7416 if (ctx
->Const
.GLSLOptimizeConservatively
) {
7417 /* Do it once and repeat only if there's unsupported control flow. */
7419 do_common_optimization(ir
, true, true, options
,
7420 ctx
->Const
.NativeIntegers
);
7421 lower_if_to_cond_assign((gl_shader_stage
)i
, ir
,
7422 options
->MaxIfDepth
, if_threshold
);
7423 } while (has_unsupported_control_flow(ir
, options
));
7425 /* Repeat it until it stops making changes. */
7428 progress
= do_common_optimization(ir
, true, true, options
,
7429 ctx
->Const
.NativeIntegers
);
7430 progress
|= lower_if_to_cond_assign((gl_shader_stage
)i
, ir
,
7431 options
->MaxIfDepth
, if_threshold
);
7435 /* Do this again to lower ir_binop_vector_extract introduced
7436 * by optimization passes.
7438 do_vec_index_to_cond_assign(ir
);
7440 validate_ir_tree(ir
);
7442 struct gl_program
*linked_prog
=
7443 get_mesa_program_tgsi(ctx
, prog
, shader
);
7444 st_set_prog_affected_state_flags(linked_prog
);
7447 if (!ctx
->Driver
.ProgramStringNotify(ctx
,
7448 _mesa_shader_stage_to_program(i
),
7450 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
7462 st_translate_stream_output_info(struct gl_transform_feedback_info
*info
,
7463 const ubyte outputMapping
[],
7464 struct pipe_stream_output_info
*so
)
7469 so
->num_outputs
= 0;
7473 for (i
= 0; i
< info
->NumOutputs
; i
++) {
7474 so
->output
[i
].register_index
=
7475 outputMapping
[info
->Outputs
[i
].OutputRegister
];
7476 so
->output
[i
].start_component
= info
->Outputs
[i
].ComponentOffset
;
7477 so
->output
[i
].num_components
= info
->Outputs
[i
].NumComponents
;
7478 so
->output
[i
].output_buffer
= info
->Outputs
[i
].OutputBuffer
;
7479 so
->output
[i
].dst_offset
= info
->Outputs
[i
].DstOffset
;
7480 so
->output
[i
].stream
= info
->Outputs
[i
].StreamId
;
7483 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
7484 so
->stride
[i
] = info
->Buffers
[i
].Stride
;
7486 so
->num_outputs
= info
->NumOutputs
;