2 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
3 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
4 * Copyright © 2010 Intel Corporation
5 * Copyright © 2011 Bryan Cain
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
28 * \file glsl_to_tgsi.cpp
30 * Translate GLSL IR to TGSI.
33 #include "st_glsl_to_tgsi.h"
35 #include "compiler/glsl/glsl_parser_extras.h"
36 #include "compiler/glsl/ir_optimization.h"
37 #include "compiler/glsl/program.h"
39 #include "main/errors.h"
40 #include "main/shaderobj.h"
41 #include "main/uniforms.h"
42 #include "main/shaderapi.h"
43 #include "main/shaderimage.h"
44 #include "program/prog_instruction.h"
46 #include "pipe/p_context.h"
47 #include "pipe/p_screen.h"
48 #include "tgsi/tgsi_ureg.h"
49 #include "tgsi/tgsi_info.h"
50 #include "util/u_math.h"
51 #include "util/u_memory.h"
52 #include "st_program.h"
53 #include "st_mesa_to_tgsi.h"
54 #include "st_format.h"
55 #include "st_glsl_types.h"
57 #include "st_shader_cache.h"
59 #include "util/hash_table.h"
62 #define PROGRAM_ANY_CONST ((1 << PROGRAM_STATE_VAR) | \
63 (1 << PROGRAM_CONSTANT) | \
64 (1 << PROGRAM_UNIFORM))
66 #define MAX_GLSL_TEXTURE_OFFSET 4
71 static int swizzle_for_size(int size
);
73 static int swizzle_for_type(const glsl_type
*type
, int component
= 0)
75 unsigned num_elements
= 4;
78 type
= type
->without_array();
79 if (type
->is_scalar() || type
->is_vector() || type
->is_matrix())
80 num_elements
= type
->vector_elements
;
83 int swizzle
= swizzle_for_size(num_elements
);
84 assert(num_elements
+ component
<= 4);
86 swizzle
+= component
* MAKE_SWIZZLE4(1, 1, 1, 1);
90 static unsigned is_precise(const ir_variable
*ir
)
94 return ir
->data
.precise
|| ir
->data
.invariant
;
98 * This struct is a corresponding struct to TGSI ureg_src.
102 st_src_reg(gl_register_file file
, int index
, const glsl_type
*type
,
103 int component
= 0, unsigned array_id
= 0)
105 assert(file
!= PROGRAM_ARRAY
|| array_id
!= 0);
108 this->swizzle
= swizzle_for_type(type
, component
);
112 this->type
= type
? type
->base_type
: GLSL_TYPE_ERROR
;
113 this->reladdr
= NULL
;
114 this->reladdr2
= NULL
;
115 this->has_index2
= false;
116 this->double_reg2
= false;
117 this->array_id
= array_id
;
118 this->is_double_vertex_input
= false;
121 st_src_reg(gl_register_file file
, int index
, enum glsl_base_type type
)
123 assert(file
!= PROGRAM_ARRAY
); /* need array_id > 0 */
128 this->swizzle
= SWIZZLE_XYZW
;
131 this->reladdr
= NULL
;
132 this->reladdr2
= NULL
;
133 this->has_index2
= false;
134 this->double_reg2
= false;
136 this->is_double_vertex_input
= false;
139 st_src_reg(gl_register_file file
, int index
, enum glsl_base_type type
, int index2D
)
141 assert(file
!= PROGRAM_ARRAY
); /* need array_id > 0 */
145 this->index2D
= index2D
;
146 this->swizzle
= SWIZZLE_XYZW
;
149 this->reladdr
= NULL
;
150 this->reladdr2
= NULL
;
151 this->has_index2
= false;
152 this->double_reg2
= false;
154 this->is_double_vertex_input
= false;
159 this->type
= GLSL_TYPE_ERROR
;
160 this->file
= PROGRAM_UNDEFINED
;
166 this->reladdr
= NULL
;
167 this->reladdr2
= NULL
;
168 this->has_index2
= false;
169 this->double_reg2
= false;
171 this->is_double_vertex_input
= false;
174 explicit st_src_reg(st_dst_reg reg
);
176 int32_t index
; /**< temporary index, VERT_ATTRIB_*, VARYING_SLOT_*, etc. */
178 uint16_t swizzle
; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
179 int negate
:4; /**< NEGATE_XYZW mask from mesa */
181 enum glsl_base_type type
:5; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
182 unsigned has_index2
:1;
183 gl_register_file file
:5; /**< PROGRAM_* from Mesa */
185 * Is this the second half of a double register pair?
186 * currently used for input mapping only.
188 unsigned double_reg2
:1;
189 unsigned is_double_vertex_input
:1;
190 unsigned array_id
:10;
192 /** Register index should be offset by the integer in this reg. */
194 st_src_reg
*reladdr2
;
198 st_src_reg reg
= *this;
207 st_dst_reg(gl_register_file file
, int writemask
, enum glsl_base_type type
, int index
)
209 assert(file
!= PROGRAM_ARRAY
); /* need array_id > 0 */
213 this->writemask
= writemask
;
214 this->reladdr
= NULL
;
215 this->reladdr2
= NULL
;
216 this->has_index2
= false;
221 st_dst_reg(gl_register_file file
, int writemask
, enum glsl_base_type type
)
223 assert(file
!= PROGRAM_ARRAY
); /* need array_id > 0 */
227 this->writemask
= writemask
;
228 this->reladdr
= NULL
;
229 this->reladdr2
= NULL
;
230 this->has_index2
= false;
237 this->type
= GLSL_TYPE_ERROR
;
238 this->file
= PROGRAM_UNDEFINED
;
242 this->reladdr
= NULL
;
243 this->reladdr2
= NULL
;
244 this->has_index2
= false;
248 explicit st_dst_reg(st_src_reg reg
);
250 int32_t index
; /**< temporary index, VERT_ATTRIB_*, VARYING_SLOT_*, etc. */
252 gl_register_file file
:5; /**< PROGRAM_* from Mesa */
253 unsigned writemask
:4; /**< Bitfield of WRITEMASK_[XYZW] */
254 enum glsl_base_type type
:5; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
255 unsigned has_index2
:1;
256 unsigned array_id
:10;
258 /** Register index should be offset by the integer in this reg. */
260 st_src_reg
*reladdr2
;
263 st_src_reg::st_src_reg(st_dst_reg reg
)
265 this->type
= reg
.type
;
266 this->file
= reg
.file
;
267 this->index
= reg
.index
;
268 this->swizzle
= SWIZZLE_XYZW
;
271 this->reladdr
= reg
.reladdr
;
272 this->index2D
= reg
.index2D
;
273 this->reladdr2
= reg
.reladdr2
;
274 this->has_index2
= reg
.has_index2
;
275 this->double_reg2
= false;
276 this->array_id
= reg
.array_id
;
277 this->is_double_vertex_input
= false;
280 st_dst_reg::st_dst_reg(st_src_reg reg
)
282 this->type
= reg
.type
;
283 this->file
= reg
.file
;
284 this->index
= reg
.index
;
285 this->writemask
= WRITEMASK_XYZW
;
286 this->reladdr
= reg
.reladdr
;
287 this->index2D
= reg
.index2D
;
288 this->reladdr2
= reg
.reladdr2
;
289 this->has_index2
= reg
.has_index2
;
290 this->array_id
= reg
.array_id
;
293 class glsl_to_tgsi_instruction
: public exec_node
{
295 DECLARE_RALLOC_CXX_OPERATORS(glsl_to_tgsi_instruction
)
299 st_src_reg resource
; /**< sampler, image or buffer register */
300 st_src_reg
*tex_offsets
;
302 /** Pointer to the ir source this tree came from for debugging */
305 unsigned op
:8; /**< TGSI opcode */
308 unsigned is_64bit_expanded
:1;
309 unsigned sampler_base
:5;
310 unsigned sampler_array_size
:6; /**< 1-based size of sampler array, 1 if not array */
311 unsigned tex_target
:4; /**< One of TEXTURE_*_INDEX */
312 glsl_base_type tex_type
:5;
313 unsigned tex_shadow
:1;
314 unsigned image_format
:9;
315 unsigned tex_offset_num_offset
:3;
316 unsigned dead_mask
:4; /**< Used in dead code elimination */
317 unsigned buffer_access
:3; /**< buffer access type */
319 const struct tgsi_opcode_info
*info
;
322 class variable_storage
{
323 DECLARE_RZALLOC_CXX_OPERATORS(variable_storage
)
326 variable_storage(ir_variable
*var
, gl_register_file file
, int index
,
327 unsigned array_id
= 0)
328 : file(file
), index(index
), component(0), var(var
), array_id(array_id
)
330 assert(file
!= PROGRAM_ARRAY
|| array_id
!= 0);
333 gl_register_file file
;
336 /* Explicit component location. This is given in terms of the GLSL-style
337 * swizzles where each double is a single component, i.e. for 64-bit types
338 * it can only be 0 or 1.
341 ir_variable
*var
; /* variable that maps to this, if any */
345 class immediate_storage
: public exec_node
{
347 immediate_storage(gl_constant_value
*values
, int size32
, int type
)
349 memcpy(this->values
, values
, size32
* sizeof(gl_constant_value
));
350 this->size32
= size32
;
354 /* doubles are stored across 2 gl_constant_values */
355 gl_constant_value values
[4];
356 int size32
; /**< Number of 32-bit components (1-4) */
357 int type
; /**< GL_DOUBLE, GL_FLOAT, GL_INT, GL_BOOL, or GL_UNSIGNED_INT */
360 static const st_src_reg undef_src
= st_src_reg(PROGRAM_UNDEFINED
, 0, GLSL_TYPE_ERROR
);
361 static const st_dst_reg undef_dst
= st_dst_reg(PROGRAM_UNDEFINED
, SWIZZLE_NOOP
, GLSL_TYPE_ERROR
);
365 unsigned array_id
; /* TGSI ArrayID; 1-based: 0 means not an array */
368 unsigned gs_out_streams
;
369 enum glsl_interp_mode interp
;
370 enum glsl_base_type base_type
;
371 ubyte usage_mask
; /* GLSL-style usage-mask, i.e. single bit per double */
374 static struct inout_decl
*
375 find_inout_array(struct inout_decl
*decls
, unsigned count
, unsigned array_id
)
377 assert(array_id
!= 0);
379 for (unsigned i
= 0; i
< count
; i
++) {
380 struct inout_decl
*decl
= &decls
[i
];
382 if (array_id
== decl
->array_id
) {
390 static enum glsl_base_type
391 find_array_type(struct inout_decl
*decls
, unsigned count
, unsigned array_id
)
394 return GLSL_TYPE_ERROR
;
395 struct inout_decl
*decl
= find_inout_array(decls
, count
, array_id
);
397 return decl
->base_type
;
398 return GLSL_TYPE_ERROR
;
401 struct rename_reg_pair
{
406 struct glsl_to_tgsi_visitor
: public ir_visitor
{
408 glsl_to_tgsi_visitor();
409 ~glsl_to_tgsi_visitor();
411 struct gl_context
*ctx
;
412 struct gl_program
*prog
;
413 struct gl_shader_program
*shader_program
;
414 struct gl_linked_shader
*shader
;
415 struct gl_shader_compiler_options
*options
;
419 unsigned *array_sizes
;
420 unsigned max_num_arrays
;
423 struct inout_decl inputs
[4 * PIPE_MAX_SHADER_INPUTS
];
425 unsigned num_input_arrays
;
426 struct inout_decl outputs
[4 * PIPE_MAX_SHADER_OUTPUTS
];
427 unsigned num_outputs
;
428 unsigned num_output_arrays
;
430 int num_address_regs
;
431 uint32_t samplers_used
;
432 glsl_base_type sampler_types
[PIPE_MAX_SAMPLERS
];
433 int sampler_targets
[PIPE_MAX_SAMPLERS
]; /**< One of TGSI_TEXTURE_* */
435 int image_targets
[PIPE_MAX_SHADER_IMAGES
];
436 unsigned image_formats
[PIPE_MAX_SHADER_IMAGES
];
437 bool indirect_addr_consts
;
438 int wpos_transform_const
;
441 bool native_integers
;
444 bool use_shared_memory
;
448 variable_storage
*find_variable_storage(ir_variable
*var
);
450 int add_constant(gl_register_file file
, gl_constant_value values
[8],
451 int size
, int datatype
, uint16_t *swizzle_out
);
453 st_src_reg
get_temp(const glsl_type
*type
);
454 void reladdr_to_temp(ir_instruction
*ir
, st_src_reg
*reg
, int *num_reladdr
);
456 st_src_reg
st_src_reg_for_double(double val
);
457 st_src_reg
st_src_reg_for_float(float val
);
458 st_src_reg
st_src_reg_for_int(int val
);
459 st_src_reg
st_src_reg_for_type(enum glsl_base_type type
, int val
);
462 * \name Visit methods
464 * As typical for the visitor pattern, there must be one \c visit method for
465 * each concrete subclass of \c ir_instruction. Virtual base classes within
466 * the hierarchy should not have \c visit methods.
469 virtual void visit(ir_variable
*);
470 virtual void visit(ir_loop
*);
471 virtual void visit(ir_loop_jump
*);
472 virtual void visit(ir_function_signature
*);
473 virtual void visit(ir_function
*);
474 virtual void visit(ir_expression
*);
475 virtual void visit(ir_swizzle
*);
476 virtual void visit(ir_dereference_variable
*);
477 virtual void visit(ir_dereference_array
*);
478 virtual void visit(ir_dereference_record
*);
479 virtual void visit(ir_assignment
*);
480 virtual void visit(ir_constant
*);
481 virtual void visit(ir_call
*);
482 virtual void visit(ir_return
*);
483 virtual void visit(ir_discard
*);
484 virtual void visit(ir_texture
*);
485 virtual void visit(ir_if
*);
486 virtual void visit(ir_emit_vertex
*);
487 virtual void visit(ir_end_primitive
*);
488 virtual void visit(ir_barrier
*);
491 void visit_expression(ir_expression
*, st_src_reg
*) ATTRIBUTE_NOINLINE
;
493 void visit_atomic_counter_intrinsic(ir_call
*);
494 void visit_ssbo_intrinsic(ir_call
*);
495 void visit_membar_intrinsic(ir_call
*);
496 void visit_shared_intrinsic(ir_call
*);
497 void visit_image_intrinsic(ir_call
*);
498 void visit_generic_intrinsic(ir_call
*, unsigned op
);
502 /** List of variable_storage */
503 struct hash_table
*variables
;
505 /** List of immediate_storage */
506 exec_list immediates
;
507 unsigned num_immediates
;
509 /** List of glsl_to_tgsi_instruction */
510 exec_list instructions
;
512 glsl_to_tgsi_instruction
*emit_asm(ir_instruction
*ir
, unsigned op
,
513 st_dst_reg dst
= undef_dst
,
514 st_src_reg src0
= undef_src
,
515 st_src_reg src1
= undef_src
,
516 st_src_reg src2
= undef_src
,
517 st_src_reg src3
= undef_src
);
519 glsl_to_tgsi_instruction
*emit_asm(ir_instruction
*ir
, unsigned op
,
520 st_dst_reg dst
, st_dst_reg dst1
,
521 st_src_reg src0
= undef_src
,
522 st_src_reg src1
= undef_src
,
523 st_src_reg src2
= undef_src
,
524 st_src_reg src3
= undef_src
);
526 unsigned get_opcode(unsigned op
,
528 st_src_reg src0
, st_src_reg src1
);
531 * Emit the correct dot-product instruction for the type of arguments
533 glsl_to_tgsi_instruction
*emit_dp(ir_instruction
*ir
,
539 void emit_scalar(ir_instruction
*ir
, unsigned op
,
540 st_dst_reg dst
, st_src_reg src0
);
542 void emit_scalar(ir_instruction
*ir
, unsigned op
,
543 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
545 void emit_arl(ir_instruction
*ir
, st_dst_reg dst
, st_src_reg src0
);
547 void get_deref_offsets(ir_dereference
*ir
,
548 unsigned *array_size
,
553 void calc_deref_offsets(ir_dereference
*tail
,
554 unsigned *array_elements
,
556 st_src_reg
*indirect
,
558 st_src_reg
canonicalize_gather_offset(st_src_reg offset
);
560 bool try_emit_mad(ir_expression
*ir
,
562 bool try_emit_mad_for_and_not(ir_expression
*ir
,
565 void emit_swz(ir_expression
*ir
);
567 bool process_move_condition(ir_rvalue
*ir
);
569 void simplify_cmp(void);
571 void rename_temp_registers(struct rename_reg_pair
*renames
);
572 void get_first_temp_read(int *first_reads
);
573 void get_first_temp_write(int *first_writes
);
574 void get_last_temp_read_first_temp_write(int *last_reads
, int *first_writes
);
575 void get_last_temp_write(int *last_writes
);
577 void copy_propagate(void);
578 int eliminate_dead_code(void);
580 void merge_two_dsts(void);
581 void merge_registers(void);
582 void renumber_registers(void);
584 void emit_block_mov(ir_assignment
*ir
, const struct glsl_type
*type
,
585 st_dst_reg
*l
, st_src_reg
*r
,
586 st_src_reg
*cond
, bool cond_swap
);
591 static st_dst_reg address_reg
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
, 0);
592 static st_dst_reg address_reg2
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
, 1);
593 static st_dst_reg sampler_reladdr
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
, 2);
596 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...) PRINTFLIKE(2, 3);
599 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
603 ralloc_vasprintf_append(&prog
->data
->InfoLog
, fmt
, args
);
606 prog
->data
->LinkStatus
= linking_failure
;
610 swizzle_for_size(int size
)
612 static const int size_swizzles
[4] = {
613 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
614 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
615 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
616 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
619 assert((size
>= 1) && (size
<= 4));
620 return size_swizzles
[size
- 1];
624 is_resource_instruction(unsigned opcode
)
627 case TGSI_OPCODE_RESQ
:
628 case TGSI_OPCODE_LOAD
:
629 case TGSI_OPCODE_ATOMUADD
:
630 case TGSI_OPCODE_ATOMXCHG
:
631 case TGSI_OPCODE_ATOMCAS
:
632 case TGSI_OPCODE_ATOMAND
:
633 case TGSI_OPCODE_ATOMOR
:
634 case TGSI_OPCODE_ATOMXOR
:
635 case TGSI_OPCODE_ATOMUMIN
:
636 case TGSI_OPCODE_ATOMUMAX
:
637 case TGSI_OPCODE_ATOMIMIN
:
638 case TGSI_OPCODE_ATOMIMAX
:
646 num_inst_dst_regs(const glsl_to_tgsi_instruction
*op
)
648 return op
->info
->num_dst
;
652 num_inst_src_regs(const glsl_to_tgsi_instruction
*op
)
654 return op
->info
->is_tex
|| is_resource_instruction(op
->op
) ?
655 op
->info
->num_src
- 1 : op
->info
->num_src
;
658 glsl_to_tgsi_instruction
*
659 glsl_to_tgsi_visitor::emit_asm(ir_instruction
*ir
, unsigned op
,
660 st_dst_reg dst
, st_dst_reg dst1
,
661 st_src_reg src0
, st_src_reg src1
,
662 st_src_reg src2
, st_src_reg src3
)
664 glsl_to_tgsi_instruction
*inst
= new(mem_ctx
) glsl_to_tgsi_instruction();
665 int num_reladdr
= 0, i
, j
;
666 bool dst_is_64bit
[2];
668 op
= get_opcode(op
, dst
, src0
, src1
);
670 /* If we have to do relative addressing, we want to load the ARL
671 * reg directly for one of the regs, and preload the other reladdr
672 * sources into temps.
674 num_reladdr
+= dst
.reladdr
!= NULL
|| dst
.reladdr2
;
675 num_reladdr
+= dst1
.reladdr
!= NULL
|| dst1
.reladdr2
;
676 num_reladdr
+= src0
.reladdr
!= NULL
|| src0
.reladdr2
!= NULL
;
677 num_reladdr
+= src1
.reladdr
!= NULL
|| src1
.reladdr2
!= NULL
;
678 num_reladdr
+= src2
.reladdr
!= NULL
|| src2
.reladdr2
!= NULL
;
679 num_reladdr
+= src3
.reladdr
!= NULL
|| src3
.reladdr2
!= NULL
;
681 reladdr_to_temp(ir
, &src3
, &num_reladdr
);
682 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
683 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
684 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
686 if (dst
.reladdr
|| dst
.reladdr2
) {
688 emit_arl(ir
, address_reg
, *dst
.reladdr
);
690 emit_arl(ir
, address_reg2
, *dst
.reladdr2
);
694 emit_arl(ir
, address_reg
, *dst1
.reladdr
);
697 assert(num_reladdr
== 0);
699 /* inst->op has only 8 bits. */
700 STATIC_ASSERT(TGSI_OPCODE_LAST
<= 255);
703 inst
->precise
= this->precise
;
704 inst
->info
= tgsi_get_opcode_info(op
);
711 inst
->is_64bit_expanded
= false;
714 inst
->tex_offsets
= NULL
;
715 inst
->tex_offset_num_offset
= 0;
717 inst
->tex_shadow
= 0;
718 /* default to float, for paths where this is not initialized
719 * (since 0==UINT which is likely wrong):
721 inst
->tex_type
= GLSL_TYPE_FLOAT
;
723 /* Update indirect addressing status used by TGSI */
724 if (dst
.reladdr
|| dst
.reladdr2
) {
726 case PROGRAM_STATE_VAR
:
727 case PROGRAM_CONSTANT
:
728 case PROGRAM_UNIFORM
:
729 this->indirect_addr_consts
= true;
731 case PROGRAM_IMMEDIATE
:
732 assert(!"immediates should not have indirect addressing");
739 for (i
= 0; i
< 4; i
++) {
740 if(inst
->src
[i
].reladdr
) {
741 switch(inst
->src
[i
].file
) {
742 case PROGRAM_STATE_VAR
:
743 case PROGRAM_CONSTANT
:
744 case PROGRAM_UNIFORM
:
745 this->indirect_addr_consts
= true;
747 case PROGRAM_IMMEDIATE
:
748 assert(!"immediates should not have indirect addressing");
758 * This section contains the double processing.
759 * GLSL just represents doubles as single channel values,
760 * however most HW and TGSI represent doubles as pairs of register channels.
762 * so we have to fixup destination writemask/index and src swizzle/indexes.
763 * dest writemasks need to translate from single channel write mask
764 * to a dual-channel writemask, but also need to modify the index,
765 * if we are touching the Z,W fields in the pre-translated writemask.
767 * src channels have similiar index modifications along with swizzle
768 * changes to we pick the XY, ZW pairs from the correct index.
770 * GLSL [0].x -> TGSI [0].xy
771 * GLSL [0].y -> TGSI [0].zw
772 * GLSL [0].z -> TGSI [1].xy
773 * GLSL [0].w -> TGSI [1].zw
775 for (j
= 0; j
< 2; j
++) {
776 dst_is_64bit
[j
] = glsl_base_type_is_64bit(inst
->dst
[j
].type
);
777 if (!dst_is_64bit
[j
] && inst
->dst
[j
].file
== PROGRAM_OUTPUT
&& inst
->dst
[j
].type
== GLSL_TYPE_ARRAY
) {
778 enum glsl_base_type type
= find_array_type(this->outputs
, this->num_outputs
, inst
->dst
[j
].array_id
);
779 if (glsl_base_type_is_64bit(type
))
780 dst_is_64bit
[j
] = true;
784 if (dst_is_64bit
[0] || dst_is_64bit
[1] ||
785 glsl_base_type_is_64bit(inst
->src
[0].type
)) {
786 glsl_to_tgsi_instruction
*dinst
= NULL
;
787 int initial_src_swz
[4], initial_src_idx
[4];
788 int initial_dst_idx
[2], initial_dst_writemask
[2];
789 /* select the writemask for dst0 or dst1 */
790 unsigned writemask
= inst
->dst
[1].file
== PROGRAM_UNDEFINED
? inst
->dst
[0].writemask
: inst
->dst
[1].writemask
;
792 /* copy out the writemask, index and swizzles for all src/dsts. */
793 for (j
= 0; j
< 2; j
++) {
794 initial_dst_writemask
[j
] = inst
->dst
[j
].writemask
;
795 initial_dst_idx
[j
] = inst
->dst
[j
].index
;
798 for (j
= 0; j
< 4; j
++) {
799 initial_src_swz
[j
] = inst
->src
[j
].swizzle
;
800 initial_src_idx
[j
] = inst
->src
[j
].index
;
804 * scan all the components in the dst writemask
805 * generate an instruction for each of them if required.
810 int i
= u_bit_scan(&writemask
);
812 /* before emitting the instruction, see if we have to adjust load / store
814 if (i
> 1 && (inst
->op
== TGSI_OPCODE_LOAD
|| inst
->op
== TGSI_OPCODE_STORE
) &&
815 addr
.file
== PROGRAM_UNDEFINED
) {
816 /* We have to advance the buffer address by 16 */
817 addr
= get_temp(glsl_type::uint_type
);
818 emit_asm(ir
, TGSI_OPCODE_UADD
, st_dst_reg(addr
),
819 inst
->src
[0], st_src_reg_for_int(16));
822 /* first time use previous instruction */
826 /* create a new instructions for subsequent attempts */
827 dinst
= new(mem_ctx
) glsl_to_tgsi_instruction();
832 this->instructions
.push_tail(dinst
);
833 dinst
->is_64bit_expanded
= true;
835 /* modify the destination if we are splitting */
836 for (j
= 0; j
< 2; j
++) {
837 if (dst_is_64bit
[j
]) {
838 dinst
->dst
[j
].writemask
= (i
& 1) ? WRITEMASK_ZW
: WRITEMASK_XY
;
839 dinst
->dst
[j
].index
= initial_dst_idx
[j
];
841 if (dinst
->op
== TGSI_OPCODE_LOAD
|| dinst
->op
== TGSI_OPCODE_STORE
)
842 dinst
->src
[0] = addr
;
843 if (dinst
->op
!= TGSI_OPCODE_STORE
)
844 dinst
->dst
[j
].index
++;
847 /* if we aren't writing to a double, just get the bit of the initial writemask
849 dinst
->dst
[j
].writemask
= initial_dst_writemask
[j
] & (1 << i
);
853 /* modify the src registers */
854 for (j
= 0; j
< 4; j
++) {
855 int swz
= GET_SWZ(initial_src_swz
[j
], i
);
857 if (glsl_base_type_is_64bit(dinst
->src
[j
].type
)) {
858 dinst
->src
[j
].index
= initial_src_idx
[j
];
860 dinst
->src
[j
].double_reg2
= true;
861 dinst
->src
[j
].index
++;
865 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_W
, SWIZZLE_Z
, SWIZZLE_W
);
867 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
870 /* some opcodes are special case in what they use as sources
871 - [FUI]2D/[UI]2I64 is a float/[u]int src0, DLDEXP is integer src1 */
872 if (op
== TGSI_OPCODE_F2D
|| op
== TGSI_OPCODE_U2D
|| op
== TGSI_OPCODE_I2D
||
873 op
== TGSI_OPCODE_I2I64
|| op
== TGSI_OPCODE_U2I64
||
874 op
== TGSI_OPCODE_DLDEXP
||
875 (op
== TGSI_OPCODE_UCMP
&& dst_is_64bit
[0])) {
876 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(swz
, swz
, swz
, swz
);
883 this->instructions
.push_tail(inst
);
890 glsl_to_tgsi_instruction
*
891 glsl_to_tgsi_visitor::emit_asm(ir_instruction
*ir
, unsigned op
,
893 st_src_reg src0
, st_src_reg src1
,
894 st_src_reg src2
, st_src_reg src3
)
896 return emit_asm(ir
, op
, dst
, undef_dst
, src0
, src1
, src2
, src3
);
900 * Determines whether to use an integer, unsigned integer, or float opcode
901 * based on the operands and input opcode, then emits the result.
904 glsl_to_tgsi_visitor::get_opcode(unsigned op
,
906 st_src_reg src0
, st_src_reg src1
)
908 enum glsl_base_type type
= GLSL_TYPE_FLOAT
;
910 if (op
== TGSI_OPCODE_MOV
)
913 assert(src0
.type
!= GLSL_TYPE_ARRAY
);
914 assert(src0
.type
!= GLSL_TYPE_STRUCT
);
915 assert(src1
.type
!= GLSL_TYPE_ARRAY
);
916 assert(src1
.type
!= GLSL_TYPE_STRUCT
);
918 if (is_resource_instruction(op
))
920 else if (src0
.type
== GLSL_TYPE_INT64
|| src1
.type
== GLSL_TYPE_INT64
)
921 type
= GLSL_TYPE_INT64
;
922 else if (src0
.type
== GLSL_TYPE_UINT64
|| src1
.type
== GLSL_TYPE_UINT64
)
923 type
= GLSL_TYPE_UINT64
;
924 else if (src0
.type
== GLSL_TYPE_DOUBLE
|| src1
.type
== GLSL_TYPE_DOUBLE
)
925 type
= GLSL_TYPE_DOUBLE
;
926 else if (src0
.type
== GLSL_TYPE_FLOAT
|| src1
.type
== GLSL_TYPE_FLOAT
)
927 type
= GLSL_TYPE_FLOAT
;
928 else if (native_integers
)
929 type
= src0
.type
== GLSL_TYPE_BOOL
? GLSL_TYPE_INT
: src0
.type
;
931 #define case7(c, f, i, u, d, i64, ui64) \
932 case TGSI_OPCODE_##c: \
933 if (type == GLSL_TYPE_UINT64) \
934 op = TGSI_OPCODE_##ui64; \
935 else if (type == GLSL_TYPE_INT64) \
936 op = TGSI_OPCODE_##i64; \
937 else if (type == GLSL_TYPE_DOUBLE) \
938 op = TGSI_OPCODE_##d; \
939 else if (type == GLSL_TYPE_INT) \
940 op = TGSI_OPCODE_##i; \
941 else if (type == GLSL_TYPE_UINT) \
942 op = TGSI_OPCODE_##u; \
944 op = TGSI_OPCODE_##f; \
946 #define case5(c, f, i, u, d) \
947 case TGSI_OPCODE_##c: \
948 if (type == GLSL_TYPE_DOUBLE) \
949 op = TGSI_OPCODE_##d; \
950 else if (type == GLSL_TYPE_INT) \
951 op = TGSI_OPCODE_##i; \
952 else if (type == GLSL_TYPE_UINT) \
953 op = TGSI_OPCODE_##u; \
955 op = TGSI_OPCODE_##f; \
958 #define case4(c, f, i, u) \
959 case TGSI_OPCODE_##c: \
960 if (type == GLSL_TYPE_INT) \
961 op = TGSI_OPCODE_##i; \
962 else if (type == GLSL_TYPE_UINT) \
963 op = TGSI_OPCODE_##u; \
965 op = TGSI_OPCODE_##f; \
968 #define case3(f, i, u) case4(f, f, i, u)
969 #define case6d(f, i, u, d, i64, u64) case7(f, f, i, u, d, i64, u64)
970 #define case3fid(f, i, d) case5(f, f, i, i, d)
971 #define case3fid64(f, i, d, i64) case7(f, f, i, i, d, i64, i64)
972 #define case2fi(f, i) case4(f, f, i, i)
973 #define case2iu(i, u) case4(i, LAST, i, u)
975 #define case2iu64(i, i64) case7(i, LAST, i, i, LAST, i64, i64)
976 #define case4iu64(i, u, i64, u64) case7(i, LAST, i, u, LAST, i64, u64)
978 #define casecomp(c, f, i, u, d, i64, ui64) \
979 case TGSI_OPCODE_##c: \
980 if (type == GLSL_TYPE_INT64) \
981 op = TGSI_OPCODE_##i64; \
982 else if (type == GLSL_TYPE_UINT64) \
983 op = TGSI_OPCODE_##ui64; \
984 else if (type == GLSL_TYPE_DOUBLE) \
985 op = TGSI_OPCODE_##d; \
986 else if (type == GLSL_TYPE_INT || type == GLSL_TYPE_SUBROUTINE) \
987 op = TGSI_OPCODE_##i; \
988 else if (type == GLSL_TYPE_UINT) \
989 op = TGSI_OPCODE_##u; \
990 else if (native_integers) \
991 op = TGSI_OPCODE_##f; \
993 op = TGSI_OPCODE_##c; \
997 case3fid64(ADD
, UADD
, DADD
, U64ADD
);
998 case3fid64(MUL
, UMUL
, DMUL
, U64MUL
);
999 case3fid(MAD
, UMAD
, DMAD
);
1000 case3fid(FMA
, UMAD
, DFMA
);
1001 case6d(DIV
, IDIV
, UDIV
, DDIV
, I64DIV
, U64DIV
);
1002 case6d(MAX
, IMAX
, UMAX
, DMAX
, I64MAX
, U64MAX
);
1003 case6d(MIN
, IMIN
, UMIN
, DMIN
, I64MIN
, U64MIN
);
1004 case4iu64(MOD
, UMOD
, I64MOD
, U64MOD
);
1006 casecomp(SEQ
, FSEQ
, USEQ
, USEQ
, DSEQ
, U64SEQ
, U64SEQ
);
1007 casecomp(SNE
, FSNE
, USNE
, USNE
, DSNE
, U64SNE
, U64SNE
);
1008 casecomp(SGE
, FSGE
, ISGE
, USGE
, DSGE
, I64SGE
, U64SGE
);
1009 casecomp(SLT
, FSLT
, ISLT
, USLT
, DSLT
, I64SLT
, U64SLT
);
1011 case2iu64(SHL
, U64SHL
);
1012 case4iu64(ISHR
, USHR
, I64SHR
, U64SHR
);
1014 case3fid64(SSG
, ISSG
, DSSG
, I64SSG
);
1016 case2iu(IBFE
, UBFE
);
1017 case2iu(IMSB
, UMSB
);
1018 case2iu(IMUL_HI
, UMUL_HI
);
1020 case3fid(SQRT
, SQRT
, DSQRT
);
1022 case3fid(RCP
, RCP
, DRCP
);
1023 case3fid(RSQ
, RSQ
, DRSQ
);
1025 case3fid(FRC
, FRC
, DFRAC
);
1026 case3fid(TRUNC
, TRUNC
, DTRUNC
);
1027 case3fid(CEIL
, CEIL
, DCEIL
);
1028 case3fid(FLR
, FLR
, DFLR
);
1029 case3fid(ROUND
, ROUND
, DROUND
);
1031 case2iu(ATOMIMAX
, ATOMUMAX
);
1032 case2iu(ATOMIMIN
, ATOMUMIN
);
1037 assert(op
!= TGSI_OPCODE_LAST
);
1041 glsl_to_tgsi_instruction
*
1042 glsl_to_tgsi_visitor::emit_dp(ir_instruction
*ir
,
1043 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
,
1046 static const unsigned dot_opcodes
[] = {
1047 TGSI_OPCODE_DP2
, TGSI_OPCODE_DP3
, TGSI_OPCODE_DP4
1050 return emit_asm(ir
, dot_opcodes
[elements
- 2], dst
, src0
, src1
);
1054 * Emits TGSI scalar opcodes to produce unique answers across channels.
1056 * Some TGSI opcodes are scalar-only, like ARB_fp/vp. The src X
1057 * channel determines the result across all channels. So to do a vec4
1058 * of this operation, we want to emit a scalar per source channel used
1059 * to produce dest channels.
1062 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
1064 st_src_reg orig_src0
, st_src_reg orig_src1
)
1067 int done_mask
= ~dst
.writemask
;
1069 /* TGSI RCP is a scalar operation splatting results to all channels,
1070 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
1073 for (i
= 0; i
< 4; i
++) {
1074 GLuint this_mask
= (1 << i
);
1075 st_src_reg src0
= orig_src0
;
1076 st_src_reg src1
= orig_src1
;
1078 if (done_mask
& this_mask
)
1081 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
1082 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
1083 for (j
= i
+ 1; j
< 4; j
++) {
1084 /* If there is another enabled component in the destination that is
1085 * derived from the same inputs, generate its value on this pass as
1088 if (!(done_mask
& (1 << j
)) &&
1089 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
1090 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
1091 this_mask
|= (1 << j
);
1094 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
1095 src0_swiz
, src0_swiz
);
1096 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
1097 src1_swiz
, src1_swiz
);
1099 dst
.writemask
= this_mask
;
1100 emit_asm(ir
, op
, dst
, src0
, src1
);
1101 done_mask
|= this_mask
;
1106 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
1107 st_dst_reg dst
, st_src_reg src0
)
1109 st_src_reg undef
= undef_src
;
1111 undef
.swizzle
= SWIZZLE_XXXX
;
1113 emit_scalar(ir
, op
, dst
, src0
, undef
);
1117 glsl_to_tgsi_visitor::emit_arl(ir_instruction
*ir
,
1118 st_dst_reg dst
, st_src_reg src0
)
1120 int op
= TGSI_OPCODE_ARL
;
1122 if (src0
.type
== GLSL_TYPE_INT
|| src0
.type
== GLSL_TYPE_UINT
)
1123 op
= TGSI_OPCODE_UARL
;
1125 assert(dst
.file
== PROGRAM_ADDRESS
);
1126 if (dst
.index
>= this->num_address_regs
)
1127 this->num_address_regs
= dst
.index
+ 1;
1129 emit_asm(NULL
, op
, dst
, src0
);
1133 glsl_to_tgsi_visitor::add_constant(gl_register_file file
,
1134 gl_constant_value values
[8], int size
, int datatype
,
1135 uint16_t *swizzle_out
)
1137 if (file
== PROGRAM_CONSTANT
) {
1138 GLuint swizzle
= swizzle_out
? *swizzle_out
: 0;
1139 int result
= _mesa_add_typed_unnamed_constant(this->prog
->Parameters
, values
,
1140 size
, datatype
, &swizzle
);
1142 *swizzle_out
= swizzle
;
1146 assert(file
== PROGRAM_IMMEDIATE
);
1149 immediate_storage
*entry
;
1150 int size32
= size
* ((datatype
== GL_DOUBLE
||
1151 datatype
== GL_INT64_ARB
||
1152 datatype
== GL_UNSIGNED_INT64_ARB
)? 2 : 1);
1155 /* Search immediate storage to see if we already have an identical
1156 * immediate that we can use instead of adding a duplicate entry.
1158 foreach_in_list(immediate_storage
, entry
, &this->immediates
) {
1159 immediate_storage
*tmp
= entry
;
1161 for (i
= 0; i
* 4 < size32
; i
++) {
1162 int slot_size
= MIN2(size32
- (i
* 4), 4);
1163 if (tmp
->type
!= datatype
|| tmp
->size32
!= slot_size
)
1165 if (memcmp(tmp
->values
, &values
[i
* 4],
1166 slot_size
* sizeof(gl_constant_value
)))
1169 /* Everything matches, keep going until the full size is matched */
1170 tmp
= (immediate_storage
*)tmp
->next
;
1173 /* The full value matched */
1174 if (i
* 4 >= size32
)
1180 for (i
= 0; i
* 4 < size32
; i
++) {
1181 int slot_size
= MIN2(size32
- (i
* 4), 4);
1182 /* Add this immediate to the list. */
1183 entry
= new(mem_ctx
) immediate_storage(&values
[i
* 4], slot_size
, datatype
);
1184 this->immediates
.push_tail(entry
);
1185 this->num_immediates
++;
1191 glsl_to_tgsi_visitor::st_src_reg_for_float(float val
)
1193 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_FLOAT
);
1194 union gl_constant_value uval
;
1197 src
.index
= add_constant(src
.file
, &uval
, 1, GL_FLOAT
, &src
.swizzle
);
1203 glsl_to_tgsi_visitor::st_src_reg_for_double(double val
)
1205 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_DOUBLE
);
1206 union gl_constant_value uval
[2];
1208 memcpy(uval
, &val
, sizeof(uval
));
1209 src
.index
= add_constant(src
.file
, uval
, 1, GL_DOUBLE
, &src
.swizzle
);
1210 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
1215 glsl_to_tgsi_visitor::st_src_reg_for_int(int val
)
1217 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_INT
);
1218 union gl_constant_value uval
;
1220 assert(native_integers
);
1223 src
.index
= add_constant(src
.file
, &uval
, 1, GL_INT
, &src
.swizzle
);
1229 glsl_to_tgsi_visitor::st_src_reg_for_type(enum glsl_base_type type
, int val
)
1231 if (native_integers
)
1232 return type
== GLSL_TYPE_FLOAT
? st_src_reg_for_float(val
) :
1233 st_src_reg_for_int(val
);
1235 return st_src_reg_for_float(val
);
1239 attrib_type_size(const struct glsl_type
*type
, bool is_vs_input
)
1241 return st_glsl_attrib_type_size(type
, is_vs_input
);
1245 type_size(const struct glsl_type
*type
)
1247 return st_glsl_type_size(type
);
1251 * If the given GLSL type is an array or matrix or a structure containing
1252 * an array/matrix member, return true. Else return false.
1254 * This is used to determine which kind of temp storage (PROGRAM_TEMPORARY
1255 * or PROGRAM_ARRAY) should be used for variables of this type. Anytime
1256 * we have an array that might be indexed with a variable, we need to use
1257 * the later storage type.
1260 type_has_array_or_matrix(const glsl_type
*type
)
1262 if (type
->is_array() || type
->is_matrix())
1265 if (type
->is_record()) {
1266 for (unsigned i
= 0; i
< type
->length
; i
++) {
1267 if (type_has_array_or_matrix(type
->fields
.structure
[i
].type
)) {
1278 * In the initial pass of codegen, we assign temporary numbers to
1279 * intermediate results. (not SSA -- variable assignments will reuse
1283 glsl_to_tgsi_visitor::get_temp(const glsl_type
*type
)
1287 src
.type
= native_integers
? type
->base_type
: GLSL_TYPE_FLOAT
;
1292 if (!options
->EmitNoIndirectTemp
&& type_has_array_or_matrix(type
)) {
1293 if (next_array
>= max_num_arrays
) {
1294 max_num_arrays
+= 32;
1295 array_sizes
= (unsigned*)
1296 realloc(array_sizes
, sizeof(array_sizes
[0]) * max_num_arrays
);
1299 src
.file
= PROGRAM_ARRAY
;
1301 src
.array_id
= next_array
+ 1;
1302 array_sizes
[next_array
] = type_size(type
);
1306 src
.file
= PROGRAM_TEMPORARY
;
1307 src
.index
= next_temp
;
1308 next_temp
+= type_size(type
);
1311 if (type
->is_array() || type
->is_record()) {
1312 src
.swizzle
= SWIZZLE_NOOP
;
1314 src
.swizzle
= swizzle_for_size(type
->vector_elements
);
1321 glsl_to_tgsi_visitor::find_variable_storage(ir_variable
*var
)
1323 struct hash_entry
*entry
;
1325 entry
= _mesa_hash_table_search(this->variables
, var
);
1329 return (variable_storage
*)entry
->data
;
1333 glsl_to_tgsi_visitor::visit(ir_variable
*ir
)
1335 if (strcmp(ir
->name
, "gl_FragCoord") == 0) {
1336 this->prog
->OriginUpperLeft
= ir
->data
.origin_upper_left
;
1337 this->prog
->PixelCenterInteger
= ir
->data
.pixel_center_integer
;
1340 if (ir
->data
.mode
== ir_var_uniform
&& strncmp(ir
->name
, "gl_", 3) == 0) {
1342 const ir_state_slot
*const slots
= ir
->get_state_slots();
1343 assert(slots
!= NULL
);
1345 /* Check if this statevar's setup in the STATE file exactly
1346 * matches how we'll want to reference it as a
1347 * struct/array/whatever. If not, then we need to move it into
1348 * temporary storage and hope that it'll get copy-propagated
1351 for (i
= 0; i
< ir
->get_num_state_slots(); i
++) {
1352 if (slots
[i
].swizzle
!= SWIZZLE_XYZW
) {
1357 variable_storage
*storage
;
1359 if (i
== ir
->get_num_state_slots()) {
1360 /* We'll set the index later. */
1361 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_STATE_VAR
, -1);
1363 _mesa_hash_table_insert(this->variables
, ir
, storage
);
1367 /* The variable_storage constructor allocates slots based on the size
1368 * of the type. However, this had better match the number of state
1369 * elements that we're going to copy into the new temporary.
1371 assert((int) ir
->get_num_state_slots() == type_size(ir
->type
));
1373 dst
= st_dst_reg(get_temp(ir
->type
));
1375 storage
= new(mem_ctx
) variable_storage(ir
, dst
.file
, dst
.index
,
1378 _mesa_hash_table_insert(this->variables
, ir
, storage
);
1382 for (unsigned int i
= 0; i
< ir
->get_num_state_slots(); i
++) {
1383 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
1384 (gl_state_index
*)slots
[i
].tokens
);
1386 if (storage
->file
== PROGRAM_STATE_VAR
) {
1387 if (storage
->index
== -1) {
1388 storage
->index
= index
;
1390 assert(index
== storage
->index
+ (int)i
);
1393 /* We use GLSL_TYPE_FLOAT here regardless of the actual type of
1394 * the data being moved since MOV does not care about the type of
1395 * data it is moving, and we don't want to declare registers with
1396 * array or struct types.
1398 st_src_reg
src(PROGRAM_STATE_VAR
, index
, GLSL_TYPE_FLOAT
);
1399 src
.swizzle
= slots
[i
].swizzle
;
1400 emit_asm(ir
, TGSI_OPCODE_MOV
, dst
, src
);
1401 /* even a float takes up a whole vec4 reg in a struct/array. */
1406 if (storage
->file
== PROGRAM_TEMPORARY
&&
1407 dst
.index
!= storage
->index
+ (int) ir
->get_num_state_slots()) {
1408 fail_link(this->shader_program
,
1409 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
1410 ir
->name
, dst
.index
- storage
->index
,
1411 type_size(ir
->type
));
1417 glsl_to_tgsi_visitor::visit(ir_loop
*ir
)
1419 emit_asm(NULL
, TGSI_OPCODE_BGNLOOP
);
1421 visit_exec_list(&ir
->body_instructions
, this);
1423 emit_asm(NULL
, TGSI_OPCODE_ENDLOOP
);
1427 glsl_to_tgsi_visitor::visit(ir_loop_jump
*ir
)
1430 case ir_loop_jump::jump_break
:
1431 emit_asm(NULL
, TGSI_OPCODE_BRK
);
1433 case ir_loop_jump::jump_continue
:
1434 emit_asm(NULL
, TGSI_OPCODE_CONT
);
1441 glsl_to_tgsi_visitor::visit(ir_function_signature
*ir
)
1448 glsl_to_tgsi_visitor::visit(ir_function
*ir
)
1450 /* Ignore function bodies other than main() -- we shouldn't see calls to
1451 * them since they should all be inlined before we get to glsl_to_tgsi.
1453 if (strcmp(ir
->name
, "main") == 0) {
1454 const ir_function_signature
*sig
;
1457 sig
= ir
->matching_signature(NULL
, &empty
, false);
1461 foreach_in_list(ir_instruction
, ir
, &sig
->body
) {
1468 glsl_to_tgsi_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
1470 int nonmul_operand
= 1 - mul_operand
;
1472 st_dst_reg result_dst
;
1474 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
1475 if (!expr
|| expr
->operation
!= ir_binop_mul
)
1478 expr
->operands
[0]->accept(this);
1480 expr
->operands
[1]->accept(this);
1482 ir
->operands
[nonmul_operand
]->accept(this);
1485 this->result
= get_temp(ir
->type
);
1486 result_dst
= st_dst_reg(this->result
);
1487 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1488 emit_asm(ir
, TGSI_OPCODE_MAD
, result_dst
, a
, b
, c
);
1494 * Emit MAD(a, -b, a) instead of AND(a, NOT(b))
1496 * The logic values are 1.0 for true and 0.0 for false. Logical-and is
1497 * implemented using multiplication, and logical-or is implemented using
1498 * addition. Logical-not can be implemented as (true - x), or (1.0 - x).
1499 * As result, the logical expression (a & !b) can be rewritten as:
1503 * - (a * 1) - (a * b)
1507 * This final expression can be implemented as a single MAD(a, -b, a)
1511 glsl_to_tgsi_visitor::try_emit_mad_for_and_not(ir_expression
*ir
, int try_operand
)
1513 const int other_operand
= 1 - try_operand
;
1516 ir_expression
*expr
= ir
->operands
[try_operand
]->as_expression();
1517 if (!expr
|| expr
->operation
!= ir_unop_logic_not
)
1520 ir
->operands
[other_operand
]->accept(this);
1522 expr
->operands
[0]->accept(this);
1525 b
.negate
= ~b
.negate
;
1527 this->result
= get_temp(ir
->type
);
1528 emit_asm(ir
, TGSI_OPCODE_MAD
, st_dst_reg(this->result
), a
, b
, a
);
1534 glsl_to_tgsi_visitor::reladdr_to_temp(ir_instruction
*ir
,
1535 st_src_reg
*reg
, int *num_reladdr
)
1537 if (!reg
->reladdr
&& !reg
->reladdr2
)
1540 if (reg
->reladdr
) emit_arl(ir
, address_reg
, *reg
->reladdr
);
1541 if (reg
->reladdr2
) emit_arl(ir
, address_reg2
, *reg
->reladdr2
);
1543 if (*num_reladdr
!= 1) {
1544 st_src_reg temp
= get_temp(reg
->type
== GLSL_TYPE_DOUBLE
? glsl_type::dvec4_type
: glsl_type::vec4_type
);
1546 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), *reg
);
1554 glsl_to_tgsi_visitor::visit(ir_expression
*ir
)
1556 st_src_reg op
[ARRAY_SIZE(ir
->operands
)];
1558 /* Quick peephole: Emit MAD(a, b, c) instead of ADD(MUL(a, b), c)
1560 if (ir
->operation
== ir_binop_add
) {
1561 if (try_emit_mad(ir
, 1))
1563 if (try_emit_mad(ir
, 0))
1567 /* Quick peephole: Emit OPCODE_MAD(-a, -b, a) instead of AND(a, NOT(b))
1569 if (!native_integers
&& ir
->operation
== ir_binop_logic_and
) {
1570 if (try_emit_mad_for_and_not(ir
, 1))
1572 if (try_emit_mad_for_and_not(ir
, 0))
1576 if (ir
->operation
== ir_quadop_vector
)
1577 assert(!"ir_quadop_vector should have been lowered");
1579 for (unsigned int operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1580 this->result
.file
= PROGRAM_UNDEFINED
;
1581 ir
->operands
[operand
]->accept(this);
1582 if (this->result
.file
== PROGRAM_UNDEFINED
) {
1583 printf("Failed to get tree for expression operand:\n");
1584 ir
->operands
[operand
]->print();
1588 op
[operand
] = this->result
;
1590 /* Matrix expression operands should have been broken down to vector
1591 * operations already.
1593 assert(!ir
->operands
[operand
]->type
->is_matrix());
1596 visit_expression(ir
, op
);
1599 /* The non-recursive part of the expression visitor lives in a separate
1600 * function and should be prevented from being inlined, to avoid a stack
1601 * explosion when deeply nested expressions are visited.
1604 glsl_to_tgsi_visitor::visit_expression(ir_expression
* ir
, st_src_reg
*op
)
1606 st_src_reg result_src
;
1607 st_dst_reg result_dst
;
1609 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1610 if (ir
->operands
[1]) {
1611 vector_elements
= MAX2(vector_elements
,
1612 ir
->operands
[1]->type
->vector_elements
);
1615 this->result
.file
= PROGRAM_UNDEFINED
;
1617 /* Storage for our result. Ideally for an assignment we'd be using
1618 * the actual storage for the result here, instead.
1620 result_src
= get_temp(ir
->type
);
1621 /* convenience for the emit functions below. */
1622 result_dst
= st_dst_reg(result_src
);
1623 /* Limit writes to the channels that will be used by result_src later.
1624 * This does limit this temp's use as a temporary for multi-instruction
1627 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1629 switch (ir
->operation
) {
1630 case ir_unop_logic_not
:
1631 if (result_dst
.type
!= GLSL_TYPE_FLOAT
)
1632 emit_asm(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1634 /* Previously 'SEQ dst, src, 0.0' was used for this. However, many
1635 * older GPUs implement SEQ using multiple instructions (i915 uses two
1636 * SGE instructions and a MUL instruction). Since our logic values are
1637 * 0.0 and 1.0, 1-x also implements !x.
1639 op
[0].negate
= ~op
[0].negate
;
1640 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], st_src_reg_for_float(1.0));
1644 if (result_dst
.type
== GLSL_TYPE_INT64
|| result_dst
.type
== GLSL_TYPE_UINT64
)
1645 emit_asm(ir
, TGSI_OPCODE_I64NEG
, result_dst
, op
[0]);
1646 else if (result_dst
.type
== GLSL_TYPE_INT
|| result_dst
.type
== GLSL_TYPE_UINT
)
1647 emit_asm(ir
, TGSI_OPCODE_INEG
, result_dst
, op
[0]);
1648 else if (result_dst
.type
== GLSL_TYPE_DOUBLE
)
1649 emit_asm(ir
, TGSI_OPCODE_DNEG
, result_dst
, op
[0]);
1651 op
[0].negate
= ~op
[0].negate
;
1655 case ir_unop_subroutine_to_int
:
1656 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
1659 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1660 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0].get_abs());
1661 else if (result_dst
.type
== GLSL_TYPE_DOUBLE
)
1662 emit_asm(ir
, TGSI_OPCODE_DABS
, result_dst
, op
[0]);
1663 else if (result_dst
.type
== GLSL_TYPE_INT64
|| result_dst
.type
== GLSL_TYPE_UINT64
)
1664 emit_asm(ir
, TGSI_OPCODE_I64ABS
, result_dst
, op
[0]);
1666 emit_asm(ir
, TGSI_OPCODE_IABS
, result_dst
, op
[0]);
1669 emit_asm(ir
, TGSI_OPCODE_SSG
, result_dst
, op
[0]);
1672 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, op
[0]);
1676 emit_scalar(ir
, TGSI_OPCODE_EX2
, result_dst
, op
[0]);
1679 assert(!"not reached: should be handled by exp_to_exp2");
1682 assert(!"not reached: should be handled by log_to_log2");
1685 emit_scalar(ir
, TGSI_OPCODE_LG2
, result_dst
, op
[0]);
1688 emit_scalar(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1691 emit_scalar(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1693 case ir_unop_saturate
: {
1694 glsl_to_tgsi_instruction
*inst
;
1695 inst
= emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
1696 inst
->saturate
= true;
1701 case ir_unop_dFdx_coarse
:
1702 emit_asm(ir
, TGSI_OPCODE_DDX
, result_dst
, op
[0]);
1704 case ir_unop_dFdx_fine
:
1705 emit_asm(ir
, TGSI_OPCODE_DDX_FINE
, result_dst
, op
[0]);
1708 case ir_unop_dFdy_coarse
:
1709 case ir_unop_dFdy_fine
:
1711 /* The X component contains 1 or -1 depending on whether the framebuffer
1712 * is a FBO or the window system buffer, respectively.
1713 * It is then multiplied with the source operand of DDY.
1715 static const gl_state_index transform_y_state
[STATE_LENGTH
]
1716 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
};
1718 unsigned transform_y_index
=
1719 _mesa_add_state_reference(this->prog
->Parameters
,
1722 st_src_reg transform_y
= st_src_reg(PROGRAM_STATE_VAR
,
1724 glsl_type::vec4_type
);
1725 transform_y
.swizzle
= SWIZZLE_XXXX
;
1727 st_src_reg temp
= get_temp(glsl_type::vec4_type
);
1729 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(temp
), transform_y
, op
[0]);
1730 emit_asm(ir
, ir
->operation
== ir_unop_dFdy_fine
?
1731 TGSI_OPCODE_DDY_FINE
: TGSI_OPCODE_DDY
, result_dst
, temp
);
1735 case ir_unop_frexp_sig
:
1736 emit_asm(ir
, TGSI_OPCODE_DFRACEXP
, result_dst
, undef_dst
, op
[0]);
1739 case ir_unop_frexp_exp
:
1740 emit_asm(ir
, TGSI_OPCODE_DFRACEXP
, undef_dst
, result_dst
, op
[0]);
1743 case ir_unop_noise
: {
1744 /* At some point, a motivated person could add a better
1745 * implementation of noise. Currently not even the nvidia
1746 * binary drivers do anything more than this. In any case, the
1747 * place to do this is in the GL state tracker, not the poor
1750 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, st_src_reg_for_float(0.5));
1755 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1758 op
[1].negate
= ~op
[1].negate
;
1759 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1763 emit_asm(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1766 emit_asm(ir
, TGSI_OPCODE_DIV
, result_dst
, op
[0], op
[1]);
1769 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1770 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1772 emit_asm(ir
, TGSI_OPCODE_MOD
, result_dst
, op
[0], op
[1]);
1776 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[0], op
[1]);
1778 case ir_binop_greater
:
1779 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[1], op
[0]);
1781 case ir_binop_lequal
:
1782 emit_asm(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[1], op
[0]);
1784 case ir_binop_gequal
:
1785 emit_asm(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[0], op
[1]);
1787 case ir_binop_equal
:
1788 emit_asm(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1790 case ir_binop_nequal
:
1791 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1793 case ir_binop_all_equal
:
1794 /* "==" operator producing a scalar boolean. */
1795 if (ir
->operands
[0]->type
->is_vector() ||
1796 ir
->operands
[1]->type
->is_vector()) {
1797 st_src_reg temp
= get_temp(native_integers
?
1798 glsl_type::uvec4_type
:
1799 glsl_type::vec4_type
);
1801 if (native_integers
) {
1802 st_dst_reg temp_dst
= st_dst_reg(temp
);
1803 st_src_reg temp1
= st_src_reg(temp
), temp2
= st_src_reg(temp
);
1805 if (ir
->operands
[0]->type
->is_boolean() &&
1806 ir
->operands
[1]->as_constant() &&
1807 ir
->operands
[1]->as_constant()->is_one()) {
1808 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), op
[0]);
1810 emit_asm(ir
, TGSI_OPCODE_SEQ
, st_dst_reg(temp
), op
[0], op
[1]);
1813 /* Emit 1-3 AND operations to combine the SEQ results. */
1814 switch (ir
->operands
[0]->type
->vector_elements
) {
1818 temp_dst
.writemask
= WRITEMASK_Y
;
1819 temp1
.swizzle
= SWIZZLE_YYYY
;
1820 temp2
.swizzle
= SWIZZLE_ZZZZ
;
1821 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1824 temp_dst
.writemask
= WRITEMASK_X
;
1825 temp1
.swizzle
= SWIZZLE_XXXX
;
1826 temp2
.swizzle
= SWIZZLE_YYYY
;
1827 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1828 temp_dst
.writemask
= WRITEMASK_Y
;
1829 temp1
.swizzle
= SWIZZLE_ZZZZ
;
1830 temp2
.swizzle
= SWIZZLE_WWWW
;
1831 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1834 temp1
.swizzle
= SWIZZLE_XXXX
;
1835 temp2
.swizzle
= SWIZZLE_YYYY
;
1836 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, temp1
, temp2
);
1838 emit_asm(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1840 /* After the dot-product, the value will be an integer on the
1841 * range [0,4]. Zero becomes 1.0, and positive values become zero.
1843 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1845 /* Negating the result of the dot-product gives values on the range
1846 * [-4, 0]. Zero becomes 1.0, and negative values become zero.
1847 * This is achieved using SGE.
1849 st_src_reg sge_src
= result_src
;
1850 sge_src
.negate
= ~sge_src
.negate
;
1851 emit_asm(ir
, TGSI_OPCODE_SGE
, result_dst
, sge_src
, st_src_reg_for_float(0.0));
1854 emit_asm(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1857 case ir_binop_any_nequal
:
1858 /* "!=" operator producing a scalar boolean. */
1859 if (ir
->operands
[0]->type
->is_vector() ||
1860 ir
->operands
[1]->type
->is_vector()) {
1861 st_src_reg temp
= get_temp(native_integers
?
1862 glsl_type::uvec4_type
:
1863 glsl_type::vec4_type
);
1864 if (ir
->operands
[0]->type
->is_boolean() &&
1865 ir
->operands
[1]->as_constant() &&
1866 ir
->operands
[1]->as_constant()->is_zero()) {
1867 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), op
[0]);
1869 emit_asm(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1872 if (native_integers
) {
1873 st_dst_reg temp_dst
= st_dst_reg(temp
);
1874 st_src_reg temp1
= st_src_reg(temp
), temp2
= st_src_reg(temp
);
1876 /* Emit 1-3 OR operations to combine the SNE results. */
1877 switch (ir
->operands
[0]->type
->vector_elements
) {
1881 temp_dst
.writemask
= WRITEMASK_Y
;
1882 temp1
.swizzle
= SWIZZLE_YYYY
;
1883 temp2
.swizzle
= SWIZZLE_ZZZZ
;
1884 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1887 temp_dst
.writemask
= WRITEMASK_X
;
1888 temp1
.swizzle
= SWIZZLE_XXXX
;
1889 temp2
.swizzle
= SWIZZLE_YYYY
;
1890 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1891 temp_dst
.writemask
= WRITEMASK_Y
;
1892 temp1
.swizzle
= SWIZZLE_ZZZZ
;
1893 temp2
.swizzle
= SWIZZLE_WWWW
;
1894 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1897 temp1
.swizzle
= SWIZZLE_XXXX
;
1898 temp2
.swizzle
= SWIZZLE_YYYY
;
1899 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, temp1
, temp2
);
1901 /* After the dot-product, the value will be an integer on the
1902 * range [0,4]. Zero stays zero, and positive values become 1.0.
1904 glsl_to_tgsi_instruction
*const dp
=
1905 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1906 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1907 /* The clamping to [0,1] can be done for free in the fragment
1908 * shader with a saturate.
1910 dp
->saturate
= true;
1912 /* Negating the result of the dot-product gives values on the range
1913 * [-4, 0]. Zero stays zero, and negative values become 1.0. This
1914 * achieved using SLT.
1916 st_src_reg slt_src
= result_src
;
1917 slt_src
.negate
= ~slt_src
.negate
;
1918 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1922 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1926 case ir_binop_logic_xor
:
1927 if (native_integers
)
1928 emit_asm(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0], op
[1]);
1930 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1933 case ir_binop_logic_or
: {
1934 if (native_integers
) {
1935 /* If integers are used as booleans, we can use an actual "or"
1938 assert(native_integers
);
1939 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0], op
[1]);
1941 /* After the addition, the value will be an integer on the
1942 * range [0,2]. Zero stays zero, and positive values become 1.0.
1944 glsl_to_tgsi_instruction
*add
=
1945 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1946 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1947 /* The clamping to [0,1] can be done for free in the fragment
1948 * shader with a saturate if floats are being used as boolean values.
1950 add
->saturate
= true;
1952 /* Negating the result of the addition gives values on the range
1953 * [-2, 0]. Zero stays zero, and negative values become 1.0. This
1954 * is achieved using SLT.
1956 st_src_reg slt_src
= result_src
;
1957 slt_src
.negate
= ~slt_src
.negate
;
1958 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1964 case ir_binop_logic_and
:
1965 /* If native integers are disabled, the bool args are stored as float 0.0
1966 * or 1.0, so "mul" gives us "and". If they're enabled, just use the
1967 * actual AND opcode.
1969 if (native_integers
)
1970 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], op
[1]);
1972 emit_asm(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1976 assert(ir
->operands
[0]->type
->is_vector());
1977 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1978 emit_dp(ir
, result_dst
, op
[0], op
[1],
1979 ir
->operands
[0]->type
->vector_elements
);
1984 emit_scalar(ir
, TGSI_OPCODE_SQRT
, result_dst
, op
[0]);
1986 /* This is the only instruction sequence that makes the game "Risen"
1987 * render correctly. ABS is not required for the game, but since GLSL
1988 * declares negative values as "undefined", allowing us to do whatever
1989 * we want, I choose to use ABS to match DX9 and pre-GLSL RSQ
1992 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0].get_abs());
1993 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, result_src
);
1997 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
2000 if (native_integers
) {
2001 emit_asm(ir
, TGSI_OPCODE_I2F
, result_dst
, op
[0]);
2004 /* fallthrough to next case otherwise */
2006 if (native_integers
) {
2007 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], st_src_reg_for_float(1.0));
2010 /* fallthrough to next case otherwise */
2013 case ir_unop_i642u64
:
2014 case ir_unop_u642i64
:
2015 /* Converting between signed and unsigned integers is a no-op. */
2017 result_src
.type
= result_dst
.type
;
2020 if (native_integers
) {
2021 /* Booleans are stored as integers using ~0 for true and 0 for false.
2022 * GLSL requires that int(bool) return 1 for true and 0 for false.
2023 * This conversion is done with AND, but it could be done with NEG.
2025 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], st_src_reg_for_int(1));
2027 /* Booleans and integers are both stored as floats when native
2028 * integers are disabled.
2034 if (native_integers
)
2035 emit_asm(ir
, TGSI_OPCODE_F2I
, result_dst
, op
[0]);
2037 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
2040 if (native_integers
)
2041 emit_asm(ir
, TGSI_OPCODE_F2U
, result_dst
, op
[0]);
2043 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
2045 case ir_unop_bitcast_f2i
:
2046 case ir_unop_bitcast_f2u
:
2047 /* Make sure we don't propagate the negate modifier to integer opcodes. */
2048 if (op
[0].negate
|| op
[0].abs
)
2049 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
2052 result_src
.type
= ir
->operation
== ir_unop_bitcast_f2i
? GLSL_TYPE_INT
:
2055 case ir_unop_bitcast_i2f
:
2056 case ir_unop_bitcast_u2f
:
2058 result_src
.type
= GLSL_TYPE_FLOAT
;
2061 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_float(0.0));
2064 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_double(0.0));
2067 if (native_integers
)
2068 emit_asm(ir
, TGSI_OPCODE_USNE
, result_dst
, op
[0], st_src_reg_for_int(0));
2070 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_float(0.0));
2072 case ir_unop_bitcast_u642d
:
2073 case ir_unop_bitcast_i642d
:
2075 result_src
.type
= GLSL_TYPE_DOUBLE
;
2077 case ir_unop_bitcast_d2i64
:
2079 result_src
.type
= GLSL_TYPE_INT64
;
2081 case ir_unop_bitcast_d2u64
:
2083 result_src
.type
= GLSL_TYPE_UINT64
;
2086 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
2089 emit_asm(ir
, TGSI_OPCODE_CEIL
, result_dst
, op
[0]);
2092 emit_asm(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
2094 case ir_unop_round_even
:
2095 emit_asm(ir
, TGSI_OPCODE_ROUND
, result_dst
, op
[0]);
2098 emit_asm(ir
, TGSI_OPCODE_FRC
, result_dst
, op
[0]);
2102 emit_asm(ir
, TGSI_OPCODE_MIN
, result_dst
, op
[0], op
[1]);
2105 emit_asm(ir
, TGSI_OPCODE_MAX
, result_dst
, op
[0], op
[1]);
2108 emit_scalar(ir
, TGSI_OPCODE_POW
, result_dst
, op
[0], op
[1]);
2111 case ir_unop_bit_not
:
2112 if (native_integers
) {
2113 emit_asm(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
2117 if (native_integers
) {
2118 emit_asm(ir
, TGSI_OPCODE_U2F
, result_dst
, op
[0]);
2121 case ir_binop_lshift
:
2122 case ir_binop_rshift
:
2123 if (native_integers
) {
2124 unsigned opcode
= ir
->operation
== ir_binop_lshift
? TGSI_OPCODE_SHL
2128 if (glsl_base_type_is_64bit(op
[0].type
)) {
2129 /* GLSL shift operations have 32-bit shift counts, but TGSI uses
2132 count
= get_temp(glsl_type::u64vec(ir
->operands
[1]->type
->components()));
2133 emit_asm(ir
, TGSI_OPCODE_U2I64
, st_dst_reg(count
), op
[1]);
2138 emit_asm(ir
, opcode
, result_dst
, op
[0], count
);
2141 case ir_binop_bit_and
:
2142 if (native_integers
) {
2143 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], op
[1]);
2146 case ir_binop_bit_xor
:
2147 if (native_integers
) {
2148 emit_asm(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0], op
[1]);
2151 case ir_binop_bit_or
:
2152 if (native_integers
) {
2153 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0], op
[1]);
2157 assert(!"GLSL 1.30 features unsupported");
2160 case ir_binop_ubo_load
: {
2161 ir_constant
*const_uniform_block
= ir
->operands
[0]->as_constant();
2162 ir_constant
*const_offset_ir
= ir
->operands
[1]->as_constant();
2163 unsigned const_offset
= const_offset_ir
? const_offset_ir
->value
.u
[0] : 0;
2164 unsigned const_block
= const_uniform_block
? const_uniform_block
->value
.u
[0] + 1 : 0;
2165 st_src_reg index_reg
= get_temp(glsl_type::uint_type
);
2168 cbuf
.type
= ir
->type
->base_type
;
2169 cbuf
.file
= PROGRAM_CONSTANT
;
2171 cbuf
.reladdr
= NULL
;
2175 assert(ir
->type
->is_vector() || ir
->type
->is_scalar());
2177 if (const_offset_ir
) {
2178 /* Constant index into constant buffer */
2179 cbuf
.reladdr
= NULL
;
2180 cbuf
.index
= const_offset
/ 16;
2183 ir_expression
*offset_expr
= ir
->operands
[1]->as_expression();
2184 st_src_reg offset
= op
[1];
2186 /* The OpenGL spec is written in such a way that accesses with
2187 * non-constant offset are almost always vec4-aligned. The only
2188 * exception to this are members of structs in arrays of structs:
2189 * each struct in an array of structs is at least vec4-aligned,
2190 * but single-element and [ui]vec2 members of the struct may be at
2191 * an offset that is not a multiple of 16 bytes.
2193 * Here, we extract that offset, relying on previous passes to always
2194 * generate offset expressions of the form (+ expr constant_offset).
2196 * Note that the std430 layout, which allows more cases of alignment
2197 * less than vec4 in arrays, is not supported for uniform blocks, so
2198 * we do not have to deal with it here.
2200 if (offset_expr
&& offset_expr
->operation
== ir_binop_add
) {
2201 const_offset_ir
= offset_expr
->operands
[1]->as_constant();
2202 if (const_offset_ir
) {
2203 const_offset
= const_offset_ir
->value
.u
[0];
2204 cbuf
.index
= const_offset
/ 16;
2205 offset_expr
->operands
[0]->accept(this);
2206 offset
= this->result
;
2210 /* Relative/variable index into constant buffer */
2211 emit_asm(ir
, TGSI_OPCODE_USHR
, st_dst_reg(index_reg
), offset
,
2212 st_src_reg_for_int(4));
2213 cbuf
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2214 memcpy(cbuf
.reladdr
, &index_reg
, sizeof(index_reg
));
2217 if (const_uniform_block
) {
2218 /* Constant constant buffer */
2219 cbuf
.reladdr2
= NULL
;
2220 cbuf
.index2D
= const_block
;
2221 cbuf
.has_index2
= true;
2224 /* Relative/variable constant buffer */
2225 cbuf
.reladdr2
= ralloc(mem_ctx
, st_src_reg
);
2227 memcpy(cbuf
.reladdr2
, &op
[0], sizeof(st_src_reg
));
2228 cbuf
.has_index2
= true;
2231 cbuf
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
2232 if (glsl_base_type_is_64bit(cbuf
.type
))
2233 cbuf
.swizzle
+= MAKE_SWIZZLE4(const_offset
% 16 / 8,
2234 const_offset
% 16 / 8,
2235 const_offset
% 16 / 8,
2236 const_offset
% 16 / 8);
2238 cbuf
.swizzle
+= MAKE_SWIZZLE4(const_offset
% 16 / 4,
2239 const_offset
% 16 / 4,
2240 const_offset
% 16 / 4,
2241 const_offset
% 16 / 4);
2243 if (ir
->type
->is_boolean()) {
2244 emit_asm(ir
, TGSI_OPCODE_USNE
, result_dst
, cbuf
, st_src_reg_for_int(0));
2246 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, cbuf
);
2251 /* note: we have to reorder the three args here */
2252 emit_asm(ir
, TGSI_OPCODE_LRP
, result_dst
, op
[2], op
[1], op
[0]);
2255 if (this->ctx
->Const
.NativeIntegers
)
2256 emit_asm(ir
, TGSI_OPCODE_UCMP
, result_dst
, op
[0], op
[1], op
[2]);
2258 op
[0].negate
= ~op
[0].negate
;
2259 emit_asm(ir
, TGSI_OPCODE_CMP
, result_dst
, op
[0], op
[1], op
[2]);
2262 case ir_triop_bitfield_extract
:
2263 emit_asm(ir
, TGSI_OPCODE_IBFE
, result_dst
, op
[0], op
[1], op
[2]);
2265 case ir_quadop_bitfield_insert
:
2266 emit_asm(ir
, TGSI_OPCODE_BFI
, result_dst
, op
[0], op
[1], op
[2], op
[3]);
2268 case ir_unop_bitfield_reverse
:
2269 emit_asm(ir
, TGSI_OPCODE_BREV
, result_dst
, op
[0]);
2271 case ir_unop_bit_count
:
2272 emit_asm(ir
, TGSI_OPCODE_POPC
, result_dst
, op
[0]);
2274 case ir_unop_find_msb
:
2275 emit_asm(ir
, TGSI_OPCODE_IMSB
, result_dst
, op
[0]);
2277 case ir_unop_find_lsb
:
2278 emit_asm(ir
, TGSI_OPCODE_LSB
, result_dst
, op
[0]);
2280 case ir_binop_imul_high
:
2281 emit_asm(ir
, TGSI_OPCODE_IMUL_HI
, result_dst
, op
[0], op
[1]);
2284 /* In theory, MAD is incorrect here. */
2286 emit_asm(ir
, TGSI_OPCODE_FMA
, result_dst
, op
[0], op
[1], op
[2]);
2288 emit_asm(ir
, TGSI_OPCODE_MAD
, result_dst
, op
[0], op
[1], op
[2]);
2290 case ir_unop_interpolate_at_centroid
:
2291 emit_asm(ir
, TGSI_OPCODE_INTERP_CENTROID
, result_dst
, op
[0]);
2293 case ir_binop_interpolate_at_offset
: {
2294 /* The y coordinate needs to be flipped for the default fb */
2295 static const gl_state_index transform_y_state
[STATE_LENGTH
]
2296 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
};
2298 unsigned transform_y_index
=
2299 _mesa_add_state_reference(this->prog
->Parameters
,
2302 st_src_reg transform_y
= st_src_reg(PROGRAM_STATE_VAR
,
2304 glsl_type::vec4_type
);
2305 transform_y
.swizzle
= SWIZZLE_XXXX
;
2307 st_src_reg temp
= get_temp(glsl_type::vec2_type
);
2308 st_dst_reg temp_dst
= st_dst_reg(temp
);
2310 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[1]);
2311 temp_dst
.writemask
= WRITEMASK_Y
;
2312 emit_asm(ir
, TGSI_OPCODE_MUL
, temp_dst
, transform_y
, op
[1]);
2313 emit_asm(ir
, TGSI_OPCODE_INTERP_OFFSET
, result_dst
, op
[0], temp
);
2316 case ir_binop_interpolate_at_sample
:
2317 emit_asm(ir
, TGSI_OPCODE_INTERP_SAMPLE
, result_dst
, op
[0], op
[1]);
2321 emit_asm(ir
, TGSI_OPCODE_D2F
, result_dst
, op
[0]);
2324 emit_asm(ir
, TGSI_OPCODE_F2D
, result_dst
, op
[0]);
2327 emit_asm(ir
, TGSI_OPCODE_D2I
, result_dst
, op
[0]);
2330 emit_asm(ir
, TGSI_OPCODE_I2D
, result_dst
, op
[0]);
2333 emit_asm(ir
, TGSI_OPCODE_D2U
, result_dst
, op
[0]);
2336 emit_asm(ir
, TGSI_OPCODE_U2D
, result_dst
, op
[0]);
2338 case ir_unop_unpack_double_2x32
:
2339 case ir_unop_pack_double_2x32
:
2340 case ir_unop_unpack_int_2x32
:
2341 case ir_unop_pack_int_2x32
:
2342 case ir_unop_unpack_uint_2x32
:
2343 case ir_unop_pack_uint_2x32
:
2344 case ir_unop_unpack_sampler_2x32
:
2345 case ir_unop_pack_sampler_2x32
:
2346 case ir_unop_unpack_image_2x32
:
2347 case ir_unop_pack_image_2x32
:
2348 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
2351 case ir_binop_ldexp
:
2352 if (ir
->operands
[0]->type
->is_double()) {
2353 emit_asm(ir
, TGSI_OPCODE_DLDEXP
, result_dst
, op
[0], op
[1]);
2355 assert(!"Invalid ldexp for non-double opcode in glsl_to_tgsi_visitor::visit()");
2359 case ir_unop_pack_half_2x16
:
2360 emit_asm(ir
, TGSI_OPCODE_PK2H
, result_dst
, op
[0]);
2362 case ir_unop_unpack_half_2x16
:
2363 emit_asm(ir
, TGSI_OPCODE_UP2H
, result_dst
, op
[0]);
2366 case ir_unop_get_buffer_size
: {
2367 ir_constant
*const_offset
= ir
->operands
[0]->as_constant();
2370 ctx
->Const
.Program
[shader
->Stage
].MaxAtomicBuffers
+
2371 (const_offset
? const_offset
->value
.u
[0] : 0),
2373 if (!const_offset
) {
2374 buffer
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2375 *buffer
.reladdr
= op
[0];
2376 emit_arl(ir
, sampler_reladdr
, op
[0]);
2378 emit_asm(ir
, TGSI_OPCODE_RESQ
, result_dst
)->resource
= buffer
;
2384 case ir_unop_b2i64
: {
2385 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2386 st_dst_reg temp_dst
= st_dst_reg(temp
);
2387 unsigned orig_swz
= op
[0].swizzle
;
2389 * To convert unsigned to 64-bit:
2390 * zero Y channel, copy X channel.
2392 temp_dst
.writemask
= WRITEMASK_Y
;
2393 if (vector_elements
> 1)
2394 temp_dst
.writemask
|= WRITEMASK_W
;
2395 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, st_src_reg_for_int(0));
2396 temp_dst
.writemask
= WRITEMASK_X
;
2397 if (vector_elements
> 1)
2398 temp_dst
.writemask
|= WRITEMASK_Z
;
2399 op
[0].swizzle
= MAKE_SWIZZLE4(GET_SWZ(orig_swz
, 0), GET_SWZ(orig_swz
, 0),
2400 GET_SWZ(orig_swz
, 1), GET_SWZ(orig_swz
, 1));
2401 if (ir
->operation
== ir_unop_u2i64
|| ir
->operation
== ir_unop_u2u64
)
2402 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2404 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, op
[0], st_src_reg_for_int(1));
2406 result_src
.type
= GLSL_TYPE_UINT64
;
2407 if (vector_elements
> 2) {
2408 /* Subtle: We rely on the fact that get_temp here returns the next
2409 * TGSI temporary register directly after the temp register used for
2410 * the first two components, so that the result gets picked up
2413 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2414 st_dst_reg temp_dst
= st_dst_reg(temp
);
2415 temp_dst
.writemask
= WRITEMASK_Y
;
2416 if (vector_elements
> 3)
2417 temp_dst
.writemask
|= WRITEMASK_W
;
2418 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, st_src_reg_for_int(0));
2420 temp_dst
.writemask
= WRITEMASK_X
;
2421 if (vector_elements
> 3)
2422 temp_dst
.writemask
|= WRITEMASK_Z
;
2423 op
[0].swizzle
= MAKE_SWIZZLE4(GET_SWZ(orig_swz
, 2), GET_SWZ(orig_swz
, 2),
2424 GET_SWZ(orig_swz
, 3), GET_SWZ(orig_swz
, 3));
2425 if (ir
->operation
== ir_unop_u2i64
|| ir
->operation
== ir_unop_u2u64
)
2426 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2428 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, op
[0], st_src_reg_for_int(1));
2435 case ir_unop_i642u
: {
2436 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2437 st_dst_reg temp_dst
= st_dst_reg(temp
);
2438 unsigned orig_swz
= op
[0].swizzle
;
2439 unsigned orig_idx
= op
[0].index
;
2441 temp_dst
.writemask
= WRITEMASK_X
;
2443 for (el
= 0; el
< vector_elements
; el
++) {
2444 unsigned swz
= GET_SWZ(orig_swz
, el
);
2446 op
[0].swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_Z
, SWIZZLE_Z
, SWIZZLE_Z
);
2448 op
[0].swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
);
2450 op
[0].index
= orig_idx
+ 1;
2451 op
[0].type
= GLSL_TYPE_UINT
;
2452 temp_dst
.writemask
= WRITEMASK_X
<< el
;
2453 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2456 if (ir
->operation
== ir_unop_u642u
|| ir
->operation
== ir_unop_i642u
)
2457 result_src
.type
= GLSL_TYPE_UINT
;
2459 result_src
.type
= GLSL_TYPE_INT
;
2463 emit_asm(ir
, TGSI_OPCODE_U64SNE
, result_dst
, op
[0], st_src_reg_for_int(0));
2466 emit_asm(ir
, TGSI_OPCODE_I642F
, result_dst
, op
[0]);
2469 emit_asm(ir
, TGSI_OPCODE_U642F
, result_dst
, op
[0]);
2472 emit_asm(ir
, TGSI_OPCODE_I642D
, result_dst
, op
[0]);
2475 emit_asm(ir
, TGSI_OPCODE_U642D
, result_dst
, op
[0]);
2478 emit_asm(ir
, TGSI_OPCODE_I2I64
, result_dst
, op
[0]);
2481 emit_asm(ir
, TGSI_OPCODE_F2I64
, result_dst
, op
[0]);
2484 emit_asm(ir
, TGSI_OPCODE_D2I64
, result_dst
, op
[0]);
2487 emit_asm(ir
, TGSI_OPCODE_I2I64
, result_dst
, op
[0]);
2490 emit_asm(ir
, TGSI_OPCODE_F2U64
, result_dst
, op
[0]);
2493 emit_asm(ir
, TGSI_OPCODE_D2U64
, result_dst
, op
[0]);
2495 /* these might be needed */
2496 case ir_unop_pack_snorm_2x16
:
2497 case ir_unop_pack_unorm_2x16
:
2498 case ir_unop_pack_snorm_4x8
:
2499 case ir_unop_pack_unorm_4x8
:
2501 case ir_unop_unpack_snorm_2x16
:
2502 case ir_unop_unpack_unorm_2x16
:
2503 case ir_unop_unpack_snorm_4x8
:
2504 case ir_unop_unpack_unorm_4x8
:
2506 case ir_quadop_vector
:
2507 case ir_binop_vector_extract
:
2508 case ir_triop_vector_insert
:
2509 case ir_binop_carry
:
2510 case ir_binop_borrow
:
2511 case ir_unop_ssbo_unsized_array_length
:
2512 /* This operation is not supported, or should have already been handled.
2514 assert(!"Invalid ir opcode in glsl_to_tgsi_visitor::visit()");
2518 this->result
= result_src
;
2523 glsl_to_tgsi_visitor::visit(ir_swizzle
*ir
)
2529 /* Note that this is only swizzles in expressions, not those on the left
2530 * hand side of an assignment, which do write masking. See ir_assignment
2534 ir
->val
->accept(this);
2536 assert(src
.file
!= PROGRAM_UNDEFINED
);
2537 assert(ir
->type
->vector_elements
> 0);
2539 for (i
= 0; i
< 4; i
++) {
2540 if (i
< ir
->type
->vector_elements
) {
2543 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.x
);
2546 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.y
);
2549 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.z
);
2552 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.w
);
2556 /* If the type is smaller than a vec4, replicate the last
2559 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
2563 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2568 /* Test if the variable is an array. Note that geometry and
2569 * tessellation shader inputs are outputs are always arrays (except
2570 * for patch inputs), so only the array element type is considered.
2573 is_inout_array(unsigned stage
, ir_variable
*var
, bool *remove_array
)
2575 const glsl_type
*type
= var
->type
;
2577 *remove_array
= false;
2579 if ((stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_in
) ||
2580 (stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_out
))
2583 if (((stage
== MESA_SHADER_GEOMETRY
&& var
->data
.mode
== ir_var_shader_in
) ||
2584 (stage
== MESA_SHADER_TESS_EVAL
&& var
->data
.mode
== ir_var_shader_in
) ||
2585 stage
== MESA_SHADER_TESS_CTRL
) &&
2587 if (!var
->type
->is_array())
2588 return false; /* a system value probably */
2590 type
= var
->type
->fields
.array
;
2591 *remove_array
= true;
2594 return type
->is_array() || type
->is_matrix();
2598 st_translate_interp_loc(ir_variable
*var
)
2600 if (var
->data
.centroid
)
2601 return TGSI_INTERPOLATE_LOC_CENTROID
;
2602 else if (var
->data
.sample
)
2603 return TGSI_INTERPOLATE_LOC_SAMPLE
;
2605 return TGSI_INTERPOLATE_LOC_CENTER
;
2609 glsl_to_tgsi_visitor::visit(ir_dereference_variable
*ir
)
2611 variable_storage
*entry
= find_variable_storage(ir
->var
);
2612 ir_variable
*var
= ir
->var
;
2616 switch (var
->data
.mode
) {
2617 case ir_var_uniform
:
2618 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_UNIFORM
,
2619 var
->data
.param_index
);
2620 _mesa_hash_table_insert(this->variables
, var
, entry
);
2622 case ir_var_shader_in
: {
2623 /* The linker assigns locations for varyings and attributes,
2624 * including deprecated builtins (like gl_Color), user-assign
2625 * generic attributes (glBindVertexLocation), and
2626 * user-defined varyings.
2628 assert(var
->data
.location
!= -1);
2630 const glsl_type
*type_without_array
= var
->type
->without_array();
2631 struct inout_decl
*decl
= &inputs
[num_inputs
];
2632 unsigned component
= var
->data
.location_frac
;
2633 unsigned num_components
;
2636 if (type_without_array
->is_64bit())
2637 component
= component
/ 2;
2638 if (type_without_array
->vector_elements
)
2639 num_components
= type_without_array
->vector_elements
;
2643 decl
->mesa_index
= var
->data
.location
;
2644 decl
->interp
= (glsl_interp_mode
) var
->data
.interpolation
;
2645 decl
->interp_loc
= st_translate_interp_loc(var
);
2646 decl
->base_type
= type_without_array
->base_type
;
2647 decl
->usage_mask
= u_bit_consecutive(component
, num_components
);
2649 if (is_inout_array(shader
->Stage
, var
, &remove_array
)) {
2650 decl
->array_id
= num_input_arrays
+ 1;
2657 decl
->size
= type_size(var
->type
->fields
.array
);
2659 decl
->size
= type_size(var
->type
);
2661 entry
= new(mem_ctx
) variable_storage(var
,
2665 entry
->component
= component
;
2667 _mesa_hash_table_insert(this->variables
, var
, entry
);
2671 case ir_var_shader_out
: {
2672 assert(var
->data
.location
!= -1);
2674 const glsl_type
*type_without_array
= var
->type
->without_array();
2675 struct inout_decl
*decl
= &outputs
[num_outputs
];
2676 unsigned component
= var
->data
.location_frac
;
2677 unsigned num_components
;
2680 if (type_without_array
->is_64bit())
2681 component
= component
/ 2;
2682 if (type_without_array
->vector_elements
)
2683 num_components
= type_without_array
->vector_elements
;
2687 decl
->mesa_index
= var
->data
.location
+ FRAG_RESULT_MAX
* var
->data
.index
;
2688 decl
->base_type
= type_without_array
->base_type
;
2689 decl
->usage_mask
= u_bit_consecutive(component
, num_components
);
2690 if (var
->data
.stream
& (1u << 31)) {
2691 decl
->gs_out_streams
= var
->data
.stream
& ~(1u << 31);
2693 assert(var
->data
.stream
< 4);
2694 decl
->gs_out_streams
= 0;
2695 for (unsigned i
= 0; i
< num_components
; ++i
)
2696 decl
->gs_out_streams
|= var
->data
.stream
<< (2 * (component
+ i
));
2699 if (is_inout_array(shader
->Stage
, var
, &remove_array
)) {
2700 decl
->array_id
= num_output_arrays
+ 1;
2701 num_output_arrays
++;
2707 decl
->size
= type_size(var
->type
->fields
.array
);
2709 decl
->size
= type_size(var
->type
);
2711 if (var
->data
.fb_fetch_output
) {
2712 st_dst_reg dst
= st_dst_reg(get_temp(var
->type
));
2713 st_src_reg src
= st_src_reg(PROGRAM_OUTPUT
, decl
->mesa_index
,
2714 var
->type
, component
, decl
->array_id
);
2715 emit_asm(NULL
, TGSI_OPCODE_FBFETCH
, dst
, src
);
2716 entry
= new(mem_ctx
) variable_storage(var
, dst
.file
, dst
.index
,
2719 entry
= new(mem_ctx
) variable_storage(var
,
2724 entry
->component
= component
;
2726 _mesa_hash_table_insert(this->variables
, var
, entry
);
2730 case ir_var_system_value
:
2731 entry
= new(mem_ctx
) variable_storage(var
,
2732 PROGRAM_SYSTEM_VALUE
,
2733 var
->data
.location
);
2736 case ir_var_temporary
:
2737 st_src_reg src
= get_temp(var
->type
);
2739 entry
= new(mem_ctx
) variable_storage(var
, src
.file
, src
.index
,
2741 _mesa_hash_table_insert(this->variables
, var
, entry
);
2747 printf("Failed to make storage for %s\n", var
->name
);
2752 this->result
= st_src_reg(entry
->file
, entry
->index
, var
->type
,
2753 entry
->component
, entry
->array_id
);
2754 if (this->shader
->Stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_in
&& var
->type
->is_double())
2755 this->result
.is_double_vertex_input
= true;
2756 if (!native_integers
)
2757 this->result
.type
= GLSL_TYPE_FLOAT
;
2761 shrink_array_declarations(struct inout_decl
*decls
, unsigned count
,
2762 GLbitfield64
* usage_mask
,
2763 GLbitfield64 double_usage_mask
,
2764 GLbitfield
* patch_usage_mask
)
2769 /* Fix array declarations by removing unused array elements at both ends
2770 * of the arrays. For example, mat4[3] where only mat[1] is used.
2772 for (i
= 0; i
< count
; i
++) {
2773 struct inout_decl
*decl
= &decls
[i
];
2774 if (!decl
->array_id
)
2777 /* Shrink the beginning. */
2778 for (j
= 0; j
< (int)decl
->size
; j
++) {
2779 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
) {
2780 if (*patch_usage_mask
&
2781 BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
))
2785 if (*usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
))
2787 if (double_usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
-1))
2796 /* Shrink the end. */
2797 for (j
= decl
->size
-1; j
>= 0; j
--) {
2798 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
) {
2799 if (*patch_usage_mask
&
2800 BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
))
2804 if (*usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
))
2806 if (double_usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
-1))
2813 /* When not all entries of an array are accessed, we mark them as used
2814 * here anyway, to ensure that the input/output mapping logic doesn't get
2817 * TODO This happens when an array isn't used via indirect access, which
2818 * some game ports do (at least eON-based). There is an optimization
2819 * opportunity here by replacing the array declaration with non-array
2820 * declarations of those slots that are actually used.
2822 for (j
= 1; j
< (int)decl
->size
; ++j
) {
2823 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
)
2824 *patch_usage_mask
|= BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
);
2826 *usage_mask
|= BITFIELD64_BIT(decl
->mesa_index
+ j
);
2832 glsl_to_tgsi_visitor::visit(ir_dereference_array
*ir
)
2836 int element_size
= type_size(ir
->type
);
2839 index
= ir
->array_index
->constant_expression_value();
2841 ir
->array
->accept(this);
2844 if (ir
->array
->ir_type
!= ir_type_dereference_array
) {
2845 switch (this->prog
->Target
) {
2846 case GL_TESS_CONTROL_PROGRAM_NV
:
2847 is_2D
= (src
.file
== PROGRAM_INPUT
|| src
.file
== PROGRAM_OUTPUT
) &&
2848 !ir
->variable_referenced()->data
.patch
;
2850 case GL_TESS_EVALUATION_PROGRAM_NV
:
2851 is_2D
= src
.file
== PROGRAM_INPUT
&&
2852 !ir
->variable_referenced()->data
.patch
;
2854 case GL_GEOMETRY_PROGRAM_NV
:
2855 is_2D
= src
.file
== PROGRAM_INPUT
;
2865 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
2866 src
.file
== PROGRAM_INPUT
)
2867 element_size
= attrib_type_size(ir
->type
, true);
2869 src
.index2D
= index
->value
.i
[0];
2870 src
.has_index2
= true;
2872 src
.index
+= index
->value
.i
[0] * element_size
;
2874 /* Variable index array dereference. It eats the "vec4" of the
2875 * base of the array and an index that offsets the TGSI register
2878 ir
->array_index
->accept(this);
2880 st_src_reg index_reg
;
2882 if (element_size
== 1) {
2883 index_reg
= this->result
;
2885 index_reg
= get_temp(native_integers
?
2886 glsl_type::int_type
: glsl_type::float_type
);
2888 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(index_reg
),
2889 this->result
, st_src_reg_for_type(index_reg
.type
, element_size
));
2892 /* If there was already a relative address register involved, add the
2893 * new and the old together to get the new offset.
2895 if (!is_2D
&& src
.reladdr
!= NULL
) {
2896 st_src_reg accum_reg
= get_temp(native_integers
?
2897 glsl_type::int_type
: glsl_type::float_type
);
2899 emit_asm(ir
, TGSI_OPCODE_ADD
, st_dst_reg(accum_reg
),
2900 index_reg
, *src
.reladdr
);
2902 index_reg
= accum_reg
;
2906 src
.reladdr2
= ralloc(mem_ctx
, st_src_reg
);
2907 memcpy(src
.reladdr2
, &index_reg
, sizeof(index_reg
));
2909 src
.has_index2
= true;
2911 src
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2912 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
2916 /* Change the register type to the element type of the array. */
2917 src
.type
= ir
->type
->base_type
;
2923 glsl_to_tgsi_visitor::visit(ir_dereference_record
*ir
)
2926 const glsl_type
*struct_type
= ir
->record
->type
;
2929 ir
->record
->accept(this);
2931 for (i
= 0; i
< struct_type
->length
; i
++) {
2932 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
2934 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
2937 /* If the type is smaller than a vec4, replicate the last channel out. */
2938 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
2939 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
2941 this->result
.swizzle
= SWIZZLE_NOOP
;
2943 this->result
.index
+= offset
;
2944 this->result
.type
= ir
->type
->base_type
;
2948 * We want to be careful in assignment setup to hit the actual storage
2949 * instead of potentially using a temporary like we might with the
2950 * ir_dereference handler.
2953 get_assignment_lhs(ir_dereference
*ir
, glsl_to_tgsi_visitor
*v
, int *component
)
2955 /* The LHS must be a dereference. If the LHS is a variable indexed array
2956 * access of a vector, it must be separated into a series conditional moves
2957 * before reaching this point (see ir_vec_index_to_cond_assign).
2959 assert(ir
->as_dereference());
2960 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
2962 assert(!deref_array
->array
->type
->is_vector());
2965 /* Use the rvalue deref handler for the most part. We write swizzles using
2966 * the writemask, but we do extract the base component for enhanced layouts
2967 * from the source swizzle.
2970 *component
= GET_SWZ(v
->result
.swizzle
, 0);
2971 return st_dst_reg(v
->result
);
2975 * Process the condition of a conditional assignment
2977 * Examines the condition of a conditional assignment to generate the optimal
2978 * first operand of a \c CMP instruction. If the condition is a relational
2979 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
2980 * used as the source for the \c CMP instruction. Otherwise the comparison
2981 * is processed to a boolean result, and the boolean result is used as the
2982 * operand to the CMP instruction.
2985 glsl_to_tgsi_visitor::process_move_condition(ir_rvalue
*ir
)
2987 ir_rvalue
*src_ir
= ir
;
2989 bool switch_order
= false;
2991 ir_expression
*const expr
= ir
->as_expression();
2993 if (native_integers
) {
2994 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
2995 enum glsl_base_type type
= expr
->operands
[0]->type
->base_type
;
2996 if (type
== GLSL_TYPE_INT
|| type
== GLSL_TYPE_UINT
||
2997 type
== GLSL_TYPE_BOOL
) {
2998 if (expr
->operation
== ir_binop_equal
) {
2999 if (expr
->operands
[0]->is_zero()) {
3000 src_ir
= expr
->operands
[1];
3001 switch_order
= true;
3003 else if (expr
->operands
[1]->is_zero()) {
3004 src_ir
= expr
->operands
[0];
3005 switch_order
= true;
3008 else if (expr
->operation
== ir_binop_nequal
) {
3009 if (expr
->operands
[0]->is_zero()) {
3010 src_ir
= expr
->operands
[1];
3012 else if (expr
->operands
[1]->is_zero()) {
3013 src_ir
= expr
->operands
[0];
3019 src_ir
->accept(this);
3020 return switch_order
;
3023 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
3024 bool zero_on_left
= false;
3026 if (expr
->operands
[0]->is_zero()) {
3027 src_ir
= expr
->operands
[1];
3028 zero_on_left
= true;
3029 } else if (expr
->operands
[1]->is_zero()) {
3030 src_ir
= expr
->operands
[0];
3031 zero_on_left
= false;
3035 * (a < 0) T F F ( a < 0) T F F
3036 * (0 < a) F F T (-a < 0) F F T
3037 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
3038 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
3039 * (a > 0) F F T (-a < 0) F F T
3040 * (0 > a) T F F ( a < 0) T F F
3041 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
3042 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
3044 * Note that exchanging the order of 0 and 'a' in the comparison simply
3045 * means that the value of 'a' should be negated.
3048 switch (expr
->operation
) {
3050 switch_order
= false;
3051 negate
= zero_on_left
;
3054 case ir_binop_greater
:
3055 switch_order
= false;
3056 negate
= !zero_on_left
;
3059 case ir_binop_lequal
:
3060 switch_order
= true;
3061 negate
= !zero_on_left
;
3064 case ir_binop_gequal
:
3065 switch_order
= true;
3066 negate
= zero_on_left
;
3070 /* This isn't the right kind of comparison afterall, so make sure
3071 * the whole condition is visited.
3079 src_ir
->accept(this);
3081 /* We use the TGSI_OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
3082 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
3083 * choose which value TGSI_OPCODE_CMP produces without an extra instruction
3084 * computing the condition.
3087 this->result
.negate
= ~this->result
.negate
;
3089 return switch_order
;
3093 glsl_to_tgsi_visitor::emit_block_mov(ir_assignment
*ir
, const struct glsl_type
*type
,
3094 st_dst_reg
*l
, st_src_reg
*r
,
3095 st_src_reg
*cond
, bool cond_swap
)
3097 if (type
->is_record()) {
3098 for (unsigned int i
= 0; i
< type
->length
; i
++) {
3099 emit_block_mov(ir
, type
->fields
.structure
[i
].type
, l
, r
,
3105 if (type
->is_array()) {
3106 for (unsigned int i
= 0; i
< type
->length
; i
++) {
3107 emit_block_mov(ir
, type
->fields
.array
, l
, r
, cond
, cond_swap
);
3112 if (type
->is_matrix()) {
3113 const struct glsl_type
*vec_type
;
3115 vec_type
= glsl_type::get_instance(type
->is_double() ? GLSL_TYPE_DOUBLE
: GLSL_TYPE_FLOAT
,
3116 type
->vector_elements
, 1);
3118 for (int i
= 0; i
< type
->matrix_columns
; i
++) {
3119 emit_block_mov(ir
, vec_type
, l
, r
, cond
, cond_swap
);
3124 assert(type
->is_scalar() || type
->is_vector());
3126 l
->type
= type
->base_type
;
3127 r
->type
= type
->base_type
;
3129 st_src_reg l_src
= st_src_reg(*l
);
3130 l_src
.swizzle
= swizzle_for_size(type
->vector_elements
);
3132 if (native_integers
) {
3133 emit_asm(ir
, TGSI_OPCODE_UCMP
, *l
, *cond
,
3134 cond_swap
? l_src
: *r
,
3135 cond_swap
? *r
: l_src
);
3137 emit_asm(ir
, TGSI_OPCODE_CMP
, *l
, *cond
,
3138 cond_swap
? l_src
: *r
,
3139 cond_swap
? *r
: l_src
);
3142 emit_asm(ir
, TGSI_OPCODE_MOV
, *l
, *r
);
3146 if (type
->is_dual_slot()) {
3148 if (r
->is_double_vertex_input
== false)
3154 glsl_to_tgsi_visitor::visit(ir_assignment
*ir
)
3160 /* all generated instructions need to be flaged as precise */
3161 this->precise
= is_precise(ir
->lhs
->variable_referenced());
3162 ir
->rhs
->accept(this);
3165 l
= get_assignment_lhs(ir
->lhs
, this, &dst_component
);
3169 int first_enabled_chan
= 0;
3171 ir_variable
*variable
= ir
->lhs
->variable_referenced();
3173 if (shader
->Stage
== MESA_SHADER_FRAGMENT
&&
3174 variable
->data
.mode
== ir_var_shader_out
&&
3175 (variable
->data
.location
== FRAG_RESULT_DEPTH
||
3176 variable
->data
.location
== FRAG_RESULT_STENCIL
)) {
3177 assert(ir
->lhs
->type
->is_scalar());
3178 assert(ir
->write_mask
== WRITEMASK_X
);
3180 if (variable
->data
.location
== FRAG_RESULT_DEPTH
)
3181 l
.writemask
= WRITEMASK_Z
;
3183 assert(variable
->data
.location
== FRAG_RESULT_STENCIL
);
3184 l
.writemask
= WRITEMASK_Y
;
3186 } else if (ir
->write_mask
== 0) {
3187 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
3189 unsigned num_elements
= ir
->lhs
->type
->without_array()->vector_elements
;
3192 l
.writemask
= u_bit_consecutive(0, num_elements
);
3194 /* The type is a struct or an array of (array of) structs. */
3195 l
.writemask
= WRITEMASK_XYZW
;
3198 l
.writemask
= ir
->write_mask
;
3201 for (int i
= 0; i
< 4; i
++) {
3202 if (l
.writemask
& (1 << i
)) {
3203 first_enabled_chan
= GET_SWZ(r
.swizzle
, i
);
3208 l
.writemask
= l
.writemask
<< dst_component
;
3210 /* Swizzle a small RHS vector into the channels being written.
3212 * glsl ir treats write_mask as dictating how many channels are
3213 * present on the RHS while TGSI treats write_mask as just
3214 * showing which channels of the vec4 RHS get written.
3216 for (int i
= 0; i
< 4; i
++) {
3217 if (l
.writemask
& (1 << i
))
3218 swizzles
[i
] = GET_SWZ(r
.swizzle
, rhs_chan
++);
3220 swizzles
[i
] = first_enabled_chan
;
3222 r
.swizzle
= MAKE_SWIZZLE4(swizzles
[0], swizzles
[1],
3223 swizzles
[2], swizzles
[3]);
3226 assert(l
.file
!= PROGRAM_UNDEFINED
);
3227 assert(r
.file
!= PROGRAM_UNDEFINED
);
3229 if (ir
->condition
) {
3230 const bool switch_order
= this->process_move_condition(ir
->condition
);
3231 st_src_reg condition
= this->result
;
3233 emit_block_mov(ir
, ir
->lhs
->type
, &l
, &r
, &condition
, switch_order
);
3234 } else if (ir
->rhs
->as_expression() &&
3235 this->instructions
.get_tail() &&
3236 ir
->rhs
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->ir
&&
3237 !((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->is_64bit_expanded
&&
3238 type_size(ir
->lhs
->type
) == 1 &&
3239 l
.writemask
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->dst
[0].writemask
) {
3240 /* To avoid emitting an extra MOV when assigning an expression to a
3241 * variable, emit the last instruction of the expression again, but
3242 * replace the destination register with the target of the assignment.
3243 * Dead code elimination will remove the original instruction.
3245 glsl_to_tgsi_instruction
*inst
, *new_inst
;
3246 inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
3247 new_inst
= emit_asm(ir
, inst
->op
, l
, inst
->src
[0], inst
->src
[1], inst
->src
[2], inst
->src
[3]);
3248 new_inst
->saturate
= inst
->saturate
;
3249 inst
->dead_mask
= inst
->dst
[0].writemask
;
3251 emit_block_mov(ir
, ir
->rhs
->type
, &l
, &r
, NULL
, false);
3258 glsl_to_tgsi_visitor::visit(ir_constant
*ir
)
3261 GLdouble stack_vals
[4] = { 0 };
3262 gl_constant_value
*values
= (gl_constant_value
*) stack_vals
;
3263 GLenum gl_type
= GL_NONE
;
3265 static int in_array
= 0;
3266 gl_register_file file
= in_array
? PROGRAM_CONSTANT
: PROGRAM_IMMEDIATE
;
3268 /* Unfortunately, 4 floats is all we can get into
3269 * _mesa_add_typed_unnamed_constant. So, make a temp to store an
3270 * aggregate constant and move each constant value into it. If we
3271 * get lucky, copy propagation will eliminate the extra moves.
3273 if (ir
->type
->is_record()) {
3274 st_src_reg temp_base
= get_temp(ir
->type
);
3275 st_dst_reg temp
= st_dst_reg(temp_base
);
3277 foreach_in_list(ir_constant
, field_value
, &ir
->components
) {
3278 int size
= type_size(field_value
->type
);
3282 field_value
->accept(this);
3285 for (i
= 0; i
< (unsigned int)size
; i
++) {
3286 emit_asm(ir
, TGSI_OPCODE_MOV
, temp
, src
);
3292 this->result
= temp_base
;
3296 if (ir
->type
->is_array()) {
3297 st_src_reg temp_base
= get_temp(ir
->type
);
3298 st_dst_reg temp
= st_dst_reg(temp_base
);
3299 int size
= type_size(ir
->type
->fields
.array
);
3304 for (i
= 0; i
< ir
->type
->length
; i
++) {
3305 ir
->array_elements
[i
]->accept(this);
3307 for (int j
= 0; j
< size
; j
++) {
3308 emit_asm(ir
, TGSI_OPCODE_MOV
, temp
, src
);
3314 this->result
= temp_base
;
3319 if (ir
->type
->is_matrix()) {
3320 st_src_reg mat
= get_temp(ir
->type
);
3321 st_dst_reg mat_column
= st_dst_reg(mat
);
3323 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
3324 switch (ir
->type
->base_type
) {
3325 case GLSL_TYPE_FLOAT
:
3326 values
= (gl_constant_value
*) &ir
->value
.f
[i
* ir
->type
->vector_elements
];
3328 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
3329 src
.index
= add_constant(file
,
3331 ir
->type
->vector_elements
,
3334 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3336 case GLSL_TYPE_DOUBLE
:
3337 values
= (gl_constant_value
*) &ir
->value
.d
[i
* ir
->type
->vector_elements
];
3338 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
3339 src
.index
= add_constant(file
,
3341 ir
->type
->vector_elements
,
3344 if (ir
->type
->vector_elements
>= 2) {
3345 mat_column
.writemask
= WRITEMASK_XY
;
3346 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
3347 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3349 mat_column
.writemask
= WRITEMASK_X
;
3350 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
);
3351 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3354 if (ir
->type
->vector_elements
> 2) {
3355 if (ir
->type
->vector_elements
== 4) {
3356 mat_column
.writemask
= WRITEMASK_ZW
;
3357 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
3358 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3360 mat_column
.writemask
= WRITEMASK_Z
;
3361 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
);
3362 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3363 mat_column
.writemask
= WRITEMASK_XYZW
;
3364 src
.swizzle
= SWIZZLE_XYZW
;
3370 unreachable("Illegal matrix constant type.\n");
3379 switch (ir
->type
->base_type
) {
3380 case GLSL_TYPE_FLOAT
:
3382 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3383 values
[i
].f
= ir
->value
.f
[i
];
3386 case GLSL_TYPE_DOUBLE
:
3387 gl_type
= GL_DOUBLE
;
3388 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3389 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(double));
3392 case GLSL_TYPE_INT64
:
3393 gl_type
= GL_INT64_ARB
;
3394 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3395 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(int64_t));
3398 case GLSL_TYPE_UINT64
:
3399 gl_type
= GL_UNSIGNED_INT64_ARB
;
3400 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3401 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(uint64_t));
3404 case GLSL_TYPE_UINT
:
3405 gl_type
= native_integers
? GL_UNSIGNED_INT
: GL_FLOAT
;
3406 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3407 if (native_integers
)
3408 values
[i
].u
= ir
->value
.u
[i
];
3410 values
[i
].f
= ir
->value
.u
[i
];
3414 gl_type
= native_integers
? GL_INT
: GL_FLOAT
;
3415 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3416 if (native_integers
)
3417 values
[i
].i
= ir
->value
.i
[i
];
3419 values
[i
].f
= ir
->value
.i
[i
];
3422 case GLSL_TYPE_BOOL
:
3423 gl_type
= native_integers
? GL_BOOL
: GL_FLOAT
;
3424 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3425 values
[i
].u
= ir
->value
.b
[i
] ? ctx
->Const
.UniformBooleanTrue
: 0;
3429 assert(!"Non-float/uint/int/bool constant");
3432 this->result
= st_src_reg(file
, -1, ir
->type
);
3433 this->result
.index
= add_constant(file
,
3435 ir
->type
->vector_elements
,
3437 &this->result
.swizzle
);
3441 glsl_to_tgsi_visitor::visit_atomic_counter_intrinsic(ir_call
*ir
)
3443 exec_node
*param
= ir
->actual_parameters
.get_head();
3444 ir_dereference
*deref
= static_cast<ir_dereference
*>(param
);
3445 ir_variable
*location
= deref
->variable_referenced();
3448 PROGRAM_BUFFER
, location
->data
.binding
, GLSL_TYPE_ATOMIC_UINT
);
3450 /* Calculate the surface offset */
3452 unsigned array_size
= 0, base
= 0;
3455 get_deref_offsets(deref
, &array_size
, &base
, &index
, &offset
, false);
3457 if (offset
.file
!= PROGRAM_UNDEFINED
) {
3458 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(offset
),
3459 offset
, st_src_reg_for_int(ATOMIC_COUNTER_SIZE
));
3460 emit_asm(ir
, TGSI_OPCODE_ADD
, st_dst_reg(offset
),
3461 offset
, st_src_reg_for_int(location
->data
.offset
+ index
* ATOMIC_COUNTER_SIZE
));
3463 offset
= st_src_reg_for_int(location
->data
.offset
+ index
* ATOMIC_COUNTER_SIZE
);
3466 ir
->return_deref
->accept(this);
3467 st_dst_reg
dst(this->result
);
3468 dst
.writemask
= WRITEMASK_X
;
3470 glsl_to_tgsi_instruction
*inst
;
3472 if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_read
) {
3473 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, offset
);
3474 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_increment
) {
3475 inst
= emit_asm(ir
, TGSI_OPCODE_ATOMUADD
, dst
, offset
,
3476 st_src_reg_for_int(1));
3477 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_predecrement
) {
3478 inst
= emit_asm(ir
, TGSI_OPCODE_ATOMUADD
, dst
, offset
,
3479 st_src_reg_for_int(-1));
3480 emit_asm(ir
, TGSI_OPCODE_ADD
, dst
, this->result
, st_src_reg_for_int(-1));
3482 param
= param
->get_next();
3483 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3486 st_src_reg data
= this->result
, data2
= undef_src
;
3488 switch (ir
->callee
->intrinsic_id
) {
3489 case ir_intrinsic_atomic_counter_add
:
3490 opcode
= TGSI_OPCODE_ATOMUADD
;
3492 case ir_intrinsic_atomic_counter_min
:
3493 opcode
= TGSI_OPCODE_ATOMIMIN
;
3495 case ir_intrinsic_atomic_counter_max
:
3496 opcode
= TGSI_OPCODE_ATOMIMAX
;
3498 case ir_intrinsic_atomic_counter_and
:
3499 opcode
= TGSI_OPCODE_ATOMAND
;
3501 case ir_intrinsic_atomic_counter_or
:
3502 opcode
= TGSI_OPCODE_ATOMOR
;
3504 case ir_intrinsic_atomic_counter_xor
:
3505 opcode
= TGSI_OPCODE_ATOMXOR
;
3507 case ir_intrinsic_atomic_counter_exchange
:
3508 opcode
= TGSI_OPCODE_ATOMXCHG
;
3510 case ir_intrinsic_atomic_counter_comp_swap
: {
3511 opcode
= TGSI_OPCODE_ATOMCAS
;
3512 param
= param
->get_next();
3513 val
= ((ir_instruction
*)param
)->as_rvalue();
3515 data2
= this->result
;
3519 assert(!"Unexpected intrinsic");
3523 inst
= emit_asm(ir
, opcode
, dst
, offset
, data
, data2
);
3526 inst
->resource
= buffer
;
3530 glsl_to_tgsi_visitor::visit_ssbo_intrinsic(ir_call
*ir
)
3532 exec_node
*param
= ir
->actual_parameters
.get_head();
3534 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
3536 param
= param
->get_next();
3537 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
3539 ir_constant
*const_block
= block
->as_constant();
3543 ctx
->Const
.Program
[shader
->Stage
].MaxAtomicBuffers
+
3544 (const_block
? const_block
->value
.u
[0] : 0),
3548 block
->accept(this);
3549 buffer
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
3550 *buffer
.reladdr
= this->result
;
3551 emit_arl(ir
, sampler_reladdr
, this->result
);
3554 /* Calculate the surface offset */
3555 offset
->accept(this);
3556 st_src_reg off
= this->result
;
3558 st_dst_reg dst
= undef_dst
;
3559 if (ir
->return_deref
) {
3560 ir
->return_deref
->accept(this);
3561 dst
= st_dst_reg(this->result
);
3562 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3565 glsl_to_tgsi_instruction
*inst
;
3567 if (ir
->callee
->intrinsic_id
== ir_intrinsic_ssbo_load
) {
3568 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, off
);
3569 if (dst
.type
== GLSL_TYPE_BOOL
)
3570 emit_asm(ir
, TGSI_OPCODE_USNE
, dst
, st_src_reg(dst
), 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
;
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 /* The emit_asm() might have actually split the op into pieces, e.g. for
3635 * double stores. We have to go back and fix up all the generated ops.
3637 unsigned op
= inst
->op
;
3639 inst
->resource
= buffer
;
3641 inst
->buffer_access
= access
->value
.u
[0];
3643 if (inst
== this->instructions
.get_head_raw())
3645 inst
= (glsl_to_tgsi_instruction
*)inst
->get_prev();
3647 if (inst
->op
== TGSI_OPCODE_UADD
) {
3648 if (inst
== this->instructions
.get_head_raw())
3650 inst
= (glsl_to_tgsi_instruction
*)inst
->get_prev();
3652 } while (inst
->op
== op
&& inst
->resource
.file
== PROGRAM_UNDEFINED
);
3656 glsl_to_tgsi_visitor::visit_membar_intrinsic(ir_call
*ir
)
3658 switch (ir
->callee
->intrinsic_id
) {
3659 case ir_intrinsic_memory_barrier
:
3660 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3661 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
|
3662 TGSI_MEMBAR_ATOMIC_BUFFER
|
3663 TGSI_MEMBAR_SHADER_IMAGE
|
3664 TGSI_MEMBAR_SHARED
));
3666 case ir_intrinsic_memory_barrier_atomic_counter
:
3667 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3668 st_src_reg_for_int(TGSI_MEMBAR_ATOMIC_BUFFER
));
3670 case ir_intrinsic_memory_barrier_buffer
:
3671 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3672 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
));
3674 case ir_intrinsic_memory_barrier_image
:
3675 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3676 st_src_reg_for_int(TGSI_MEMBAR_SHADER_IMAGE
));
3678 case ir_intrinsic_memory_barrier_shared
:
3679 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3680 st_src_reg_for_int(TGSI_MEMBAR_SHARED
));
3682 case ir_intrinsic_group_memory_barrier
:
3683 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3684 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
|
3685 TGSI_MEMBAR_ATOMIC_BUFFER
|
3686 TGSI_MEMBAR_SHADER_IMAGE
|
3687 TGSI_MEMBAR_SHARED
|
3688 TGSI_MEMBAR_THREAD_GROUP
));
3691 assert(!"Unexpected memory barrier intrinsic");
3696 glsl_to_tgsi_visitor::visit_shared_intrinsic(ir_call
*ir
)
3698 exec_node
*param
= ir
->actual_parameters
.get_head();
3700 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
3702 st_src_reg
buffer(PROGRAM_MEMORY
, 0, GLSL_TYPE_UINT
);
3704 /* Calculate the surface offset */
3705 offset
->accept(this);
3706 st_src_reg off
= this->result
;
3708 st_dst_reg dst
= undef_dst
;
3709 if (ir
->return_deref
) {
3710 ir
->return_deref
->accept(this);
3711 dst
= st_dst_reg(this->result
);
3712 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3715 glsl_to_tgsi_instruction
*inst
;
3717 if (ir
->callee
->intrinsic_id
== ir_intrinsic_shared_load
) {
3718 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, off
);
3719 inst
->resource
= buffer
;
3720 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_shared_store
) {
3721 param
= param
->get_next();
3722 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3725 param
= param
->get_next();
3726 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
3728 dst
.writemask
= write_mask
->value
.u
[0];
3730 dst
.type
= this->result
.type
;
3731 inst
= emit_asm(ir
, TGSI_OPCODE_STORE
, dst
, off
, this->result
);
3732 inst
->resource
= buffer
;
3734 param
= param
->get_next();
3735 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3738 st_src_reg data
= this->result
, data2
= undef_src
;
3740 switch (ir
->callee
->intrinsic_id
) {
3741 case ir_intrinsic_shared_atomic_add
:
3742 opcode
= TGSI_OPCODE_ATOMUADD
;
3744 case ir_intrinsic_shared_atomic_min
:
3745 opcode
= TGSI_OPCODE_ATOMIMIN
;
3747 case ir_intrinsic_shared_atomic_max
:
3748 opcode
= TGSI_OPCODE_ATOMIMAX
;
3750 case ir_intrinsic_shared_atomic_and
:
3751 opcode
= TGSI_OPCODE_ATOMAND
;
3753 case ir_intrinsic_shared_atomic_or
:
3754 opcode
= TGSI_OPCODE_ATOMOR
;
3756 case ir_intrinsic_shared_atomic_xor
:
3757 opcode
= TGSI_OPCODE_ATOMXOR
;
3759 case ir_intrinsic_shared_atomic_exchange
:
3760 opcode
= TGSI_OPCODE_ATOMXCHG
;
3762 case ir_intrinsic_shared_atomic_comp_swap
:
3763 opcode
= TGSI_OPCODE_ATOMCAS
;
3764 param
= param
->get_next();
3765 val
= ((ir_instruction
*)param
)->as_rvalue();
3767 data2
= this->result
;
3770 assert(!"Unexpected intrinsic");
3774 inst
= emit_asm(ir
, opcode
, dst
, off
, data
, data2
);
3775 inst
->resource
= buffer
;
3780 get_image_qualifiers(ir_dereference
*ir
, const glsl_type
**type
,
3781 bool *memory_coherent
, bool *memory_volatile
,
3782 bool *memory_restrict
, unsigned *image_format
)
3785 switch (ir
->ir_type
) {
3786 case ir_type_dereference_record
: {
3787 ir_dereference_record
*deref_record
= ir
->as_dereference_record();
3788 const glsl_type
*struct_type
= deref_record
->record
->type
;
3790 for (unsigned i
= 0; i
< struct_type
->length
; i
++) {
3791 if (!strcmp(struct_type
->fields
.structure
[i
].name
,
3792 deref_record
->field
)) {
3793 *type
= struct_type
->fields
.structure
[i
].type
;
3795 struct_type
->fields
.structure
[i
].memory_coherent
;
3797 struct_type
->fields
.structure
[i
].memory_volatile
;
3799 struct_type
->fields
.structure
[i
].memory_restrict
;
3801 struct_type
->fields
.structure
[i
].image_format
;
3808 case ir_type_dereference_array
: {
3809 ir_dereference_array
*deref_arr
= ir
->as_dereference_array();
3810 get_image_qualifiers((ir_dereference
*)deref_arr
->array
, type
,
3811 memory_coherent
, memory_volatile
, memory_restrict
,
3816 case ir_type_dereference_variable
: {
3817 ir_variable
*var
= ir
->variable_referenced();
3819 *type
= var
->type
->without_array();
3820 *memory_coherent
= var
->data
.memory_coherent
;
3821 *memory_volatile
= var
->data
.memory_volatile
;
3822 *memory_restrict
= var
->data
.memory_restrict
;
3823 *image_format
= var
->data
.image_format
;
3833 glsl_to_tgsi_visitor::visit_image_intrinsic(ir_call
*ir
)
3835 exec_node
*param
= ir
->actual_parameters
.get_head();
3837 ir_dereference
*img
= (ir_dereference
*)param
;
3838 const ir_variable
*imgvar
= img
->variable_referenced();
3839 unsigned sampler_array_size
= 1, sampler_base
= 0;
3840 bool memory_coherent
= false, memory_volatile
= false, memory_restrict
= false;
3841 unsigned image_format
= 0;
3842 const glsl_type
*type
= NULL
;
3844 get_image_qualifiers(img
, &type
, &memory_coherent
, &memory_volatile
,
3845 &memory_restrict
, &image_format
);
3848 st_src_reg
image(PROGRAM_IMAGE
, 0, GLSL_TYPE_UINT
);
3850 get_deref_offsets(img
, &sampler_array_size
, &sampler_base
,
3851 &index
, &reladdr
, !imgvar
->contains_bindless());
3853 image
.index
= index
;
3854 if (reladdr
.file
!= PROGRAM_UNDEFINED
) {
3855 image
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
3856 *image
.reladdr
= reladdr
;
3857 emit_arl(ir
, sampler_reladdr
, reladdr
);
3860 st_dst_reg dst
= undef_dst
;
3861 if (ir
->return_deref
) {
3862 ir
->return_deref
->accept(this);
3863 dst
= st_dst_reg(this->result
);
3864 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3867 glsl_to_tgsi_instruction
*inst
;
3869 if (ir
->callee
->intrinsic_id
== ir_intrinsic_image_size
) {
3870 dst
.writemask
= WRITEMASK_XYZ
;
3871 inst
= emit_asm(ir
, TGSI_OPCODE_RESQ
, dst
);
3872 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_image_samples
) {
3873 st_src_reg res
= get_temp(glsl_type::ivec4_type
);
3874 st_dst_reg dstres
= st_dst_reg(res
);
3875 dstres
.writemask
= WRITEMASK_W
;
3876 inst
= emit_asm(ir
, TGSI_OPCODE_RESQ
, dstres
);
3877 res
.swizzle
= SWIZZLE_WWWW
;
3878 emit_asm(ir
, TGSI_OPCODE_MOV
, dst
, res
);
3880 st_src_reg arg1
= undef_src
, arg2
= undef_src
;
3882 st_dst_reg coord_dst
;
3883 coord
= get_temp(glsl_type::ivec4_type
);
3884 coord_dst
= st_dst_reg(coord
);
3885 coord_dst
.writemask
= (1 << type
->coordinate_components()) - 1;
3886 param
= param
->get_next();
3887 ((ir_dereference
*)param
)->accept(this);
3888 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
3889 coord
.swizzle
= SWIZZLE_XXXX
;
3890 switch (type
->coordinate_components()) {
3891 case 4: assert(!"unexpected coord count");
3893 case 3: coord
.swizzle
|= SWIZZLE_Z
<< 6;
3895 case 2: coord
.swizzle
|= SWIZZLE_Y
<< 3;
3898 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
3899 param
= param
->get_next();
3900 ((ir_dereference
*)param
)->accept(this);
3901 st_src_reg sample
= this->result
;
3902 sample
.swizzle
= SWIZZLE_XXXX
;
3903 coord_dst
.writemask
= WRITEMASK_W
;
3904 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, sample
);
3905 coord
.swizzle
|= SWIZZLE_W
<< 9;
3908 param
= param
->get_next();
3909 if (!param
->is_tail_sentinel()) {
3910 ((ir_dereference
*)param
)->accept(this);
3911 arg1
= this->result
;
3912 param
= param
->get_next();
3915 if (!param
->is_tail_sentinel()) {
3916 ((ir_dereference
*)param
)->accept(this);
3917 arg2
= this->result
;
3918 param
= param
->get_next();
3921 assert(param
->is_tail_sentinel());
3924 switch (ir
->callee
->intrinsic_id
) {
3925 case ir_intrinsic_image_load
:
3926 opcode
= TGSI_OPCODE_LOAD
;
3928 case ir_intrinsic_image_store
:
3929 opcode
= TGSI_OPCODE_STORE
;
3931 case ir_intrinsic_image_atomic_add
:
3932 opcode
= TGSI_OPCODE_ATOMUADD
;
3934 case ir_intrinsic_image_atomic_min
:
3935 opcode
= TGSI_OPCODE_ATOMIMIN
;
3937 case ir_intrinsic_image_atomic_max
:
3938 opcode
= TGSI_OPCODE_ATOMIMAX
;
3940 case ir_intrinsic_image_atomic_and
:
3941 opcode
= TGSI_OPCODE_ATOMAND
;
3943 case ir_intrinsic_image_atomic_or
:
3944 opcode
= TGSI_OPCODE_ATOMOR
;
3946 case ir_intrinsic_image_atomic_xor
:
3947 opcode
= TGSI_OPCODE_ATOMXOR
;
3949 case ir_intrinsic_image_atomic_exchange
:
3950 opcode
= TGSI_OPCODE_ATOMXCHG
;
3952 case ir_intrinsic_image_atomic_comp_swap
:
3953 opcode
= TGSI_OPCODE_ATOMCAS
;
3956 assert(!"Unexpected intrinsic");
3960 inst
= emit_asm(ir
, opcode
, dst
, coord
, arg1
, arg2
);
3961 if (opcode
== TGSI_OPCODE_STORE
)
3962 inst
->dst
[0].writemask
= WRITEMASK_XYZW
;
3965 if (imgvar
->contains_bindless()) {
3967 inst
->resource
= this->result
;
3968 inst
->resource
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
3969 SWIZZLE_X
, SWIZZLE_Y
);
3971 inst
->resource
= image
;
3972 inst
->sampler_array_size
= sampler_array_size
;
3973 inst
->sampler_base
= sampler_base
;
3976 inst
->tex_target
= type
->sampler_index();
3977 inst
->image_format
= st_mesa_format_to_pipe_format(st_context(ctx
),
3978 _mesa_get_shader_image_format(image_format
));
3980 if (memory_coherent
)
3981 inst
->buffer_access
|= TGSI_MEMORY_COHERENT
;
3982 if (memory_restrict
)
3983 inst
->buffer_access
|= TGSI_MEMORY_RESTRICT
;
3984 if (memory_volatile
)
3985 inst
->buffer_access
|= TGSI_MEMORY_VOLATILE
;
3989 glsl_to_tgsi_visitor::visit_generic_intrinsic(ir_call
*ir
, unsigned op
)
3991 ir
->return_deref
->accept(this);
3992 st_dst_reg dst
= st_dst_reg(this->result
);
3994 dst
.writemask
= u_bit_consecutive(0, ir
->return_deref
->var
->type
->vector_elements
);
3996 st_src_reg src
[4] = { undef_src
, undef_src
, undef_src
, undef_src
};
3997 unsigned num_src
= 0;
3998 foreach_in_list(ir_rvalue
, param
, &ir
->actual_parameters
) {
3999 assert(num_src
< ARRAY_SIZE(src
));
4001 this->result
.file
= PROGRAM_UNDEFINED
;
4002 param
->accept(this);
4003 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
4005 src
[num_src
] = this->result
;
4009 emit_asm(ir
, op
, dst
, src
[0], src
[1], src
[2], src
[3]);
4013 glsl_to_tgsi_visitor::visit(ir_call
*ir
)
4015 ir_function_signature
*sig
= ir
->callee
;
4017 /* Filter out intrinsics */
4018 switch (sig
->intrinsic_id
) {
4019 case ir_intrinsic_atomic_counter_read
:
4020 case ir_intrinsic_atomic_counter_increment
:
4021 case ir_intrinsic_atomic_counter_predecrement
:
4022 case ir_intrinsic_atomic_counter_add
:
4023 case ir_intrinsic_atomic_counter_min
:
4024 case ir_intrinsic_atomic_counter_max
:
4025 case ir_intrinsic_atomic_counter_and
:
4026 case ir_intrinsic_atomic_counter_or
:
4027 case ir_intrinsic_atomic_counter_xor
:
4028 case ir_intrinsic_atomic_counter_exchange
:
4029 case ir_intrinsic_atomic_counter_comp_swap
:
4030 visit_atomic_counter_intrinsic(ir
);
4033 case ir_intrinsic_ssbo_load
:
4034 case ir_intrinsic_ssbo_store
:
4035 case ir_intrinsic_ssbo_atomic_add
:
4036 case ir_intrinsic_ssbo_atomic_min
:
4037 case ir_intrinsic_ssbo_atomic_max
:
4038 case ir_intrinsic_ssbo_atomic_and
:
4039 case ir_intrinsic_ssbo_atomic_or
:
4040 case ir_intrinsic_ssbo_atomic_xor
:
4041 case ir_intrinsic_ssbo_atomic_exchange
:
4042 case ir_intrinsic_ssbo_atomic_comp_swap
:
4043 visit_ssbo_intrinsic(ir
);
4046 case ir_intrinsic_memory_barrier
:
4047 case ir_intrinsic_memory_barrier_atomic_counter
:
4048 case ir_intrinsic_memory_barrier_buffer
:
4049 case ir_intrinsic_memory_barrier_image
:
4050 case ir_intrinsic_memory_barrier_shared
:
4051 case ir_intrinsic_group_memory_barrier
:
4052 visit_membar_intrinsic(ir
);
4055 case ir_intrinsic_shared_load
:
4056 case ir_intrinsic_shared_store
:
4057 case ir_intrinsic_shared_atomic_add
:
4058 case ir_intrinsic_shared_atomic_min
:
4059 case ir_intrinsic_shared_atomic_max
:
4060 case ir_intrinsic_shared_atomic_and
:
4061 case ir_intrinsic_shared_atomic_or
:
4062 case ir_intrinsic_shared_atomic_xor
:
4063 case ir_intrinsic_shared_atomic_exchange
:
4064 case ir_intrinsic_shared_atomic_comp_swap
:
4065 visit_shared_intrinsic(ir
);
4068 case ir_intrinsic_image_load
:
4069 case ir_intrinsic_image_store
:
4070 case ir_intrinsic_image_atomic_add
:
4071 case ir_intrinsic_image_atomic_min
:
4072 case ir_intrinsic_image_atomic_max
:
4073 case ir_intrinsic_image_atomic_and
:
4074 case ir_intrinsic_image_atomic_or
:
4075 case ir_intrinsic_image_atomic_xor
:
4076 case ir_intrinsic_image_atomic_exchange
:
4077 case ir_intrinsic_image_atomic_comp_swap
:
4078 case ir_intrinsic_image_size
:
4079 case ir_intrinsic_image_samples
:
4080 visit_image_intrinsic(ir
);
4083 case ir_intrinsic_shader_clock
:
4084 visit_generic_intrinsic(ir
, TGSI_OPCODE_CLOCK
);
4087 case ir_intrinsic_vote_all
:
4088 visit_generic_intrinsic(ir
, TGSI_OPCODE_VOTE_ALL
);
4090 case ir_intrinsic_vote_any
:
4091 visit_generic_intrinsic(ir
, TGSI_OPCODE_VOTE_ANY
);
4093 case ir_intrinsic_vote_eq
:
4094 visit_generic_intrinsic(ir
, TGSI_OPCODE_VOTE_EQ
);
4096 case ir_intrinsic_ballot
:
4097 visit_generic_intrinsic(ir
, TGSI_OPCODE_BALLOT
);
4099 case ir_intrinsic_read_first_invocation
:
4100 visit_generic_intrinsic(ir
, TGSI_OPCODE_READ_FIRST
);
4102 case ir_intrinsic_read_invocation
:
4103 visit_generic_intrinsic(ir
, TGSI_OPCODE_READ_INVOC
);
4106 case ir_intrinsic_invalid
:
4107 case ir_intrinsic_generic_load
:
4108 case ir_intrinsic_generic_store
:
4109 case ir_intrinsic_generic_atomic_add
:
4110 case ir_intrinsic_generic_atomic_and
:
4111 case ir_intrinsic_generic_atomic_or
:
4112 case ir_intrinsic_generic_atomic_xor
:
4113 case ir_intrinsic_generic_atomic_min
:
4114 case ir_intrinsic_generic_atomic_max
:
4115 case ir_intrinsic_generic_atomic_exchange
:
4116 case ir_intrinsic_generic_atomic_comp_swap
:
4117 unreachable("Invalid intrinsic");
4122 glsl_to_tgsi_visitor::calc_deref_offsets(ir_dereference
*tail
,
4123 unsigned *array_elements
,
4125 st_src_reg
*indirect
,
4128 switch (tail
->ir_type
) {
4129 case ir_type_dereference_record
: {
4130 ir_dereference_record
*deref_record
= tail
->as_dereference_record();
4131 const glsl_type
*struct_type
= deref_record
->record
->type
;
4132 int field_index
= deref_record
->record
->type
->field_index(deref_record
->field
);
4134 calc_deref_offsets(deref_record
->record
->as_dereference(), array_elements
, index
, indirect
, location
);
4136 assert(field_index
>= 0);
4137 *location
+= struct_type
->record_location_offset(field_index
);
4141 case ir_type_dereference_array
: {
4142 ir_dereference_array
*deref_arr
= tail
->as_dereference_array();
4143 ir_constant
*array_index
= deref_arr
->array_index
->constant_expression_value();
4146 st_src_reg temp_reg
;
4147 st_dst_reg temp_dst
;
4149 temp_reg
= get_temp(glsl_type::uint_type
);
4150 temp_dst
= st_dst_reg(temp_reg
);
4151 temp_dst
.writemask
= 1;
4153 deref_arr
->array_index
->accept(this);
4154 if (*array_elements
!= 1)
4155 emit_asm(NULL
, TGSI_OPCODE_MUL
, temp_dst
, this->result
, st_src_reg_for_int(*array_elements
));
4157 emit_asm(NULL
, TGSI_OPCODE_MOV
, temp_dst
, this->result
);
4159 if (indirect
->file
== PROGRAM_UNDEFINED
)
4160 *indirect
= temp_reg
;
4162 temp_dst
= st_dst_reg(*indirect
);
4163 temp_dst
.writemask
= 1;
4164 emit_asm(NULL
, TGSI_OPCODE_ADD
, temp_dst
, *indirect
, temp_reg
);
4167 *index
+= array_index
->value
.u
[0] * *array_elements
;
4169 *array_elements
*= deref_arr
->array
->type
->length
;
4171 calc_deref_offsets(deref_arr
->array
->as_dereference(), array_elements
, index
, indirect
, location
);
4180 glsl_to_tgsi_visitor::get_deref_offsets(ir_dereference
*ir
,
4181 unsigned *array_size
,
4184 st_src_reg
*reladdr
,
4187 GLuint shader
= _mesa_program_enum_to_shader_stage(this->prog
->Target
);
4188 unsigned location
= 0;
4189 ir_variable
*var
= ir
->variable_referenced();
4191 memset(reladdr
, 0, sizeof(*reladdr
));
4192 reladdr
->file
= PROGRAM_UNDEFINED
;
4198 location
= var
->data
.location
;
4199 calc_deref_offsets(ir
, array_size
, index
, reladdr
, &location
);
4202 * If we end up with no indirect then adjust the base to the index,
4203 * and set the array size to 1.
4205 if (reladdr
->file
== PROGRAM_UNDEFINED
) {
4211 assert(location
!= 0xffffffff);
4212 *base
+= this->shader_program
->data
->UniformStorage
[location
].opaque
[shader
].index
;
4213 *index
+= this->shader_program
->data
->UniformStorage
[location
].opaque
[shader
].index
;
4218 glsl_to_tgsi_visitor::canonicalize_gather_offset(st_src_reg offset
)
4220 if (offset
.reladdr
|| offset
.reladdr2
) {
4221 st_src_reg tmp
= get_temp(glsl_type::ivec2_type
);
4222 st_dst_reg tmp_dst
= st_dst_reg(tmp
);
4223 tmp_dst
.writemask
= WRITEMASK_XY
;
4224 emit_asm(NULL
, TGSI_OPCODE_MOV
, tmp_dst
, offset
);
4232 glsl_to_tgsi_visitor::visit(ir_texture
*ir
)
4234 st_src_reg result_src
, coord
, cube_sc
, lod_info
, projector
, dx
, dy
;
4235 st_src_reg offset
[MAX_GLSL_TEXTURE_OFFSET
], sample_index
, component
;
4236 st_src_reg levels_src
, reladdr
;
4237 st_dst_reg result_dst
, coord_dst
, cube_sc_dst
;
4238 glsl_to_tgsi_instruction
*inst
= NULL
;
4239 unsigned opcode
= TGSI_OPCODE_NOP
;
4240 const glsl_type
*sampler_type
= ir
->sampler
->type
;
4241 unsigned sampler_array_size
= 1, sampler_base
= 0;
4242 bool is_cube_array
= false, is_cube_shadow
= false;
4243 ir_variable
*var
= ir
->sampler
->variable_referenced();
4246 /* if we are a cube array sampler or a cube shadow */
4247 if (sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
) {
4248 is_cube_array
= sampler_type
->sampler_array
;
4249 is_cube_shadow
= sampler_type
->sampler_shadow
;
4252 if (ir
->coordinate
) {
4253 ir
->coordinate
->accept(this);
4255 /* Put our coords in a temp. We'll need to modify them for shadow,
4256 * projection, or LOD, so the only case we'd use it as-is is if
4257 * we're doing plain old texturing. The optimization passes on
4258 * glsl_to_tgsi_visitor should handle cleaning up our mess in that case.
4260 coord
= get_temp(glsl_type::vec4_type
);
4261 coord_dst
= st_dst_reg(coord
);
4262 coord_dst
.writemask
= (1 << ir
->coordinate
->type
->vector_elements
) - 1;
4263 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
4266 if (ir
->projector
) {
4267 ir
->projector
->accept(this);
4268 projector
= this->result
;
4271 /* Storage for our result. Ideally for an assignment we'd be using
4272 * the actual storage for the result here, instead.
4274 result_src
= get_temp(ir
->type
);
4275 result_dst
= st_dst_reg(result_src
);
4276 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
4280 opcode
= (is_cube_array
&& ir
->shadow_comparator
) ? TGSI_OPCODE_TEX2
: TGSI_OPCODE_TEX
;
4282 ir
->offset
->accept(this);
4283 offset
[0] = this->result
;
4287 if (is_cube_array
|| is_cube_shadow
) {
4288 opcode
= TGSI_OPCODE_TXB2
;
4291 opcode
= TGSI_OPCODE_TXB
;
4293 ir
->lod_info
.bias
->accept(this);
4294 lod_info
= this->result
;
4296 ir
->offset
->accept(this);
4297 offset
[0] = this->result
;
4301 if (this->has_tex_txf_lz
&& ir
->lod_info
.lod
->is_zero()) {
4302 opcode
= TGSI_OPCODE_TEX_LZ
;
4304 opcode
= is_cube_array
? TGSI_OPCODE_TXL2
: TGSI_OPCODE_TXL
;
4305 ir
->lod_info
.lod
->accept(this);
4306 lod_info
= this->result
;
4309 ir
->offset
->accept(this);
4310 offset
[0] = this->result
;
4314 opcode
= TGSI_OPCODE_TXD
;
4315 ir
->lod_info
.grad
.dPdx
->accept(this);
4317 ir
->lod_info
.grad
.dPdy
->accept(this);
4320 ir
->offset
->accept(this);
4321 offset
[0] = this->result
;
4325 opcode
= TGSI_OPCODE_TXQ
;
4326 ir
->lod_info
.lod
->accept(this);
4327 lod_info
= this->result
;
4329 case ir_query_levels
:
4330 opcode
= TGSI_OPCODE_TXQ
;
4331 lod_info
= undef_src
;
4332 levels_src
= get_temp(ir
->type
);
4335 if (this->has_tex_txf_lz
&& ir
->lod_info
.lod
->is_zero()) {
4336 opcode
= TGSI_OPCODE_TXF_LZ
;
4338 opcode
= TGSI_OPCODE_TXF
;
4339 ir
->lod_info
.lod
->accept(this);
4340 lod_info
= this->result
;
4343 ir
->offset
->accept(this);
4344 offset
[0] = this->result
;
4348 opcode
= TGSI_OPCODE_TXF
;
4349 ir
->lod_info
.sample_index
->accept(this);
4350 sample_index
= this->result
;
4353 opcode
= TGSI_OPCODE_TG4
;
4354 ir
->lod_info
.component
->accept(this);
4355 component
= this->result
;
4357 ir
->offset
->accept(this);
4358 if (ir
->offset
->type
->is_array()) {
4359 const glsl_type
*elt_type
= ir
->offset
->type
->fields
.array
;
4360 for (i
= 0; i
< ir
->offset
->type
->length
; i
++) {
4361 offset
[i
] = this->result
;
4362 offset
[i
].index
+= i
* type_size(elt_type
);
4363 offset
[i
].type
= elt_type
->base_type
;
4364 offset
[i
].swizzle
= swizzle_for_size(elt_type
->vector_elements
);
4365 offset
[i
] = canonicalize_gather_offset(offset
[i
]);
4368 offset
[0] = canonicalize_gather_offset(this->result
);
4373 opcode
= TGSI_OPCODE_LODQ
;
4375 case ir_texture_samples
:
4376 opcode
= TGSI_OPCODE_TXQS
;
4378 case ir_samples_identical
:
4379 unreachable("Unexpected ir_samples_identical opcode");
4382 if (ir
->projector
) {
4383 if (opcode
== TGSI_OPCODE_TEX
) {
4384 /* Slot the projector in as the last component of the coord. */
4385 coord_dst
.writemask
= WRITEMASK_W
;
4386 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, projector
);
4387 coord_dst
.writemask
= WRITEMASK_XYZW
;
4388 opcode
= TGSI_OPCODE_TXP
;
4390 st_src_reg coord_w
= coord
;
4391 coord_w
.swizzle
= SWIZZLE_WWWW
;
4393 /* For the other TEX opcodes there's no projective version
4394 * since the last slot is taken up by LOD info. Do the
4395 * projective divide now.
4397 coord_dst
.writemask
= WRITEMASK_W
;
4398 emit_asm(ir
, TGSI_OPCODE_RCP
, coord_dst
, projector
);
4400 /* In the case where we have to project the coordinates "by hand,"
4401 * the shadow comparator value must also be projected.
4403 st_src_reg tmp_src
= coord
;
4404 if (ir
->shadow_comparator
) {
4405 /* Slot the shadow value in as the second to last component of the
4408 ir
->shadow_comparator
->accept(this);
4410 tmp_src
= get_temp(glsl_type::vec4_type
);
4411 st_dst_reg tmp_dst
= st_dst_reg(tmp_src
);
4413 /* Projective division not allowed for array samplers. */
4414 assert(!sampler_type
->sampler_array
);
4416 tmp_dst
.writemask
= WRITEMASK_Z
;
4417 emit_asm(ir
, TGSI_OPCODE_MOV
, tmp_dst
, this->result
);
4419 tmp_dst
.writemask
= WRITEMASK_XY
;
4420 emit_asm(ir
, TGSI_OPCODE_MOV
, tmp_dst
, coord
);
4423 coord_dst
.writemask
= WRITEMASK_XYZ
;
4424 emit_asm(ir
, TGSI_OPCODE_MUL
, coord_dst
, tmp_src
, coord_w
);
4426 coord_dst
.writemask
= WRITEMASK_XYZW
;
4427 coord
.swizzle
= SWIZZLE_XYZW
;
4431 /* If projection is done and the opcode is not TGSI_OPCODE_TXP, then the shadow
4432 * comparator was put in the correct place (and projected) by the code,
4433 * above, that handles by-hand projection.
4435 if (ir
->shadow_comparator
&& (!ir
->projector
|| opcode
== TGSI_OPCODE_TXP
)) {
4436 /* Slot the shadow value in as the second to last component of the
4439 ir
->shadow_comparator
->accept(this);
4441 if (is_cube_array
) {
4442 cube_sc
= get_temp(glsl_type::float_type
);
4443 cube_sc_dst
= st_dst_reg(cube_sc
);
4444 cube_sc_dst
.writemask
= WRITEMASK_X
;
4445 emit_asm(ir
, TGSI_OPCODE_MOV
, cube_sc_dst
, this->result
);
4446 cube_sc_dst
.writemask
= WRITEMASK_X
;
4449 if ((sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_2D
&&
4450 sampler_type
->sampler_array
) ||
4451 sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
) {
4452 coord_dst
.writemask
= WRITEMASK_W
;
4454 coord_dst
.writemask
= WRITEMASK_Z
;
4456 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
4457 coord_dst
.writemask
= WRITEMASK_XYZW
;
4461 if (ir
->op
== ir_txf_ms
) {
4462 coord_dst
.writemask
= WRITEMASK_W
;
4463 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, sample_index
);
4464 coord_dst
.writemask
= WRITEMASK_XYZW
;
4465 } else if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXB
||
4466 opcode
== TGSI_OPCODE_TXF
) {
4467 /* TGSI stores LOD or LOD bias in the last channel of the coords. */
4468 coord_dst
.writemask
= WRITEMASK_W
;
4469 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, lod_info
);
4470 coord_dst
.writemask
= WRITEMASK_XYZW
;
4473 st_src_reg
sampler(PROGRAM_SAMPLER
, 0, GLSL_TYPE_UINT
);
4476 get_deref_offsets(ir
->sampler
, &sampler_array_size
, &sampler_base
,
4477 &index
, &reladdr
, !var
->contains_bindless());
4479 sampler
.index
= index
;
4480 if (reladdr
.file
!= PROGRAM_UNDEFINED
) {
4481 sampler
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
4482 *sampler
.reladdr
= reladdr
;
4483 emit_arl(ir
, sampler_reladdr
, reladdr
);
4486 if (opcode
== TGSI_OPCODE_TXD
)
4487 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, dx
, dy
);
4488 else if (opcode
== TGSI_OPCODE_TXQ
) {
4489 if (ir
->op
== ir_query_levels
) {
4490 /* the level is stored in W */
4491 inst
= emit_asm(ir
, opcode
, st_dst_reg(levels_src
), lod_info
);
4492 result_dst
.writemask
= WRITEMASK_X
;
4493 levels_src
.swizzle
= SWIZZLE_WWWW
;
4494 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, levels_src
);
4496 inst
= emit_asm(ir
, opcode
, result_dst
, lod_info
);
4497 } else if (opcode
== TGSI_OPCODE_TXQS
) {
4498 inst
= emit_asm(ir
, opcode
, result_dst
);
4499 } else if (opcode
== TGSI_OPCODE_TXL2
|| opcode
== TGSI_OPCODE_TXB2
) {
4500 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, lod_info
);
4501 } else if (opcode
== TGSI_OPCODE_TEX2
) {
4502 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, cube_sc
);
4503 } else if (opcode
== TGSI_OPCODE_TG4
) {
4504 if (is_cube_array
&& ir
->shadow_comparator
) {
4505 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, cube_sc
);
4507 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, component
);
4510 inst
= emit_asm(ir
, opcode
, result_dst
, coord
);
4512 if (ir
->shadow_comparator
)
4513 inst
->tex_shadow
= GL_TRUE
;
4515 if (var
->contains_bindless()) {
4516 ir
->sampler
->accept(this);
4517 inst
->resource
= this->result
;
4518 inst
->resource
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
4519 SWIZZLE_X
, SWIZZLE_Y
);
4521 inst
->resource
= sampler
;
4522 inst
->sampler_array_size
= sampler_array_size
;
4523 inst
->sampler_base
= sampler_base
;
4527 if (!inst
->tex_offsets
)
4528 inst
->tex_offsets
= rzalloc_array(inst
, st_src_reg
, MAX_GLSL_TEXTURE_OFFSET
);
4530 for (i
= 0; i
< MAX_GLSL_TEXTURE_OFFSET
&& offset
[i
].file
!= PROGRAM_UNDEFINED
; i
++)
4531 inst
->tex_offsets
[i
] = offset
[i
];
4532 inst
->tex_offset_num_offset
= i
;
4535 inst
->tex_target
= sampler_type
->sampler_index();
4536 inst
->tex_type
= ir
->type
->base_type
;
4538 this->result
= result_src
;
4542 glsl_to_tgsi_visitor::visit(ir_return
*ir
)
4544 assert(!ir
->get_value());
4546 emit_asm(ir
, TGSI_OPCODE_RET
);
4550 glsl_to_tgsi_visitor::visit(ir_discard
*ir
)
4552 if (ir
->condition
) {
4553 ir
->condition
->accept(this);
4554 st_src_reg condition
= this->result
;
4556 /* Convert the bool condition to a float so we can negate. */
4557 if (native_integers
) {
4558 st_src_reg temp
= get_temp(ir
->condition
->type
);
4559 emit_asm(ir
, TGSI_OPCODE_AND
, st_dst_reg(temp
),
4560 condition
, st_src_reg_for_float(1.0));
4564 condition
.negate
= ~condition
.negate
;
4565 emit_asm(ir
, TGSI_OPCODE_KILL_IF
, undef_dst
, condition
);
4567 /* unconditional kil */
4568 emit_asm(ir
, TGSI_OPCODE_KILL
);
4573 glsl_to_tgsi_visitor::visit(ir_if
*ir
)
4576 glsl_to_tgsi_instruction
*if_inst
;
4578 ir
->condition
->accept(this);
4579 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
4581 if_opcode
= native_integers
? TGSI_OPCODE_UIF
: TGSI_OPCODE_IF
;
4583 if_inst
= emit_asm(ir
->condition
, if_opcode
, undef_dst
, this->result
);
4585 this->instructions
.push_tail(if_inst
);
4587 visit_exec_list(&ir
->then_instructions
, this);
4589 if (!ir
->else_instructions
.is_empty()) {
4590 emit_asm(ir
->condition
, TGSI_OPCODE_ELSE
);
4591 visit_exec_list(&ir
->else_instructions
, this);
4594 if_inst
= emit_asm(ir
->condition
, TGSI_OPCODE_ENDIF
);
4599 glsl_to_tgsi_visitor::visit(ir_emit_vertex
*ir
)
4601 assert(this->prog
->Target
== GL_GEOMETRY_PROGRAM_NV
);
4603 ir
->stream
->accept(this);
4604 emit_asm(ir
, TGSI_OPCODE_EMIT
, undef_dst
, this->result
);
4608 glsl_to_tgsi_visitor::visit(ir_end_primitive
*ir
)
4610 assert(this->prog
->Target
== GL_GEOMETRY_PROGRAM_NV
);
4612 ir
->stream
->accept(this);
4613 emit_asm(ir
, TGSI_OPCODE_ENDPRIM
, undef_dst
, this->result
);
4617 glsl_to_tgsi_visitor::visit(ir_barrier
*ir
)
4619 assert(this->prog
->Target
== GL_TESS_CONTROL_PROGRAM_NV
||
4620 this->prog
->Target
== GL_COMPUTE_PROGRAM_NV
);
4622 emit_asm(ir
, TGSI_OPCODE_BARRIER
);
4625 glsl_to_tgsi_visitor::glsl_to_tgsi_visitor()
4627 STATIC_ASSERT(sizeof(samplers_used
) * 8 >= PIPE_MAX_SAMPLERS
);
4629 result
.file
= PROGRAM_UNDEFINED
;
4636 num_input_arrays
= 0;
4637 num_output_arrays
= 0;
4639 num_address_regs
= 0;
4642 indirect_addr_consts
= false;
4643 wpos_transform_const
= -1;
4645 native_integers
= false;
4646 mem_ctx
= ralloc_context(NULL
);
4649 shader_program
= NULL
;
4654 use_shared_memory
= false;
4655 has_tex_txf_lz
= false;
4659 static void var_destroy(struct hash_entry
*entry
)
4661 variable_storage
*storage
= (variable_storage
*)entry
->data
;
4666 glsl_to_tgsi_visitor::~glsl_to_tgsi_visitor()
4668 _mesa_hash_table_destroy(variables
, var_destroy
);
4670 ralloc_free(mem_ctx
);
4673 extern "C" void free_glsl_to_tgsi_visitor(glsl_to_tgsi_visitor
*v
)
4680 * Count resources used by the given gpu program (number of texture
4684 count_resources(glsl_to_tgsi_visitor
*v
, gl_program
*prog
)
4686 v
->samplers_used
= 0;
4689 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &v
->instructions
) {
4690 if (inst
->info
->is_tex
) {
4691 for (int i
= 0; i
< inst
->sampler_array_size
; i
++) {
4692 unsigned idx
= inst
->sampler_base
+ i
;
4693 v
->samplers_used
|= 1u << idx
;
4695 debug_assert(idx
< (int)ARRAY_SIZE(v
->sampler_types
));
4696 v
->sampler_types
[idx
] = inst
->tex_type
;
4697 v
->sampler_targets
[idx
] =
4698 st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
4700 if (inst
->tex_shadow
) {
4701 prog
->ShadowSamplers
|= 1 << (inst
->resource
.index
+ i
);
4706 if (inst
->tex_target
== TEXTURE_EXTERNAL_INDEX
)
4707 prog
->ExternalSamplersUsed
|= 1 << inst
->resource
.index
;
4709 if (inst
->resource
.file
!= PROGRAM_UNDEFINED
&& (
4710 is_resource_instruction(inst
->op
) ||
4711 inst
->op
== TGSI_OPCODE_STORE
)) {
4712 if (inst
->resource
.file
== PROGRAM_MEMORY
) {
4713 v
->use_shared_memory
= true;
4714 } else if (inst
->resource
.file
== PROGRAM_IMAGE
) {
4715 for (int i
= 0; i
< inst
->sampler_array_size
; i
++) {
4716 unsigned idx
= inst
->sampler_base
+ i
;
4717 v
->images_used
|= 1 << idx
;
4718 v
->image_targets
[idx
] =
4719 st_translate_texture_target(inst
->tex_target
, false);
4720 v
->image_formats
[idx
] = inst
->image_format
;
4725 prog
->SamplersUsed
= v
->samplers_used
;
4727 if (v
->shader_program
!= NULL
)
4728 _mesa_update_shader_textures_used(v
->shader_program
, prog
);
4732 * Returns the mask of channels (bitmask of WRITEMASK_X,Y,Z,W) which
4733 * are read from the given src in this instruction
4736 get_src_arg_mask(st_dst_reg dst
, st_src_reg src
)
4738 int read_mask
= 0, comp
;
4740 /* Now, given the src swizzle and the written channels, find which
4741 * components are actually read
4743 for (comp
= 0; comp
< 4; ++comp
) {
4744 const unsigned coord
= GET_SWZ(src
.swizzle
, comp
);
4746 if (dst
.writemask
& (1 << comp
) && coord
<= SWIZZLE_W
)
4747 read_mask
|= 1 << coord
;
4754 * This pass replaces CMP T0, T1 T2 T0 with MOV T0, T2 when the CMP
4755 * instruction is the first instruction to write to register T0. There are
4756 * several lowering passes done in GLSL IR (e.g. branches and
4757 * relative addressing) that create a large number of conditional assignments
4758 * that ir_to_mesa converts to CMP instructions like the one mentioned above.
4760 * Here is why this conversion is safe:
4761 * CMP T0, T1 T2 T0 can be expanded to:
4767 * If (T1 < 0.0) evaluates to true then our replacement MOV T0, T2 is the same
4768 * as the original program. If (T1 < 0.0) evaluates to false, executing
4769 * MOV T0, T0 will store a garbage value in T0 since T0 is uninitialized.
4770 * Therefore, it doesn't matter that we are replacing MOV T0, T0 with MOV T0, T2
4771 * because any instruction that was going to read from T0 after this was going
4772 * to read a garbage value anyway.
4775 glsl_to_tgsi_visitor::simplify_cmp(void)
4777 int tempWritesSize
= 0;
4778 unsigned *tempWrites
= NULL
;
4779 unsigned outputWrites
[VARYING_SLOT_TESS_MAX
];
4781 memset(outputWrites
, 0, sizeof(outputWrites
));
4783 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4784 unsigned prevWriteMask
= 0;
4786 /* Give up if we encounter relative addressing or flow control. */
4787 if (inst
->dst
[0].reladdr
|| inst
->dst
[0].reladdr2
||
4788 inst
->dst
[1].reladdr
|| inst
->dst
[1].reladdr2
||
4789 inst
->info
->is_branch
||
4790 inst
->op
== TGSI_OPCODE_CONT
||
4791 inst
->op
== TGSI_OPCODE_END
||
4792 inst
->op
== TGSI_OPCODE_RET
) {
4796 if (inst
->dst
[0].file
== PROGRAM_OUTPUT
) {
4797 assert(inst
->dst
[0].index
< (signed)ARRAY_SIZE(outputWrites
));
4798 prevWriteMask
= outputWrites
[inst
->dst
[0].index
];
4799 outputWrites
[inst
->dst
[0].index
] |= inst
->dst
[0].writemask
;
4800 } else if (inst
->dst
[0].file
== PROGRAM_TEMPORARY
) {
4801 if (inst
->dst
[0].index
>= tempWritesSize
) {
4802 const int inc
= 4096;
4804 tempWrites
= (unsigned*)
4806 (tempWritesSize
+ inc
) * sizeof(unsigned));
4810 memset(tempWrites
+ tempWritesSize
, 0, inc
* sizeof(unsigned));
4811 tempWritesSize
+= inc
;
4814 prevWriteMask
= tempWrites
[inst
->dst
[0].index
];
4815 tempWrites
[inst
->dst
[0].index
] |= inst
->dst
[0].writemask
;
4819 /* For a CMP to be considered a conditional write, the destination
4820 * register and source register two must be the same. */
4821 if (inst
->op
== TGSI_OPCODE_CMP
4822 && !(inst
->dst
[0].writemask
& prevWriteMask
)
4823 && inst
->src
[2].file
== inst
->dst
[0].file
4824 && inst
->src
[2].index
== inst
->dst
[0].index
4825 && inst
->dst
[0].writemask
== get_src_arg_mask(inst
->dst
[0], inst
->src
[2])) {
4827 inst
->op
= TGSI_OPCODE_MOV
;
4828 inst
->info
= tgsi_get_opcode_info(inst
->op
);
4829 inst
->src
[0] = inst
->src
[1];
4836 /* Replaces all references to a temporary register index with another index. */
4838 glsl_to_tgsi_visitor::rename_temp_registers(struct rename_reg_pair
*renames
)
4840 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4842 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
4843 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
) {
4844 int old_idx
= inst
->src
[j
].index
;
4845 if (renames
[old_idx
].valid
)
4846 inst
->src
[j
].index
= renames
[old_idx
].new_reg
;
4850 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
4851 if (inst
->tex_offsets
[j
].file
== PROGRAM_TEMPORARY
) {
4852 int old_idx
= inst
->tex_offsets
[j
].index
;
4853 if (renames
[old_idx
].valid
)
4854 inst
->tex_offsets
[j
].index
= renames
[old_idx
].new_reg
;
4858 if (inst
->resource
.file
== PROGRAM_TEMPORARY
) {
4859 int old_idx
= inst
->resource
.index
;
4860 if (renames
[old_idx
].valid
)
4861 inst
->resource
.index
= renames
[old_idx
].new_reg
;
4864 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4865 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
) {
4866 int old_idx
= inst
->dst
[j
].index
;
4867 if (renames
[old_idx
].valid
)
4868 inst
->dst
[j
].index
= renames
[old_idx
].new_reg
;}
4874 glsl_to_tgsi_visitor::get_first_temp_write(int *first_writes
)
4876 int depth
= 0; /* loop depth */
4877 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
4880 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4881 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4882 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
) {
4883 if (first_writes
[inst
->dst
[j
].index
] == -1)
4884 first_writes
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4888 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
4891 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
4901 glsl_to_tgsi_visitor::get_first_temp_read(int *first_reads
)
4903 int depth
= 0; /* loop depth */
4904 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
4907 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4908 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
4909 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
) {
4910 if (first_reads
[inst
->src
[j
].index
] == -1)
4911 first_reads
[inst
->src
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4914 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
4915 if (inst
->tex_offsets
[j
].file
== PROGRAM_TEMPORARY
) {
4916 if (first_reads
[inst
->tex_offsets
[j
].index
] == -1)
4917 first_reads
[inst
->tex_offsets
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4920 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
4923 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
4933 glsl_to_tgsi_visitor::get_last_temp_read_first_temp_write(int *last_reads
, int *first_writes
)
4935 int depth
= 0; /* loop depth */
4936 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
4939 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4940 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
4941 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
)
4942 last_reads
[inst
->src
[j
].index
] = (depth
== 0) ? i
: -2;
4944 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4945 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
) {
4946 if (first_writes
[inst
->dst
[j
].index
] == -1)
4947 first_writes
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4948 last_reads
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: -2;
4951 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
4952 if (inst
->tex_offsets
[j
].file
== PROGRAM_TEMPORARY
)
4953 last_reads
[inst
->tex_offsets
[j
].index
] = (depth
== 0) ? i
: -2;
4955 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
4958 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
4961 for (k
= 0; k
< this->next_temp
; k
++) {
4962 if (last_reads
[k
] == -2) {
4974 glsl_to_tgsi_visitor::get_last_temp_write(int *last_writes
)
4976 int depth
= 0; /* loop depth */
4980 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4981 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4982 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
)
4983 last_writes
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: -2;
4986 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
4988 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
4990 for (k
= 0; k
< this->next_temp
; k
++) {
4991 if (last_writes
[k
] == -2) {
5002 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
5003 * channels for copy propagation and updates following instructions to
5004 * use the original versions.
5006 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
5007 * will occur. As an example, a TXP production before this pass:
5009 * 0: MOV TEMP[1], INPUT[4].xyyy;
5010 * 1: MOV TEMP[1].w, INPUT[4].wwww;
5011 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
5015 * 0: MOV TEMP[1], INPUT[4].xyyy;
5016 * 1: MOV TEMP[1].w, INPUT[4].wwww;
5017 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5019 * which allows for dead code elimination on TEMP[1]'s writes.
5022 glsl_to_tgsi_visitor::copy_propagate(void)
5024 glsl_to_tgsi_instruction
**acp
= rzalloc_array(mem_ctx
,
5025 glsl_to_tgsi_instruction
*,
5026 this->next_temp
* 4);
5027 int *acp_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
5030 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5031 assert(inst
->dst
[0].file
!= PROGRAM_TEMPORARY
5032 || inst
->dst
[0].index
< this->next_temp
);
5034 /* First, do any copy propagation possible into the src regs. */
5035 for (int r
= 0; r
< 3; r
++) {
5036 glsl_to_tgsi_instruction
*first
= NULL
;
5038 int acp_base
= inst
->src
[r
].index
* 4;
5040 if (inst
->src
[r
].file
!= PROGRAM_TEMPORARY
||
5041 inst
->src
[r
].reladdr
||
5042 inst
->src
[r
].reladdr2
)
5045 /* See if we can find entries in the ACP consisting of MOVs
5046 * from the same src register for all the swizzled channels
5047 * of this src register reference.
5049 for (int i
= 0; i
< 4; i
++) {
5050 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
5051 glsl_to_tgsi_instruction
*copy_chan
= acp
[acp_base
+ src_chan
];
5058 assert(acp_level
[acp_base
+ src_chan
] <= level
);
5063 if (first
->src
[0].file
!= copy_chan
->src
[0].file
||
5064 first
->src
[0].index
!= copy_chan
->src
[0].index
||
5065 first
->src
[0].double_reg2
!= copy_chan
->src
[0].double_reg2
||
5066 first
->src
[0].index2D
!= copy_chan
->src
[0].index2D
) {
5074 /* We've now validated that we can copy-propagate to
5075 * replace this src register reference. Do it.
5077 inst
->src
[r
].file
= first
->src
[0].file
;
5078 inst
->src
[r
].index
= first
->src
[0].index
;
5079 inst
->src
[r
].index2D
= first
->src
[0].index2D
;
5080 inst
->src
[r
].has_index2
= first
->src
[0].has_index2
;
5081 inst
->src
[r
].double_reg2
= first
->src
[0].double_reg2
;
5082 inst
->src
[r
].array_id
= first
->src
[0].array_id
;
5085 for (int i
= 0; i
< 4; i
++) {
5086 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
5087 glsl_to_tgsi_instruction
*copy_inst
= acp
[acp_base
+ src_chan
];
5088 swizzle
|= (GET_SWZ(copy_inst
->src
[0].swizzle
, src_chan
) << (3 * i
));
5090 inst
->src
[r
].swizzle
= swizzle
;
5095 case TGSI_OPCODE_BGNLOOP
:
5096 case TGSI_OPCODE_ENDLOOP
:
5097 /* End of a basic block, clear the ACP entirely. */
5098 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
5101 case TGSI_OPCODE_IF
:
5102 case TGSI_OPCODE_UIF
:
5106 case TGSI_OPCODE_ENDIF
:
5107 case TGSI_OPCODE_ELSE
:
5108 /* Clear all channels written inside the block from the ACP, but
5109 * leaving those that were not touched.
5111 for (int r
= 0; r
< this->next_temp
; r
++) {
5112 for (int c
= 0; c
< 4; c
++) {
5113 if (!acp
[4 * r
+ c
])
5116 if (acp_level
[4 * r
+ c
] >= level
)
5117 acp
[4 * r
+ c
] = NULL
;
5120 if (inst
->op
== TGSI_OPCODE_ENDIF
)
5125 /* Continuing the block, clear any written channels from
5128 for (int d
= 0; d
< 2; d
++) {
5129 if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
&& inst
->dst
[d
].reladdr
) {
5130 /* Any temporary might be written, so no copy propagation
5131 * across this instruction.
5133 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
5134 } else if (inst
->dst
[d
].file
== PROGRAM_OUTPUT
&&
5135 inst
->dst
[d
].reladdr
) {
5136 /* Any output might be written, so no copy propagation
5137 * from outputs across this instruction.
5139 for (int r
= 0; r
< this->next_temp
; r
++) {
5140 for (int c
= 0; c
< 4; c
++) {
5141 if (!acp
[4 * r
+ c
])
5144 if (acp
[4 * r
+ c
]->src
[0].file
== PROGRAM_OUTPUT
)
5145 acp
[4 * r
+ c
] = NULL
;
5148 } else if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
||
5149 inst
->dst
[d
].file
== PROGRAM_OUTPUT
) {
5150 /* Clear where it's used as dst. */
5151 if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
) {
5152 for (int c
= 0; c
< 4; c
++) {
5153 if (inst
->dst
[d
].writemask
& (1 << c
))
5154 acp
[4 * inst
->dst
[d
].index
+ c
] = NULL
;
5158 /* Clear where it's used as src. */
5159 for (int r
= 0; r
< this->next_temp
; r
++) {
5160 for (int c
= 0; c
< 4; c
++) {
5161 if (!acp
[4 * r
+ c
])
5164 int src_chan
= GET_SWZ(acp
[4 * r
+ c
]->src
[0].swizzle
, c
);
5166 if (acp
[4 * r
+ c
]->src
[0].file
== inst
->dst
[d
].file
&&
5167 acp
[4 * r
+ c
]->src
[0].index
== inst
->dst
[d
].index
&&
5168 inst
->dst
[d
].writemask
& (1 << src_chan
)) {
5169 acp
[4 * r
+ c
] = NULL
;
5178 /* If this is a copy, add it to the ACP. */
5179 if (inst
->op
== TGSI_OPCODE_MOV
&&
5180 inst
->dst
[0].file
== PROGRAM_TEMPORARY
&&
5181 !(inst
->dst
[0].file
== inst
->src
[0].file
&&
5182 inst
->dst
[0].index
== inst
->src
[0].index
) &&
5183 !inst
->dst
[0].reladdr
&&
5184 !inst
->dst
[0].reladdr2
&&
5186 inst
->src
[0].file
!= PROGRAM_ARRAY
&&
5187 !inst
->src
[0].reladdr
&&
5188 !inst
->src
[0].reladdr2
&&
5189 !inst
->src
[0].negate
&&
5190 !inst
->src
[0].abs
) {
5191 for (int i
= 0; i
< 4; i
++) {
5192 if (inst
->dst
[0].writemask
& (1 << i
)) {
5193 acp
[4 * inst
->dst
[0].index
+ i
] = inst
;
5194 acp_level
[4 * inst
->dst
[0].index
+ i
] = level
;
5200 ralloc_free(acp_level
);
5205 * On a basic block basis, tracks available PROGRAM_TEMPORARY registers for dead
5208 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
5209 * will occur. As an example, a TXP production after copy propagation but
5212 * 0: MOV TEMP[1], INPUT[4].xyyy;
5213 * 1: MOV TEMP[1].w, INPUT[4].wwww;
5214 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5216 * and after this pass:
5218 * 0: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5221 glsl_to_tgsi_visitor::eliminate_dead_code(void)
5223 glsl_to_tgsi_instruction
**writes
= rzalloc_array(mem_ctx
,
5224 glsl_to_tgsi_instruction
*,
5225 this->next_temp
* 4);
5226 int *write_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
5230 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5231 assert(inst
->dst
[0].file
!= PROGRAM_TEMPORARY
5232 || inst
->dst
[0].index
< this->next_temp
);
5235 case TGSI_OPCODE_BGNLOOP
:
5236 case TGSI_OPCODE_ENDLOOP
:
5237 case TGSI_OPCODE_CONT
:
5238 case TGSI_OPCODE_BRK
:
5239 /* End of a basic block, clear the write array entirely.
5241 * This keeps us from killing dead code when the writes are
5242 * on either side of a loop, even when the register isn't touched
5243 * inside the loop. However, glsl_to_tgsi_visitor doesn't seem to emit
5244 * dead code of this type, so it shouldn't make a difference as long as
5245 * the dead code elimination pass in the GLSL compiler does its job.
5247 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5250 case TGSI_OPCODE_ENDIF
:
5251 case TGSI_OPCODE_ELSE
:
5252 /* Promote the recorded level of all channels written inside the
5253 * preceding if or else block to the level above the if/else block.
5255 for (int r
= 0; r
< this->next_temp
; r
++) {
5256 for (int c
= 0; c
< 4; c
++) {
5257 if (!writes
[4 * r
+ c
])
5260 if (write_level
[4 * r
+ c
] == level
)
5261 write_level
[4 * r
+ c
] = level
-1;
5264 if(inst
->op
== TGSI_OPCODE_ENDIF
)
5268 case TGSI_OPCODE_IF
:
5269 case TGSI_OPCODE_UIF
:
5271 /* fallthrough to default case to mark the condition as read */
5273 /* Continuing the block, clear any channels from the write array that
5274 * are read by this instruction.
5276 for (unsigned i
= 0; i
< ARRAY_SIZE(inst
->src
); i
++) {
5277 if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
&& inst
->src
[i
].reladdr
){
5278 /* Any temporary might be read, so no dead code elimination
5279 * across this instruction.
5281 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5282 } else if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
) {
5283 /* Clear where it's used as src. */
5284 int src_chans
= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 0);
5285 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 1);
5286 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 2);
5287 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 3);
5289 for (int c
= 0; c
< 4; c
++) {
5290 if (src_chans
& (1 << c
))
5291 writes
[4 * inst
->src
[i
].index
+ c
] = NULL
;
5295 for (unsigned i
= 0; i
< inst
->tex_offset_num_offset
; i
++) {
5296 if (inst
->tex_offsets
[i
].file
== PROGRAM_TEMPORARY
&& inst
->tex_offsets
[i
].reladdr
){
5297 /* Any temporary might be read, so no dead code elimination
5298 * across this instruction.
5300 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5301 } else if (inst
->tex_offsets
[i
].file
== PROGRAM_TEMPORARY
) {
5302 /* Clear where it's used as src. */
5303 int src_chans
= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 0);
5304 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 1);
5305 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 2);
5306 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 3);
5308 for (int c
= 0; c
< 4; c
++) {
5309 if (src_chans
& (1 << c
))
5310 writes
[4 * inst
->tex_offsets
[i
].index
+ c
] = NULL
;
5315 if (inst
->resource
.file
== PROGRAM_TEMPORARY
) {
5318 src_chans
= 1 << GET_SWZ(inst
->resource
.swizzle
, 0);
5319 src_chans
|= 1 << GET_SWZ(inst
->resource
.swizzle
, 1);
5320 src_chans
|= 1 << GET_SWZ(inst
->resource
.swizzle
, 2);
5321 src_chans
|= 1 << GET_SWZ(inst
->resource
.swizzle
, 3);
5323 for (int c
= 0; c
< 4; c
++) {
5324 if (src_chans
& (1 << c
))
5325 writes
[4 * inst
->resource
.index
+ c
] = NULL
;
5332 /* If this instruction writes to a temporary, add it to the write array.
5333 * If there is already an instruction in the write array for one or more
5334 * of the channels, flag that channel write as dead.
5336 for (unsigned i
= 0; i
< ARRAY_SIZE(inst
->dst
); i
++) {
5337 if (inst
->dst
[i
].file
== PROGRAM_TEMPORARY
&&
5338 !inst
->dst
[i
].reladdr
) {
5339 for (int c
= 0; c
< 4; c
++) {
5340 if (inst
->dst
[i
].writemask
& (1 << c
)) {
5341 if (writes
[4 * inst
->dst
[i
].index
+ c
]) {
5342 if (write_level
[4 * inst
->dst
[i
].index
+ c
] < level
)
5345 writes
[4 * inst
->dst
[i
].index
+ c
]->dead_mask
|= (1 << c
);
5347 writes
[4 * inst
->dst
[i
].index
+ c
] = inst
;
5348 write_level
[4 * inst
->dst
[i
].index
+ c
] = level
;
5355 /* Anything still in the write array at this point is dead code. */
5356 for (int r
= 0; r
< this->next_temp
; r
++) {
5357 for (int c
= 0; c
< 4; c
++) {
5358 glsl_to_tgsi_instruction
*inst
= writes
[4 * r
+ c
];
5360 inst
->dead_mask
|= (1 << c
);
5364 /* Now actually remove the instructions that are completely dead and update
5365 * the writemask of other instructions with dead channels.
5367 foreach_in_list_safe(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5368 if (!inst
->dead_mask
|| !inst
->dst
[0].writemask
)
5370 /* No amount of dead masks should remove memory stores */
5371 if (inst
->info
->is_store
)
5374 if ((inst
->dst
[0].writemask
& ~inst
->dead_mask
) == 0) {
5379 if (glsl_base_type_is_64bit(inst
->dst
[0].type
)) {
5380 if (inst
->dead_mask
== WRITEMASK_XY
||
5381 inst
->dead_mask
== WRITEMASK_ZW
)
5382 inst
->dst
[0].writemask
&= ~(inst
->dead_mask
);
5384 inst
->dst
[0].writemask
&= ~(inst
->dead_mask
);
5388 ralloc_free(write_level
);
5389 ralloc_free(writes
);
5394 /* merge DFRACEXP instructions into one. */
5396 glsl_to_tgsi_visitor::merge_two_dsts(void)
5398 foreach_in_list_safe(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5399 glsl_to_tgsi_instruction
*inst2
;
5401 if (num_inst_dst_regs(inst
) != 2)
5404 if (inst
->dst
[0].file
!= PROGRAM_UNDEFINED
&&
5405 inst
->dst
[1].file
!= PROGRAM_UNDEFINED
)
5408 inst2
= (glsl_to_tgsi_instruction
*) inst
->next
;
5411 if (inst
->src
[0].file
== inst2
->src
[0].file
&&
5412 inst
->src
[0].index
== inst2
->src
[0].index
&&
5413 inst
->src
[0].type
== inst2
->src
[0].type
&&
5414 inst
->src
[0].swizzle
== inst2
->src
[0].swizzle
)
5416 inst2
= (glsl_to_tgsi_instruction
*) inst2
->next
;
5422 if (inst
->dst
[0].file
== PROGRAM_UNDEFINED
) {
5424 inst
->dst
[0] = inst2
->dst
[0];
5425 } else if (inst
->dst
[1].file
== PROGRAM_UNDEFINED
) {
5426 inst
->dst
[1] = inst2
->dst
[1];
5437 /* Merges temporary registers together where possible to reduce the number of
5438 * registers needed to run a program.
5440 * Produces optimal code only after copy propagation and dead code elimination
5443 glsl_to_tgsi_visitor::merge_registers(void)
5445 int *last_reads
= ralloc_array(mem_ctx
, int, this->next_temp
);
5446 int *first_writes
= ralloc_array(mem_ctx
, int, this->next_temp
);
5447 struct rename_reg_pair
*renames
= rzalloc_array(mem_ctx
, struct rename_reg_pair
, this->next_temp
);
5450 /* Read the indices of the last read and first write to each temp register
5451 * into an array so that we don't have to traverse the instruction list as
5453 for (i
= 0; i
< this->next_temp
; i
++) {
5455 first_writes
[i
] = -1;
5457 get_last_temp_read_first_temp_write(last_reads
, first_writes
);
5459 /* Start looking for registers with non-overlapping usages that can be
5460 * merged together. */
5461 for (i
= 0; i
< this->next_temp
; i
++) {
5462 /* Don't touch unused registers. */
5463 if (last_reads
[i
] < 0 || first_writes
[i
] < 0) continue;
5465 for (j
= 0; j
< this->next_temp
; j
++) {
5466 /* Don't touch unused registers. */
5467 if (last_reads
[j
] < 0 || first_writes
[j
] < 0) continue;
5469 /* We can merge the two registers if the first write to j is after or
5470 * in the same instruction as the last read from i. Note that the
5471 * register at index i will always be used earlier or at the same time
5472 * as the register at index j. */
5473 if (first_writes
[i
] <= first_writes
[j
] &&
5474 last_reads
[i
] <= first_writes
[j
]) {
5475 renames
[j
].new_reg
= i
;
5476 renames
[j
].valid
= true;
5478 /* Update the first_writes and last_reads arrays with the new
5479 * values for the merged register index, and mark the newly unused
5480 * register index as such. */
5481 assert(last_reads
[j
] >= last_reads
[i
]);
5482 last_reads
[i
] = last_reads
[j
];
5483 first_writes
[j
] = -1;
5489 rename_temp_registers(renames
);
5490 ralloc_free(renames
);
5491 ralloc_free(last_reads
);
5492 ralloc_free(first_writes
);
5495 /* Reassign indices to temporary registers by reusing unused indices created
5496 * by optimization passes. */
5498 glsl_to_tgsi_visitor::renumber_registers(void)
5502 int *first_writes
= ralloc_array(mem_ctx
, int, this->next_temp
);
5503 struct rename_reg_pair
*renames
= rzalloc_array(mem_ctx
, struct rename_reg_pair
, this->next_temp
);
5505 for (i
= 0; i
< this->next_temp
; i
++) {
5506 first_writes
[i
] = -1;
5508 get_first_temp_write(first_writes
);
5510 for (i
= 0; i
< this->next_temp
; i
++) {
5511 if (first_writes
[i
] < 0) continue;
5512 if (i
!= new_index
) {
5513 renames
[i
].new_reg
= new_index
;
5514 renames
[i
].valid
= true;
5519 rename_temp_registers(renames
);
5520 this->next_temp
= new_index
;
5521 ralloc_free(renames
);
5522 ralloc_free(first_writes
);
5525 /* ------------------------- TGSI conversion stuff -------------------------- */
5528 * Intermediate state used during shader translation.
5530 struct st_translate
{
5531 struct ureg_program
*ureg
;
5533 unsigned temps_size
;
5534 struct ureg_dst
*temps
;
5536 struct ureg_dst
*arrays
;
5537 unsigned num_temp_arrays
;
5538 struct ureg_src
*constants
;
5540 struct ureg_src
*immediates
;
5542 struct ureg_dst outputs
[PIPE_MAX_SHADER_OUTPUTS
];
5543 struct ureg_src inputs
[PIPE_MAX_SHADER_INPUTS
];
5544 struct ureg_dst address
[3];
5545 struct ureg_src samplers
[PIPE_MAX_SAMPLERS
];
5546 struct ureg_src buffers
[PIPE_MAX_SHADER_BUFFERS
];
5547 struct ureg_src images
[PIPE_MAX_SHADER_IMAGES
];
5548 struct ureg_src systemValues
[SYSTEM_VALUE_MAX
];
5549 struct ureg_src shared_memory
;
5550 unsigned *array_sizes
;
5551 struct inout_decl
*input_decls
;
5552 unsigned num_input_decls
;
5553 struct inout_decl
*output_decls
;
5554 unsigned num_output_decls
;
5556 const ubyte
*inputMapping
;
5557 const ubyte
*outputMapping
;
5559 unsigned procType
; /**< PIPE_SHADER_VERTEX/FRAGMENT */
5562 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
5564 _mesa_sysval_to_semantic(unsigned sysval
)
5568 case SYSTEM_VALUE_VERTEX_ID
:
5569 return TGSI_SEMANTIC_VERTEXID
;
5570 case SYSTEM_VALUE_INSTANCE_ID
:
5571 return TGSI_SEMANTIC_INSTANCEID
;
5572 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
5573 return TGSI_SEMANTIC_VERTEXID_NOBASE
;
5574 case SYSTEM_VALUE_BASE_VERTEX
:
5575 return TGSI_SEMANTIC_BASEVERTEX
;
5576 case SYSTEM_VALUE_BASE_INSTANCE
:
5577 return TGSI_SEMANTIC_BASEINSTANCE
;
5578 case SYSTEM_VALUE_DRAW_ID
:
5579 return TGSI_SEMANTIC_DRAWID
;
5581 /* Geometry shader */
5582 case SYSTEM_VALUE_INVOCATION_ID
:
5583 return TGSI_SEMANTIC_INVOCATIONID
;
5585 /* Fragment shader */
5586 case SYSTEM_VALUE_FRAG_COORD
:
5587 return TGSI_SEMANTIC_POSITION
;
5588 case SYSTEM_VALUE_FRONT_FACE
:
5589 return TGSI_SEMANTIC_FACE
;
5590 case SYSTEM_VALUE_SAMPLE_ID
:
5591 return TGSI_SEMANTIC_SAMPLEID
;
5592 case SYSTEM_VALUE_SAMPLE_POS
:
5593 return TGSI_SEMANTIC_SAMPLEPOS
;
5594 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
5595 return TGSI_SEMANTIC_SAMPLEMASK
;
5596 case SYSTEM_VALUE_HELPER_INVOCATION
:
5597 return TGSI_SEMANTIC_HELPER_INVOCATION
;
5599 /* Tessellation shader */
5600 case SYSTEM_VALUE_TESS_COORD
:
5601 return TGSI_SEMANTIC_TESSCOORD
;
5602 case SYSTEM_VALUE_VERTICES_IN
:
5603 return TGSI_SEMANTIC_VERTICESIN
;
5604 case SYSTEM_VALUE_PRIMITIVE_ID
:
5605 return TGSI_SEMANTIC_PRIMID
;
5606 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
5607 return TGSI_SEMANTIC_TESSOUTER
;
5608 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
5609 return TGSI_SEMANTIC_TESSINNER
;
5611 /* Compute shader */
5612 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
5613 return TGSI_SEMANTIC_THREAD_ID
;
5614 case SYSTEM_VALUE_WORK_GROUP_ID
:
5615 return TGSI_SEMANTIC_BLOCK_ID
;
5616 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
5617 return TGSI_SEMANTIC_GRID_SIZE
;
5618 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
5619 return TGSI_SEMANTIC_BLOCK_SIZE
;
5621 /* ARB_shader_ballot */
5622 case SYSTEM_VALUE_SUBGROUP_SIZE
:
5623 return TGSI_SEMANTIC_SUBGROUP_SIZE
;
5624 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
5625 return TGSI_SEMANTIC_SUBGROUP_INVOCATION
;
5626 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
5627 return TGSI_SEMANTIC_SUBGROUP_EQ_MASK
;
5628 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
5629 return TGSI_SEMANTIC_SUBGROUP_GE_MASK
;
5630 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
5631 return TGSI_SEMANTIC_SUBGROUP_GT_MASK
;
5632 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
5633 return TGSI_SEMANTIC_SUBGROUP_LE_MASK
;
5634 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
5635 return TGSI_SEMANTIC_SUBGROUP_LT_MASK
;
5638 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
5639 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
5640 case SYSTEM_VALUE_VERTEX_CNT
:
5642 assert(!"Unexpected SYSTEM_VALUE_ enum");
5643 return TGSI_SEMANTIC_COUNT
;
5648 * Map a glsl_to_tgsi constant/immediate to a TGSI immediate.
5650 static struct ureg_src
5651 emit_immediate(struct st_translate
*t
,
5652 gl_constant_value values
[4],
5655 struct ureg_program
*ureg
= t
->ureg
;
5660 return ureg_DECL_immediate(ureg
, &values
[0].f
, size
);
5662 return ureg_DECL_immediate_f64(ureg
, (double *)&values
[0].f
, size
);
5664 return ureg_DECL_immediate_int64(ureg
, (int64_t *)&values
[0].f
, size
);
5665 case GL_UNSIGNED_INT64_ARB
:
5666 return ureg_DECL_immediate_uint64(ureg
, (uint64_t *)&values
[0].f
, size
);
5668 return ureg_DECL_immediate_int(ureg
, &values
[0].i
, size
);
5669 case GL_UNSIGNED_INT
:
5671 return ureg_DECL_immediate_uint(ureg
, &values
[0].u
, size
);
5673 assert(!"should not get here - type must be float, int, uint, or bool");
5674 return ureg_src_undef();
5679 * Map a glsl_to_tgsi dst register to a TGSI ureg_dst register.
5681 static struct ureg_dst
5682 dst_register(struct st_translate
*t
, gl_register_file file
, unsigned index
,
5688 case PROGRAM_UNDEFINED
:
5689 return ureg_dst_undef();
5691 case PROGRAM_TEMPORARY
:
5692 /* Allocate space for temporaries on demand. */
5693 if (index
>= t
->temps_size
) {
5694 const int inc
= align(index
- t
->temps_size
+ 1, 4096);
5696 t
->temps
= (struct ureg_dst
*)
5698 (t
->temps_size
+ inc
) * sizeof(struct ureg_dst
));
5700 return ureg_dst_undef();
5702 memset(t
->temps
+ t
->temps_size
, 0, inc
* sizeof(struct ureg_dst
));
5703 t
->temps_size
+= inc
;
5706 if (ureg_dst_is_undef(t
->temps
[index
]))
5707 t
->temps
[index
] = ureg_DECL_local_temporary(t
->ureg
);
5709 return t
->temps
[index
];
5712 assert(array_id
&& array_id
<= t
->num_temp_arrays
);
5713 array
= array_id
- 1;
5715 if (ureg_dst_is_undef(t
->arrays
[array
]))
5716 t
->arrays
[array
] = ureg_DECL_array_temporary(
5717 t
->ureg
, t
->array_sizes
[array
], TRUE
);
5719 return ureg_dst_array_offset(t
->arrays
[array
], index
);
5721 case PROGRAM_OUTPUT
:
5723 if (t
->procType
== PIPE_SHADER_FRAGMENT
)
5724 assert(index
< 2 * FRAG_RESULT_MAX
);
5725 else if (t
->procType
== PIPE_SHADER_TESS_CTRL
||
5726 t
->procType
== PIPE_SHADER_TESS_EVAL
)
5727 assert(index
< VARYING_SLOT_TESS_MAX
);
5729 assert(index
< VARYING_SLOT_MAX
);
5731 assert(t
->outputMapping
[index
] < ARRAY_SIZE(t
->outputs
));
5732 assert(t
->outputs
[t
->outputMapping
[index
]].File
!= TGSI_FILE_NULL
);
5733 return t
->outputs
[t
->outputMapping
[index
]];
5736 struct inout_decl
*decl
= find_inout_array(t
->output_decls
, t
->num_output_decls
, array_id
);
5737 unsigned mesa_index
= decl
->mesa_index
;
5738 int slot
= t
->outputMapping
[mesa_index
];
5740 assert(slot
!= -1 && t
->outputs
[slot
].File
== TGSI_FILE_OUTPUT
);
5742 struct ureg_dst dst
= t
->outputs
[slot
];
5743 dst
.ArrayID
= array_id
;
5744 return ureg_dst_array_offset(dst
, index
- mesa_index
);
5747 case PROGRAM_ADDRESS
:
5748 return t
->address
[index
];
5751 assert(!"unknown dst register file");
5752 return ureg_dst_undef();
5757 * Map a glsl_to_tgsi src register to a TGSI ureg_src register.
5759 static struct ureg_src
5760 src_register(struct st_translate
*t
, const st_src_reg
*reg
)
5762 int index
= reg
->index
;
5763 int double_reg2
= reg
->double_reg2
? 1 : 0;
5766 case PROGRAM_UNDEFINED
:
5767 return ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
5769 case PROGRAM_TEMPORARY
:
5771 return ureg_src(dst_register(t
, reg
->file
, reg
->index
, reg
->array_id
));
5773 case PROGRAM_OUTPUT
: {
5774 struct ureg_dst dst
= dst_register(t
, reg
->file
, reg
->index
, reg
->array_id
);
5775 assert(dst
.WriteMask
!= 0);
5776 unsigned shift
= ffs(dst
.WriteMask
) - 1;
5777 return ureg_swizzle(ureg_src(dst
),
5781 MIN2(shift
+ 3, 3));
5784 case PROGRAM_UNIFORM
:
5785 assert(reg
->index
>= 0);
5786 return reg
->index
< t
->num_constants
?
5787 t
->constants
[reg
->index
] : ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
5788 case PROGRAM_STATE_VAR
:
5789 case PROGRAM_CONSTANT
: /* ie, immediate */
5790 if (reg
->has_index2
)
5791 return ureg_src_register(TGSI_FILE_CONSTANT
, reg
->index
);
5793 return reg
->index
>= 0 && reg
->index
< t
->num_constants
?
5794 t
->constants
[reg
->index
] : ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
5796 case PROGRAM_IMMEDIATE
:
5797 assert(reg
->index
>= 0 && reg
->index
< t
->num_immediates
);
5798 return t
->immediates
[reg
->index
];
5801 /* GLSL inputs are 64-bit containers, so we have to
5802 * map back to the original index and add the offset after
5804 index
-= double_reg2
;
5805 if (!reg
->array_id
) {
5806 assert(t
->inputMapping
[index
] < ARRAY_SIZE(t
->inputs
));
5807 assert(t
->inputs
[t
->inputMapping
[index
]].File
!= TGSI_FILE_NULL
);
5808 return t
->inputs
[t
->inputMapping
[index
] + double_reg2
];
5811 struct inout_decl
*decl
= find_inout_array(t
->input_decls
, t
->num_input_decls
, reg
->array_id
);
5812 unsigned mesa_index
= decl
->mesa_index
;
5813 int slot
= t
->inputMapping
[mesa_index
];
5815 assert(slot
!= -1 && t
->inputs
[slot
].File
== TGSI_FILE_INPUT
);
5817 struct ureg_src src
= t
->inputs
[slot
];
5818 src
.ArrayID
= reg
->array_id
;
5819 return ureg_src_array_offset(src
, index
+ double_reg2
- mesa_index
);
5822 case PROGRAM_ADDRESS
:
5823 return ureg_src(t
->address
[reg
->index
]);
5825 case PROGRAM_SYSTEM_VALUE
:
5826 assert(reg
->index
< (int) ARRAY_SIZE(t
->systemValues
));
5827 return t
->systemValues
[reg
->index
];
5830 assert(!"unknown src register file");
5831 return ureg_src_undef();
5836 * Create a TGSI ureg_dst register from an st_dst_reg.
5838 static struct ureg_dst
5839 translate_dst(struct st_translate
*t
,
5840 const st_dst_reg
*dst_reg
,
5843 struct ureg_dst dst
= dst_register(t
, dst_reg
->file
, dst_reg
->index
,
5846 if (dst
.File
== TGSI_FILE_NULL
)
5849 dst
= ureg_writemask(dst
, dst_reg
->writemask
);
5852 dst
= ureg_saturate(dst
);
5854 if (dst_reg
->reladdr
!= NULL
) {
5855 assert(dst_reg
->file
!= PROGRAM_TEMPORARY
);
5856 dst
= ureg_dst_indirect(dst
, ureg_src(t
->address
[0]));
5859 if (dst_reg
->has_index2
) {
5860 if (dst_reg
->reladdr2
)
5861 dst
= ureg_dst_dimension_indirect(dst
, ureg_src(t
->address
[1]),
5864 dst
= ureg_dst_dimension(dst
, dst_reg
->index2D
);
5871 * Create a TGSI ureg_src register from an st_src_reg.
5873 static struct ureg_src
5874 translate_src(struct st_translate
*t
, const st_src_reg
*src_reg
)
5876 struct ureg_src src
= src_register(t
, src_reg
);
5878 if (src_reg
->has_index2
) {
5879 /* 2D indexes occur with geometry shader inputs (attrib, vertex)
5880 * and UBO constant buffers (buffer, position).
5882 if (src_reg
->reladdr2
)
5883 src
= ureg_src_dimension_indirect(src
, ureg_src(t
->address
[1]),
5886 src
= ureg_src_dimension(src
, src_reg
->index2D
);
5889 src
= ureg_swizzle(src
,
5890 GET_SWZ(src_reg
->swizzle
, 0) & 0x3,
5891 GET_SWZ(src_reg
->swizzle
, 1) & 0x3,
5892 GET_SWZ(src_reg
->swizzle
, 2) & 0x3,
5893 GET_SWZ(src_reg
->swizzle
, 3) & 0x3);
5896 src
= ureg_abs(src
);
5898 if ((src_reg
->negate
& 0xf) == NEGATE_XYZW
)
5899 src
= ureg_negate(src
);
5901 if (src_reg
->reladdr
!= NULL
) {
5902 assert(src_reg
->file
!= PROGRAM_TEMPORARY
);
5903 src
= ureg_src_indirect(src
, ureg_src(t
->address
[0]));
5909 static struct tgsi_texture_offset
5910 translate_tex_offset(struct st_translate
*t
,
5911 const st_src_reg
*in_offset
)
5913 struct tgsi_texture_offset offset
;
5914 struct ureg_src src
= translate_src(t
, in_offset
);
5916 offset
.File
= src
.File
;
5917 offset
.Index
= src
.Index
;
5918 offset
.SwizzleX
= src
.SwizzleX
;
5919 offset
.SwizzleY
= src
.SwizzleY
;
5920 offset
.SwizzleZ
= src
.SwizzleZ
;
5923 assert(!src
.Indirect
);
5924 assert(!src
.DimIndirect
);
5925 assert(!src
.Dimension
);
5926 assert(!src
.Absolute
); /* those shouldn't be used with integers anyway */
5927 assert(!src
.Negate
);
5933 compile_tgsi_instruction(struct st_translate
*t
,
5934 const glsl_to_tgsi_instruction
*inst
)
5936 struct ureg_program
*ureg
= t
->ureg
;
5938 struct ureg_dst dst
[2];
5939 struct ureg_src src
[4];
5940 struct tgsi_texture_offset texoffsets
[MAX_GLSL_TEXTURE_OFFSET
];
5944 unsigned tex_target
= 0;
5946 num_dst
= num_inst_dst_regs(inst
);
5947 num_src
= num_inst_src_regs(inst
);
5949 for (i
= 0; i
< num_dst
; i
++)
5950 dst
[i
] = translate_dst(t
,
5954 for (i
= 0; i
< num_src
; i
++)
5955 src
[i
] = translate_src(t
, &inst
->src
[i
]);
5958 case TGSI_OPCODE_BGNLOOP
:
5959 case TGSI_OPCODE_ELSE
:
5960 case TGSI_OPCODE_ENDLOOP
:
5961 case TGSI_OPCODE_IF
:
5962 case TGSI_OPCODE_UIF
:
5963 assert(num_dst
== 0);
5964 ureg_insn(ureg
, inst
->op
, NULL
, 0, src
, num_src
, inst
->precise
);
5967 case TGSI_OPCODE_TEX
:
5968 case TGSI_OPCODE_TEX_LZ
:
5969 case TGSI_OPCODE_TXB
:
5970 case TGSI_OPCODE_TXD
:
5971 case TGSI_OPCODE_TXL
:
5972 case TGSI_OPCODE_TXP
:
5973 case TGSI_OPCODE_TXQ
:
5974 case TGSI_OPCODE_TXQS
:
5975 case TGSI_OPCODE_TXF
:
5976 case TGSI_OPCODE_TXF_LZ
:
5977 case TGSI_OPCODE_TEX2
:
5978 case TGSI_OPCODE_TXB2
:
5979 case TGSI_OPCODE_TXL2
:
5980 case TGSI_OPCODE_TG4
:
5981 case TGSI_OPCODE_LODQ
:
5982 if (inst
->resource
.file
== PROGRAM_SAMPLER
) {
5983 src
[num_src
] = t
->samplers
[inst
->resource
.index
];
5985 /* Bindless samplers. */
5986 src
[num_src
] = translate_src(t
, &inst
->resource
);
5988 assert(src
[num_src
].File
!= TGSI_FILE_NULL
);
5989 if (inst
->resource
.reladdr
)
5991 ureg_src_indirect(src
[num_src
], ureg_src(t
->address
[2]));
5993 for (i
= 0; i
< (int)inst
->tex_offset_num_offset
; i
++) {
5994 texoffsets
[i
] = translate_tex_offset(t
, &inst
->tex_offsets
[i
]);
5996 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
6002 st_translate_texture_type(inst
->tex_type
),
6003 texoffsets
, inst
->tex_offset_num_offset
,
6007 case TGSI_OPCODE_RESQ
:
6008 case TGSI_OPCODE_LOAD
:
6009 case TGSI_OPCODE_ATOMUADD
:
6010 case TGSI_OPCODE_ATOMXCHG
:
6011 case TGSI_OPCODE_ATOMCAS
:
6012 case TGSI_OPCODE_ATOMAND
:
6013 case TGSI_OPCODE_ATOMOR
:
6014 case TGSI_OPCODE_ATOMXOR
:
6015 case TGSI_OPCODE_ATOMUMIN
:
6016 case TGSI_OPCODE_ATOMUMAX
:
6017 case TGSI_OPCODE_ATOMIMIN
:
6018 case TGSI_OPCODE_ATOMIMAX
:
6019 for (i
= num_src
- 1; i
>= 0; i
--)
6020 src
[i
+ 1] = src
[i
];
6022 if (inst
->resource
.file
== PROGRAM_MEMORY
) {
6023 src
[0] = t
->shared_memory
;
6024 } else if (inst
->resource
.file
== PROGRAM_BUFFER
) {
6025 src
[0] = t
->buffers
[inst
->resource
.index
];
6027 if (inst
->resource
.file
== PROGRAM_IMAGE
) {
6028 src
[0] = t
->images
[inst
->resource
.index
];
6030 /* Bindless images. */
6031 src
[0] = translate_src(t
, &inst
->resource
);
6033 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
6035 if (inst
->resource
.reladdr
)
6036 src
[0] = ureg_src_indirect(src
[0], ureg_src(t
->address
[2]));
6037 assert(src
[0].File
!= TGSI_FILE_NULL
);
6038 ureg_memory_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
,
6039 inst
->buffer_access
,
6040 tex_target
, inst
->image_format
);
6043 case TGSI_OPCODE_STORE
:
6044 if (inst
->resource
.file
== PROGRAM_MEMORY
) {
6045 dst
[0] = ureg_dst(t
->shared_memory
);
6046 } else if (inst
->resource
.file
== PROGRAM_BUFFER
) {
6047 dst
[0] = ureg_dst(t
->buffers
[inst
->resource
.index
]);
6049 if (inst
->resource
.file
== PROGRAM_IMAGE
) {
6050 dst
[0] = ureg_dst(t
->images
[inst
->resource
.index
]);
6052 /* Bindless images. */
6053 dst
[0] = ureg_dst(translate_src(t
, &inst
->resource
));
6055 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
6057 dst
[0] = ureg_writemask(dst
[0], inst
->dst
[0].writemask
);
6058 if (inst
->resource
.reladdr
)
6059 dst
[0] = ureg_dst_indirect(dst
[0], ureg_src(t
->address
[2]));
6060 assert(dst
[0].File
!= TGSI_FILE_NULL
);
6061 ureg_memory_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
,
6062 inst
->buffer_access
,
6063 tex_target
, inst
->image_format
);
6066 case TGSI_OPCODE_SCS
:
6067 dst
[0] = ureg_writemask(dst
[0], TGSI_WRITEMASK_XY
);
6068 ureg_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
, inst
->precise
);
6075 src
, num_src
, inst
->precise
);
6081 * Emit the TGSI instructions for inverting and adjusting WPOS.
6082 * This code is unavoidable because it also depends on whether
6083 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
6086 emit_wpos_adjustment(struct gl_context
*ctx
,
6087 struct st_translate
*t
,
6088 int wpos_transform_const
,
6090 GLfloat adjX
, GLfloat adjY
[2])
6092 struct ureg_program
*ureg
= t
->ureg
;
6094 assert(wpos_transform_const
>= 0);
6096 /* Fragment program uses fragment position input.
6097 * Need to replace instances of INPUT[WPOS] with temp T
6098 * where T = INPUT[WPOS] is inverted by Y.
6100 struct ureg_src wpostrans
= ureg_DECL_constant(ureg
, wpos_transform_const
);
6101 struct ureg_dst wpos_temp
= ureg_DECL_temporary( ureg
);
6102 struct ureg_src
*wpos
=
6103 ctx
->Const
.GLSLFragCoordIsSysVal
?
6104 &t
->systemValues
[SYSTEM_VALUE_FRAG_COORD
] :
6105 &t
->inputs
[t
->inputMapping
[VARYING_SLOT_POS
]];
6106 struct ureg_src wpos_input
= *wpos
;
6108 /* First, apply the coordinate shift: */
6109 if (adjX
|| adjY
[0] || adjY
[1]) {
6110 if (adjY
[0] != adjY
[1]) {
6111 /* Adjust the y coordinate by adjY[1] or adjY[0] respectively
6112 * depending on whether inversion is actually going to be applied
6113 * or not, which is determined by testing against the inversion
6114 * state variable used below, which will be either +1 or -1.
6116 struct ureg_dst adj_temp
= ureg_DECL_local_temporary(ureg
);
6118 ureg_CMP(ureg
, adj_temp
,
6119 ureg_scalar(wpostrans
, invert
? 2 : 0),
6120 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
),
6121 ureg_imm4f(ureg
, adjX
, adjY
[1], 0.0f
, 0.0f
));
6122 ureg_ADD(ureg
, wpos_temp
, wpos_input
, ureg_src(adj_temp
));
6124 ureg_ADD(ureg
, wpos_temp
, wpos_input
,
6125 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
));
6127 wpos_input
= ureg_src(wpos_temp
);
6129 /* MOV wpos_temp, input[wpos]
6131 ureg_MOV( ureg
, wpos_temp
, wpos_input
);
6134 /* Now the conditional y flip: STATE_FB_WPOS_Y_TRANSFORM.xy/zw will be
6135 * inversion/identity, or the other way around if we're drawing to an FBO.
6138 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
6141 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
6143 ureg_scalar(wpostrans
, 0),
6144 ureg_scalar(wpostrans
, 1));
6146 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
6149 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
6151 ureg_scalar(wpostrans
, 2),
6152 ureg_scalar(wpostrans
, 3));
6155 /* Use wpos_temp as position input from here on:
6157 *wpos
= ureg_src(wpos_temp
);
6162 * Emit fragment position/ooordinate code.
6165 emit_wpos(struct st_context
*st
,
6166 struct st_translate
*t
,
6167 const struct gl_program
*program
,
6168 struct ureg_program
*ureg
,
6169 int wpos_transform_const
)
6171 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
6172 GLfloat adjX
= 0.0f
;
6173 GLfloat adjY
[2] = { 0.0f
, 0.0f
};
6174 boolean invert
= FALSE
;
6176 /* Query the pixel center conventions supported by the pipe driver and set
6177 * adjX, adjY to help out if it cannot handle the requested one internally.
6179 * The bias of the y-coordinate depends on whether y-inversion takes place
6180 * (adjY[1]) or not (adjY[0]), which is in turn dependent on whether we are
6181 * drawing to an FBO (causes additional inversion), and whether the pipe
6182 * driver origin and the requested origin differ (the latter condition is
6183 * stored in the 'invert' variable).
6185 * For height = 100 (i = integer, h = half-integer, l = lower, u = upper):
6187 * center shift only:
6192 * l,i -> u,i: ( 0.0 + 1.0) * -1 + 100 = 99
6193 * l,h -> u,h: ( 0.5 + 0.0) * -1 + 100 = 99.5
6194 * u,i -> l,i: (99.0 + 1.0) * -1 + 100 = 0
6195 * u,h -> l,h: (99.5 + 0.0) * -1 + 100 = 0.5
6197 * inversion and center shift:
6198 * l,i -> u,h: ( 0.0 + 0.5) * -1 + 100 = 99.5
6199 * l,h -> u,i: ( 0.5 + 0.5) * -1 + 100 = 99
6200 * u,i -> l,h: (99.0 + 0.5) * -1 + 100 = 0.5
6201 * u,h -> l,i: (99.5 + 0.5) * -1 + 100 = 0
6203 if (program
->OriginUpperLeft
) {
6204 /* Fragment shader wants origin in upper-left */
6205 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
)) {
6206 /* the driver supports upper-left origin */
6208 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
)) {
6209 /* the driver supports lower-left origin, need to invert Y */
6210 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_ORIGIN
,
6211 TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
6218 /* Fragment shader wants origin in lower-left */
6219 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
))
6220 /* the driver supports lower-left origin */
6221 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_ORIGIN
,
6222 TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
6223 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
))
6224 /* the driver supports upper-left origin, need to invert Y */
6230 if (program
->PixelCenterInteger
) {
6231 /* Fragment shader wants pixel center integer */
6232 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
6233 /* the driver supports pixel center integer */
6235 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
,
6236 TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
6238 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
6239 /* the driver supports pixel center half integer, need to bias X,Y */
6248 /* Fragment shader wants pixel center half integer */
6249 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
6250 /* the driver supports pixel center half integer */
6252 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
6253 /* the driver supports pixel center integer, need to bias X,Y */
6254 adjX
= adjY
[0] = adjY
[1] = 0.5f
;
6255 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
,
6256 TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
6262 /* we invert after adjustment so that we avoid the MOV to temporary,
6263 * and reuse the adjustment ADD instead */
6264 emit_wpos_adjustment(st
->ctx
, t
, wpos_transform_const
, invert
, adjX
, adjY
);
6268 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
6269 * TGSI uses +1 for front, -1 for back.
6270 * This function converts the TGSI value to the GL value. Simply clamping/
6271 * saturating the value to [0,1] does the job.
6274 emit_face_var(struct gl_context
*ctx
, struct st_translate
*t
)
6276 struct ureg_program
*ureg
= t
->ureg
;
6277 struct ureg_dst face_temp
= ureg_DECL_temporary(ureg
);
6278 struct ureg_src face_input
= t
->inputs
[t
->inputMapping
[VARYING_SLOT_FACE
]];
6280 if (ctx
->Const
.NativeIntegers
) {
6281 ureg_FSGE(ureg
, face_temp
, face_input
, ureg_imm1f(ureg
, 0));
6284 /* MOV_SAT face_temp, input[face] */
6285 ureg_MOV(ureg
, ureg_saturate(face_temp
), face_input
);
6288 /* Use face_temp as face input from here on: */
6289 t
->inputs
[t
->inputMapping
[VARYING_SLOT_FACE
]] = ureg_src(face_temp
);
6293 emit_compute_block_size(const struct gl_program
*prog
,
6294 struct ureg_program
*ureg
) {
6295 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
,
6296 prog
->info
.cs
.local_size
[0]);
6297 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
,
6298 prog
->info
.cs
.local_size
[1]);
6299 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
,
6300 prog
->info
.cs
.local_size
[2]);
6303 struct sort_inout_decls
{
6304 bool operator()(const struct inout_decl
&a
, const struct inout_decl
&b
) const {
6305 return mapping
[a
.mesa_index
] < mapping
[b
.mesa_index
];
6308 const ubyte
*mapping
;
6311 /* Sort the given array of decls by the corresponding slot (TGSI file index).
6313 * This is for the benefit of older drivers which are broken when the
6314 * declarations aren't sorted in this way.
6317 sort_inout_decls_by_slot(struct inout_decl
*decls
,
6319 const ubyte mapping
[])
6321 sort_inout_decls sorter
;
6322 sorter
.mapping
= mapping
;
6323 std::sort(decls
, decls
+ count
, sorter
);
6327 st_translate_interp(enum glsl_interp_mode glsl_qual
, GLuint varying
)
6329 switch (glsl_qual
) {
6330 case INTERP_MODE_NONE
:
6331 if (varying
== VARYING_SLOT_COL0
|| varying
== VARYING_SLOT_COL1
)
6332 return TGSI_INTERPOLATE_COLOR
;
6333 return TGSI_INTERPOLATE_PERSPECTIVE
;
6334 case INTERP_MODE_SMOOTH
:
6335 return TGSI_INTERPOLATE_PERSPECTIVE
;
6336 case INTERP_MODE_FLAT
:
6337 return TGSI_INTERPOLATE_CONSTANT
;
6338 case INTERP_MODE_NOPERSPECTIVE
:
6339 return TGSI_INTERPOLATE_LINEAR
;
6341 assert(0 && "unexpected interp mode in st_translate_interp()");
6342 return TGSI_INTERPOLATE_PERSPECTIVE
;
6347 * Translate intermediate IR (glsl_to_tgsi_instruction) to TGSI format.
6348 * \param program the program to translate
6349 * \param numInputs number of input registers used
6350 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
6352 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
6353 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
6355 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
6356 * \param numOutputs number of output registers used
6357 * \param outputMapping maps Mesa fragment program outputs to TGSI
6359 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
6360 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
6363 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
6365 extern "C" enum pipe_error
6366 st_translate_program(
6367 struct gl_context
*ctx
,
6369 struct ureg_program
*ureg
,
6370 glsl_to_tgsi_visitor
*program
,
6371 const struct gl_program
*proginfo
,
6373 const ubyte inputMapping
[],
6374 const ubyte inputSlotToAttr
[],
6375 const ubyte inputSemanticName
[],
6376 const ubyte inputSemanticIndex
[],
6377 const ubyte interpMode
[],
6379 const ubyte outputMapping
[],
6380 const ubyte outputSemanticName
[],
6381 const ubyte outputSemanticIndex
[])
6383 struct st_translate
*t
;
6385 struct gl_program_constants
*frag_const
=
6386 &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
6387 enum pipe_error ret
= PIPE_OK
;
6389 assert(numInputs
<= ARRAY_SIZE(t
->inputs
));
6390 assert(numOutputs
<= ARRAY_SIZE(t
->outputs
));
6392 t
= CALLOC_STRUCT(st_translate
);
6394 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
6398 t
->procType
= procType
;
6399 t
->inputMapping
= inputMapping
;
6400 t
->outputMapping
= outputMapping
;
6402 t
->num_temp_arrays
= program
->next_array
;
6403 if (t
->num_temp_arrays
)
6404 t
->arrays
= (struct ureg_dst
*)
6405 calloc(t
->num_temp_arrays
, sizeof(t
->arrays
[0]));
6408 * Declare input attributes.
6411 case PIPE_SHADER_FRAGMENT
:
6412 case PIPE_SHADER_GEOMETRY
:
6413 case PIPE_SHADER_TESS_EVAL
:
6414 case PIPE_SHADER_TESS_CTRL
:
6415 sort_inout_decls_by_slot(program
->inputs
, program
->num_inputs
, inputMapping
);
6417 for (i
= 0; i
< program
->num_inputs
; ++i
) {
6418 struct inout_decl
*decl
= &program
->inputs
[i
];
6419 unsigned slot
= inputMapping
[decl
->mesa_index
];
6420 struct ureg_src src
;
6421 ubyte tgsi_usage_mask
= decl
->usage_mask
;
6423 if (glsl_base_type_is_64bit(decl
->base_type
)) {
6424 if (tgsi_usage_mask
== 1)
6425 tgsi_usage_mask
= TGSI_WRITEMASK_XY
;
6426 else if (tgsi_usage_mask
== 2)
6427 tgsi_usage_mask
= TGSI_WRITEMASK_ZW
;
6429 tgsi_usage_mask
= TGSI_WRITEMASK_XYZW
;
6432 unsigned interp_mode
= 0;
6433 unsigned interp_location
= 0;
6434 if (procType
== PIPE_SHADER_FRAGMENT
) {
6436 interp_mode
= interpMode
[slot
] != TGSI_INTERPOLATE_COUNT
?
6438 st_translate_interp(decl
->interp
, inputSlotToAttr
[slot
]);
6440 interp_location
= decl
->interp_loc
;
6443 src
= ureg_DECL_fs_input_cyl_centroid_layout(ureg
,
6444 inputSemanticName
[slot
], inputSemanticIndex
[slot
],
6445 interp_mode
, 0, interp_location
, slot
, tgsi_usage_mask
,
6446 decl
->array_id
, decl
->size
);
6448 for (unsigned j
= 0; j
< decl
->size
; ++j
) {
6449 if (t
->inputs
[slot
+ j
].File
!= TGSI_FILE_INPUT
) {
6450 /* The ArrayID is set up in dst_register */
6451 t
->inputs
[slot
+ j
] = src
;
6452 t
->inputs
[slot
+ j
].ArrayID
= 0;
6453 t
->inputs
[slot
+ j
].Index
+= j
;
6458 case PIPE_SHADER_VERTEX
:
6459 for (i
= 0; i
< numInputs
; i
++) {
6460 t
->inputs
[i
] = ureg_DECL_vs_input(ureg
, i
);
6463 case PIPE_SHADER_COMPUTE
:
6470 * Declare output attributes.
6473 case PIPE_SHADER_FRAGMENT
:
6474 case PIPE_SHADER_COMPUTE
:
6476 case PIPE_SHADER_GEOMETRY
:
6477 case PIPE_SHADER_TESS_EVAL
:
6478 case PIPE_SHADER_TESS_CTRL
:
6479 case PIPE_SHADER_VERTEX
:
6480 sort_inout_decls_by_slot(program
->outputs
, program
->num_outputs
, outputMapping
);
6482 for (i
= 0; i
< program
->num_outputs
; ++i
) {
6483 struct inout_decl
*decl
= &program
->outputs
[i
];
6484 unsigned slot
= outputMapping
[decl
->mesa_index
];
6485 struct ureg_dst dst
;
6486 ubyte tgsi_usage_mask
= decl
->usage_mask
;
6488 if (glsl_base_type_is_64bit(decl
->base_type
)) {
6489 if (tgsi_usage_mask
== 1)
6490 tgsi_usage_mask
= TGSI_WRITEMASK_XY
;
6491 else if (tgsi_usage_mask
== 2)
6492 tgsi_usage_mask
= TGSI_WRITEMASK_ZW
;
6494 tgsi_usage_mask
= TGSI_WRITEMASK_XYZW
;
6497 dst
= ureg_DECL_output_layout(ureg
,
6498 outputSemanticName
[slot
], outputSemanticIndex
[slot
],
6499 decl
->gs_out_streams
,
6500 slot
, tgsi_usage_mask
, decl
->array_id
, decl
->size
);
6502 for (unsigned j
= 0; j
< decl
->size
; ++j
) {
6503 if (t
->outputs
[slot
+ j
].File
!= TGSI_FILE_OUTPUT
) {
6504 /* The ArrayID is set up in dst_register */
6505 t
->outputs
[slot
+ j
] = dst
;
6506 t
->outputs
[slot
+ j
].ArrayID
= 0;
6507 t
->outputs
[slot
+ j
].Index
+= j
;
6516 if (procType
== PIPE_SHADER_FRAGMENT
) {
6517 if (program
->shader
->Program
->info
.fs
.early_fragment_tests
||
6518 program
->shader
->Program
->info
.fs
.post_depth_coverage
) {
6519 ureg_property(ureg
, TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL
, 1);
6521 if (program
->shader
->Program
->info
.fs
.post_depth_coverage
)
6522 ureg_property(ureg
, TGSI_PROPERTY_FS_POST_DEPTH_COVERAGE
, 1);
6525 if (proginfo
->info
.inputs_read
& VARYING_BIT_POS
) {
6526 /* Must do this after setting up t->inputs. */
6527 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
,
6528 program
->wpos_transform_const
);
6531 if (proginfo
->info
.inputs_read
& VARYING_BIT_FACE
)
6532 emit_face_var(ctx
, t
);
6534 for (i
= 0; i
< numOutputs
; i
++) {
6535 switch (outputSemanticName
[i
]) {
6536 case TGSI_SEMANTIC_POSITION
:
6537 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6538 TGSI_SEMANTIC_POSITION
, /* Z/Depth */
6539 outputSemanticIndex
[i
]);
6540 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Z
);
6542 case TGSI_SEMANTIC_STENCIL
:
6543 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6544 TGSI_SEMANTIC_STENCIL
, /* Stencil */
6545 outputSemanticIndex
[i
]);
6546 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Y
);
6548 case TGSI_SEMANTIC_COLOR
:
6549 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6550 TGSI_SEMANTIC_COLOR
,
6551 outputSemanticIndex
[i
]);
6553 case TGSI_SEMANTIC_SAMPLEMASK
:
6554 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6555 TGSI_SEMANTIC_SAMPLEMASK
,
6556 outputSemanticIndex
[i
]);
6557 /* TODO: If we ever support more than 32 samples, this will have
6558 * to become an array.
6560 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_X
);
6563 assert(!"fragment shader outputs must be POSITION/STENCIL/COLOR");
6564 ret
= PIPE_ERROR_BAD_INPUT
;
6569 else if (procType
== PIPE_SHADER_VERTEX
) {
6570 for (i
= 0; i
< numOutputs
; i
++) {
6571 if (outputSemanticName
[i
] == TGSI_SEMANTIC_FOG
) {
6572 /* force register to contain a fog coordinate in the form (F, 0, 0, 1). */
6574 ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_YZW
),
6575 ureg_imm4f(ureg
, 0.0f
, 0.0f
, 0.0f
, 1.0f
));
6576 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_X
);
6581 if (procType
== PIPE_SHADER_COMPUTE
) {
6582 emit_compute_block_size(proginfo
, ureg
);
6585 /* Declare address register.
6587 if (program
->num_address_regs
> 0) {
6588 assert(program
->num_address_regs
<= 3);
6589 for (int i
= 0; i
< program
->num_address_regs
; i
++)
6590 t
->address
[i
] = ureg_DECL_address(ureg
);
6593 /* Declare misc input registers
6596 GLbitfield sysInputs
= proginfo
->info
.system_values_read
;
6598 for (i
= 0; sysInputs
; i
++) {
6599 if (sysInputs
& (1 << i
)) {
6600 unsigned semName
= _mesa_sysval_to_semantic(i
);
6602 t
->systemValues
[i
] = ureg_DECL_system_value(ureg
, semName
, 0);
6604 if (semName
== TGSI_SEMANTIC_INSTANCEID
||
6605 semName
== TGSI_SEMANTIC_VERTEXID
) {
6606 /* From Gallium perspective, these system values are always
6607 * integer, and require native integer support. However, if
6608 * native integer is supported on the vertex stage but not the
6609 * pixel stage (e.g, i915g + draw), Mesa will generate IR that
6610 * assumes these system values are floats. To resolve the
6611 * inconsistency, we insert a U2F.
6613 struct st_context
*st
= st_context(ctx
);
6614 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
6615 assert(procType
== PIPE_SHADER_VERTEX
);
6616 assert(pscreen
->get_shader_param(pscreen
, PIPE_SHADER_VERTEX
, PIPE_SHADER_CAP_INTEGERS
));
6618 if (!ctx
->Const
.NativeIntegers
) {
6619 struct ureg_dst temp
= ureg_DECL_local_temporary(t
->ureg
);
6620 ureg_U2F( t
->ureg
, ureg_writemask(temp
, TGSI_WRITEMASK_X
), t
->systemValues
[i
]);
6621 t
->systemValues
[i
] = ureg_scalar(ureg_src(temp
), 0);
6625 if (procType
== PIPE_SHADER_FRAGMENT
&&
6626 semName
== TGSI_SEMANTIC_POSITION
)
6627 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
,
6628 program
->wpos_transform_const
);
6630 sysInputs
&= ~(1 << i
);
6635 t
->array_sizes
= program
->array_sizes
;
6636 t
->input_decls
= program
->inputs
;
6637 t
->num_input_decls
= program
->num_inputs
;
6638 t
->output_decls
= program
->outputs
;
6639 t
->num_output_decls
= program
->num_outputs
;
6641 /* Emit constants and uniforms. TGSI uses a single index space for these,
6642 * so we put all the translated regs in t->constants.
6644 if (proginfo
->Parameters
) {
6645 t
->constants
= (struct ureg_src
*)
6646 calloc(proginfo
->Parameters
->NumParameters
, sizeof(t
->constants
[0]));
6647 if (t
->constants
== NULL
) {
6648 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
6651 t
->num_constants
= proginfo
->Parameters
->NumParameters
;
6653 for (i
= 0; i
< proginfo
->Parameters
->NumParameters
; i
++) {
6654 switch (proginfo
->Parameters
->Parameters
[i
].Type
) {
6655 case PROGRAM_STATE_VAR
:
6656 case PROGRAM_UNIFORM
:
6657 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
6660 /* Emit immediates for PROGRAM_CONSTANT only when there's no indirect
6661 * addressing of the const buffer.
6662 * FIXME: Be smarter and recognize param arrays:
6663 * indirect addressing is only valid within the referenced
6666 case PROGRAM_CONSTANT
:
6667 if (program
->indirect_addr_consts
)
6668 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
6670 t
->constants
[i
] = emit_immediate(t
,
6671 proginfo
->Parameters
->ParameterValues
[i
],
6672 proginfo
->Parameters
->Parameters
[i
].DataType
,
6681 for (i
= 0; i
< proginfo
->info
.num_ubos
; i
++) {
6682 unsigned size
= proginfo
->sh
.UniformBlocks
[i
]->UniformBufferSize
;
6683 unsigned num_const_vecs
= (size
+ 15) / 16;
6684 unsigned first
, last
;
6685 assert(num_const_vecs
> 0);
6687 last
= num_const_vecs
> 0 ? num_const_vecs
- 1 : 0;
6688 ureg_DECL_constant2D(t
->ureg
, first
, last
, i
+ 1);
6691 /* Emit immediate values.
6693 t
->immediates
= (struct ureg_src
*)
6694 calloc(program
->num_immediates
, sizeof(struct ureg_src
));
6695 if (t
->immediates
== NULL
) {
6696 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
6699 t
->num_immediates
= program
->num_immediates
;
6702 foreach_in_list(immediate_storage
, imm
, &program
->immediates
) {
6703 assert(i
< program
->num_immediates
);
6704 t
->immediates
[i
++] = emit_immediate(t
, imm
->values
, imm
->type
, imm
->size32
);
6706 assert(i
== program
->num_immediates
);
6708 /* texture samplers */
6709 for (i
= 0; i
< frag_const
->MaxTextureImageUnits
; i
++) {
6710 if (program
->samplers_used
& (1u << i
)) {
6711 unsigned type
= st_translate_texture_type(program
->sampler_types
[i
]);
6713 t
->samplers
[i
] = ureg_DECL_sampler(ureg
, i
);
6715 ureg_DECL_sampler_view( ureg
, i
, program
->sampler_targets
[i
],
6716 type
, type
, type
, type
);
6720 /* Declare atomic and shader storage buffers. */
6722 struct gl_program
*prog
= program
->prog
;
6724 for (i
= 0; i
< prog
->info
.num_abos
; i
++) {
6725 unsigned index
= prog
->sh
.AtomicBuffers
[i
]->Binding
;
6726 assert(index
< frag_const
->MaxAtomicBuffers
);
6727 t
->buffers
[index
] = ureg_DECL_buffer(ureg
, index
, true);
6730 assert(prog
->info
.num_ssbos
<= frag_const
->MaxShaderStorageBlocks
);
6731 for (i
= 0; i
< prog
->info
.num_ssbos
; i
++) {
6732 unsigned index
= frag_const
->MaxAtomicBuffers
+ i
;
6733 t
->buffers
[index
] = ureg_DECL_buffer(ureg
, index
, false);
6737 if (program
->use_shared_memory
)
6738 t
->shared_memory
= ureg_DECL_memory(ureg
, TGSI_MEMORY_TYPE_SHARED
);
6740 for (i
= 0; i
< program
->shader
->Program
->info
.num_images
; i
++) {
6741 if (program
->images_used
& (1 << i
)) {
6742 t
->images
[i
] = ureg_DECL_image(ureg
, i
,
6743 program
->image_targets
[i
],
6744 program
->image_formats
[i
],
6749 /* Emit each instruction in turn:
6751 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &program
->instructions
)
6752 compile_tgsi_instruction(t
, inst
);
6754 /* Set the next shader stage hint for VS and TES. */
6756 case PIPE_SHADER_VERTEX
:
6757 case PIPE_SHADER_TESS_EVAL
:
6758 if (program
->shader_program
->SeparateShader
)
6761 for (i
= program
->shader
->Stage
+1; i
<= MESA_SHADER_FRAGMENT
; i
++) {
6762 if (program
->shader_program
->_LinkedShaders
[i
]) {
6763 ureg_set_next_shader_processor(
6764 ureg
, pipe_shader_type_from_mesa((gl_shader_stage
)i
));
6776 t
->num_constants
= 0;
6777 free(t
->immediates
);
6778 t
->num_immediates
= 0;
6784 /* ----------------------------- End TGSI code ------------------------------ */
6788 * Convert a shader's GLSL IR into a Mesa gl_program, although without
6789 * generating Mesa IR.
6791 static struct gl_program
*
6792 get_mesa_program_tgsi(struct gl_context
*ctx
,
6793 struct gl_shader_program
*shader_program
,
6794 struct gl_linked_shader
*shader
)
6796 glsl_to_tgsi_visitor
* v
;
6797 struct gl_program
*prog
;
6798 struct gl_shader_compiler_options
*options
=
6799 &ctx
->Const
.ShaderCompilerOptions
[shader
->Stage
];
6800 struct pipe_screen
*pscreen
= ctx
->st
->pipe
->screen
;
6801 enum pipe_shader_type ptarget
= st_shader_stage_to_ptarget(shader
->Stage
);
6802 unsigned skip_merge_registers
;
6804 validate_ir_tree(shader
->ir
);
6806 prog
= shader
->Program
;
6808 prog
->Parameters
= _mesa_new_parameter_list();
6809 v
= new glsl_to_tgsi_visitor();
6812 v
->shader_program
= shader_program
;
6814 v
->options
= options
;
6815 v
->glsl_version
= ctx
->Const
.GLSLVersion
;
6816 v
->native_integers
= ctx
->Const
.NativeIntegers
;
6818 v
->have_sqrt
= pscreen
->get_shader_param(pscreen
, ptarget
,
6819 PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED
);
6820 v
->have_fma
= pscreen
->get_shader_param(pscreen
, ptarget
,
6821 PIPE_SHADER_CAP_TGSI_FMA_SUPPORTED
);
6822 v
->has_tex_txf_lz
= pscreen
->get_param(pscreen
,
6823 PIPE_CAP_TGSI_TEX_TXF_LZ
);
6825 v
->variables
= _mesa_hash_table_create(v
->mem_ctx
, _mesa_hash_pointer
,
6826 _mesa_key_pointer_equal
);
6827 skip_merge_registers
=
6828 pscreen
->get_shader_param(pscreen
, ptarget
,
6829 PIPE_SHADER_CAP_TGSI_SKIP_MERGE_REGISTERS
);
6831 _mesa_generate_parameters_list_for_uniforms(shader_program
, shader
,
6834 /* Remove reads from output registers. */
6835 if (!pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_CAN_READ_OUTPUTS
))
6836 lower_output_reads(shader
->Stage
, shader
->ir
);
6838 /* Emit intermediate IR for main(). */
6839 visit_exec_list(shader
->ir
, v
);
6842 /* Print out some information (for debugging purposes) used by the
6843 * optimization passes. */
6846 int *first_writes
= ralloc_array(v
->mem_ctx
, int, v
->next_temp
);
6847 int *first_reads
= ralloc_array(v
->mem_ctx
, int, v
->next_temp
);
6848 int *last_writes
= ralloc_array(v
->mem_ctx
, int, v
->next_temp
);
6849 int *last_reads
= ralloc_array(v
->mem_ctx
, int, v
->next_temp
);
6851 for (i
= 0; i
< v
->next_temp
; i
++) {
6852 first_writes
[i
] = -1;
6853 first_reads
[i
] = -1;
6854 last_writes
[i
] = -1;
6857 v
->get_first_temp_read(first_reads
);
6858 v
->get_last_temp_read_first_temp_write(last_reads
, first_writes
);
6859 v
->get_last_temp_write(last_writes
);
6860 for (i
= 0; i
< v
->next_temp
; i
++)
6861 printf("Temp %d: FR=%3d FW=%3d LR=%3d LW=%3d\n", i
, first_reads
[i
],
6865 ralloc_free(first_writes
);
6866 ralloc_free(first_reads
);
6867 ralloc_free(last_writes
);
6868 ralloc_free(last_reads
);
6872 /* Perform optimizations on the instructions in the glsl_to_tgsi_visitor. */
6875 if (shader
->Stage
!= MESA_SHADER_TESS_CTRL
&&
6876 shader
->Stage
!= MESA_SHADER_TESS_EVAL
)
6877 v
->copy_propagate();
6879 while (v
->eliminate_dead_code());
6881 v
->merge_two_dsts();
6882 if (!skip_merge_registers
)
6883 v
->merge_registers();
6884 v
->renumber_registers();
6886 /* Write the END instruction. */
6887 v
->emit_asm(NULL
, TGSI_OPCODE_END
);
6889 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
6891 _mesa_log("GLSL IR for linked %s program %d:\n",
6892 _mesa_shader_stage_to_string(shader
->Stage
),
6893 shader_program
->Name
);
6894 _mesa_print_ir(_mesa_get_log_file(), shader
->ir
, NULL
);
6898 do_set_program_inouts(shader
->ir
, prog
, shader
->Stage
);
6899 _mesa_copy_linked_program_data(shader_program
, shader
);
6900 shrink_array_declarations(v
->inputs
, v
->num_inputs
,
6901 &prog
->info
.inputs_read
,
6902 prog
->info
.double_inputs_read
,
6903 &prog
->info
.patch_inputs_read
);
6904 shrink_array_declarations(v
->outputs
, v
->num_outputs
,
6905 &prog
->info
.outputs_written
, 0ULL,
6906 &prog
->info
.patch_outputs_written
);
6907 count_resources(v
, prog
);
6909 /* The GLSL IR won't be needed anymore. */
6910 ralloc_free(shader
->ir
);
6913 /* This must be done before the uniform storage is associated. */
6914 if (shader
->Stage
== MESA_SHADER_FRAGMENT
&&
6915 (prog
->info
.inputs_read
& VARYING_BIT_POS
||
6916 prog
->info
.system_values_read
& (1 << SYSTEM_VALUE_FRAG_COORD
))) {
6917 static const gl_state_index wposTransformState
[STATE_LENGTH
] = {
6918 STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
6921 v
->wpos_transform_const
= _mesa_add_state_reference(prog
->Parameters
,
6922 wposTransformState
);
6925 /* Avoid reallocation of the program parameter list, because the uniform
6926 * storage is only associated with the original parameter list.
6927 * This should be enough for Bitmap and DrawPixels constants.
6929 _mesa_reserve_parameter_storage(prog
->Parameters
, 8);
6931 /* This has to be done last. Any operation the can cause
6932 * prog->ParameterValues to get reallocated (e.g., anything that adds a
6933 * program constant) has to happen before creating this linkage.
6935 _mesa_associate_uniform_storage(ctx
, shader_program
, prog
, true);
6936 if (!shader_program
->data
->LinkStatus
) {
6937 free_glsl_to_tgsi_visitor(v
);
6938 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
6942 struct st_vertex_program
*stvp
;
6943 struct st_fragment_program
*stfp
;
6944 struct st_common_program
*stp
;
6945 struct st_compute_program
*stcp
;
6947 switch (shader
->Stage
) {
6948 case MESA_SHADER_VERTEX
:
6949 stvp
= (struct st_vertex_program
*)prog
;
6950 stvp
->glsl_to_tgsi
= v
;
6952 case MESA_SHADER_FRAGMENT
:
6953 stfp
= (struct st_fragment_program
*)prog
;
6954 stfp
->glsl_to_tgsi
= v
;
6956 case MESA_SHADER_TESS_CTRL
:
6957 case MESA_SHADER_TESS_EVAL
:
6958 case MESA_SHADER_GEOMETRY
:
6959 stp
= st_common_program(prog
);
6960 stp
->glsl_to_tgsi
= v
;
6962 case MESA_SHADER_COMPUTE
:
6963 stcp
= (struct st_compute_program
*)prog
;
6964 stcp
->glsl_to_tgsi
= v
;
6967 assert(!"should not be reached");
6974 /* See if there are unsupported control flow statements. */
6975 class ir_control_flow_info_visitor
: public ir_hierarchical_visitor
{
6977 const struct gl_shader_compiler_options
*options
;
6979 ir_control_flow_info_visitor(const struct gl_shader_compiler_options
*options
)
6985 virtual ir_visitor_status
visit_enter(ir_function
*ir
)
6987 /* Other functions are skipped (same as glsl_to_tgsi). */
6988 if (strcmp(ir
->name
, "main") == 0)
6989 return visit_continue
;
6991 return visit_continue_with_parent
;
6994 virtual ir_visitor_status
visit_enter(ir_call
*ir
)
6996 if (!ir
->callee
->is_intrinsic()) {
6997 unsupported
= true; /* it's a function call */
7000 return visit_continue
;
7003 virtual ir_visitor_status
visit_enter(ir_return
*ir
)
7005 if (options
->EmitNoMainReturn
) {
7009 return visit_continue
;
7016 has_unsupported_control_flow(exec_list
*ir
,
7017 const struct gl_shader_compiler_options
*options
)
7019 ir_control_flow_info_visitor
visitor(options
);
7020 visit_list_elements(&visitor
, ir
);
7021 return visitor
.unsupported
;
7028 * Called via ctx->Driver.LinkShader()
7029 * This actually involves converting GLSL IR into an intermediate TGSI-like IR
7030 * with code lowering and other optimizations.
7033 st_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
7035 /* Return early if we are loading the shader from on-disk cache */
7036 if (st_load_tgsi_from_disk_cache(ctx
, prog
)) {
7040 struct pipe_screen
*pscreen
= ctx
->st
->pipe
->screen
;
7041 assert(prog
->data
->LinkStatus
);
7043 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
7044 if (prog
->_LinkedShaders
[i
] == NULL
)
7047 struct gl_linked_shader
*shader
= prog
->_LinkedShaders
[i
];
7048 exec_list
*ir
= shader
->ir
;
7049 gl_shader_stage stage
= shader
->Stage
;
7050 const struct gl_shader_compiler_options
*options
=
7051 &ctx
->Const
.ShaderCompilerOptions
[stage
];
7052 enum pipe_shader_type ptarget
= st_shader_stage_to_ptarget(stage
);
7053 bool have_dround
= pscreen
->get_shader_param(pscreen
, ptarget
,
7054 PIPE_SHADER_CAP_TGSI_DROUND_SUPPORTED
);
7055 bool have_dfrexp
= pscreen
->get_shader_param(pscreen
, ptarget
,
7056 PIPE_SHADER_CAP_TGSI_DFRACEXP_DLDEXP_SUPPORTED
);
7057 unsigned if_threshold
= pscreen
->get_shader_param(pscreen
, ptarget
,
7058 PIPE_SHADER_CAP_LOWER_IF_THRESHOLD
);
7060 /* If there are forms of indirect addressing that the driver
7061 * cannot handle, perform the lowering pass.
7063 if (options
->EmitNoIndirectInput
|| options
->EmitNoIndirectOutput
||
7064 options
->EmitNoIndirectTemp
|| options
->EmitNoIndirectUniform
) {
7065 lower_variable_index_to_cond_assign(stage
, ir
,
7066 options
->EmitNoIndirectInput
,
7067 options
->EmitNoIndirectOutput
,
7068 options
->EmitNoIndirectTemp
,
7069 options
->EmitNoIndirectUniform
);
7072 if (!pscreen
->get_param(pscreen
, PIPE_CAP_INT64_DIVMOD
))
7073 lower_64bit_integer_instructions(ir
, DIV64
| MOD64
);
7075 if (ctx
->Extensions
.ARB_shading_language_packing
) {
7076 unsigned lower_inst
= LOWER_PACK_SNORM_2x16
|
7077 LOWER_UNPACK_SNORM_2x16
|
7078 LOWER_PACK_UNORM_2x16
|
7079 LOWER_UNPACK_UNORM_2x16
|
7080 LOWER_PACK_SNORM_4x8
|
7081 LOWER_UNPACK_SNORM_4x8
|
7082 LOWER_UNPACK_UNORM_4x8
|
7083 LOWER_PACK_UNORM_4x8
;
7085 if (ctx
->Extensions
.ARB_gpu_shader5
)
7086 lower_inst
|= LOWER_PACK_USE_BFI
|
7088 if (!ctx
->st
->has_half_float_packing
)
7089 lower_inst
|= LOWER_PACK_HALF_2x16
|
7090 LOWER_UNPACK_HALF_2x16
;
7092 lower_packing_builtins(ir
, lower_inst
);
7095 if (!pscreen
->get_param(pscreen
, PIPE_CAP_TEXTURE_GATHER_OFFSETS
))
7096 lower_offset_arrays(ir
);
7097 do_mat_op_to_vec(ir
);
7099 if (stage
== MESA_SHADER_FRAGMENT
)
7100 lower_blend_equation_advanced(shader
);
7102 lower_instructions(ir
,
7108 (have_dfrexp
? 0 : DFREXP_DLDEXP_TO_ARITH
) |
7111 (have_dround
? 0 : DOPS_TO_DFRAC
) |
7112 (options
->EmitNoPow
? POW_TO_EXP2
: 0) |
7113 (!ctx
->Const
.NativeIntegers
? INT_DIV_TO_MUL_RCP
: 0) |
7114 (options
->EmitNoSat
? SAT_TO_CLAMP
: 0) |
7115 (ctx
->Const
.ForceGLSLAbsSqrt
? SQRT_TO_ABS_SQRT
: 0) |
7116 /* Assume that if ARB_gpu_shader5 is not supported
7117 * then all of the extended integer functions need
7118 * lowering. It may be necessary to add some caps
7119 * for individual instructions.
7121 (!ctx
->Extensions
.ARB_gpu_shader5
7122 ? BIT_COUNT_TO_MATH
|
7126 FIND_LSB_TO_FLOAT_CAST
|
7127 FIND_MSB_TO_FLOAT_CAST
|
7131 do_vec_index_to_cond_assign(ir
);
7132 lower_vector_insert(ir
, true);
7133 lower_quadop_vector(ir
, false);
7135 if (options
->MaxIfDepth
== 0) {
7139 if (ctx
->Const
.GLSLOptimizeConservatively
) {
7140 /* Do it once and repeat only if there's unsupported control flow. */
7142 do_common_optimization(ir
, true, true, options
,
7143 ctx
->Const
.NativeIntegers
);
7144 lower_if_to_cond_assign((gl_shader_stage
)i
, ir
,
7145 options
->MaxIfDepth
, if_threshold
);
7146 } while (has_unsupported_control_flow(ir
, options
));
7148 /* Repeat it until it stops making changes. */
7151 progress
= do_common_optimization(ir
, true, true, options
,
7152 ctx
->Const
.NativeIntegers
);
7153 progress
|= lower_if_to_cond_assign((gl_shader_stage
)i
, ir
,
7154 options
->MaxIfDepth
, if_threshold
);
7158 validate_ir_tree(ir
);
7161 build_program_resource_list(ctx
, prog
);
7163 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
7164 struct gl_linked_shader
*shader
= prog
->_LinkedShaders
[i
];
7168 enum pipe_shader_type ptarget
=
7169 st_shader_stage_to_ptarget(shader
->Stage
);
7170 enum pipe_shader_ir preferred_ir
= (enum pipe_shader_ir
)
7171 pscreen
->get_shader_param(pscreen
, ptarget
,
7172 PIPE_SHADER_CAP_PREFERRED_IR
);
7174 struct gl_program
*linked_prog
= NULL
;
7175 if (preferred_ir
== PIPE_SHADER_IR_NIR
) {
7176 /* TODO only for GLSL VS/FS/CS for now: */
7177 switch (shader
->Stage
) {
7178 case MESA_SHADER_VERTEX
:
7179 case MESA_SHADER_FRAGMENT
:
7180 case MESA_SHADER_COMPUTE
:
7181 linked_prog
= st_nir_get_mesa_program(ctx
, prog
, shader
);
7186 linked_prog
= get_mesa_program_tgsi(ctx
, prog
, shader
);
7190 st_set_prog_affected_state_flags(linked_prog
);
7191 if (!ctx
->Driver
.ProgramStringNotify(ctx
,
7192 _mesa_shader_stage_to_program(i
),
7194 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
7204 st_translate_stream_output_info(glsl_to_tgsi_visitor
*glsl_to_tgsi
,
7205 const ubyte outputMapping
[],
7206 struct pipe_stream_output_info
*so
)
7208 if (!glsl_to_tgsi
->shader_program
->last_vert_prog
)
7211 struct gl_transform_feedback_info
*info
=
7212 glsl_to_tgsi
->shader_program
->last_vert_prog
->sh
.LinkedTransformFeedback
;
7213 st_translate_stream_output_info2(info
, outputMapping
, so
);
7217 st_translate_stream_output_info2(struct gl_transform_feedback_info
*info
,
7218 const ubyte outputMapping
[],
7219 struct pipe_stream_output_info
*so
)
7223 for (i
= 0; i
< info
->NumOutputs
; i
++) {
7224 so
->output
[i
].register_index
=
7225 outputMapping
[info
->Outputs
[i
].OutputRegister
];
7226 so
->output
[i
].start_component
= info
->Outputs
[i
].ComponentOffset
;
7227 so
->output
[i
].num_components
= info
->Outputs
[i
].NumComponents
;
7228 so
->output
[i
].output_buffer
= info
->Outputs
[i
].OutputBuffer
;
7229 so
->output
[i
].dst_offset
= info
->Outputs
[i
].DstOffset
;
7230 so
->output
[i
].stream
= info
->Outputs
[i
].StreamId
;
7233 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
7234 so
->stride
[i
] = info
->Buffers
[i
].Stride
;
7236 so
->num_outputs
= info
->NumOutputs
;