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);
91 * This struct is a corresponding struct to TGSI ureg_src.
95 st_src_reg(gl_register_file file
, int index
, const glsl_type
*type
,
96 int component
= 0, unsigned array_id
= 0)
98 assert(file
!= PROGRAM_ARRAY
|| array_id
!= 0);
101 this->swizzle
= swizzle_for_type(type
, component
);
105 this->type
= type
? type
->base_type
: GLSL_TYPE_ERROR
;
106 this->reladdr
= NULL
;
107 this->reladdr2
= NULL
;
108 this->has_index2
= false;
109 this->double_reg2
= false;
110 this->array_id
= array_id
;
111 this->is_double_vertex_input
= false;
114 st_src_reg(gl_register_file file
, int index
, enum glsl_base_type type
)
116 assert(file
!= PROGRAM_ARRAY
); /* need array_id > 0 */
121 this->swizzle
= SWIZZLE_XYZW
;
124 this->reladdr
= NULL
;
125 this->reladdr2
= NULL
;
126 this->has_index2
= false;
127 this->double_reg2
= false;
129 this->is_double_vertex_input
= false;
132 st_src_reg(gl_register_file file
, int index
, enum glsl_base_type type
, int index2D
)
134 assert(file
!= PROGRAM_ARRAY
); /* need array_id > 0 */
138 this->index2D
= index2D
;
139 this->swizzle
= SWIZZLE_XYZW
;
142 this->reladdr
= NULL
;
143 this->reladdr2
= NULL
;
144 this->has_index2
= false;
145 this->double_reg2
= false;
147 this->is_double_vertex_input
= false;
152 this->type
= GLSL_TYPE_ERROR
;
153 this->file
= PROGRAM_UNDEFINED
;
159 this->reladdr
= NULL
;
160 this->reladdr2
= NULL
;
161 this->has_index2
= false;
162 this->double_reg2
= false;
164 this->is_double_vertex_input
= false;
167 explicit st_src_reg(st_dst_reg reg
);
169 int32_t index
; /**< temporary index, VERT_ATTRIB_*, VARYING_SLOT_*, etc. */
171 uint16_t swizzle
; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
172 int negate
:4; /**< NEGATE_XYZW mask from mesa */
174 enum glsl_base_type type
:5; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
175 unsigned has_index2
:1;
176 gl_register_file file
:5; /**< PROGRAM_* from Mesa */
178 * Is this the second half of a double register pair?
179 * currently used for input mapping only.
181 unsigned double_reg2
:1;
182 unsigned is_double_vertex_input
:1;
183 unsigned array_id
:10;
185 /** Register index should be offset by the integer in this reg. */
187 st_src_reg
*reladdr2
;
191 st_src_reg reg
= *this;
200 st_dst_reg(gl_register_file file
, int writemask
, enum glsl_base_type type
, int index
)
202 assert(file
!= PROGRAM_ARRAY
); /* need array_id > 0 */
206 this->writemask
= writemask
;
207 this->reladdr
= NULL
;
208 this->reladdr2
= NULL
;
209 this->has_index2
= false;
214 st_dst_reg(gl_register_file file
, int writemask
, enum glsl_base_type type
)
216 assert(file
!= PROGRAM_ARRAY
); /* need array_id > 0 */
220 this->writemask
= writemask
;
221 this->reladdr
= NULL
;
222 this->reladdr2
= NULL
;
223 this->has_index2
= false;
230 this->type
= GLSL_TYPE_ERROR
;
231 this->file
= PROGRAM_UNDEFINED
;
235 this->reladdr
= NULL
;
236 this->reladdr2
= NULL
;
237 this->has_index2
= false;
241 explicit st_dst_reg(st_src_reg reg
);
243 int32_t index
; /**< temporary index, VERT_ATTRIB_*, VARYING_SLOT_*, etc. */
245 gl_register_file file
:5; /**< PROGRAM_* from Mesa */
246 unsigned writemask
:4; /**< Bitfield of WRITEMASK_[XYZW] */
247 enum glsl_base_type type
:5; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
248 unsigned has_index2
:1;
249 unsigned array_id
:10;
251 /** Register index should be offset by the integer in this reg. */
253 st_src_reg
*reladdr2
;
256 st_src_reg::st_src_reg(st_dst_reg reg
)
258 this->type
= reg
.type
;
259 this->file
= reg
.file
;
260 this->index
= reg
.index
;
261 this->swizzle
= SWIZZLE_XYZW
;
264 this->reladdr
= reg
.reladdr
;
265 this->index2D
= reg
.index2D
;
266 this->reladdr2
= reg
.reladdr2
;
267 this->has_index2
= reg
.has_index2
;
268 this->double_reg2
= false;
269 this->array_id
= reg
.array_id
;
270 this->is_double_vertex_input
= false;
273 st_dst_reg::st_dst_reg(st_src_reg reg
)
275 this->type
= reg
.type
;
276 this->file
= reg
.file
;
277 this->index
= reg
.index
;
278 this->writemask
= WRITEMASK_XYZW
;
279 this->reladdr
= reg
.reladdr
;
280 this->index2D
= reg
.index2D
;
281 this->reladdr2
= reg
.reladdr2
;
282 this->has_index2
= reg
.has_index2
;
283 this->array_id
= reg
.array_id
;
286 class glsl_to_tgsi_instruction
: public exec_node
{
288 DECLARE_RALLOC_CXX_OPERATORS(glsl_to_tgsi_instruction
)
292 st_src_reg resource
; /**< sampler or buffer register */
293 st_src_reg
*tex_offsets
;
295 /** Pointer to the ir source this tree came from for debugging */
298 unsigned op
:8; /**< TGSI opcode */
300 unsigned is_64bit_expanded
:1;
301 unsigned sampler_base
:5;
302 unsigned sampler_array_size
:6; /**< 1-based size of sampler array, 1 if not array */
303 unsigned tex_target
:4; /**< One of TEXTURE_*_INDEX */
304 glsl_base_type tex_type
:5;
305 unsigned tex_shadow
:1;
306 unsigned image_format
:9;
307 unsigned tex_offset_num_offset
:3;
308 unsigned dead_mask
:4; /**< Used in dead code elimination */
309 unsigned buffer_access
:3; /**< buffer access type */
311 const struct tgsi_opcode_info
*info
;
314 class variable_storage
{
315 DECLARE_RZALLOC_CXX_OPERATORS(variable_storage
)
318 variable_storage(ir_variable
*var
, gl_register_file file
, int index
,
319 unsigned array_id
= 0)
320 : file(file
), index(index
), component(0), var(var
), array_id(array_id
)
322 assert(file
!= PROGRAM_ARRAY
|| array_id
!= 0);
325 gl_register_file file
;
328 /* Explicit component location. This is given in terms of the GLSL-style
329 * swizzles where each double is a single component, i.e. for 64-bit types
330 * it can only be 0 or 1.
333 ir_variable
*var
; /* variable that maps to this, if any */
337 class immediate_storage
: public exec_node
{
339 immediate_storage(gl_constant_value
*values
, int size32
, int type
)
341 memcpy(this->values
, values
, size32
* sizeof(gl_constant_value
));
342 this->size32
= size32
;
346 /* doubles are stored across 2 gl_constant_values */
347 gl_constant_value values
[4];
348 int size32
; /**< Number of 32-bit components (1-4) */
349 int type
; /**< GL_DOUBLE, GL_FLOAT, GL_INT, GL_BOOL, or GL_UNSIGNED_INT */
352 static const st_src_reg undef_src
= st_src_reg(PROGRAM_UNDEFINED
, 0, GLSL_TYPE_ERROR
);
353 static const st_dst_reg undef_dst
= st_dst_reg(PROGRAM_UNDEFINED
, SWIZZLE_NOOP
, GLSL_TYPE_ERROR
);
357 unsigned array_id
; /* TGSI ArrayID; 1-based: 0 means not an array */
360 unsigned gs_out_streams
;
361 enum glsl_interp_mode interp
;
362 enum glsl_base_type base_type
;
363 ubyte usage_mask
; /* GLSL-style usage-mask, i.e. single bit per double */
366 static struct inout_decl
*
367 find_inout_array(struct inout_decl
*decls
, unsigned count
, unsigned array_id
)
369 assert(array_id
!= 0);
371 for (unsigned i
= 0; i
< count
; i
++) {
372 struct inout_decl
*decl
= &decls
[i
];
374 if (array_id
== decl
->array_id
) {
382 static enum glsl_base_type
383 find_array_type(struct inout_decl
*decls
, unsigned count
, unsigned array_id
)
386 return GLSL_TYPE_ERROR
;
387 struct inout_decl
*decl
= find_inout_array(decls
, count
, array_id
);
389 return decl
->base_type
;
390 return GLSL_TYPE_ERROR
;
393 struct rename_reg_pair
{
398 struct glsl_to_tgsi_visitor
: public ir_visitor
{
400 glsl_to_tgsi_visitor();
401 ~glsl_to_tgsi_visitor();
403 struct gl_context
*ctx
;
404 struct gl_program
*prog
;
405 struct gl_shader_program
*shader_program
;
406 struct gl_linked_shader
*shader
;
407 struct gl_shader_compiler_options
*options
;
411 unsigned *array_sizes
;
412 unsigned max_num_arrays
;
415 struct inout_decl inputs
[4 * PIPE_MAX_SHADER_INPUTS
];
417 unsigned num_input_arrays
;
418 struct inout_decl outputs
[4 * PIPE_MAX_SHADER_OUTPUTS
];
419 unsigned num_outputs
;
420 unsigned num_output_arrays
;
422 int num_address_regs
;
423 uint32_t samplers_used
;
424 glsl_base_type sampler_types
[PIPE_MAX_SAMPLERS
];
425 int sampler_targets
[PIPE_MAX_SAMPLERS
]; /**< One of TGSI_TEXTURE_* */
427 int image_targets
[PIPE_MAX_SHADER_IMAGES
];
428 unsigned image_formats
[PIPE_MAX_SHADER_IMAGES
];
429 bool indirect_addr_consts
;
430 int wpos_transform_const
;
433 bool native_integers
;
436 bool use_shared_memory
;
439 variable_storage
*find_variable_storage(ir_variable
*var
);
441 int add_constant(gl_register_file file
, gl_constant_value values
[8],
442 int size
, int datatype
, uint16_t *swizzle_out
);
444 st_src_reg
get_temp(const glsl_type
*type
);
445 void reladdr_to_temp(ir_instruction
*ir
, st_src_reg
*reg
, int *num_reladdr
);
447 st_src_reg
st_src_reg_for_double(double val
);
448 st_src_reg
st_src_reg_for_float(float val
);
449 st_src_reg
st_src_reg_for_int(int val
);
450 st_src_reg
st_src_reg_for_type(enum glsl_base_type type
, int val
);
453 * \name Visit methods
455 * As typical for the visitor pattern, there must be one \c visit method for
456 * each concrete subclass of \c ir_instruction. Virtual base classes within
457 * the hierarchy should not have \c visit methods.
460 virtual void visit(ir_variable
*);
461 virtual void visit(ir_loop
*);
462 virtual void visit(ir_loop_jump
*);
463 virtual void visit(ir_function_signature
*);
464 virtual void visit(ir_function
*);
465 virtual void visit(ir_expression
*);
466 virtual void visit(ir_swizzle
*);
467 virtual void visit(ir_dereference_variable
*);
468 virtual void visit(ir_dereference_array
*);
469 virtual void visit(ir_dereference_record
*);
470 virtual void visit(ir_assignment
*);
471 virtual void visit(ir_constant
*);
472 virtual void visit(ir_call
*);
473 virtual void visit(ir_return
*);
474 virtual void visit(ir_discard
*);
475 virtual void visit(ir_texture
*);
476 virtual void visit(ir_if
*);
477 virtual void visit(ir_emit_vertex
*);
478 virtual void visit(ir_end_primitive
*);
479 virtual void visit(ir_barrier
*);
482 void visit_expression(ir_expression
*, st_src_reg
*) ATTRIBUTE_NOINLINE
;
484 void visit_atomic_counter_intrinsic(ir_call
*);
485 void visit_ssbo_intrinsic(ir_call
*);
486 void visit_membar_intrinsic(ir_call
*);
487 void visit_shared_intrinsic(ir_call
*);
488 void visit_image_intrinsic(ir_call
*);
489 void visit_generic_intrinsic(ir_call
*, unsigned op
);
493 /** List of variable_storage */
494 struct hash_table
*variables
;
496 /** List of immediate_storage */
497 exec_list immediates
;
498 unsigned num_immediates
;
500 /** List of glsl_to_tgsi_instruction */
501 exec_list instructions
;
503 glsl_to_tgsi_instruction
*emit_asm(ir_instruction
*ir
, unsigned op
,
504 st_dst_reg dst
= undef_dst
,
505 st_src_reg src0
= undef_src
,
506 st_src_reg src1
= undef_src
,
507 st_src_reg src2
= undef_src
,
508 st_src_reg src3
= undef_src
);
510 glsl_to_tgsi_instruction
*emit_asm(ir_instruction
*ir
, unsigned op
,
511 st_dst_reg dst
, st_dst_reg dst1
,
512 st_src_reg src0
= undef_src
,
513 st_src_reg src1
= undef_src
,
514 st_src_reg src2
= undef_src
,
515 st_src_reg src3
= undef_src
);
517 unsigned get_opcode(unsigned op
,
519 st_src_reg src0
, st_src_reg src1
);
522 * Emit the correct dot-product instruction for the type of arguments
524 glsl_to_tgsi_instruction
*emit_dp(ir_instruction
*ir
,
530 void emit_scalar(ir_instruction
*ir
, unsigned op
,
531 st_dst_reg dst
, st_src_reg src0
);
533 void emit_scalar(ir_instruction
*ir
, unsigned op
,
534 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
536 void emit_arl(ir_instruction
*ir
, st_dst_reg dst
, st_src_reg src0
);
538 void get_deref_offsets(ir_dereference
*ir
,
539 unsigned *array_size
,
544 void calc_deref_offsets(ir_dereference
*tail
,
545 unsigned *array_elements
,
547 st_src_reg
*indirect
,
549 st_src_reg
canonicalize_gather_offset(st_src_reg offset
);
551 bool try_emit_mad(ir_expression
*ir
,
553 bool try_emit_mad_for_and_not(ir_expression
*ir
,
556 void emit_swz(ir_expression
*ir
);
558 bool process_move_condition(ir_rvalue
*ir
);
560 void simplify_cmp(void);
562 void rename_temp_registers(struct rename_reg_pair
*renames
);
563 void get_first_temp_read(int *first_reads
);
564 void get_first_temp_write(int *first_writes
);
565 void get_last_temp_read_first_temp_write(int *last_reads
, int *first_writes
);
566 void get_last_temp_write(int *last_writes
);
568 void copy_propagate(void);
569 int eliminate_dead_code(void);
571 void merge_two_dsts(void);
572 void merge_registers(void);
573 void renumber_registers(void);
575 void emit_block_mov(ir_assignment
*ir
, const struct glsl_type
*type
,
576 st_dst_reg
*l
, st_src_reg
*r
,
577 st_src_reg
*cond
, bool cond_swap
);
582 static st_dst_reg address_reg
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
, 0);
583 static st_dst_reg address_reg2
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
, 1);
584 static st_dst_reg sampler_reladdr
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
, 2);
587 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...) PRINTFLIKE(2, 3);
590 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
594 ralloc_vasprintf_append(&prog
->data
->InfoLog
, fmt
, args
);
597 prog
->data
->LinkStatus
= linking_failure
;
601 swizzle_for_size(int size
)
603 static const int size_swizzles
[4] = {
604 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
605 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
606 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
607 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
610 assert((size
>= 1) && (size
<= 4));
611 return size_swizzles
[size
- 1];
615 is_resource_instruction(unsigned opcode
)
618 case TGSI_OPCODE_RESQ
:
619 case TGSI_OPCODE_LOAD
:
620 case TGSI_OPCODE_ATOMUADD
:
621 case TGSI_OPCODE_ATOMXCHG
:
622 case TGSI_OPCODE_ATOMCAS
:
623 case TGSI_OPCODE_ATOMAND
:
624 case TGSI_OPCODE_ATOMOR
:
625 case TGSI_OPCODE_ATOMXOR
:
626 case TGSI_OPCODE_ATOMUMIN
:
627 case TGSI_OPCODE_ATOMUMAX
:
628 case TGSI_OPCODE_ATOMIMIN
:
629 case TGSI_OPCODE_ATOMIMAX
:
637 num_inst_dst_regs(const glsl_to_tgsi_instruction
*op
)
639 return op
->info
->num_dst
;
643 num_inst_src_regs(const glsl_to_tgsi_instruction
*op
)
645 return op
->info
->is_tex
|| is_resource_instruction(op
->op
) ?
646 op
->info
->num_src
- 1 : op
->info
->num_src
;
649 glsl_to_tgsi_instruction
*
650 glsl_to_tgsi_visitor::emit_asm(ir_instruction
*ir
, unsigned op
,
651 st_dst_reg dst
, st_dst_reg dst1
,
652 st_src_reg src0
, st_src_reg src1
,
653 st_src_reg src2
, st_src_reg src3
)
655 glsl_to_tgsi_instruction
*inst
= new(mem_ctx
) glsl_to_tgsi_instruction();
656 int num_reladdr
= 0, i
, j
;
657 bool dst_is_64bit
[2];
659 op
= get_opcode(op
, dst
, src0
, src1
);
661 /* If we have to do relative addressing, we want to load the ARL
662 * reg directly for one of the regs, and preload the other reladdr
663 * sources into temps.
665 num_reladdr
+= dst
.reladdr
!= NULL
|| dst
.reladdr2
;
666 num_reladdr
+= dst1
.reladdr
!= NULL
|| dst1
.reladdr2
;
667 num_reladdr
+= src0
.reladdr
!= NULL
|| src0
.reladdr2
!= NULL
;
668 num_reladdr
+= src1
.reladdr
!= NULL
|| src1
.reladdr2
!= NULL
;
669 num_reladdr
+= src2
.reladdr
!= NULL
|| src2
.reladdr2
!= NULL
;
670 num_reladdr
+= src3
.reladdr
!= NULL
|| src3
.reladdr2
!= NULL
;
672 reladdr_to_temp(ir
, &src3
, &num_reladdr
);
673 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
674 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
675 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
677 if (dst
.reladdr
|| dst
.reladdr2
) {
679 emit_arl(ir
, address_reg
, *dst
.reladdr
);
681 emit_arl(ir
, address_reg2
, *dst
.reladdr2
);
685 emit_arl(ir
, address_reg
, *dst1
.reladdr
);
688 assert(num_reladdr
== 0);
690 /* inst->op has only 8 bits. */
691 STATIC_ASSERT(TGSI_OPCODE_LAST
<= 255);
694 inst
->info
= tgsi_get_opcode_info(op
);
701 inst
->is_64bit_expanded
= false;
704 inst
->tex_offsets
= NULL
;
705 inst
->tex_offset_num_offset
= 0;
707 inst
->tex_shadow
= 0;
708 /* default to float, for paths where this is not initialized
709 * (since 0==UINT which is likely wrong):
711 inst
->tex_type
= GLSL_TYPE_FLOAT
;
713 /* Update indirect addressing status used by TGSI */
714 if (dst
.reladdr
|| dst
.reladdr2
) {
716 case PROGRAM_STATE_VAR
:
717 case PROGRAM_CONSTANT
:
718 case PROGRAM_UNIFORM
:
719 this->indirect_addr_consts
= true;
721 case PROGRAM_IMMEDIATE
:
722 assert(!"immediates should not have indirect addressing");
729 for (i
= 0; i
< 4; i
++) {
730 if(inst
->src
[i
].reladdr
) {
731 switch(inst
->src
[i
].file
) {
732 case PROGRAM_STATE_VAR
:
733 case PROGRAM_CONSTANT
:
734 case PROGRAM_UNIFORM
:
735 this->indirect_addr_consts
= true;
737 case PROGRAM_IMMEDIATE
:
738 assert(!"immediates should not have indirect addressing");
748 * This section contains the double processing.
749 * GLSL just represents doubles as single channel values,
750 * however most HW and TGSI represent doubles as pairs of register channels.
752 * so we have to fixup destination writemask/index and src swizzle/indexes.
753 * dest writemasks need to translate from single channel write mask
754 * to a dual-channel writemask, but also need to modify the index,
755 * if we are touching the Z,W fields in the pre-translated writemask.
757 * src channels have similiar index modifications along with swizzle
758 * changes to we pick the XY, ZW pairs from the correct index.
760 * GLSL [0].x -> TGSI [0].xy
761 * GLSL [0].y -> TGSI [0].zw
762 * GLSL [0].z -> TGSI [1].xy
763 * GLSL [0].w -> TGSI [1].zw
765 for (j
= 0; j
< 2; j
++) {
766 dst_is_64bit
[j
] = glsl_base_type_is_64bit(inst
->dst
[j
].type
);
767 if (!dst_is_64bit
[j
] && inst
->dst
[j
].file
== PROGRAM_OUTPUT
&& inst
->dst
[j
].type
== GLSL_TYPE_ARRAY
) {
768 enum glsl_base_type type
= find_array_type(this->outputs
, this->num_outputs
, inst
->dst
[j
].array_id
);
769 if (glsl_base_type_is_64bit(type
))
770 dst_is_64bit
[j
] = true;
774 if (dst_is_64bit
[0] || dst_is_64bit
[1] ||
775 glsl_base_type_is_64bit(inst
->src
[0].type
)) {
776 glsl_to_tgsi_instruction
*dinst
= NULL
;
777 int initial_src_swz
[4], initial_src_idx
[4];
778 int initial_dst_idx
[2], initial_dst_writemask
[2];
779 /* select the writemask for dst0 or dst1 */
780 unsigned writemask
= inst
->dst
[1].file
== PROGRAM_UNDEFINED
? inst
->dst
[0].writemask
: inst
->dst
[1].writemask
;
782 /* copy out the writemask, index and swizzles for all src/dsts. */
783 for (j
= 0; j
< 2; j
++) {
784 initial_dst_writemask
[j
] = inst
->dst
[j
].writemask
;
785 initial_dst_idx
[j
] = inst
->dst
[j
].index
;
788 for (j
= 0; j
< 4; j
++) {
789 initial_src_swz
[j
] = inst
->src
[j
].swizzle
;
790 initial_src_idx
[j
] = inst
->src
[j
].index
;
794 * scan all the components in the dst writemask
795 * generate an instruction for each of them if required.
800 int i
= u_bit_scan(&writemask
);
802 /* before emitting the instruction, see if we have to adjust load / store
804 if (i
> 1 && (inst
->op
== TGSI_OPCODE_LOAD
|| inst
->op
== TGSI_OPCODE_STORE
) &&
805 addr
.file
== PROGRAM_UNDEFINED
) {
806 /* We have to advance the buffer address by 16 */
807 addr
= get_temp(glsl_type::uint_type
);
808 emit_asm(ir
, TGSI_OPCODE_UADD
, st_dst_reg(addr
),
809 inst
->src
[0], st_src_reg_for_int(16));
812 /* first time use previous instruction */
816 /* create a new instructions for subsequent attempts */
817 dinst
= new(mem_ctx
) glsl_to_tgsi_instruction();
822 this->instructions
.push_tail(dinst
);
823 dinst
->is_64bit_expanded
= true;
825 /* modify the destination if we are splitting */
826 for (j
= 0; j
< 2; j
++) {
827 if (dst_is_64bit
[j
]) {
828 dinst
->dst
[j
].writemask
= (i
& 1) ? WRITEMASK_ZW
: WRITEMASK_XY
;
829 dinst
->dst
[j
].index
= initial_dst_idx
[j
];
831 if (dinst
->op
== TGSI_OPCODE_LOAD
|| dinst
->op
== TGSI_OPCODE_STORE
)
832 dinst
->src
[0] = addr
;
833 if (dinst
->op
!= TGSI_OPCODE_STORE
)
834 dinst
->dst
[j
].index
++;
837 /* if we aren't writing to a double, just get the bit of the initial writemask
839 dinst
->dst
[j
].writemask
= initial_dst_writemask
[j
] & (1 << i
);
843 /* modify the src registers */
844 for (j
= 0; j
< 4; j
++) {
845 int swz
= GET_SWZ(initial_src_swz
[j
], i
);
847 if (glsl_base_type_is_64bit(dinst
->src
[j
].type
)) {
848 dinst
->src
[j
].index
= initial_src_idx
[j
];
850 dinst
->src
[j
].double_reg2
= true;
851 dinst
->src
[j
].index
++;
855 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_W
, SWIZZLE_Z
, SWIZZLE_W
);
857 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
860 /* some opcodes are special case in what they use as sources
861 - [FUI]2D/[UI]2I64 is a float/[u]int src0, DLDEXP is integer src1 */
862 if (op
== TGSI_OPCODE_F2D
|| op
== TGSI_OPCODE_U2D
|| op
== TGSI_OPCODE_I2D
||
863 op
== TGSI_OPCODE_I2I64
|| op
== TGSI_OPCODE_U2I64
||
864 op
== TGSI_OPCODE_DLDEXP
||
865 (op
== TGSI_OPCODE_UCMP
&& dst_is_64bit
[0])) {
866 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(swz
, swz
, swz
, swz
);
873 this->instructions
.push_tail(inst
);
880 glsl_to_tgsi_instruction
*
881 glsl_to_tgsi_visitor::emit_asm(ir_instruction
*ir
, unsigned op
,
883 st_src_reg src0
, st_src_reg src1
,
884 st_src_reg src2
, st_src_reg src3
)
886 return emit_asm(ir
, op
, dst
, undef_dst
, src0
, src1
, src2
, src3
);
890 * Determines whether to use an integer, unsigned integer, or float opcode
891 * based on the operands and input opcode, then emits the result.
894 glsl_to_tgsi_visitor::get_opcode(unsigned op
,
896 st_src_reg src0
, st_src_reg src1
)
898 enum glsl_base_type type
= GLSL_TYPE_FLOAT
;
900 if (op
== TGSI_OPCODE_MOV
)
903 assert(src0
.type
!= GLSL_TYPE_ARRAY
);
904 assert(src0
.type
!= GLSL_TYPE_STRUCT
);
905 assert(src1
.type
!= GLSL_TYPE_ARRAY
);
906 assert(src1
.type
!= GLSL_TYPE_STRUCT
);
908 if (is_resource_instruction(op
))
910 else if (src0
.type
== GLSL_TYPE_INT64
|| src1
.type
== GLSL_TYPE_INT64
)
911 type
= GLSL_TYPE_INT64
;
912 else if (src0
.type
== GLSL_TYPE_UINT64
|| src1
.type
== GLSL_TYPE_UINT64
)
913 type
= GLSL_TYPE_UINT64
;
914 else if (src0
.type
== GLSL_TYPE_DOUBLE
|| src1
.type
== GLSL_TYPE_DOUBLE
)
915 type
= GLSL_TYPE_DOUBLE
;
916 else if (src0
.type
== GLSL_TYPE_FLOAT
|| src1
.type
== GLSL_TYPE_FLOAT
)
917 type
= GLSL_TYPE_FLOAT
;
918 else if (native_integers
)
919 type
= src0
.type
== GLSL_TYPE_BOOL
? GLSL_TYPE_INT
: src0
.type
;
921 #define case7(c, f, i, u, d, i64, ui64) \
922 case TGSI_OPCODE_##c: \
923 if (type == GLSL_TYPE_UINT64) \
924 op = TGSI_OPCODE_##ui64; \
925 else if (type == GLSL_TYPE_INT64) \
926 op = TGSI_OPCODE_##i64; \
927 else if (type == GLSL_TYPE_DOUBLE) \
928 op = TGSI_OPCODE_##d; \
929 else if (type == GLSL_TYPE_INT) \
930 op = TGSI_OPCODE_##i; \
931 else if (type == GLSL_TYPE_UINT) \
932 op = TGSI_OPCODE_##u; \
934 op = TGSI_OPCODE_##f; \
936 #define case5(c, f, i, u, d) \
937 case TGSI_OPCODE_##c: \
938 if (type == GLSL_TYPE_DOUBLE) \
939 op = TGSI_OPCODE_##d; \
940 else if (type == GLSL_TYPE_INT) \
941 op = TGSI_OPCODE_##i; \
942 else if (type == GLSL_TYPE_UINT) \
943 op = TGSI_OPCODE_##u; \
945 op = TGSI_OPCODE_##f; \
948 #define case4(c, f, i, u) \
949 case TGSI_OPCODE_##c: \
950 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 case3(f, i, u) case4(f, f, i, u)
959 #define case6d(f, i, u, d, i64, u64) case7(f, f, i, u, d, i64, u64)
960 #define case3fid(f, i, d) case5(f, f, i, i, d)
961 #define case3fid64(f, i, d, i64) case7(f, f, i, i, d, i64, i64)
962 #define case2fi(f, i) case4(f, f, i, i)
963 #define case2iu(i, u) case4(i, LAST, i, u)
965 #define case2iu64(i, i64) case7(i, LAST, i, i, LAST, i64, i64)
966 #define case4iu64(i, u, i64, u64) case7(i, LAST, i, u, LAST, i64, u64)
968 #define casecomp(c, f, i, u, d, i64, ui64) \
969 case TGSI_OPCODE_##c: \
970 if (type == GLSL_TYPE_INT64) \
971 op = TGSI_OPCODE_##i64; \
972 else if (type == GLSL_TYPE_UINT64) \
973 op = TGSI_OPCODE_##ui64; \
974 else if (type == GLSL_TYPE_DOUBLE) \
975 op = TGSI_OPCODE_##d; \
976 else if (type == GLSL_TYPE_INT || type == GLSL_TYPE_SUBROUTINE) \
977 op = TGSI_OPCODE_##i; \
978 else if (type == GLSL_TYPE_UINT) \
979 op = TGSI_OPCODE_##u; \
980 else if (native_integers) \
981 op = TGSI_OPCODE_##f; \
983 op = TGSI_OPCODE_##c; \
987 case3fid64(ADD
, UADD
, DADD
, U64ADD
);
988 case3fid64(MUL
, UMUL
, DMUL
, U64MUL
);
989 case3fid(MAD
, UMAD
, DMAD
);
990 case3fid(FMA
, UMAD
, DFMA
);
991 case6d(DIV
, IDIV
, UDIV
, DDIV
, I64DIV
, U64DIV
);
992 case6d(MAX
, IMAX
, UMAX
, DMAX
, I64MAX
, U64MAX
);
993 case6d(MIN
, IMIN
, UMIN
, DMIN
, I64MIN
, U64MIN
);
994 case4iu64(MOD
, UMOD
, I64MOD
, U64MOD
);
996 casecomp(SEQ
, FSEQ
, USEQ
, USEQ
, DSEQ
, U64SEQ
, U64SEQ
);
997 casecomp(SNE
, FSNE
, USNE
, USNE
, DSNE
, U64SNE
, U64SNE
);
998 casecomp(SGE
, FSGE
, ISGE
, USGE
, DSGE
, I64SGE
, U64SGE
);
999 casecomp(SLT
, FSLT
, ISLT
, USLT
, DSLT
, I64SLT
, U64SLT
);
1001 case2iu64(SHL
, U64SHL
);
1002 case4iu64(ISHR
, USHR
, I64SHR
, U64SHR
);
1004 case3fid64(SSG
, ISSG
, DSSG
, I64SSG
);
1006 case2iu(IBFE
, UBFE
);
1007 case2iu(IMSB
, UMSB
);
1008 case2iu(IMUL_HI
, UMUL_HI
);
1010 case3fid(SQRT
, SQRT
, DSQRT
);
1012 case3fid(RCP
, RCP
, DRCP
);
1013 case3fid(RSQ
, RSQ
, DRSQ
);
1015 case3fid(FRC
, FRC
, DFRAC
);
1016 case3fid(TRUNC
, TRUNC
, DTRUNC
);
1017 case3fid(CEIL
, CEIL
, DCEIL
);
1018 case3fid(FLR
, FLR
, DFLR
);
1019 case3fid(ROUND
, ROUND
, DROUND
);
1021 case2iu(ATOMIMAX
, ATOMUMAX
);
1022 case2iu(ATOMIMIN
, ATOMUMIN
);
1027 assert(op
!= TGSI_OPCODE_LAST
);
1031 glsl_to_tgsi_instruction
*
1032 glsl_to_tgsi_visitor::emit_dp(ir_instruction
*ir
,
1033 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
,
1036 static const unsigned dot_opcodes
[] = {
1037 TGSI_OPCODE_DP2
, TGSI_OPCODE_DP3
, TGSI_OPCODE_DP4
1040 return emit_asm(ir
, dot_opcodes
[elements
- 2], dst
, src0
, src1
);
1044 * Emits TGSI scalar opcodes to produce unique answers across channels.
1046 * Some TGSI opcodes are scalar-only, like ARB_fp/vp. The src X
1047 * channel determines the result across all channels. So to do a vec4
1048 * of this operation, we want to emit a scalar per source channel used
1049 * to produce dest channels.
1052 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
1054 st_src_reg orig_src0
, st_src_reg orig_src1
)
1057 int done_mask
= ~dst
.writemask
;
1059 /* TGSI RCP is a scalar operation splatting results to all channels,
1060 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
1063 for (i
= 0; i
< 4; i
++) {
1064 GLuint this_mask
= (1 << i
);
1065 st_src_reg src0
= orig_src0
;
1066 st_src_reg src1
= orig_src1
;
1068 if (done_mask
& this_mask
)
1071 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
1072 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
1073 for (j
= i
+ 1; j
< 4; j
++) {
1074 /* If there is another enabled component in the destination that is
1075 * derived from the same inputs, generate its value on this pass as
1078 if (!(done_mask
& (1 << j
)) &&
1079 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
1080 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
1081 this_mask
|= (1 << j
);
1084 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
1085 src0_swiz
, src0_swiz
);
1086 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
1087 src1_swiz
, src1_swiz
);
1089 dst
.writemask
= this_mask
;
1090 emit_asm(ir
, op
, dst
, src0
, src1
);
1091 done_mask
|= this_mask
;
1096 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
1097 st_dst_reg dst
, st_src_reg src0
)
1099 st_src_reg undef
= undef_src
;
1101 undef
.swizzle
= SWIZZLE_XXXX
;
1103 emit_scalar(ir
, op
, dst
, src0
, undef
);
1107 glsl_to_tgsi_visitor::emit_arl(ir_instruction
*ir
,
1108 st_dst_reg dst
, st_src_reg src0
)
1110 int op
= TGSI_OPCODE_ARL
;
1112 if (src0
.type
== GLSL_TYPE_INT
|| src0
.type
== GLSL_TYPE_UINT
)
1113 op
= TGSI_OPCODE_UARL
;
1115 assert(dst
.file
== PROGRAM_ADDRESS
);
1116 if (dst
.index
>= this->num_address_regs
)
1117 this->num_address_regs
= dst
.index
+ 1;
1119 emit_asm(NULL
, op
, dst
, src0
);
1123 glsl_to_tgsi_visitor::add_constant(gl_register_file file
,
1124 gl_constant_value values
[8], int size
, int datatype
,
1125 uint16_t *swizzle_out
)
1127 if (file
== PROGRAM_CONSTANT
) {
1128 GLuint swizzle
= swizzle_out
? *swizzle_out
: 0;
1129 int result
= _mesa_add_typed_unnamed_constant(this->prog
->Parameters
, values
,
1130 size
, datatype
, &swizzle
);
1132 *swizzle_out
= swizzle
;
1136 assert(file
== PROGRAM_IMMEDIATE
);
1139 immediate_storage
*entry
;
1140 int size32
= size
* ((datatype
== GL_DOUBLE
||
1141 datatype
== GL_INT64_ARB
||
1142 datatype
== GL_UNSIGNED_INT64_ARB
)? 2 : 1);
1145 /* Search immediate storage to see if we already have an identical
1146 * immediate that we can use instead of adding a duplicate entry.
1148 foreach_in_list(immediate_storage
, entry
, &this->immediates
) {
1149 immediate_storage
*tmp
= entry
;
1151 for (i
= 0; i
* 4 < size32
; i
++) {
1152 int slot_size
= MIN2(size32
- (i
* 4), 4);
1153 if (tmp
->type
!= datatype
|| tmp
->size32
!= slot_size
)
1155 if (memcmp(tmp
->values
, &values
[i
* 4],
1156 slot_size
* sizeof(gl_constant_value
)))
1159 /* Everything matches, keep going until the full size is matched */
1160 tmp
= (immediate_storage
*)tmp
->next
;
1163 /* The full value matched */
1164 if (i
* 4 >= size32
)
1170 for (i
= 0; i
* 4 < size32
; i
++) {
1171 int slot_size
= MIN2(size32
- (i
* 4), 4);
1172 /* Add this immediate to the list. */
1173 entry
= new(mem_ctx
) immediate_storage(&values
[i
* 4], slot_size
, datatype
);
1174 this->immediates
.push_tail(entry
);
1175 this->num_immediates
++;
1181 glsl_to_tgsi_visitor::st_src_reg_for_float(float val
)
1183 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_FLOAT
);
1184 union gl_constant_value uval
;
1187 src
.index
= add_constant(src
.file
, &uval
, 1, GL_FLOAT
, &src
.swizzle
);
1193 glsl_to_tgsi_visitor::st_src_reg_for_double(double val
)
1195 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_DOUBLE
);
1196 union gl_constant_value uval
[2];
1198 memcpy(uval
, &val
, sizeof(uval
));
1199 src
.index
= add_constant(src
.file
, uval
, 1, GL_DOUBLE
, &src
.swizzle
);
1200 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
1205 glsl_to_tgsi_visitor::st_src_reg_for_int(int val
)
1207 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_INT
);
1208 union gl_constant_value uval
;
1210 assert(native_integers
);
1213 src
.index
= add_constant(src
.file
, &uval
, 1, GL_INT
, &src
.swizzle
);
1219 glsl_to_tgsi_visitor::st_src_reg_for_type(enum glsl_base_type type
, int val
)
1221 if (native_integers
)
1222 return type
== GLSL_TYPE_FLOAT
? st_src_reg_for_float(val
) :
1223 st_src_reg_for_int(val
);
1225 return st_src_reg_for_float(val
);
1229 attrib_type_size(const struct glsl_type
*type
, bool is_vs_input
)
1231 return st_glsl_attrib_type_size(type
, is_vs_input
);
1235 type_size(const struct glsl_type
*type
)
1237 return st_glsl_type_size(type
);
1241 * If the given GLSL type is an array or matrix or a structure containing
1242 * an array/matrix member, return true. Else return false.
1244 * This is used to determine which kind of temp storage (PROGRAM_TEMPORARY
1245 * or PROGRAM_ARRAY) should be used for variables of this type. Anytime
1246 * we have an array that might be indexed with a variable, we need to use
1247 * the later storage type.
1250 type_has_array_or_matrix(const glsl_type
*type
)
1252 if (type
->is_array() || type
->is_matrix())
1255 if (type
->is_record()) {
1256 for (unsigned i
= 0; i
< type
->length
; i
++) {
1257 if (type_has_array_or_matrix(type
->fields
.structure
[i
].type
)) {
1268 * In the initial pass of codegen, we assign temporary numbers to
1269 * intermediate results. (not SSA -- variable assignments will reuse
1273 glsl_to_tgsi_visitor::get_temp(const glsl_type
*type
)
1277 src
.type
= native_integers
? type
->base_type
: GLSL_TYPE_FLOAT
;
1282 if (!options
->EmitNoIndirectTemp
&& type_has_array_or_matrix(type
)) {
1283 if (next_array
>= max_num_arrays
) {
1284 max_num_arrays
+= 32;
1285 array_sizes
= (unsigned*)
1286 realloc(array_sizes
, sizeof(array_sizes
[0]) * max_num_arrays
);
1289 src
.file
= PROGRAM_ARRAY
;
1291 src
.array_id
= next_array
+ 1;
1292 array_sizes
[next_array
] = type_size(type
);
1296 src
.file
= PROGRAM_TEMPORARY
;
1297 src
.index
= next_temp
;
1298 next_temp
+= type_size(type
);
1301 if (type
->is_array() || type
->is_record()) {
1302 src
.swizzle
= SWIZZLE_NOOP
;
1304 src
.swizzle
= swizzle_for_size(type
->vector_elements
);
1311 glsl_to_tgsi_visitor::find_variable_storage(ir_variable
*var
)
1313 struct hash_entry
*entry
;
1315 entry
= _mesa_hash_table_search(this->variables
, var
);
1319 return (variable_storage
*)entry
->data
;
1323 glsl_to_tgsi_visitor::visit(ir_variable
*ir
)
1325 if (strcmp(ir
->name
, "gl_FragCoord") == 0) {
1326 this->prog
->OriginUpperLeft
= ir
->data
.origin_upper_left
;
1327 this->prog
->PixelCenterInteger
= ir
->data
.pixel_center_integer
;
1330 if (ir
->data
.mode
== ir_var_uniform
&& strncmp(ir
->name
, "gl_", 3) == 0) {
1332 const ir_state_slot
*const slots
= ir
->get_state_slots();
1333 assert(slots
!= NULL
);
1335 /* Check if this statevar's setup in the STATE file exactly
1336 * matches how we'll want to reference it as a
1337 * struct/array/whatever. If not, then we need to move it into
1338 * temporary storage and hope that it'll get copy-propagated
1341 for (i
= 0; i
< ir
->get_num_state_slots(); i
++) {
1342 if (slots
[i
].swizzle
!= SWIZZLE_XYZW
) {
1347 variable_storage
*storage
;
1349 if (i
== ir
->get_num_state_slots()) {
1350 /* We'll set the index later. */
1351 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_STATE_VAR
, -1);
1353 _mesa_hash_table_insert(this->variables
, ir
, storage
);
1357 /* The variable_storage constructor allocates slots based on the size
1358 * of the type. However, this had better match the number of state
1359 * elements that we're going to copy into the new temporary.
1361 assert((int) ir
->get_num_state_slots() == type_size(ir
->type
));
1363 dst
= st_dst_reg(get_temp(ir
->type
));
1365 storage
= new(mem_ctx
) variable_storage(ir
, dst
.file
, dst
.index
,
1368 _mesa_hash_table_insert(this->variables
, ir
, storage
);
1372 for (unsigned int i
= 0; i
< ir
->get_num_state_slots(); i
++) {
1373 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
1374 (gl_state_index
*)slots
[i
].tokens
);
1376 if (storage
->file
== PROGRAM_STATE_VAR
) {
1377 if (storage
->index
== -1) {
1378 storage
->index
= index
;
1380 assert(index
== storage
->index
+ (int)i
);
1383 /* We use GLSL_TYPE_FLOAT here regardless of the actual type of
1384 * the data being moved since MOV does not care about the type of
1385 * data it is moving, and we don't want to declare registers with
1386 * array or struct types.
1388 st_src_reg
src(PROGRAM_STATE_VAR
, index
, GLSL_TYPE_FLOAT
);
1389 src
.swizzle
= slots
[i
].swizzle
;
1390 emit_asm(ir
, TGSI_OPCODE_MOV
, dst
, src
);
1391 /* even a float takes up a whole vec4 reg in a struct/array. */
1396 if (storage
->file
== PROGRAM_TEMPORARY
&&
1397 dst
.index
!= storage
->index
+ (int) ir
->get_num_state_slots()) {
1398 fail_link(this->shader_program
,
1399 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
1400 ir
->name
, dst
.index
- storage
->index
,
1401 type_size(ir
->type
));
1407 glsl_to_tgsi_visitor::visit(ir_loop
*ir
)
1409 emit_asm(NULL
, TGSI_OPCODE_BGNLOOP
);
1411 visit_exec_list(&ir
->body_instructions
, this);
1413 emit_asm(NULL
, TGSI_OPCODE_ENDLOOP
);
1417 glsl_to_tgsi_visitor::visit(ir_loop_jump
*ir
)
1420 case ir_loop_jump::jump_break
:
1421 emit_asm(NULL
, TGSI_OPCODE_BRK
);
1423 case ir_loop_jump::jump_continue
:
1424 emit_asm(NULL
, TGSI_OPCODE_CONT
);
1431 glsl_to_tgsi_visitor::visit(ir_function_signature
*ir
)
1438 glsl_to_tgsi_visitor::visit(ir_function
*ir
)
1440 /* Ignore function bodies other than main() -- we shouldn't see calls to
1441 * them since they should all be inlined before we get to glsl_to_tgsi.
1443 if (strcmp(ir
->name
, "main") == 0) {
1444 const ir_function_signature
*sig
;
1447 sig
= ir
->matching_signature(NULL
, &empty
, false);
1451 foreach_in_list(ir_instruction
, ir
, &sig
->body
) {
1458 glsl_to_tgsi_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
1460 int nonmul_operand
= 1 - mul_operand
;
1462 st_dst_reg result_dst
;
1464 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
1465 if (!expr
|| expr
->operation
!= ir_binop_mul
)
1468 expr
->operands
[0]->accept(this);
1470 expr
->operands
[1]->accept(this);
1472 ir
->operands
[nonmul_operand
]->accept(this);
1475 this->result
= get_temp(ir
->type
);
1476 result_dst
= st_dst_reg(this->result
);
1477 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1478 emit_asm(ir
, TGSI_OPCODE_MAD
, result_dst
, a
, b
, c
);
1484 * Emit MAD(a, -b, a) instead of AND(a, NOT(b))
1486 * The logic values are 1.0 for true and 0.0 for false. Logical-and is
1487 * implemented using multiplication, and logical-or is implemented using
1488 * addition. Logical-not can be implemented as (true - x), or (1.0 - x).
1489 * As result, the logical expression (a & !b) can be rewritten as:
1493 * - (a * 1) - (a * b)
1497 * This final expression can be implemented as a single MAD(a, -b, a)
1501 glsl_to_tgsi_visitor::try_emit_mad_for_and_not(ir_expression
*ir
, int try_operand
)
1503 const int other_operand
= 1 - try_operand
;
1506 ir_expression
*expr
= ir
->operands
[try_operand
]->as_expression();
1507 if (!expr
|| expr
->operation
!= ir_unop_logic_not
)
1510 ir
->operands
[other_operand
]->accept(this);
1512 expr
->operands
[0]->accept(this);
1515 b
.negate
= ~b
.negate
;
1517 this->result
= get_temp(ir
->type
);
1518 emit_asm(ir
, TGSI_OPCODE_MAD
, st_dst_reg(this->result
), a
, b
, a
);
1524 glsl_to_tgsi_visitor::reladdr_to_temp(ir_instruction
*ir
,
1525 st_src_reg
*reg
, int *num_reladdr
)
1527 if (!reg
->reladdr
&& !reg
->reladdr2
)
1530 if (reg
->reladdr
) emit_arl(ir
, address_reg
, *reg
->reladdr
);
1531 if (reg
->reladdr2
) emit_arl(ir
, address_reg2
, *reg
->reladdr2
);
1533 if (*num_reladdr
!= 1) {
1534 st_src_reg temp
= get_temp(reg
->type
== GLSL_TYPE_DOUBLE
? glsl_type::dvec4_type
: glsl_type::vec4_type
);
1536 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), *reg
);
1544 glsl_to_tgsi_visitor::visit(ir_expression
*ir
)
1546 st_src_reg op
[ARRAY_SIZE(ir
->operands
)];
1548 /* Quick peephole: Emit MAD(a, b, c) instead of ADD(MUL(a, b), c)
1550 if (ir
->operation
== ir_binop_add
) {
1551 if (try_emit_mad(ir
, 1))
1553 if (try_emit_mad(ir
, 0))
1557 /* Quick peephole: Emit OPCODE_MAD(-a, -b, a) instead of AND(a, NOT(b))
1559 if (!native_integers
&& ir
->operation
== ir_binop_logic_and
) {
1560 if (try_emit_mad_for_and_not(ir
, 1))
1562 if (try_emit_mad_for_and_not(ir
, 0))
1566 if (ir
->operation
== ir_quadop_vector
)
1567 assert(!"ir_quadop_vector should have been lowered");
1569 for (unsigned int operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1570 this->result
.file
= PROGRAM_UNDEFINED
;
1571 ir
->operands
[operand
]->accept(this);
1572 if (this->result
.file
== PROGRAM_UNDEFINED
) {
1573 printf("Failed to get tree for expression operand:\n");
1574 ir
->operands
[operand
]->print();
1578 op
[operand
] = this->result
;
1580 /* Matrix expression operands should have been broken down to vector
1581 * operations already.
1583 assert(!ir
->operands
[operand
]->type
->is_matrix());
1586 visit_expression(ir
, op
);
1589 /* The non-recursive part of the expression visitor lives in a separate
1590 * function and should be prevented from being inlined, to avoid a stack
1591 * explosion when deeply nested expressions are visited.
1594 glsl_to_tgsi_visitor::visit_expression(ir_expression
* ir
, st_src_reg
*op
)
1596 st_src_reg result_src
;
1597 st_dst_reg result_dst
;
1599 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1600 if (ir
->operands
[1]) {
1601 vector_elements
= MAX2(vector_elements
,
1602 ir
->operands
[1]->type
->vector_elements
);
1605 this->result
.file
= PROGRAM_UNDEFINED
;
1607 /* Storage for our result. Ideally for an assignment we'd be using
1608 * the actual storage for the result here, instead.
1610 result_src
= get_temp(ir
->type
);
1611 /* convenience for the emit functions below. */
1612 result_dst
= st_dst_reg(result_src
);
1613 /* Limit writes to the channels that will be used by result_src later.
1614 * This does limit this temp's use as a temporary for multi-instruction
1617 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1619 switch (ir
->operation
) {
1620 case ir_unop_logic_not
:
1621 if (result_dst
.type
!= GLSL_TYPE_FLOAT
)
1622 emit_asm(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1624 /* Previously 'SEQ dst, src, 0.0' was used for this. However, many
1625 * older GPUs implement SEQ using multiple instructions (i915 uses two
1626 * SGE instructions and a MUL instruction). Since our logic values are
1627 * 0.0 and 1.0, 1-x also implements !x.
1629 op
[0].negate
= ~op
[0].negate
;
1630 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], st_src_reg_for_float(1.0));
1634 if (result_dst
.type
== GLSL_TYPE_INT64
|| result_dst
.type
== GLSL_TYPE_UINT64
)
1635 emit_asm(ir
, TGSI_OPCODE_I64NEG
, result_dst
, op
[0]);
1636 else if (result_dst
.type
== GLSL_TYPE_INT
|| result_dst
.type
== GLSL_TYPE_UINT
)
1637 emit_asm(ir
, TGSI_OPCODE_INEG
, result_dst
, op
[0]);
1638 else if (result_dst
.type
== GLSL_TYPE_DOUBLE
)
1639 emit_asm(ir
, TGSI_OPCODE_DNEG
, result_dst
, op
[0]);
1641 op
[0].negate
= ~op
[0].negate
;
1645 case ir_unop_subroutine_to_int
:
1646 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
1649 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1650 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0].get_abs());
1651 else if (result_dst
.type
== GLSL_TYPE_DOUBLE
)
1652 emit_asm(ir
, TGSI_OPCODE_DABS
, result_dst
, op
[0]);
1653 else if (result_dst
.type
== GLSL_TYPE_INT64
|| result_dst
.type
== GLSL_TYPE_UINT64
)
1654 emit_asm(ir
, TGSI_OPCODE_I64ABS
, result_dst
, op
[0]);
1656 emit_asm(ir
, TGSI_OPCODE_IABS
, result_dst
, op
[0]);
1659 emit_asm(ir
, TGSI_OPCODE_SSG
, result_dst
, op
[0]);
1662 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, op
[0]);
1666 emit_scalar(ir
, TGSI_OPCODE_EX2
, result_dst
, op
[0]);
1669 assert(!"not reached: should be handled by exp_to_exp2");
1672 assert(!"not reached: should be handled by log_to_log2");
1675 emit_scalar(ir
, TGSI_OPCODE_LG2
, result_dst
, op
[0]);
1678 emit_scalar(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1681 emit_scalar(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1683 case ir_unop_saturate
: {
1684 glsl_to_tgsi_instruction
*inst
;
1685 inst
= emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
1686 inst
->saturate
= true;
1691 case ir_unop_dFdx_coarse
:
1692 emit_asm(ir
, TGSI_OPCODE_DDX
, result_dst
, op
[0]);
1694 case ir_unop_dFdx_fine
:
1695 emit_asm(ir
, TGSI_OPCODE_DDX_FINE
, result_dst
, op
[0]);
1698 case ir_unop_dFdy_coarse
:
1699 case ir_unop_dFdy_fine
:
1701 /* The X component contains 1 or -1 depending on whether the framebuffer
1702 * is a FBO or the window system buffer, respectively.
1703 * It is then multiplied with the source operand of DDY.
1705 static const gl_state_index transform_y_state
[STATE_LENGTH
]
1706 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
};
1708 unsigned transform_y_index
=
1709 _mesa_add_state_reference(this->prog
->Parameters
,
1712 st_src_reg transform_y
= st_src_reg(PROGRAM_STATE_VAR
,
1714 glsl_type::vec4_type
);
1715 transform_y
.swizzle
= SWIZZLE_XXXX
;
1717 st_src_reg temp
= get_temp(glsl_type::vec4_type
);
1719 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(temp
), transform_y
, op
[0]);
1720 emit_asm(ir
, ir
->operation
== ir_unop_dFdy_fine
?
1721 TGSI_OPCODE_DDY_FINE
: TGSI_OPCODE_DDY
, result_dst
, temp
);
1725 case ir_unop_frexp_sig
:
1726 emit_asm(ir
, TGSI_OPCODE_DFRACEXP
, result_dst
, undef_dst
, op
[0]);
1729 case ir_unop_frexp_exp
:
1730 emit_asm(ir
, TGSI_OPCODE_DFRACEXP
, undef_dst
, result_dst
, op
[0]);
1733 case ir_unop_noise
: {
1734 /* At some point, a motivated person could add a better
1735 * implementation of noise. Currently not even the nvidia
1736 * binary drivers do anything more than this. In any case, the
1737 * place to do this is in the GL state tracker, not the poor
1740 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, st_src_reg_for_float(0.5));
1745 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1748 op
[1].negate
= ~op
[1].negate
;
1749 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1753 emit_asm(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1756 emit_asm(ir
, TGSI_OPCODE_DIV
, result_dst
, op
[0], op
[1]);
1759 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1760 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1762 emit_asm(ir
, TGSI_OPCODE_MOD
, result_dst
, op
[0], op
[1]);
1766 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[0], op
[1]);
1768 case ir_binop_greater
:
1769 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[1], op
[0]);
1771 case ir_binop_lequal
:
1772 emit_asm(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[1], op
[0]);
1774 case ir_binop_gequal
:
1775 emit_asm(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[0], op
[1]);
1777 case ir_binop_equal
:
1778 emit_asm(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1780 case ir_binop_nequal
:
1781 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1783 case ir_binop_all_equal
:
1784 /* "==" operator producing a scalar boolean. */
1785 if (ir
->operands
[0]->type
->is_vector() ||
1786 ir
->operands
[1]->type
->is_vector()) {
1787 st_src_reg temp
= get_temp(native_integers
?
1788 glsl_type::uvec4_type
:
1789 glsl_type::vec4_type
);
1791 if (native_integers
) {
1792 st_dst_reg temp_dst
= st_dst_reg(temp
);
1793 st_src_reg temp1
= st_src_reg(temp
), temp2
= st_src_reg(temp
);
1795 if (ir
->operands
[0]->type
->is_boolean() &&
1796 ir
->operands
[1]->as_constant() &&
1797 ir
->operands
[1]->as_constant()->is_one()) {
1798 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), op
[0]);
1800 emit_asm(ir
, TGSI_OPCODE_SEQ
, st_dst_reg(temp
), op
[0], op
[1]);
1803 /* Emit 1-3 AND operations to combine the SEQ results. */
1804 switch (ir
->operands
[0]->type
->vector_elements
) {
1808 temp_dst
.writemask
= WRITEMASK_Y
;
1809 temp1
.swizzle
= SWIZZLE_YYYY
;
1810 temp2
.swizzle
= SWIZZLE_ZZZZ
;
1811 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1814 temp_dst
.writemask
= WRITEMASK_X
;
1815 temp1
.swizzle
= SWIZZLE_XXXX
;
1816 temp2
.swizzle
= SWIZZLE_YYYY
;
1817 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1818 temp_dst
.writemask
= WRITEMASK_Y
;
1819 temp1
.swizzle
= SWIZZLE_ZZZZ
;
1820 temp2
.swizzle
= SWIZZLE_WWWW
;
1821 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1824 temp1
.swizzle
= SWIZZLE_XXXX
;
1825 temp2
.swizzle
= SWIZZLE_YYYY
;
1826 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, temp1
, temp2
);
1828 emit_asm(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1830 /* After the dot-product, the value will be an integer on the
1831 * range [0,4]. Zero becomes 1.0, and positive values become zero.
1833 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1835 /* Negating the result of the dot-product gives values on the range
1836 * [-4, 0]. Zero becomes 1.0, and negative values become zero.
1837 * This is achieved using SGE.
1839 st_src_reg sge_src
= result_src
;
1840 sge_src
.negate
= ~sge_src
.negate
;
1841 emit_asm(ir
, TGSI_OPCODE_SGE
, result_dst
, sge_src
, st_src_reg_for_float(0.0));
1844 emit_asm(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1847 case ir_binop_any_nequal
:
1848 /* "!=" operator producing a scalar boolean. */
1849 if (ir
->operands
[0]->type
->is_vector() ||
1850 ir
->operands
[1]->type
->is_vector()) {
1851 st_src_reg temp
= get_temp(native_integers
?
1852 glsl_type::uvec4_type
:
1853 glsl_type::vec4_type
);
1854 if (ir
->operands
[0]->type
->is_boolean() &&
1855 ir
->operands
[1]->as_constant() &&
1856 ir
->operands
[1]->as_constant()->is_zero()) {
1857 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), op
[0]);
1859 emit_asm(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1862 if (native_integers
) {
1863 st_dst_reg temp_dst
= st_dst_reg(temp
);
1864 st_src_reg temp1
= st_src_reg(temp
), temp2
= st_src_reg(temp
);
1866 /* Emit 1-3 OR operations to combine the SNE results. */
1867 switch (ir
->operands
[0]->type
->vector_elements
) {
1871 temp_dst
.writemask
= WRITEMASK_Y
;
1872 temp1
.swizzle
= SWIZZLE_YYYY
;
1873 temp2
.swizzle
= SWIZZLE_ZZZZ
;
1874 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1877 temp_dst
.writemask
= WRITEMASK_X
;
1878 temp1
.swizzle
= SWIZZLE_XXXX
;
1879 temp2
.swizzle
= SWIZZLE_YYYY
;
1880 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1881 temp_dst
.writemask
= WRITEMASK_Y
;
1882 temp1
.swizzle
= SWIZZLE_ZZZZ
;
1883 temp2
.swizzle
= SWIZZLE_WWWW
;
1884 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1887 temp1
.swizzle
= SWIZZLE_XXXX
;
1888 temp2
.swizzle
= SWIZZLE_YYYY
;
1889 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, temp1
, temp2
);
1891 /* After the dot-product, the value will be an integer on the
1892 * range [0,4]. Zero stays zero, and positive values become 1.0.
1894 glsl_to_tgsi_instruction
*const dp
=
1895 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1896 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1897 /* The clamping to [0,1] can be done for free in the fragment
1898 * shader with a saturate.
1900 dp
->saturate
= true;
1902 /* Negating the result of the dot-product gives values on the range
1903 * [-4, 0]. Zero stays zero, and negative values become 1.0. This
1904 * achieved using SLT.
1906 st_src_reg slt_src
= result_src
;
1907 slt_src
.negate
= ~slt_src
.negate
;
1908 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1912 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1916 case ir_binop_logic_xor
:
1917 if (native_integers
)
1918 emit_asm(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0], op
[1]);
1920 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1923 case ir_binop_logic_or
: {
1924 if (native_integers
) {
1925 /* If integers are used as booleans, we can use an actual "or"
1928 assert(native_integers
);
1929 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0], op
[1]);
1931 /* After the addition, the value will be an integer on the
1932 * range [0,2]. Zero stays zero, and positive values become 1.0.
1934 glsl_to_tgsi_instruction
*add
=
1935 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1936 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1937 /* The clamping to [0,1] can be done for free in the fragment
1938 * shader with a saturate if floats are being used as boolean values.
1940 add
->saturate
= true;
1942 /* Negating the result of the addition gives values on the range
1943 * [-2, 0]. Zero stays zero, and negative values become 1.0. This
1944 * is achieved using SLT.
1946 st_src_reg slt_src
= result_src
;
1947 slt_src
.negate
= ~slt_src
.negate
;
1948 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1954 case ir_binop_logic_and
:
1955 /* If native integers are disabled, the bool args are stored as float 0.0
1956 * or 1.0, so "mul" gives us "and". If they're enabled, just use the
1957 * actual AND opcode.
1959 if (native_integers
)
1960 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], op
[1]);
1962 emit_asm(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1966 assert(ir
->operands
[0]->type
->is_vector());
1967 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1968 emit_dp(ir
, result_dst
, op
[0], op
[1],
1969 ir
->operands
[0]->type
->vector_elements
);
1974 emit_scalar(ir
, TGSI_OPCODE_SQRT
, result_dst
, op
[0]);
1976 /* This is the only instruction sequence that makes the game "Risen"
1977 * render correctly. ABS is not required for the game, but since GLSL
1978 * declares negative values as "undefined", allowing us to do whatever
1979 * we want, I choose to use ABS to match DX9 and pre-GLSL RSQ
1982 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0].get_abs());
1983 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, result_src
);
1987 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1990 if (native_integers
) {
1991 emit_asm(ir
, TGSI_OPCODE_I2F
, result_dst
, op
[0]);
1994 /* fallthrough to next case otherwise */
1996 if (native_integers
) {
1997 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], st_src_reg_for_float(1.0));
2000 /* fallthrough to next case otherwise */
2003 case ir_unop_i642u64
:
2004 case ir_unop_u642i64
:
2005 /* Converting between signed and unsigned integers is a no-op. */
2007 result_src
.type
= result_dst
.type
;
2010 if (native_integers
) {
2011 /* Booleans are stored as integers using ~0 for true and 0 for false.
2012 * GLSL requires that int(bool) return 1 for true and 0 for false.
2013 * This conversion is done with AND, but it could be done with NEG.
2015 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], st_src_reg_for_int(1));
2017 /* Booleans and integers are both stored as floats when native
2018 * integers are disabled.
2024 if (native_integers
)
2025 emit_asm(ir
, TGSI_OPCODE_F2I
, result_dst
, op
[0]);
2027 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
2030 if (native_integers
)
2031 emit_asm(ir
, TGSI_OPCODE_F2U
, result_dst
, op
[0]);
2033 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
2035 case ir_unop_bitcast_f2i
:
2036 case ir_unop_bitcast_f2u
:
2037 /* Make sure we don't propagate the negate modifier to integer opcodes. */
2038 if (op
[0].negate
|| op
[0].abs
)
2039 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
2042 result_src
.type
= ir
->operation
== ir_unop_bitcast_f2i
? GLSL_TYPE_INT
:
2045 case ir_unop_bitcast_i2f
:
2046 case ir_unop_bitcast_u2f
:
2048 result_src
.type
= GLSL_TYPE_FLOAT
;
2051 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_float(0.0));
2054 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_double(0.0));
2057 if (native_integers
)
2058 emit_asm(ir
, TGSI_OPCODE_USNE
, result_dst
, op
[0], st_src_reg_for_int(0));
2060 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_float(0.0));
2062 case ir_unop_bitcast_u642d
:
2063 case ir_unop_bitcast_i642d
:
2065 result_src
.type
= GLSL_TYPE_DOUBLE
;
2067 case ir_unop_bitcast_d2i64
:
2069 result_src
.type
= GLSL_TYPE_INT64
;
2071 case ir_unop_bitcast_d2u64
:
2073 result_src
.type
= GLSL_TYPE_UINT64
;
2076 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
2079 emit_asm(ir
, TGSI_OPCODE_CEIL
, result_dst
, op
[0]);
2082 emit_asm(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
2084 case ir_unop_round_even
:
2085 emit_asm(ir
, TGSI_OPCODE_ROUND
, result_dst
, op
[0]);
2088 emit_asm(ir
, TGSI_OPCODE_FRC
, result_dst
, op
[0]);
2092 emit_asm(ir
, TGSI_OPCODE_MIN
, result_dst
, op
[0], op
[1]);
2095 emit_asm(ir
, TGSI_OPCODE_MAX
, result_dst
, op
[0], op
[1]);
2098 emit_scalar(ir
, TGSI_OPCODE_POW
, result_dst
, op
[0], op
[1]);
2101 case ir_unop_bit_not
:
2102 if (native_integers
) {
2103 emit_asm(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
2107 if (native_integers
) {
2108 emit_asm(ir
, TGSI_OPCODE_U2F
, result_dst
, op
[0]);
2111 case ir_binop_lshift
:
2112 case ir_binop_rshift
:
2113 if (native_integers
) {
2114 unsigned opcode
= ir
->operation
== ir_binop_lshift
? TGSI_OPCODE_SHL
2118 if (glsl_base_type_is_64bit(op
[0].type
)) {
2119 /* GLSL shift operations have 32-bit shift counts, but TGSI uses
2122 count
= get_temp(glsl_type::u64vec(ir
->operands
[1]->type
->components()));
2123 emit_asm(ir
, TGSI_OPCODE_U2I64
, st_dst_reg(count
), op
[1]);
2128 emit_asm(ir
, opcode
, result_dst
, op
[0], count
);
2131 case ir_binop_bit_and
:
2132 if (native_integers
) {
2133 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], op
[1]);
2136 case ir_binop_bit_xor
:
2137 if (native_integers
) {
2138 emit_asm(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0], op
[1]);
2141 case ir_binop_bit_or
:
2142 if (native_integers
) {
2143 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0], op
[1]);
2147 assert(!"GLSL 1.30 features unsupported");
2150 case ir_binop_ubo_load
: {
2151 ir_constant
*const_uniform_block
= ir
->operands
[0]->as_constant();
2152 ir_constant
*const_offset_ir
= ir
->operands
[1]->as_constant();
2153 unsigned const_offset
= const_offset_ir
? const_offset_ir
->value
.u
[0] : 0;
2154 unsigned const_block
= const_uniform_block
? const_uniform_block
->value
.u
[0] + 1 : 0;
2155 st_src_reg index_reg
= get_temp(glsl_type::uint_type
);
2158 cbuf
.type
= ir
->type
->base_type
;
2159 cbuf
.file
= PROGRAM_CONSTANT
;
2161 cbuf
.reladdr
= NULL
;
2165 assert(ir
->type
->is_vector() || ir
->type
->is_scalar());
2167 if (const_offset_ir
) {
2168 /* Constant index into constant buffer */
2169 cbuf
.reladdr
= NULL
;
2170 cbuf
.index
= const_offset
/ 16;
2173 ir_expression
*offset_expr
= ir
->operands
[1]->as_expression();
2174 st_src_reg offset
= op
[1];
2176 /* The OpenGL spec is written in such a way that accesses with
2177 * non-constant offset are almost always vec4-aligned. The only
2178 * exception to this are members of structs in arrays of structs:
2179 * each struct in an array of structs is at least vec4-aligned,
2180 * but single-element and [ui]vec2 members of the struct may be at
2181 * an offset that is not a multiple of 16 bytes.
2183 * Here, we extract that offset, relying on previous passes to always
2184 * generate offset expressions of the form (+ expr constant_offset).
2186 * Note that the std430 layout, which allows more cases of alignment
2187 * less than vec4 in arrays, is not supported for uniform blocks, so
2188 * we do not have to deal with it here.
2190 if (offset_expr
&& offset_expr
->operation
== ir_binop_add
) {
2191 const_offset_ir
= offset_expr
->operands
[1]->as_constant();
2192 if (const_offset_ir
) {
2193 const_offset
= const_offset_ir
->value
.u
[0];
2194 cbuf
.index
= const_offset
/ 16;
2195 offset_expr
->operands
[0]->accept(this);
2196 offset
= this->result
;
2200 /* Relative/variable index into constant buffer */
2201 emit_asm(ir
, TGSI_OPCODE_USHR
, st_dst_reg(index_reg
), offset
,
2202 st_src_reg_for_int(4));
2203 cbuf
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2204 memcpy(cbuf
.reladdr
, &index_reg
, sizeof(index_reg
));
2207 if (const_uniform_block
) {
2208 /* Constant constant buffer */
2209 cbuf
.reladdr2
= NULL
;
2210 cbuf
.index2D
= const_block
;
2211 cbuf
.has_index2
= true;
2214 /* Relative/variable constant buffer */
2215 cbuf
.reladdr2
= ralloc(mem_ctx
, st_src_reg
);
2217 memcpy(cbuf
.reladdr2
, &op
[0], sizeof(st_src_reg
));
2218 cbuf
.has_index2
= true;
2221 cbuf
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
2222 if (glsl_base_type_is_64bit(cbuf
.type
))
2223 cbuf
.swizzle
+= MAKE_SWIZZLE4(const_offset
% 16 / 8,
2224 const_offset
% 16 / 8,
2225 const_offset
% 16 / 8,
2226 const_offset
% 16 / 8);
2228 cbuf
.swizzle
+= MAKE_SWIZZLE4(const_offset
% 16 / 4,
2229 const_offset
% 16 / 4,
2230 const_offset
% 16 / 4,
2231 const_offset
% 16 / 4);
2233 if (ir
->type
->is_boolean()) {
2234 emit_asm(ir
, TGSI_OPCODE_USNE
, result_dst
, cbuf
, st_src_reg_for_int(0));
2236 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, cbuf
);
2241 /* note: we have to reorder the three args here */
2242 emit_asm(ir
, TGSI_OPCODE_LRP
, result_dst
, op
[2], op
[1], op
[0]);
2245 if (this->ctx
->Const
.NativeIntegers
)
2246 emit_asm(ir
, TGSI_OPCODE_UCMP
, result_dst
, op
[0], op
[1], op
[2]);
2248 op
[0].negate
= ~op
[0].negate
;
2249 emit_asm(ir
, TGSI_OPCODE_CMP
, result_dst
, op
[0], op
[1], op
[2]);
2252 case ir_triop_bitfield_extract
:
2253 emit_asm(ir
, TGSI_OPCODE_IBFE
, result_dst
, op
[0], op
[1], op
[2]);
2255 case ir_quadop_bitfield_insert
:
2256 emit_asm(ir
, TGSI_OPCODE_BFI
, result_dst
, op
[0], op
[1], op
[2], op
[3]);
2258 case ir_unop_bitfield_reverse
:
2259 emit_asm(ir
, TGSI_OPCODE_BREV
, result_dst
, op
[0]);
2261 case ir_unop_bit_count
:
2262 emit_asm(ir
, TGSI_OPCODE_POPC
, result_dst
, op
[0]);
2264 case ir_unop_find_msb
:
2265 emit_asm(ir
, TGSI_OPCODE_IMSB
, result_dst
, op
[0]);
2267 case ir_unop_find_lsb
:
2268 emit_asm(ir
, TGSI_OPCODE_LSB
, result_dst
, op
[0]);
2270 case ir_binop_imul_high
:
2271 emit_asm(ir
, TGSI_OPCODE_IMUL_HI
, result_dst
, op
[0], op
[1]);
2274 /* In theory, MAD is incorrect here. */
2276 emit_asm(ir
, TGSI_OPCODE_FMA
, result_dst
, op
[0], op
[1], op
[2]);
2278 emit_asm(ir
, TGSI_OPCODE_MAD
, result_dst
, op
[0], op
[1], op
[2]);
2280 case ir_unop_interpolate_at_centroid
:
2281 emit_asm(ir
, TGSI_OPCODE_INTERP_CENTROID
, result_dst
, op
[0]);
2283 case ir_binop_interpolate_at_offset
: {
2284 /* The y coordinate needs to be flipped for the default fb */
2285 static const gl_state_index transform_y_state
[STATE_LENGTH
]
2286 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
};
2288 unsigned transform_y_index
=
2289 _mesa_add_state_reference(this->prog
->Parameters
,
2292 st_src_reg transform_y
= st_src_reg(PROGRAM_STATE_VAR
,
2294 glsl_type::vec4_type
);
2295 transform_y
.swizzle
= SWIZZLE_XXXX
;
2297 st_src_reg temp
= get_temp(glsl_type::vec2_type
);
2298 st_dst_reg temp_dst
= st_dst_reg(temp
);
2300 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[1]);
2301 temp_dst
.writemask
= WRITEMASK_Y
;
2302 emit_asm(ir
, TGSI_OPCODE_MUL
, temp_dst
, transform_y
, op
[1]);
2303 emit_asm(ir
, TGSI_OPCODE_INTERP_OFFSET
, result_dst
, op
[0], temp
);
2306 case ir_binop_interpolate_at_sample
:
2307 emit_asm(ir
, TGSI_OPCODE_INTERP_SAMPLE
, result_dst
, op
[0], op
[1]);
2311 emit_asm(ir
, TGSI_OPCODE_D2F
, result_dst
, op
[0]);
2314 emit_asm(ir
, TGSI_OPCODE_F2D
, result_dst
, op
[0]);
2317 emit_asm(ir
, TGSI_OPCODE_D2I
, result_dst
, op
[0]);
2320 emit_asm(ir
, TGSI_OPCODE_I2D
, result_dst
, op
[0]);
2323 emit_asm(ir
, TGSI_OPCODE_D2U
, result_dst
, op
[0]);
2326 emit_asm(ir
, TGSI_OPCODE_U2D
, result_dst
, op
[0]);
2328 case ir_unop_unpack_double_2x32
:
2329 case ir_unop_pack_double_2x32
:
2330 case ir_unop_unpack_int_2x32
:
2331 case ir_unop_pack_int_2x32
:
2332 case ir_unop_unpack_uint_2x32
:
2333 case ir_unop_pack_uint_2x32
:
2334 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
2337 case ir_binop_ldexp
:
2338 if (ir
->operands
[0]->type
->is_double()) {
2339 emit_asm(ir
, TGSI_OPCODE_DLDEXP
, result_dst
, op
[0], op
[1]);
2341 assert(!"Invalid ldexp for non-double opcode in glsl_to_tgsi_visitor::visit()");
2345 case ir_unop_pack_half_2x16
:
2346 emit_asm(ir
, TGSI_OPCODE_PK2H
, result_dst
, op
[0]);
2348 case ir_unop_unpack_half_2x16
:
2349 emit_asm(ir
, TGSI_OPCODE_UP2H
, result_dst
, op
[0]);
2352 case ir_unop_get_buffer_size
: {
2353 ir_constant
*const_offset
= ir
->operands
[0]->as_constant();
2356 ctx
->Const
.Program
[shader
->Stage
].MaxAtomicBuffers
+
2357 (const_offset
? const_offset
->value
.u
[0] : 0),
2359 if (!const_offset
) {
2360 buffer
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2361 *buffer
.reladdr
= op
[0];
2362 emit_arl(ir
, sampler_reladdr
, op
[0]);
2364 emit_asm(ir
, TGSI_OPCODE_RESQ
, result_dst
)->resource
= buffer
;
2370 case ir_unop_b2i64
: {
2371 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2372 st_dst_reg temp_dst
= st_dst_reg(temp
);
2373 unsigned orig_swz
= op
[0].swizzle
;
2375 * To convert unsigned to 64-bit:
2376 * zero Y channel, copy X channel.
2378 temp_dst
.writemask
= WRITEMASK_Y
;
2379 if (vector_elements
> 1)
2380 temp_dst
.writemask
|= WRITEMASK_W
;
2381 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, st_src_reg_for_int(0));
2382 temp_dst
.writemask
= WRITEMASK_X
;
2383 if (vector_elements
> 1)
2384 temp_dst
.writemask
|= WRITEMASK_Z
;
2385 op
[0].swizzle
= MAKE_SWIZZLE4(GET_SWZ(orig_swz
, 0), GET_SWZ(orig_swz
, 0),
2386 GET_SWZ(orig_swz
, 1), GET_SWZ(orig_swz
, 1));
2387 if (ir
->operation
== ir_unop_u2i64
|| ir
->operation
== ir_unop_u2u64
)
2388 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2390 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, op
[0], st_src_reg_for_int(1));
2392 result_src
.type
= GLSL_TYPE_UINT64
;
2393 if (vector_elements
> 2) {
2394 /* Subtle: We rely on the fact that get_temp here returns the next
2395 * TGSI temporary register directly after the temp register used for
2396 * the first two components, so that the result gets picked up
2399 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2400 st_dst_reg temp_dst
= st_dst_reg(temp
);
2401 temp_dst
.writemask
= WRITEMASK_Y
;
2402 if (vector_elements
> 3)
2403 temp_dst
.writemask
|= WRITEMASK_W
;
2404 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, st_src_reg_for_int(0));
2406 temp_dst
.writemask
= WRITEMASK_X
;
2407 if (vector_elements
> 3)
2408 temp_dst
.writemask
|= WRITEMASK_Z
;
2409 op
[0].swizzle
= MAKE_SWIZZLE4(GET_SWZ(orig_swz
, 2), GET_SWZ(orig_swz
, 2),
2410 GET_SWZ(orig_swz
, 3), GET_SWZ(orig_swz
, 3));
2411 if (ir
->operation
== ir_unop_u2i64
|| ir
->operation
== ir_unop_u2u64
)
2412 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2414 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, op
[0], st_src_reg_for_int(1));
2421 case ir_unop_i642u
: {
2422 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2423 st_dst_reg temp_dst
= st_dst_reg(temp
);
2424 unsigned orig_swz
= op
[0].swizzle
;
2425 unsigned orig_idx
= op
[0].index
;
2427 temp_dst
.writemask
= WRITEMASK_X
;
2429 for (el
= 0; el
< vector_elements
; el
++) {
2430 unsigned swz
= GET_SWZ(orig_swz
, el
);
2432 op
[0].swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_Z
, SWIZZLE_Z
, SWIZZLE_Z
);
2434 op
[0].swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
);
2436 op
[0].index
= orig_idx
+ 1;
2437 op
[0].type
= GLSL_TYPE_UINT
;
2438 temp_dst
.writemask
= WRITEMASK_X
<< el
;
2439 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2442 if (ir
->operation
== ir_unop_u642u
|| ir
->operation
== ir_unop_i642u
)
2443 result_src
.type
= GLSL_TYPE_UINT
;
2445 result_src
.type
= GLSL_TYPE_INT
;
2449 emit_asm(ir
, TGSI_OPCODE_U64SNE
, result_dst
, op
[0], st_src_reg_for_int(0));
2452 emit_asm(ir
, TGSI_OPCODE_I642F
, result_dst
, op
[0]);
2455 emit_asm(ir
, TGSI_OPCODE_U642F
, result_dst
, op
[0]);
2458 emit_asm(ir
, TGSI_OPCODE_I642D
, result_dst
, op
[0]);
2461 emit_asm(ir
, TGSI_OPCODE_U642D
, result_dst
, op
[0]);
2464 emit_asm(ir
, TGSI_OPCODE_I2I64
, result_dst
, op
[0]);
2467 emit_asm(ir
, TGSI_OPCODE_F2I64
, result_dst
, op
[0]);
2470 emit_asm(ir
, TGSI_OPCODE_D2I64
, result_dst
, op
[0]);
2473 emit_asm(ir
, TGSI_OPCODE_I2I64
, result_dst
, op
[0]);
2476 emit_asm(ir
, TGSI_OPCODE_F2U64
, result_dst
, op
[0]);
2479 emit_asm(ir
, TGSI_OPCODE_D2U64
, result_dst
, op
[0]);
2481 /* these might be needed */
2482 case ir_unop_pack_snorm_2x16
:
2483 case ir_unop_pack_unorm_2x16
:
2484 case ir_unop_pack_snorm_4x8
:
2485 case ir_unop_pack_unorm_4x8
:
2487 case ir_unop_unpack_snorm_2x16
:
2488 case ir_unop_unpack_unorm_2x16
:
2489 case ir_unop_unpack_snorm_4x8
:
2490 case ir_unop_unpack_unorm_4x8
:
2492 case ir_unop_unpack_sampler_2x32
:
2493 case ir_unop_pack_sampler_2x32
:
2494 case ir_unop_unpack_image_2x32
:
2495 case ir_unop_pack_image_2x32
:
2497 case ir_quadop_vector
:
2498 case ir_binop_vector_extract
:
2499 case ir_triop_vector_insert
:
2500 case ir_binop_carry
:
2501 case ir_binop_borrow
:
2502 case ir_unop_ssbo_unsized_array_length
:
2503 /* This operation is not supported, or should have already been handled.
2505 assert(!"Invalid ir opcode in glsl_to_tgsi_visitor::visit()");
2509 this->result
= result_src
;
2514 glsl_to_tgsi_visitor::visit(ir_swizzle
*ir
)
2520 /* Note that this is only swizzles in expressions, not those on the left
2521 * hand side of an assignment, which do write masking. See ir_assignment
2525 ir
->val
->accept(this);
2527 assert(src
.file
!= PROGRAM_UNDEFINED
);
2528 assert(ir
->type
->vector_elements
> 0);
2530 for (i
= 0; i
< 4; i
++) {
2531 if (i
< ir
->type
->vector_elements
) {
2534 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.x
);
2537 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.y
);
2540 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.z
);
2543 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.w
);
2547 /* If the type is smaller than a vec4, replicate the last
2550 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
2554 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2559 /* Test if the variable is an array. Note that geometry and
2560 * tessellation shader inputs are outputs are always arrays (except
2561 * for patch inputs), so only the array element type is considered.
2564 is_inout_array(unsigned stage
, ir_variable
*var
, bool *remove_array
)
2566 const glsl_type
*type
= var
->type
;
2568 *remove_array
= false;
2570 if ((stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_in
) ||
2571 (stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_out
))
2574 if (((stage
== MESA_SHADER_GEOMETRY
&& var
->data
.mode
== ir_var_shader_in
) ||
2575 (stage
== MESA_SHADER_TESS_EVAL
&& var
->data
.mode
== ir_var_shader_in
) ||
2576 stage
== MESA_SHADER_TESS_CTRL
) &&
2578 if (!var
->type
->is_array())
2579 return false; /* a system value probably */
2581 type
= var
->type
->fields
.array
;
2582 *remove_array
= true;
2585 return type
->is_array() || type
->is_matrix();
2589 st_translate_interp_loc(ir_variable
*var
)
2591 if (var
->data
.centroid
)
2592 return TGSI_INTERPOLATE_LOC_CENTROID
;
2593 else if (var
->data
.sample
)
2594 return TGSI_INTERPOLATE_LOC_SAMPLE
;
2596 return TGSI_INTERPOLATE_LOC_CENTER
;
2600 glsl_to_tgsi_visitor::visit(ir_dereference_variable
*ir
)
2602 variable_storage
*entry
= find_variable_storage(ir
->var
);
2603 ir_variable
*var
= ir
->var
;
2607 switch (var
->data
.mode
) {
2608 case ir_var_uniform
:
2609 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_UNIFORM
,
2610 var
->data
.param_index
);
2611 _mesa_hash_table_insert(this->variables
, var
, entry
);
2613 case ir_var_shader_in
: {
2614 /* The linker assigns locations for varyings and attributes,
2615 * including deprecated builtins (like gl_Color), user-assign
2616 * generic attributes (glBindVertexLocation), and
2617 * user-defined varyings.
2619 assert(var
->data
.location
!= -1);
2621 const glsl_type
*type_without_array
= var
->type
->without_array();
2622 struct inout_decl
*decl
= &inputs
[num_inputs
];
2623 unsigned component
= var
->data
.location_frac
;
2624 unsigned num_components
;
2627 if (type_without_array
->is_64bit())
2628 component
= component
/ 2;
2629 if (type_without_array
->vector_elements
)
2630 num_components
= type_without_array
->vector_elements
;
2634 decl
->mesa_index
= var
->data
.location
;
2635 decl
->interp
= (glsl_interp_mode
) var
->data
.interpolation
;
2636 decl
->interp_loc
= st_translate_interp_loc(var
);
2637 decl
->base_type
= type_without_array
->base_type
;
2638 decl
->usage_mask
= u_bit_consecutive(component
, num_components
);
2640 if (is_inout_array(shader
->Stage
, var
, &remove_array
)) {
2641 decl
->array_id
= num_input_arrays
+ 1;
2648 decl
->size
= type_size(var
->type
->fields
.array
);
2650 decl
->size
= type_size(var
->type
);
2652 entry
= new(mem_ctx
) variable_storage(var
,
2656 entry
->component
= component
;
2658 _mesa_hash_table_insert(this->variables
, var
, entry
);
2662 case ir_var_shader_out
: {
2663 assert(var
->data
.location
!= -1);
2665 const glsl_type
*type_without_array
= var
->type
->without_array();
2666 struct inout_decl
*decl
= &outputs
[num_outputs
];
2667 unsigned component
= var
->data
.location_frac
;
2668 unsigned num_components
;
2671 if (type_without_array
->is_64bit())
2672 component
= component
/ 2;
2673 if (type_without_array
->vector_elements
)
2674 num_components
= type_without_array
->vector_elements
;
2678 decl
->mesa_index
= var
->data
.location
+ FRAG_RESULT_MAX
* var
->data
.index
;
2679 decl
->base_type
= type_without_array
->base_type
;
2680 decl
->usage_mask
= u_bit_consecutive(component
, num_components
);
2681 if (var
->data
.stream
& (1u << 31)) {
2682 decl
->gs_out_streams
= var
->data
.stream
& ~(1u << 31);
2684 assert(var
->data
.stream
< 4);
2685 decl
->gs_out_streams
= 0;
2686 for (unsigned i
= 0; i
< num_components
; ++i
)
2687 decl
->gs_out_streams
|= var
->data
.stream
<< (2 * (component
+ i
));
2690 if (is_inout_array(shader
->Stage
, var
, &remove_array
)) {
2691 decl
->array_id
= num_output_arrays
+ 1;
2692 num_output_arrays
++;
2698 decl
->size
= type_size(var
->type
->fields
.array
);
2700 decl
->size
= type_size(var
->type
);
2702 if (var
->data
.fb_fetch_output
) {
2703 st_dst_reg dst
= st_dst_reg(get_temp(var
->type
));
2704 st_src_reg src
= st_src_reg(PROGRAM_OUTPUT
, decl
->mesa_index
,
2705 var
->type
, component
, decl
->array_id
);
2706 emit_asm(NULL
, TGSI_OPCODE_FBFETCH
, dst
, src
);
2707 entry
= new(mem_ctx
) variable_storage(var
, dst
.file
, dst
.index
,
2710 entry
= new(mem_ctx
) variable_storage(var
,
2715 entry
->component
= component
;
2717 _mesa_hash_table_insert(this->variables
, var
, entry
);
2721 case ir_var_system_value
:
2722 entry
= new(mem_ctx
) variable_storage(var
,
2723 PROGRAM_SYSTEM_VALUE
,
2724 var
->data
.location
);
2727 case ir_var_temporary
:
2728 st_src_reg src
= get_temp(var
->type
);
2730 entry
= new(mem_ctx
) variable_storage(var
, src
.file
, src
.index
,
2732 _mesa_hash_table_insert(this->variables
, var
, entry
);
2738 printf("Failed to make storage for %s\n", var
->name
);
2743 this->result
= st_src_reg(entry
->file
, entry
->index
, var
->type
,
2744 entry
->component
, entry
->array_id
);
2745 if (this->shader
->Stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_in
&& var
->type
->is_double())
2746 this->result
.is_double_vertex_input
= true;
2747 if (!native_integers
)
2748 this->result
.type
= GLSL_TYPE_FLOAT
;
2752 shrink_array_declarations(struct inout_decl
*decls
, unsigned count
,
2753 GLbitfield64
* usage_mask
,
2754 GLbitfield64 double_usage_mask
,
2755 GLbitfield
* patch_usage_mask
)
2760 /* Fix array declarations by removing unused array elements at both ends
2761 * of the arrays. For example, mat4[3] where only mat[1] is used.
2763 for (i
= 0; i
< count
; i
++) {
2764 struct inout_decl
*decl
= &decls
[i
];
2765 if (!decl
->array_id
)
2768 /* Shrink the beginning. */
2769 for (j
= 0; j
< (int)decl
->size
; j
++) {
2770 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
) {
2771 if (*patch_usage_mask
&
2772 BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
))
2776 if (*usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
))
2778 if (double_usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
-1))
2787 /* Shrink the end. */
2788 for (j
= decl
->size
-1; j
>= 0; j
--) {
2789 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
) {
2790 if (*patch_usage_mask
&
2791 BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
))
2795 if (*usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
))
2797 if (double_usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
-1))
2804 /* When not all entries of an array are accessed, we mark them as used
2805 * here anyway, to ensure that the input/output mapping logic doesn't get
2808 * TODO This happens when an array isn't used via indirect access, which
2809 * some game ports do (at least eON-based). There is an optimization
2810 * opportunity here by replacing the array declaration with non-array
2811 * declarations of those slots that are actually used.
2813 for (j
= 1; j
< (int)decl
->size
; ++j
) {
2814 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
)
2815 *patch_usage_mask
|= BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
);
2817 *usage_mask
|= BITFIELD64_BIT(decl
->mesa_index
+ j
);
2823 glsl_to_tgsi_visitor::visit(ir_dereference_array
*ir
)
2827 int element_size
= type_size(ir
->type
);
2830 index
= ir
->array_index
->constant_expression_value();
2832 ir
->array
->accept(this);
2835 if (ir
->array
->ir_type
!= ir_type_dereference_array
) {
2836 switch (this->prog
->Target
) {
2837 case GL_TESS_CONTROL_PROGRAM_NV
:
2838 is_2D
= (src
.file
== PROGRAM_INPUT
|| src
.file
== PROGRAM_OUTPUT
) &&
2839 !ir
->variable_referenced()->data
.patch
;
2841 case GL_TESS_EVALUATION_PROGRAM_NV
:
2842 is_2D
= src
.file
== PROGRAM_INPUT
&&
2843 !ir
->variable_referenced()->data
.patch
;
2845 case GL_GEOMETRY_PROGRAM_NV
:
2846 is_2D
= src
.file
== PROGRAM_INPUT
;
2856 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
2857 src
.file
== PROGRAM_INPUT
)
2858 element_size
= attrib_type_size(ir
->type
, true);
2860 src
.index2D
= index
->value
.i
[0];
2861 src
.has_index2
= true;
2863 src
.index
+= index
->value
.i
[0] * element_size
;
2865 /* Variable index array dereference. It eats the "vec4" of the
2866 * base of the array and an index that offsets the TGSI register
2869 ir
->array_index
->accept(this);
2871 st_src_reg index_reg
;
2873 if (element_size
== 1) {
2874 index_reg
= this->result
;
2876 index_reg
= get_temp(native_integers
?
2877 glsl_type::int_type
: glsl_type::float_type
);
2879 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(index_reg
),
2880 this->result
, st_src_reg_for_type(index_reg
.type
, element_size
));
2883 /* If there was already a relative address register involved, add the
2884 * new and the old together to get the new offset.
2886 if (!is_2D
&& src
.reladdr
!= NULL
) {
2887 st_src_reg accum_reg
= get_temp(native_integers
?
2888 glsl_type::int_type
: glsl_type::float_type
);
2890 emit_asm(ir
, TGSI_OPCODE_ADD
, st_dst_reg(accum_reg
),
2891 index_reg
, *src
.reladdr
);
2893 index_reg
= accum_reg
;
2897 src
.reladdr2
= ralloc(mem_ctx
, st_src_reg
);
2898 memcpy(src
.reladdr2
, &index_reg
, sizeof(index_reg
));
2900 src
.has_index2
= true;
2902 src
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2903 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
2907 /* Change the register type to the element type of the array. */
2908 src
.type
= ir
->type
->base_type
;
2914 glsl_to_tgsi_visitor::visit(ir_dereference_record
*ir
)
2917 const glsl_type
*struct_type
= ir
->record
->type
;
2920 ir
->record
->accept(this);
2922 for (i
= 0; i
< struct_type
->length
; i
++) {
2923 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
2925 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
2928 /* If the type is smaller than a vec4, replicate the last channel out. */
2929 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
2930 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
2932 this->result
.swizzle
= SWIZZLE_NOOP
;
2934 this->result
.index
+= offset
;
2935 this->result
.type
= ir
->type
->base_type
;
2939 * We want to be careful in assignment setup to hit the actual storage
2940 * instead of potentially using a temporary like we might with the
2941 * ir_dereference handler.
2944 get_assignment_lhs(ir_dereference
*ir
, glsl_to_tgsi_visitor
*v
, int *component
)
2946 /* The LHS must be a dereference. If the LHS is a variable indexed array
2947 * access of a vector, it must be separated into a series conditional moves
2948 * before reaching this point (see ir_vec_index_to_cond_assign).
2950 assert(ir
->as_dereference());
2951 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
2953 assert(!deref_array
->array
->type
->is_vector());
2956 /* Use the rvalue deref handler for the most part. We write swizzles using
2957 * the writemask, but we do extract the base component for enhanced layouts
2958 * from the source swizzle.
2961 *component
= GET_SWZ(v
->result
.swizzle
, 0);
2962 return st_dst_reg(v
->result
);
2966 * Process the condition of a conditional assignment
2968 * Examines the condition of a conditional assignment to generate the optimal
2969 * first operand of a \c CMP instruction. If the condition is a relational
2970 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
2971 * used as the source for the \c CMP instruction. Otherwise the comparison
2972 * is processed to a boolean result, and the boolean result is used as the
2973 * operand to the CMP instruction.
2976 glsl_to_tgsi_visitor::process_move_condition(ir_rvalue
*ir
)
2978 ir_rvalue
*src_ir
= ir
;
2980 bool switch_order
= false;
2982 ir_expression
*const expr
= ir
->as_expression();
2984 if (native_integers
) {
2985 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
2986 enum glsl_base_type type
= expr
->operands
[0]->type
->base_type
;
2987 if (type
== GLSL_TYPE_INT
|| type
== GLSL_TYPE_UINT
||
2988 type
== GLSL_TYPE_BOOL
) {
2989 if (expr
->operation
== ir_binop_equal
) {
2990 if (expr
->operands
[0]->is_zero()) {
2991 src_ir
= expr
->operands
[1];
2992 switch_order
= true;
2994 else if (expr
->operands
[1]->is_zero()) {
2995 src_ir
= expr
->operands
[0];
2996 switch_order
= true;
2999 else if (expr
->operation
== ir_binop_nequal
) {
3000 if (expr
->operands
[0]->is_zero()) {
3001 src_ir
= expr
->operands
[1];
3003 else if (expr
->operands
[1]->is_zero()) {
3004 src_ir
= expr
->operands
[0];
3010 src_ir
->accept(this);
3011 return switch_order
;
3014 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
3015 bool zero_on_left
= false;
3017 if (expr
->operands
[0]->is_zero()) {
3018 src_ir
= expr
->operands
[1];
3019 zero_on_left
= true;
3020 } else if (expr
->operands
[1]->is_zero()) {
3021 src_ir
= expr
->operands
[0];
3022 zero_on_left
= false;
3026 * (a < 0) T F F ( a < 0) T F F
3027 * (0 < a) F F T (-a < 0) F F T
3028 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
3029 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
3030 * (a > 0) F F T (-a < 0) F F T
3031 * (0 > a) T F F ( a < 0) T F F
3032 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
3033 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
3035 * Note that exchanging the order of 0 and 'a' in the comparison simply
3036 * means that the value of 'a' should be negated.
3039 switch (expr
->operation
) {
3041 switch_order
= false;
3042 negate
= zero_on_left
;
3045 case ir_binop_greater
:
3046 switch_order
= false;
3047 negate
= !zero_on_left
;
3050 case ir_binop_lequal
:
3051 switch_order
= true;
3052 negate
= !zero_on_left
;
3055 case ir_binop_gequal
:
3056 switch_order
= true;
3057 negate
= zero_on_left
;
3061 /* This isn't the right kind of comparison afterall, so make sure
3062 * the whole condition is visited.
3070 src_ir
->accept(this);
3072 /* We use the TGSI_OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
3073 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
3074 * choose which value TGSI_OPCODE_CMP produces without an extra instruction
3075 * computing the condition.
3078 this->result
.negate
= ~this->result
.negate
;
3080 return switch_order
;
3084 glsl_to_tgsi_visitor::emit_block_mov(ir_assignment
*ir
, const struct glsl_type
*type
,
3085 st_dst_reg
*l
, st_src_reg
*r
,
3086 st_src_reg
*cond
, bool cond_swap
)
3088 if (type
->is_record()) {
3089 for (unsigned int i
= 0; i
< type
->length
; i
++) {
3090 emit_block_mov(ir
, type
->fields
.structure
[i
].type
, l
, r
,
3096 if (type
->is_array()) {
3097 for (unsigned int i
= 0; i
< type
->length
; i
++) {
3098 emit_block_mov(ir
, type
->fields
.array
, l
, r
, cond
, cond_swap
);
3103 if (type
->is_matrix()) {
3104 const struct glsl_type
*vec_type
;
3106 vec_type
= glsl_type::get_instance(type
->is_double() ? GLSL_TYPE_DOUBLE
: GLSL_TYPE_FLOAT
,
3107 type
->vector_elements
, 1);
3109 for (int i
= 0; i
< type
->matrix_columns
; i
++) {
3110 emit_block_mov(ir
, vec_type
, l
, r
, cond
, cond_swap
);
3115 assert(type
->is_scalar() || type
->is_vector());
3117 l
->type
= type
->base_type
;
3118 r
->type
= type
->base_type
;
3120 st_src_reg l_src
= st_src_reg(*l
);
3121 l_src
.swizzle
= swizzle_for_size(type
->vector_elements
);
3123 if (native_integers
) {
3124 emit_asm(ir
, TGSI_OPCODE_UCMP
, *l
, *cond
,
3125 cond_swap
? l_src
: *r
,
3126 cond_swap
? *r
: l_src
);
3128 emit_asm(ir
, TGSI_OPCODE_CMP
, *l
, *cond
,
3129 cond_swap
? l_src
: *r
,
3130 cond_swap
? *r
: l_src
);
3133 emit_asm(ir
, TGSI_OPCODE_MOV
, *l
, *r
);
3137 if (type
->is_dual_slot()) {
3139 if (r
->is_double_vertex_input
== false)
3145 glsl_to_tgsi_visitor::visit(ir_assignment
*ir
)
3151 ir
->rhs
->accept(this);
3154 l
= get_assignment_lhs(ir
->lhs
, this, &dst_component
);
3158 int first_enabled_chan
= 0;
3160 ir_variable
*variable
= ir
->lhs
->variable_referenced();
3162 if (shader
->Stage
== MESA_SHADER_FRAGMENT
&&
3163 variable
->data
.mode
== ir_var_shader_out
&&
3164 (variable
->data
.location
== FRAG_RESULT_DEPTH
||
3165 variable
->data
.location
== FRAG_RESULT_STENCIL
)) {
3166 assert(ir
->lhs
->type
->is_scalar());
3167 assert(ir
->write_mask
== WRITEMASK_X
);
3169 if (variable
->data
.location
== FRAG_RESULT_DEPTH
)
3170 l
.writemask
= WRITEMASK_Z
;
3172 assert(variable
->data
.location
== FRAG_RESULT_STENCIL
);
3173 l
.writemask
= WRITEMASK_Y
;
3175 } else if (ir
->write_mask
== 0) {
3176 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
3178 unsigned num_elements
= ir
->lhs
->type
->without_array()->vector_elements
;
3181 l
.writemask
= u_bit_consecutive(0, num_elements
);
3183 /* The type is a struct or an array of (array of) structs. */
3184 l
.writemask
= WRITEMASK_XYZW
;
3187 l
.writemask
= ir
->write_mask
;
3190 for (int i
= 0; i
< 4; i
++) {
3191 if (l
.writemask
& (1 << i
)) {
3192 first_enabled_chan
= GET_SWZ(r
.swizzle
, i
);
3197 l
.writemask
= l
.writemask
<< dst_component
;
3199 /* Swizzle a small RHS vector into the channels being written.
3201 * glsl ir treats write_mask as dictating how many channels are
3202 * present on the RHS while TGSI treats write_mask as just
3203 * showing which channels of the vec4 RHS get written.
3205 for (int i
= 0; i
< 4; i
++) {
3206 if (l
.writemask
& (1 << i
))
3207 swizzles
[i
] = GET_SWZ(r
.swizzle
, rhs_chan
++);
3209 swizzles
[i
] = first_enabled_chan
;
3211 r
.swizzle
= MAKE_SWIZZLE4(swizzles
[0], swizzles
[1],
3212 swizzles
[2], swizzles
[3]);
3215 assert(l
.file
!= PROGRAM_UNDEFINED
);
3216 assert(r
.file
!= PROGRAM_UNDEFINED
);
3218 if (ir
->condition
) {
3219 const bool switch_order
= this->process_move_condition(ir
->condition
);
3220 st_src_reg condition
= this->result
;
3222 emit_block_mov(ir
, ir
->lhs
->type
, &l
, &r
, &condition
, switch_order
);
3223 } else if (ir
->rhs
->as_expression() &&
3224 this->instructions
.get_tail() &&
3225 ir
->rhs
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->ir
&&
3226 !((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->is_64bit_expanded
&&
3227 type_size(ir
->lhs
->type
) == 1 &&
3228 l
.writemask
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->dst
[0].writemask
) {
3229 /* To avoid emitting an extra MOV when assigning an expression to a
3230 * variable, emit the last instruction of the expression again, but
3231 * replace the destination register with the target of the assignment.
3232 * Dead code elimination will remove the original instruction.
3234 glsl_to_tgsi_instruction
*inst
, *new_inst
;
3235 inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
3236 new_inst
= emit_asm(ir
, inst
->op
, l
, inst
->src
[0], inst
->src
[1], inst
->src
[2], inst
->src
[3]);
3237 new_inst
->saturate
= inst
->saturate
;
3238 inst
->dead_mask
= inst
->dst
[0].writemask
;
3240 emit_block_mov(ir
, ir
->rhs
->type
, &l
, &r
, NULL
, false);
3246 glsl_to_tgsi_visitor::visit(ir_constant
*ir
)
3249 GLdouble stack_vals
[4] = { 0 };
3250 gl_constant_value
*values
= (gl_constant_value
*) stack_vals
;
3251 GLenum gl_type
= GL_NONE
;
3253 static int in_array
= 0;
3254 gl_register_file file
= in_array
? PROGRAM_CONSTANT
: PROGRAM_IMMEDIATE
;
3256 /* Unfortunately, 4 floats is all we can get into
3257 * _mesa_add_typed_unnamed_constant. So, make a temp to store an
3258 * aggregate constant and move each constant value into it. If we
3259 * get lucky, copy propagation will eliminate the extra moves.
3261 if (ir
->type
->is_record()) {
3262 st_src_reg temp_base
= get_temp(ir
->type
);
3263 st_dst_reg temp
= st_dst_reg(temp_base
);
3265 foreach_in_list(ir_constant
, field_value
, &ir
->components
) {
3266 int size
= type_size(field_value
->type
);
3270 field_value
->accept(this);
3273 for (i
= 0; i
< (unsigned int)size
; i
++) {
3274 emit_asm(ir
, TGSI_OPCODE_MOV
, temp
, src
);
3280 this->result
= temp_base
;
3284 if (ir
->type
->is_array()) {
3285 st_src_reg temp_base
= get_temp(ir
->type
);
3286 st_dst_reg temp
= st_dst_reg(temp_base
);
3287 int size
= type_size(ir
->type
->fields
.array
);
3292 for (i
= 0; i
< ir
->type
->length
; i
++) {
3293 ir
->array_elements
[i
]->accept(this);
3295 for (int j
= 0; j
< size
; j
++) {
3296 emit_asm(ir
, TGSI_OPCODE_MOV
, temp
, src
);
3302 this->result
= temp_base
;
3307 if (ir
->type
->is_matrix()) {
3308 st_src_reg mat
= get_temp(ir
->type
);
3309 st_dst_reg mat_column
= st_dst_reg(mat
);
3311 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
3312 switch (ir
->type
->base_type
) {
3313 case GLSL_TYPE_FLOAT
:
3314 values
= (gl_constant_value
*) &ir
->value
.f
[i
* ir
->type
->vector_elements
];
3316 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
3317 src
.index
= add_constant(file
,
3319 ir
->type
->vector_elements
,
3322 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3324 case GLSL_TYPE_DOUBLE
:
3325 values
= (gl_constant_value
*) &ir
->value
.d
[i
* ir
->type
->vector_elements
];
3326 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
3327 src
.index
= add_constant(file
,
3329 ir
->type
->vector_elements
,
3332 if (ir
->type
->vector_elements
>= 2) {
3333 mat_column
.writemask
= WRITEMASK_XY
;
3334 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
3335 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3337 mat_column
.writemask
= WRITEMASK_X
;
3338 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
);
3339 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3342 if (ir
->type
->vector_elements
> 2) {
3343 if (ir
->type
->vector_elements
== 4) {
3344 mat_column
.writemask
= WRITEMASK_ZW
;
3345 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
3346 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3348 mat_column
.writemask
= WRITEMASK_Z
;
3349 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
);
3350 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3351 mat_column
.writemask
= WRITEMASK_XYZW
;
3352 src
.swizzle
= SWIZZLE_XYZW
;
3358 unreachable("Illegal matrix constant type.\n");
3367 switch (ir
->type
->base_type
) {
3368 case GLSL_TYPE_FLOAT
:
3370 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3371 values
[i
].f
= ir
->value
.f
[i
];
3374 case GLSL_TYPE_DOUBLE
:
3375 gl_type
= GL_DOUBLE
;
3376 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3377 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(double));
3380 case GLSL_TYPE_INT64
:
3381 gl_type
= GL_INT64_ARB
;
3382 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3383 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(int64_t));
3386 case GLSL_TYPE_UINT64
:
3387 gl_type
= GL_UNSIGNED_INT64_ARB
;
3388 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3389 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(uint64_t));
3392 case GLSL_TYPE_UINT
:
3393 gl_type
= native_integers
? GL_UNSIGNED_INT
: GL_FLOAT
;
3394 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3395 if (native_integers
)
3396 values
[i
].u
= ir
->value
.u
[i
];
3398 values
[i
].f
= ir
->value
.u
[i
];
3402 gl_type
= native_integers
? GL_INT
: GL_FLOAT
;
3403 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3404 if (native_integers
)
3405 values
[i
].i
= ir
->value
.i
[i
];
3407 values
[i
].f
= ir
->value
.i
[i
];
3410 case GLSL_TYPE_BOOL
:
3411 gl_type
= native_integers
? GL_BOOL
: GL_FLOAT
;
3412 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3413 values
[i
].u
= ir
->value
.b
[i
] ? ctx
->Const
.UniformBooleanTrue
: 0;
3417 assert(!"Non-float/uint/int/bool constant");
3420 this->result
= st_src_reg(file
, -1, ir
->type
);
3421 this->result
.index
= add_constant(file
,
3423 ir
->type
->vector_elements
,
3425 &this->result
.swizzle
);
3429 glsl_to_tgsi_visitor::visit_atomic_counter_intrinsic(ir_call
*ir
)
3431 exec_node
*param
= ir
->actual_parameters
.get_head();
3432 ir_dereference
*deref
= static_cast<ir_dereference
*>(param
);
3433 ir_variable
*location
= deref
->variable_referenced();
3436 PROGRAM_BUFFER
, location
->data
.binding
, GLSL_TYPE_ATOMIC_UINT
);
3438 /* Calculate the surface offset */
3440 unsigned array_size
= 0, base
= 0;
3443 get_deref_offsets(deref
, &array_size
, &base
, &index
, &offset
, false);
3445 if (offset
.file
!= PROGRAM_UNDEFINED
) {
3446 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(offset
),
3447 offset
, st_src_reg_for_int(ATOMIC_COUNTER_SIZE
));
3448 emit_asm(ir
, TGSI_OPCODE_ADD
, st_dst_reg(offset
),
3449 offset
, st_src_reg_for_int(location
->data
.offset
+ index
* ATOMIC_COUNTER_SIZE
));
3451 offset
= st_src_reg_for_int(location
->data
.offset
+ index
* ATOMIC_COUNTER_SIZE
);
3454 ir
->return_deref
->accept(this);
3455 st_dst_reg
dst(this->result
);
3456 dst
.writemask
= WRITEMASK_X
;
3458 glsl_to_tgsi_instruction
*inst
;
3460 if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_read
) {
3461 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, offset
);
3462 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_increment
) {
3463 inst
= emit_asm(ir
, TGSI_OPCODE_ATOMUADD
, dst
, offset
,
3464 st_src_reg_for_int(1));
3465 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_predecrement
) {
3466 inst
= emit_asm(ir
, TGSI_OPCODE_ATOMUADD
, dst
, offset
,
3467 st_src_reg_for_int(-1));
3468 emit_asm(ir
, TGSI_OPCODE_ADD
, dst
, this->result
, st_src_reg_for_int(-1));
3470 param
= param
->get_next();
3471 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3474 st_src_reg data
= this->result
, data2
= undef_src
;
3476 switch (ir
->callee
->intrinsic_id
) {
3477 case ir_intrinsic_atomic_counter_add
:
3478 opcode
= TGSI_OPCODE_ATOMUADD
;
3480 case ir_intrinsic_atomic_counter_min
:
3481 opcode
= TGSI_OPCODE_ATOMIMIN
;
3483 case ir_intrinsic_atomic_counter_max
:
3484 opcode
= TGSI_OPCODE_ATOMIMAX
;
3486 case ir_intrinsic_atomic_counter_and
:
3487 opcode
= TGSI_OPCODE_ATOMAND
;
3489 case ir_intrinsic_atomic_counter_or
:
3490 opcode
= TGSI_OPCODE_ATOMOR
;
3492 case ir_intrinsic_atomic_counter_xor
:
3493 opcode
= TGSI_OPCODE_ATOMXOR
;
3495 case ir_intrinsic_atomic_counter_exchange
:
3496 opcode
= TGSI_OPCODE_ATOMXCHG
;
3498 case ir_intrinsic_atomic_counter_comp_swap
: {
3499 opcode
= TGSI_OPCODE_ATOMCAS
;
3500 param
= param
->get_next();
3501 val
= ((ir_instruction
*)param
)->as_rvalue();
3503 data2
= this->result
;
3507 assert(!"Unexpected intrinsic");
3511 inst
= emit_asm(ir
, opcode
, dst
, offset
, data
, data2
);
3514 inst
->resource
= buffer
;
3518 glsl_to_tgsi_visitor::visit_ssbo_intrinsic(ir_call
*ir
)
3520 exec_node
*param
= ir
->actual_parameters
.get_head();
3522 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
3524 param
= param
->get_next();
3525 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
3527 ir_constant
*const_block
= block
->as_constant();
3531 ctx
->Const
.Program
[shader
->Stage
].MaxAtomicBuffers
+
3532 (const_block
? const_block
->value
.u
[0] : 0),
3536 block
->accept(this);
3537 buffer
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
3538 *buffer
.reladdr
= this->result
;
3539 emit_arl(ir
, sampler_reladdr
, this->result
);
3542 /* Calculate the surface offset */
3543 offset
->accept(this);
3544 st_src_reg off
= this->result
;
3546 st_dst_reg dst
= undef_dst
;
3547 if (ir
->return_deref
) {
3548 ir
->return_deref
->accept(this);
3549 dst
= st_dst_reg(this->result
);
3550 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3553 glsl_to_tgsi_instruction
*inst
;
3555 if (ir
->callee
->intrinsic_id
== ir_intrinsic_ssbo_load
) {
3556 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, off
);
3557 if (dst
.type
== GLSL_TYPE_BOOL
)
3558 emit_asm(ir
, TGSI_OPCODE_USNE
, dst
, st_src_reg(dst
), st_src_reg_for_int(0));
3559 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_ssbo_store
) {
3560 param
= param
->get_next();
3561 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3564 param
= param
->get_next();
3565 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
3567 dst
.writemask
= write_mask
->value
.u
[0];
3569 dst
.type
= this->result
.type
;
3570 inst
= emit_asm(ir
, TGSI_OPCODE_STORE
, dst
, off
, this->result
);
3572 param
= param
->get_next();
3573 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3576 st_src_reg data
= this->result
, data2
= undef_src
;
3578 switch (ir
->callee
->intrinsic_id
) {
3579 case ir_intrinsic_ssbo_atomic_add
:
3580 opcode
= TGSI_OPCODE_ATOMUADD
;
3582 case ir_intrinsic_ssbo_atomic_min
:
3583 opcode
= TGSI_OPCODE_ATOMIMIN
;
3585 case ir_intrinsic_ssbo_atomic_max
:
3586 opcode
= TGSI_OPCODE_ATOMIMAX
;
3588 case ir_intrinsic_ssbo_atomic_and
:
3589 opcode
= TGSI_OPCODE_ATOMAND
;
3591 case ir_intrinsic_ssbo_atomic_or
:
3592 opcode
= TGSI_OPCODE_ATOMOR
;
3594 case ir_intrinsic_ssbo_atomic_xor
:
3595 opcode
= TGSI_OPCODE_ATOMXOR
;
3597 case ir_intrinsic_ssbo_atomic_exchange
:
3598 opcode
= TGSI_OPCODE_ATOMXCHG
;
3600 case ir_intrinsic_ssbo_atomic_comp_swap
:
3601 opcode
= TGSI_OPCODE_ATOMCAS
;
3602 param
= param
->get_next();
3603 val
= ((ir_instruction
*)param
)->as_rvalue();
3605 data2
= this->result
;
3608 assert(!"Unexpected intrinsic");
3612 inst
= emit_asm(ir
, opcode
, dst
, off
, data
, data2
);
3615 param
= param
->get_next();
3616 ir_constant
*access
= NULL
;
3617 if (!param
->is_tail_sentinel()) {
3618 access
= ((ir_instruction
*)param
)->as_constant();
3622 /* The emit_asm() might have actually split the op into pieces, e.g. for
3623 * double stores. We have to go back and fix up all the generated ops.
3625 unsigned op
= inst
->op
;
3627 inst
->resource
= buffer
;
3629 inst
->buffer_access
= access
->value
.u
[0];
3631 if (inst
== this->instructions
.get_head_raw())
3633 inst
= (glsl_to_tgsi_instruction
*)inst
->get_prev();
3635 if (inst
->op
== TGSI_OPCODE_UADD
) {
3636 if (inst
== this->instructions
.get_head_raw())
3638 inst
= (glsl_to_tgsi_instruction
*)inst
->get_prev();
3640 } while (inst
->op
== op
&& inst
->resource
.file
== PROGRAM_UNDEFINED
);
3644 glsl_to_tgsi_visitor::visit_membar_intrinsic(ir_call
*ir
)
3646 switch (ir
->callee
->intrinsic_id
) {
3647 case ir_intrinsic_memory_barrier
:
3648 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3649 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
|
3650 TGSI_MEMBAR_ATOMIC_BUFFER
|
3651 TGSI_MEMBAR_SHADER_IMAGE
|
3652 TGSI_MEMBAR_SHARED
));
3654 case ir_intrinsic_memory_barrier_atomic_counter
:
3655 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3656 st_src_reg_for_int(TGSI_MEMBAR_ATOMIC_BUFFER
));
3658 case ir_intrinsic_memory_barrier_buffer
:
3659 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3660 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
));
3662 case ir_intrinsic_memory_barrier_image
:
3663 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3664 st_src_reg_for_int(TGSI_MEMBAR_SHADER_IMAGE
));
3666 case ir_intrinsic_memory_barrier_shared
:
3667 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3668 st_src_reg_for_int(TGSI_MEMBAR_SHARED
));
3670 case ir_intrinsic_group_memory_barrier
:
3671 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3672 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
|
3673 TGSI_MEMBAR_ATOMIC_BUFFER
|
3674 TGSI_MEMBAR_SHADER_IMAGE
|
3675 TGSI_MEMBAR_SHARED
|
3676 TGSI_MEMBAR_THREAD_GROUP
));
3679 assert(!"Unexpected memory barrier intrinsic");
3684 glsl_to_tgsi_visitor::visit_shared_intrinsic(ir_call
*ir
)
3686 exec_node
*param
= ir
->actual_parameters
.get_head();
3688 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
3690 st_src_reg
buffer(PROGRAM_MEMORY
, 0, GLSL_TYPE_UINT
);
3692 /* Calculate the surface offset */
3693 offset
->accept(this);
3694 st_src_reg off
= this->result
;
3696 st_dst_reg dst
= undef_dst
;
3697 if (ir
->return_deref
) {
3698 ir
->return_deref
->accept(this);
3699 dst
= st_dst_reg(this->result
);
3700 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3703 glsl_to_tgsi_instruction
*inst
;
3705 if (ir
->callee
->intrinsic_id
== ir_intrinsic_shared_load
) {
3706 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, off
);
3707 inst
->resource
= buffer
;
3708 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_shared_store
) {
3709 param
= param
->get_next();
3710 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3713 param
= param
->get_next();
3714 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
3716 dst
.writemask
= write_mask
->value
.u
[0];
3718 dst
.type
= this->result
.type
;
3719 inst
= emit_asm(ir
, TGSI_OPCODE_STORE
, dst
, off
, this->result
);
3720 inst
->resource
= buffer
;
3722 param
= param
->get_next();
3723 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3726 st_src_reg data
= this->result
, data2
= undef_src
;
3728 switch (ir
->callee
->intrinsic_id
) {
3729 case ir_intrinsic_shared_atomic_add
:
3730 opcode
= TGSI_OPCODE_ATOMUADD
;
3732 case ir_intrinsic_shared_atomic_min
:
3733 opcode
= TGSI_OPCODE_ATOMIMIN
;
3735 case ir_intrinsic_shared_atomic_max
:
3736 opcode
= TGSI_OPCODE_ATOMIMAX
;
3738 case ir_intrinsic_shared_atomic_and
:
3739 opcode
= TGSI_OPCODE_ATOMAND
;
3741 case ir_intrinsic_shared_atomic_or
:
3742 opcode
= TGSI_OPCODE_ATOMOR
;
3744 case ir_intrinsic_shared_atomic_xor
:
3745 opcode
= TGSI_OPCODE_ATOMXOR
;
3747 case ir_intrinsic_shared_atomic_exchange
:
3748 opcode
= TGSI_OPCODE_ATOMXCHG
;
3750 case ir_intrinsic_shared_atomic_comp_swap
:
3751 opcode
= TGSI_OPCODE_ATOMCAS
;
3752 param
= param
->get_next();
3753 val
= ((ir_instruction
*)param
)->as_rvalue();
3755 data2
= this->result
;
3758 assert(!"Unexpected intrinsic");
3762 inst
= emit_asm(ir
, opcode
, dst
, off
, data
, data2
);
3763 inst
->resource
= buffer
;
3768 glsl_to_tgsi_visitor::visit_image_intrinsic(ir_call
*ir
)
3770 exec_node
*param
= ir
->actual_parameters
.get_head();
3772 ir_dereference
*img
= (ir_dereference
*)param
;
3773 const ir_variable
*imgvar
= img
->variable_referenced();
3774 const glsl_type
*type
= imgvar
->type
->without_array();
3775 unsigned sampler_array_size
= 1, sampler_base
= 0;
3778 st_src_reg
image(PROGRAM_IMAGE
, 0, GLSL_TYPE_UINT
);
3780 get_deref_offsets(img
, &sampler_array_size
, &sampler_base
,
3781 &index
, &reladdr
, true);
3783 image
.index
= index
;
3784 if (reladdr
.file
!= PROGRAM_UNDEFINED
) {
3785 image
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
3786 *image
.reladdr
= reladdr
;
3787 emit_arl(ir
, sampler_reladdr
, reladdr
);
3790 st_dst_reg dst
= undef_dst
;
3791 if (ir
->return_deref
) {
3792 ir
->return_deref
->accept(this);
3793 dst
= st_dst_reg(this->result
);
3794 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3797 glsl_to_tgsi_instruction
*inst
;
3799 if (ir
->callee
->intrinsic_id
== ir_intrinsic_image_size
) {
3800 dst
.writemask
= WRITEMASK_XYZ
;
3801 inst
= emit_asm(ir
, TGSI_OPCODE_RESQ
, dst
);
3802 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_image_samples
) {
3803 st_src_reg res
= get_temp(glsl_type::ivec4_type
);
3804 st_dst_reg dstres
= st_dst_reg(res
);
3805 dstres
.writemask
= WRITEMASK_W
;
3806 inst
= emit_asm(ir
, TGSI_OPCODE_RESQ
, dstres
);
3807 res
.swizzle
= SWIZZLE_WWWW
;
3808 emit_asm(ir
, TGSI_OPCODE_MOV
, dst
, res
);
3810 st_src_reg arg1
= undef_src
, arg2
= undef_src
;
3812 st_dst_reg coord_dst
;
3813 coord
= get_temp(glsl_type::ivec4_type
);
3814 coord_dst
= st_dst_reg(coord
);
3815 coord_dst
.writemask
= (1 << type
->coordinate_components()) - 1;
3816 param
= param
->get_next();
3817 ((ir_dereference
*)param
)->accept(this);
3818 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
3819 coord
.swizzle
= SWIZZLE_XXXX
;
3820 switch (type
->coordinate_components()) {
3821 case 4: assert(!"unexpected coord count");
3823 case 3: coord
.swizzle
|= SWIZZLE_Z
<< 6;
3825 case 2: coord
.swizzle
|= SWIZZLE_Y
<< 3;
3828 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
3829 param
= param
->get_next();
3830 ((ir_dereference
*)param
)->accept(this);
3831 st_src_reg sample
= this->result
;
3832 sample
.swizzle
= SWIZZLE_XXXX
;
3833 coord_dst
.writemask
= WRITEMASK_W
;
3834 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, sample
);
3835 coord
.swizzle
|= SWIZZLE_W
<< 9;
3838 param
= param
->get_next();
3839 if (!param
->is_tail_sentinel()) {
3840 ((ir_dereference
*)param
)->accept(this);
3841 arg1
= this->result
;
3842 param
= param
->get_next();
3845 if (!param
->is_tail_sentinel()) {
3846 ((ir_dereference
*)param
)->accept(this);
3847 arg2
= this->result
;
3848 param
= param
->get_next();
3851 assert(param
->is_tail_sentinel());
3854 switch (ir
->callee
->intrinsic_id
) {
3855 case ir_intrinsic_image_load
:
3856 opcode
= TGSI_OPCODE_LOAD
;
3858 case ir_intrinsic_image_store
:
3859 opcode
= TGSI_OPCODE_STORE
;
3861 case ir_intrinsic_image_atomic_add
:
3862 opcode
= TGSI_OPCODE_ATOMUADD
;
3864 case ir_intrinsic_image_atomic_min
:
3865 opcode
= TGSI_OPCODE_ATOMIMIN
;
3867 case ir_intrinsic_image_atomic_max
:
3868 opcode
= TGSI_OPCODE_ATOMIMAX
;
3870 case ir_intrinsic_image_atomic_and
:
3871 opcode
= TGSI_OPCODE_ATOMAND
;
3873 case ir_intrinsic_image_atomic_or
:
3874 opcode
= TGSI_OPCODE_ATOMOR
;
3876 case ir_intrinsic_image_atomic_xor
:
3877 opcode
= TGSI_OPCODE_ATOMXOR
;
3879 case ir_intrinsic_image_atomic_exchange
:
3880 opcode
= TGSI_OPCODE_ATOMXCHG
;
3882 case ir_intrinsic_image_atomic_comp_swap
:
3883 opcode
= TGSI_OPCODE_ATOMCAS
;
3886 assert(!"Unexpected intrinsic");
3890 inst
= emit_asm(ir
, opcode
, dst
, coord
, arg1
, arg2
);
3891 if (opcode
== TGSI_OPCODE_STORE
)
3892 inst
->dst
[0].writemask
= WRITEMASK_XYZW
;
3895 inst
->resource
= image
;
3896 inst
->sampler_array_size
= sampler_array_size
;
3897 inst
->sampler_base
= sampler_base
;
3899 inst
->tex_target
= type
->sampler_index();
3900 inst
->image_format
= st_mesa_format_to_pipe_format(st_context(ctx
),
3901 _mesa_get_shader_image_format(imgvar
->data
.image_format
));
3903 if (imgvar
->data
.memory_coherent
)
3904 inst
->buffer_access
|= TGSI_MEMORY_COHERENT
;
3905 if (imgvar
->data
.memory_restrict
)
3906 inst
->buffer_access
|= TGSI_MEMORY_RESTRICT
;
3907 if (imgvar
->data
.memory_volatile
)
3908 inst
->buffer_access
|= TGSI_MEMORY_VOLATILE
;
3912 glsl_to_tgsi_visitor::visit_generic_intrinsic(ir_call
*ir
, unsigned op
)
3914 ir
->return_deref
->accept(this);
3915 st_dst_reg dst
= st_dst_reg(this->result
);
3917 st_src_reg src
[4] = { undef_src
, undef_src
, undef_src
, undef_src
};
3918 unsigned num_src
= 0;
3919 foreach_in_list(ir_rvalue
, param
, &ir
->actual_parameters
) {
3920 assert(num_src
< ARRAY_SIZE(src
));
3922 this->result
.file
= PROGRAM_UNDEFINED
;
3923 param
->accept(this);
3924 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
3926 src
[num_src
] = this->result
;
3930 emit_asm(ir
, op
, dst
, src
[0], src
[1], src
[2], src
[3]);
3934 glsl_to_tgsi_visitor::visit(ir_call
*ir
)
3936 ir_function_signature
*sig
= ir
->callee
;
3938 /* Filter out intrinsics */
3939 switch (sig
->intrinsic_id
) {
3940 case ir_intrinsic_atomic_counter_read
:
3941 case ir_intrinsic_atomic_counter_increment
:
3942 case ir_intrinsic_atomic_counter_predecrement
:
3943 case ir_intrinsic_atomic_counter_add
:
3944 case ir_intrinsic_atomic_counter_min
:
3945 case ir_intrinsic_atomic_counter_max
:
3946 case ir_intrinsic_atomic_counter_and
:
3947 case ir_intrinsic_atomic_counter_or
:
3948 case ir_intrinsic_atomic_counter_xor
:
3949 case ir_intrinsic_atomic_counter_exchange
:
3950 case ir_intrinsic_atomic_counter_comp_swap
:
3951 visit_atomic_counter_intrinsic(ir
);
3954 case ir_intrinsic_ssbo_load
:
3955 case ir_intrinsic_ssbo_store
:
3956 case ir_intrinsic_ssbo_atomic_add
:
3957 case ir_intrinsic_ssbo_atomic_min
:
3958 case ir_intrinsic_ssbo_atomic_max
:
3959 case ir_intrinsic_ssbo_atomic_and
:
3960 case ir_intrinsic_ssbo_atomic_or
:
3961 case ir_intrinsic_ssbo_atomic_xor
:
3962 case ir_intrinsic_ssbo_atomic_exchange
:
3963 case ir_intrinsic_ssbo_atomic_comp_swap
:
3964 visit_ssbo_intrinsic(ir
);
3967 case ir_intrinsic_memory_barrier
:
3968 case ir_intrinsic_memory_barrier_atomic_counter
:
3969 case ir_intrinsic_memory_barrier_buffer
:
3970 case ir_intrinsic_memory_barrier_image
:
3971 case ir_intrinsic_memory_barrier_shared
:
3972 case ir_intrinsic_group_memory_barrier
:
3973 visit_membar_intrinsic(ir
);
3976 case ir_intrinsic_shared_load
:
3977 case ir_intrinsic_shared_store
:
3978 case ir_intrinsic_shared_atomic_add
:
3979 case ir_intrinsic_shared_atomic_min
:
3980 case ir_intrinsic_shared_atomic_max
:
3981 case ir_intrinsic_shared_atomic_and
:
3982 case ir_intrinsic_shared_atomic_or
:
3983 case ir_intrinsic_shared_atomic_xor
:
3984 case ir_intrinsic_shared_atomic_exchange
:
3985 case ir_intrinsic_shared_atomic_comp_swap
:
3986 visit_shared_intrinsic(ir
);
3989 case ir_intrinsic_image_load
:
3990 case ir_intrinsic_image_store
:
3991 case ir_intrinsic_image_atomic_add
:
3992 case ir_intrinsic_image_atomic_min
:
3993 case ir_intrinsic_image_atomic_max
:
3994 case ir_intrinsic_image_atomic_and
:
3995 case ir_intrinsic_image_atomic_or
:
3996 case ir_intrinsic_image_atomic_xor
:
3997 case ir_intrinsic_image_atomic_exchange
:
3998 case ir_intrinsic_image_atomic_comp_swap
:
3999 case ir_intrinsic_image_size
:
4000 case ir_intrinsic_image_samples
:
4001 visit_image_intrinsic(ir
);
4004 case ir_intrinsic_shader_clock
:
4005 visit_generic_intrinsic(ir
, TGSI_OPCODE_CLOCK
);
4008 case ir_intrinsic_vote_all
:
4009 visit_generic_intrinsic(ir
, TGSI_OPCODE_VOTE_ALL
);
4011 case ir_intrinsic_vote_any
:
4012 visit_generic_intrinsic(ir
, TGSI_OPCODE_VOTE_ANY
);
4014 case ir_intrinsic_vote_eq
:
4015 visit_generic_intrinsic(ir
, TGSI_OPCODE_VOTE_EQ
);
4017 case ir_intrinsic_ballot
:
4018 visit_generic_intrinsic(ir
, TGSI_OPCODE_BALLOT
);
4020 case ir_intrinsic_read_first_invocation
:
4021 visit_generic_intrinsic(ir
, TGSI_OPCODE_READ_FIRST
);
4023 case ir_intrinsic_read_invocation
:
4024 visit_generic_intrinsic(ir
, TGSI_OPCODE_READ_INVOC
);
4027 case ir_intrinsic_invalid
:
4028 case ir_intrinsic_generic_load
:
4029 case ir_intrinsic_generic_store
:
4030 case ir_intrinsic_generic_atomic_add
:
4031 case ir_intrinsic_generic_atomic_and
:
4032 case ir_intrinsic_generic_atomic_or
:
4033 case ir_intrinsic_generic_atomic_xor
:
4034 case ir_intrinsic_generic_atomic_min
:
4035 case ir_intrinsic_generic_atomic_max
:
4036 case ir_intrinsic_generic_atomic_exchange
:
4037 case ir_intrinsic_generic_atomic_comp_swap
:
4038 unreachable("Invalid intrinsic");
4043 glsl_to_tgsi_visitor::calc_deref_offsets(ir_dereference
*tail
,
4044 unsigned *array_elements
,
4046 st_src_reg
*indirect
,
4049 switch (tail
->ir_type
) {
4050 case ir_type_dereference_record
: {
4051 ir_dereference_record
*deref_record
= tail
->as_dereference_record();
4052 const glsl_type
*struct_type
= deref_record
->record
->type
;
4053 int field_index
= deref_record
->record
->type
->field_index(deref_record
->field
);
4055 calc_deref_offsets(deref_record
->record
->as_dereference(), array_elements
, index
, indirect
, location
);
4057 assert(field_index
>= 0);
4058 *location
+= struct_type
->record_location_offset(field_index
);
4062 case ir_type_dereference_array
: {
4063 ir_dereference_array
*deref_arr
= tail
->as_dereference_array();
4064 ir_constant
*array_index
= deref_arr
->array_index
->constant_expression_value();
4067 st_src_reg temp_reg
;
4068 st_dst_reg temp_dst
;
4070 temp_reg
= get_temp(glsl_type::uint_type
);
4071 temp_dst
= st_dst_reg(temp_reg
);
4072 temp_dst
.writemask
= 1;
4074 deref_arr
->array_index
->accept(this);
4075 if (*array_elements
!= 1)
4076 emit_asm(NULL
, TGSI_OPCODE_MUL
, temp_dst
, this->result
, st_src_reg_for_int(*array_elements
));
4078 emit_asm(NULL
, TGSI_OPCODE_MOV
, temp_dst
, this->result
);
4080 if (indirect
->file
== PROGRAM_UNDEFINED
)
4081 *indirect
= temp_reg
;
4083 temp_dst
= st_dst_reg(*indirect
);
4084 temp_dst
.writemask
= 1;
4085 emit_asm(NULL
, TGSI_OPCODE_ADD
, temp_dst
, *indirect
, temp_reg
);
4088 *index
+= array_index
->value
.u
[0] * *array_elements
;
4090 *array_elements
*= deref_arr
->array
->type
->length
;
4092 calc_deref_offsets(deref_arr
->array
->as_dereference(), array_elements
, index
, indirect
, location
);
4101 glsl_to_tgsi_visitor::get_deref_offsets(ir_dereference
*ir
,
4102 unsigned *array_size
,
4105 st_src_reg
*reladdr
,
4108 GLuint shader
= _mesa_program_enum_to_shader_stage(this->prog
->Target
);
4109 unsigned location
= 0;
4110 ir_variable
*var
= ir
->variable_referenced();
4112 memset(reladdr
, 0, sizeof(*reladdr
));
4113 reladdr
->file
= PROGRAM_UNDEFINED
;
4119 location
= var
->data
.location
;
4120 calc_deref_offsets(ir
, array_size
, index
, reladdr
, &location
);
4123 * If we end up with no indirect then adjust the base to the index,
4124 * and set the array size to 1.
4126 if (reladdr
->file
== PROGRAM_UNDEFINED
) {
4132 assert(location
!= 0xffffffff);
4133 *base
+= this->shader_program
->data
->UniformStorage
[location
].opaque
[shader
].index
;
4134 *index
+= this->shader_program
->data
->UniformStorage
[location
].opaque
[shader
].index
;
4139 glsl_to_tgsi_visitor::canonicalize_gather_offset(st_src_reg offset
)
4141 if (offset
.reladdr
|| offset
.reladdr2
) {
4142 st_src_reg tmp
= get_temp(glsl_type::ivec2_type
);
4143 st_dst_reg tmp_dst
= st_dst_reg(tmp
);
4144 tmp_dst
.writemask
= WRITEMASK_XY
;
4145 emit_asm(NULL
, TGSI_OPCODE_MOV
, tmp_dst
, offset
);
4153 glsl_to_tgsi_visitor::visit(ir_texture
*ir
)
4155 st_src_reg result_src
, coord
, cube_sc
, lod_info
, projector
, dx
, dy
;
4156 st_src_reg offset
[MAX_GLSL_TEXTURE_OFFSET
], sample_index
, component
;
4157 st_src_reg levels_src
, reladdr
;
4158 st_dst_reg result_dst
, coord_dst
, cube_sc_dst
;
4159 glsl_to_tgsi_instruction
*inst
= NULL
;
4160 unsigned opcode
= TGSI_OPCODE_NOP
;
4161 const glsl_type
*sampler_type
= ir
->sampler
->type
;
4162 unsigned sampler_array_size
= 1, sampler_base
= 0;
4163 bool is_cube_array
= false, is_cube_shadow
= false;
4166 /* if we are a cube array sampler or a cube shadow */
4167 if (sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
) {
4168 is_cube_array
= sampler_type
->sampler_array
;
4169 is_cube_shadow
= sampler_type
->sampler_shadow
;
4172 if (ir
->coordinate
) {
4173 ir
->coordinate
->accept(this);
4175 /* Put our coords in a temp. We'll need to modify them for shadow,
4176 * projection, or LOD, so the only case we'd use it as-is is if
4177 * we're doing plain old texturing. The optimization passes on
4178 * glsl_to_tgsi_visitor should handle cleaning up our mess in that case.
4180 coord
= get_temp(glsl_type::vec4_type
);
4181 coord_dst
= st_dst_reg(coord
);
4182 coord_dst
.writemask
= (1 << ir
->coordinate
->type
->vector_elements
) - 1;
4183 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
4186 if (ir
->projector
) {
4187 ir
->projector
->accept(this);
4188 projector
= this->result
;
4191 /* Storage for our result. Ideally for an assignment we'd be using
4192 * the actual storage for the result here, instead.
4194 result_src
= get_temp(ir
->type
);
4195 result_dst
= st_dst_reg(result_src
);
4196 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
4200 opcode
= (is_cube_array
&& ir
->shadow_comparator
) ? TGSI_OPCODE_TEX2
: TGSI_OPCODE_TEX
;
4202 ir
->offset
->accept(this);
4203 offset
[0] = this->result
;
4207 if (is_cube_array
|| is_cube_shadow
) {
4208 opcode
= TGSI_OPCODE_TXB2
;
4211 opcode
= TGSI_OPCODE_TXB
;
4213 ir
->lod_info
.bias
->accept(this);
4214 lod_info
= this->result
;
4216 ir
->offset
->accept(this);
4217 offset
[0] = this->result
;
4221 if (this->has_tex_txf_lz
&& ir
->lod_info
.lod
->is_zero()) {
4222 opcode
= TGSI_OPCODE_TEX_LZ
;
4224 opcode
= is_cube_array
? TGSI_OPCODE_TXL2
: TGSI_OPCODE_TXL
;
4225 ir
->lod_info
.lod
->accept(this);
4226 lod_info
= this->result
;
4229 ir
->offset
->accept(this);
4230 offset
[0] = this->result
;
4234 opcode
= TGSI_OPCODE_TXD
;
4235 ir
->lod_info
.grad
.dPdx
->accept(this);
4237 ir
->lod_info
.grad
.dPdy
->accept(this);
4240 ir
->offset
->accept(this);
4241 offset
[0] = this->result
;
4245 opcode
= TGSI_OPCODE_TXQ
;
4246 ir
->lod_info
.lod
->accept(this);
4247 lod_info
= this->result
;
4249 case ir_query_levels
:
4250 opcode
= TGSI_OPCODE_TXQ
;
4251 lod_info
= undef_src
;
4252 levels_src
= get_temp(ir
->type
);
4255 if (this->has_tex_txf_lz
&& ir
->lod_info
.lod
->is_zero()) {
4256 opcode
= TGSI_OPCODE_TXF_LZ
;
4258 opcode
= TGSI_OPCODE_TXF
;
4259 ir
->lod_info
.lod
->accept(this);
4260 lod_info
= this->result
;
4263 ir
->offset
->accept(this);
4264 offset
[0] = this->result
;
4268 opcode
= TGSI_OPCODE_TXF
;
4269 ir
->lod_info
.sample_index
->accept(this);
4270 sample_index
= this->result
;
4273 opcode
= TGSI_OPCODE_TG4
;
4274 ir
->lod_info
.component
->accept(this);
4275 component
= this->result
;
4277 ir
->offset
->accept(this);
4278 if (ir
->offset
->type
->is_array()) {
4279 const glsl_type
*elt_type
= ir
->offset
->type
->fields
.array
;
4280 for (i
= 0; i
< ir
->offset
->type
->length
; i
++) {
4281 offset
[i
] = this->result
;
4282 offset
[i
].index
+= i
* type_size(elt_type
);
4283 offset
[i
].type
= elt_type
->base_type
;
4284 offset
[i
].swizzle
= swizzle_for_size(elt_type
->vector_elements
);
4285 offset
[i
] = canonicalize_gather_offset(offset
[i
]);
4288 offset
[0] = canonicalize_gather_offset(this->result
);
4293 opcode
= TGSI_OPCODE_LODQ
;
4295 case ir_texture_samples
:
4296 opcode
= TGSI_OPCODE_TXQS
;
4298 case ir_samples_identical
:
4299 unreachable("Unexpected ir_samples_identical opcode");
4302 if (ir
->projector
) {
4303 if (opcode
== TGSI_OPCODE_TEX
) {
4304 /* Slot the projector in as the last component of the coord. */
4305 coord_dst
.writemask
= WRITEMASK_W
;
4306 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, projector
);
4307 coord_dst
.writemask
= WRITEMASK_XYZW
;
4308 opcode
= TGSI_OPCODE_TXP
;
4310 st_src_reg coord_w
= coord
;
4311 coord_w
.swizzle
= SWIZZLE_WWWW
;
4313 /* For the other TEX opcodes there's no projective version
4314 * since the last slot is taken up by LOD info. Do the
4315 * projective divide now.
4317 coord_dst
.writemask
= WRITEMASK_W
;
4318 emit_asm(ir
, TGSI_OPCODE_RCP
, coord_dst
, projector
);
4320 /* In the case where we have to project the coordinates "by hand,"
4321 * the shadow comparator value must also be projected.
4323 st_src_reg tmp_src
= coord
;
4324 if (ir
->shadow_comparator
) {
4325 /* Slot the shadow value in as the second to last component of the
4328 ir
->shadow_comparator
->accept(this);
4330 tmp_src
= get_temp(glsl_type::vec4_type
);
4331 st_dst_reg tmp_dst
= st_dst_reg(tmp_src
);
4333 /* Projective division not allowed for array samplers. */
4334 assert(!sampler_type
->sampler_array
);
4336 tmp_dst
.writemask
= WRITEMASK_Z
;
4337 emit_asm(ir
, TGSI_OPCODE_MOV
, tmp_dst
, this->result
);
4339 tmp_dst
.writemask
= WRITEMASK_XY
;
4340 emit_asm(ir
, TGSI_OPCODE_MOV
, tmp_dst
, coord
);
4343 coord_dst
.writemask
= WRITEMASK_XYZ
;
4344 emit_asm(ir
, TGSI_OPCODE_MUL
, coord_dst
, tmp_src
, coord_w
);
4346 coord_dst
.writemask
= WRITEMASK_XYZW
;
4347 coord
.swizzle
= SWIZZLE_XYZW
;
4351 /* If projection is done and the opcode is not TGSI_OPCODE_TXP, then the shadow
4352 * comparator was put in the correct place (and projected) by the code,
4353 * above, that handles by-hand projection.
4355 if (ir
->shadow_comparator
&& (!ir
->projector
|| opcode
== TGSI_OPCODE_TXP
)) {
4356 /* Slot the shadow value in as the second to last component of the
4359 ir
->shadow_comparator
->accept(this);
4361 if (is_cube_array
) {
4362 cube_sc
= get_temp(glsl_type::float_type
);
4363 cube_sc_dst
= st_dst_reg(cube_sc
);
4364 cube_sc_dst
.writemask
= WRITEMASK_X
;
4365 emit_asm(ir
, TGSI_OPCODE_MOV
, cube_sc_dst
, this->result
);
4366 cube_sc_dst
.writemask
= WRITEMASK_X
;
4369 if ((sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_2D
&&
4370 sampler_type
->sampler_array
) ||
4371 sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
) {
4372 coord_dst
.writemask
= WRITEMASK_W
;
4374 coord_dst
.writemask
= WRITEMASK_Z
;
4376 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
4377 coord_dst
.writemask
= WRITEMASK_XYZW
;
4381 if (ir
->op
== ir_txf_ms
) {
4382 coord_dst
.writemask
= WRITEMASK_W
;
4383 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, sample_index
);
4384 coord_dst
.writemask
= WRITEMASK_XYZW
;
4385 } else if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXB
||
4386 opcode
== TGSI_OPCODE_TXF
) {
4387 /* TGSI stores LOD or LOD bias in the last channel of the coords. */
4388 coord_dst
.writemask
= WRITEMASK_W
;
4389 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, lod_info
);
4390 coord_dst
.writemask
= WRITEMASK_XYZW
;
4393 st_src_reg
sampler(PROGRAM_SAMPLER
, 0, GLSL_TYPE_UINT
);
4396 get_deref_offsets(ir
->sampler
, &sampler_array_size
, &sampler_base
,
4397 &index
, &reladdr
, true);
4399 sampler
.index
= index
;
4400 if (reladdr
.file
!= PROGRAM_UNDEFINED
) {
4401 sampler
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
4402 *sampler
.reladdr
= reladdr
;
4403 emit_arl(ir
, sampler_reladdr
, reladdr
);
4406 if (opcode
== TGSI_OPCODE_TXD
)
4407 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, dx
, dy
);
4408 else if (opcode
== TGSI_OPCODE_TXQ
) {
4409 if (ir
->op
== ir_query_levels
) {
4410 /* the level is stored in W */
4411 inst
= emit_asm(ir
, opcode
, st_dst_reg(levels_src
), lod_info
);
4412 result_dst
.writemask
= WRITEMASK_X
;
4413 levels_src
.swizzle
= SWIZZLE_WWWW
;
4414 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, levels_src
);
4416 inst
= emit_asm(ir
, opcode
, result_dst
, lod_info
);
4417 } else if (opcode
== TGSI_OPCODE_TXQS
) {
4418 inst
= emit_asm(ir
, opcode
, result_dst
);
4419 } else if (opcode
== TGSI_OPCODE_TXL2
|| opcode
== TGSI_OPCODE_TXB2
) {
4420 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, lod_info
);
4421 } else if (opcode
== TGSI_OPCODE_TEX2
) {
4422 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, cube_sc
);
4423 } else if (opcode
== TGSI_OPCODE_TG4
) {
4424 if (is_cube_array
&& ir
->shadow_comparator
) {
4425 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, cube_sc
);
4427 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, component
);
4430 inst
= emit_asm(ir
, opcode
, result_dst
, coord
);
4432 if (ir
->shadow_comparator
)
4433 inst
->tex_shadow
= GL_TRUE
;
4435 inst
->resource
= sampler
;
4436 inst
->sampler_array_size
= sampler_array_size
;
4437 inst
->sampler_base
= sampler_base
;
4440 if (!inst
->tex_offsets
)
4441 inst
->tex_offsets
= rzalloc_array(inst
, st_src_reg
, MAX_GLSL_TEXTURE_OFFSET
);
4443 for (i
= 0; i
< MAX_GLSL_TEXTURE_OFFSET
&& offset
[i
].file
!= PROGRAM_UNDEFINED
; i
++)
4444 inst
->tex_offsets
[i
] = offset
[i
];
4445 inst
->tex_offset_num_offset
= i
;
4448 inst
->tex_target
= sampler_type
->sampler_index();
4449 inst
->tex_type
= ir
->type
->base_type
;
4451 this->result
= result_src
;
4455 glsl_to_tgsi_visitor::visit(ir_return
*ir
)
4457 assert(!ir
->get_value());
4459 emit_asm(ir
, TGSI_OPCODE_RET
);
4463 glsl_to_tgsi_visitor::visit(ir_discard
*ir
)
4465 if (ir
->condition
) {
4466 ir
->condition
->accept(this);
4467 st_src_reg condition
= this->result
;
4469 /* Convert the bool condition to a float so we can negate. */
4470 if (native_integers
) {
4471 st_src_reg temp
= get_temp(ir
->condition
->type
);
4472 emit_asm(ir
, TGSI_OPCODE_AND
, st_dst_reg(temp
),
4473 condition
, st_src_reg_for_float(1.0));
4477 condition
.negate
= ~condition
.negate
;
4478 emit_asm(ir
, TGSI_OPCODE_KILL_IF
, undef_dst
, condition
);
4480 /* unconditional kil */
4481 emit_asm(ir
, TGSI_OPCODE_KILL
);
4486 glsl_to_tgsi_visitor::visit(ir_if
*ir
)
4489 glsl_to_tgsi_instruction
*if_inst
;
4491 ir
->condition
->accept(this);
4492 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
4494 if_opcode
= native_integers
? TGSI_OPCODE_UIF
: TGSI_OPCODE_IF
;
4496 if_inst
= emit_asm(ir
->condition
, if_opcode
, undef_dst
, this->result
);
4498 this->instructions
.push_tail(if_inst
);
4500 visit_exec_list(&ir
->then_instructions
, this);
4502 if (!ir
->else_instructions
.is_empty()) {
4503 emit_asm(ir
->condition
, TGSI_OPCODE_ELSE
);
4504 visit_exec_list(&ir
->else_instructions
, this);
4507 if_inst
= emit_asm(ir
->condition
, TGSI_OPCODE_ENDIF
);
4512 glsl_to_tgsi_visitor::visit(ir_emit_vertex
*ir
)
4514 assert(this->prog
->Target
== GL_GEOMETRY_PROGRAM_NV
);
4516 ir
->stream
->accept(this);
4517 emit_asm(ir
, TGSI_OPCODE_EMIT
, undef_dst
, this->result
);
4521 glsl_to_tgsi_visitor::visit(ir_end_primitive
*ir
)
4523 assert(this->prog
->Target
== GL_GEOMETRY_PROGRAM_NV
);
4525 ir
->stream
->accept(this);
4526 emit_asm(ir
, TGSI_OPCODE_ENDPRIM
, undef_dst
, this->result
);
4530 glsl_to_tgsi_visitor::visit(ir_barrier
*ir
)
4532 assert(this->prog
->Target
== GL_TESS_CONTROL_PROGRAM_NV
||
4533 this->prog
->Target
== GL_COMPUTE_PROGRAM_NV
);
4535 emit_asm(ir
, TGSI_OPCODE_BARRIER
);
4538 glsl_to_tgsi_visitor::glsl_to_tgsi_visitor()
4540 STATIC_ASSERT(sizeof(samplers_used
) * 8 >= PIPE_MAX_SAMPLERS
);
4542 result
.file
= PROGRAM_UNDEFINED
;
4549 num_input_arrays
= 0;
4550 num_output_arrays
= 0;
4552 num_address_regs
= 0;
4555 indirect_addr_consts
= false;
4556 wpos_transform_const
= -1;
4558 native_integers
= false;
4559 mem_ctx
= ralloc_context(NULL
);
4562 shader_program
= NULL
;
4567 use_shared_memory
= false;
4568 has_tex_txf_lz
= false;
4572 static void var_destroy(struct hash_entry
*entry
)
4574 variable_storage
*storage
= (variable_storage
*)entry
->data
;
4579 glsl_to_tgsi_visitor::~glsl_to_tgsi_visitor()
4581 _mesa_hash_table_destroy(variables
, var_destroy
);
4583 ralloc_free(mem_ctx
);
4586 extern "C" void free_glsl_to_tgsi_visitor(glsl_to_tgsi_visitor
*v
)
4593 * Count resources used by the given gpu program (number of texture
4597 count_resources(glsl_to_tgsi_visitor
*v
, gl_program
*prog
)
4599 v
->samplers_used
= 0;
4602 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &v
->instructions
) {
4603 if (inst
->info
->is_tex
) {
4604 for (int i
= 0; i
< inst
->sampler_array_size
; i
++) {
4605 unsigned idx
= inst
->sampler_base
+ i
;
4606 v
->samplers_used
|= 1u << idx
;
4608 debug_assert(idx
< (int)ARRAY_SIZE(v
->sampler_types
));
4609 v
->sampler_types
[idx
] = inst
->tex_type
;
4610 v
->sampler_targets
[idx
] =
4611 st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
4613 if (inst
->tex_shadow
) {
4614 prog
->ShadowSamplers
|= 1 << (inst
->resource
.index
+ i
);
4619 if (inst
->tex_target
== TEXTURE_EXTERNAL_INDEX
)
4620 prog
->ExternalSamplersUsed
|= 1 << inst
->resource
.index
;
4622 if (inst
->resource
.file
!= PROGRAM_UNDEFINED
&& (
4623 is_resource_instruction(inst
->op
) ||
4624 inst
->op
== TGSI_OPCODE_STORE
)) {
4625 if (inst
->resource
.file
== PROGRAM_MEMORY
) {
4626 v
->use_shared_memory
= true;
4627 } else if (inst
->resource
.file
== PROGRAM_IMAGE
) {
4628 for (int i
= 0; i
< inst
->sampler_array_size
; i
++) {
4629 unsigned idx
= inst
->sampler_base
+ i
;
4630 v
->images_used
|= 1 << idx
;
4631 v
->image_targets
[idx
] =
4632 st_translate_texture_target(inst
->tex_target
, false);
4633 v
->image_formats
[idx
] = inst
->image_format
;
4638 prog
->SamplersUsed
= v
->samplers_used
;
4640 if (v
->shader_program
!= NULL
)
4641 _mesa_update_shader_textures_used(v
->shader_program
, prog
);
4645 * Returns the mask of channels (bitmask of WRITEMASK_X,Y,Z,W) which
4646 * are read from the given src in this instruction
4649 get_src_arg_mask(st_dst_reg dst
, st_src_reg src
)
4651 int read_mask
= 0, comp
;
4653 /* Now, given the src swizzle and the written channels, find which
4654 * components are actually read
4656 for (comp
= 0; comp
< 4; ++comp
) {
4657 const unsigned coord
= GET_SWZ(src
.swizzle
, comp
);
4659 if (dst
.writemask
& (1 << comp
) && coord
<= SWIZZLE_W
)
4660 read_mask
|= 1 << coord
;
4667 * This pass replaces CMP T0, T1 T2 T0 with MOV T0, T2 when the CMP
4668 * instruction is the first instruction to write to register T0. There are
4669 * several lowering passes done in GLSL IR (e.g. branches and
4670 * relative addressing) that create a large number of conditional assignments
4671 * that ir_to_mesa converts to CMP instructions like the one mentioned above.
4673 * Here is why this conversion is safe:
4674 * CMP T0, T1 T2 T0 can be expanded to:
4680 * If (T1 < 0.0) evaluates to true then our replacement MOV T0, T2 is the same
4681 * as the original program. If (T1 < 0.0) evaluates to false, executing
4682 * MOV T0, T0 will store a garbage value in T0 since T0 is uninitialized.
4683 * Therefore, it doesn't matter that we are replacing MOV T0, T0 with MOV T0, T2
4684 * because any instruction that was going to read from T0 after this was going
4685 * to read a garbage value anyway.
4688 glsl_to_tgsi_visitor::simplify_cmp(void)
4690 int tempWritesSize
= 0;
4691 unsigned *tempWrites
= NULL
;
4692 unsigned outputWrites
[VARYING_SLOT_TESS_MAX
];
4694 memset(outputWrites
, 0, sizeof(outputWrites
));
4696 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4697 unsigned prevWriteMask
= 0;
4699 /* Give up if we encounter relative addressing or flow control. */
4700 if (inst
->dst
[0].reladdr
|| inst
->dst
[0].reladdr2
||
4701 inst
->dst
[1].reladdr
|| inst
->dst
[1].reladdr2
||
4702 inst
->info
->is_branch
||
4703 inst
->op
== TGSI_OPCODE_CONT
||
4704 inst
->op
== TGSI_OPCODE_END
||
4705 inst
->op
== TGSI_OPCODE_RET
) {
4709 if (inst
->dst
[0].file
== PROGRAM_OUTPUT
) {
4710 assert(inst
->dst
[0].index
< (signed)ARRAY_SIZE(outputWrites
));
4711 prevWriteMask
= outputWrites
[inst
->dst
[0].index
];
4712 outputWrites
[inst
->dst
[0].index
] |= inst
->dst
[0].writemask
;
4713 } else if (inst
->dst
[0].file
== PROGRAM_TEMPORARY
) {
4714 if (inst
->dst
[0].index
>= tempWritesSize
) {
4715 const int inc
= 4096;
4717 tempWrites
= (unsigned*)
4719 (tempWritesSize
+ inc
) * sizeof(unsigned));
4723 memset(tempWrites
+ tempWritesSize
, 0, inc
* sizeof(unsigned));
4724 tempWritesSize
+= inc
;
4727 prevWriteMask
= tempWrites
[inst
->dst
[0].index
];
4728 tempWrites
[inst
->dst
[0].index
] |= inst
->dst
[0].writemask
;
4732 /* For a CMP to be considered a conditional write, the destination
4733 * register and source register two must be the same. */
4734 if (inst
->op
== TGSI_OPCODE_CMP
4735 && !(inst
->dst
[0].writemask
& prevWriteMask
)
4736 && inst
->src
[2].file
== inst
->dst
[0].file
4737 && inst
->src
[2].index
== inst
->dst
[0].index
4738 && inst
->dst
[0].writemask
== get_src_arg_mask(inst
->dst
[0], inst
->src
[2])) {
4740 inst
->op
= TGSI_OPCODE_MOV
;
4741 inst
->info
= tgsi_get_opcode_info(inst
->op
);
4742 inst
->src
[0] = inst
->src
[1];
4749 /* Replaces all references to a temporary register index with another index. */
4751 glsl_to_tgsi_visitor::rename_temp_registers(struct rename_reg_pair
*renames
)
4753 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4755 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
4756 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
) {
4757 int old_idx
= inst
->src
[j
].index
;
4758 if (renames
[old_idx
].valid
)
4759 inst
->src
[j
].index
= renames
[old_idx
].new_reg
;
4763 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
4764 if (inst
->tex_offsets
[j
].file
== PROGRAM_TEMPORARY
) {
4765 int old_idx
= inst
->tex_offsets
[j
].index
;
4766 if (renames
[old_idx
].valid
)
4767 inst
->tex_offsets
[j
].index
= renames
[old_idx
].new_reg
;
4771 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4772 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
) {
4773 int old_idx
= inst
->dst
[j
].index
;
4774 if (renames
[old_idx
].valid
)
4775 inst
->dst
[j
].index
= renames
[old_idx
].new_reg
;}
4781 glsl_to_tgsi_visitor::get_first_temp_write(int *first_writes
)
4783 int depth
= 0; /* loop depth */
4784 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
4787 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4788 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4789 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
) {
4790 if (first_writes
[inst
->dst
[j
].index
] == -1)
4791 first_writes
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4795 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
4798 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
4808 glsl_to_tgsi_visitor::get_first_temp_read(int *first_reads
)
4810 int depth
= 0; /* loop depth */
4811 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
4814 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4815 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
4816 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
) {
4817 if (first_reads
[inst
->src
[j
].index
] == -1)
4818 first_reads
[inst
->src
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4821 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
4822 if (inst
->tex_offsets
[j
].file
== PROGRAM_TEMPORARY
) {
4823 if (first_reads
[inst
->tex_offsets
[j
].index
] == -1)
4824 first_reads
[inst
->tex_offsets
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4827 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
4830 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
4840 glsl_to_tgsi_visitor::get_last_temp_read_first_temp_write(int *last_reads
, int *first_writes
)
4842 int depth
= 0; /* loop depth */
4843 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
4846 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4847 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
4848 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
)
4849 last_reads
[inst
->src
[j
].index
] = (depth
== 0) ? i
: -2;
4851 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4852 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
) {
4853 if (first_writes
[inst
->dst
[j
].index
] == -1)
4854 first_writes
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4855 last_reads
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: -2;
4858 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
4859 if (inst
->tex_offsets
[j
].file
== PROGRAM_TEMPORARY
)
4860 last_reads
[inst
->tex_offsets
[j
].index
] = (depth
== 0) ? i
: -2;
4862 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
4865 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
4868 for (k
= 0; k
< this->next_temp
; k
++) {
4869 if (last_reads
[k
] == -2) {
4881 glsl_to_tgsi_visitor::get_last_temp_write(int *last_writes
)
4883 int depth
= 0; /* loop depth */
4887 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4888 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4889 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
)
4890 last_writes
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: -2;
4893 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
4895 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
4897 for (k
= 0; k
< this->next_temp
; k
++) {
4898 if (last_writes
[k
] == -2) {
4909 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
4910 * channels for copy propagation and updates following instructions to
4911 * use the original versions.
4913 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
4914 * will occur. As an example, a TXP production before this pass:
4916 * 0: MOV TEMP[1], INPUT[4].xyyy;
4917 * 1: MOV TEMP[1].w, INPUT[4].wwww;
4918 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
4922 * 0: MOV TEMP[1], INPUT[4].xyyy;
4923 * 1: MOV TEMP[1].w, INPUT[4].wwww;
4924 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
4926 * which allows for dead code elimination on TEMP[1]'s writes.
4929 glsl_to_tgsi_visitor::copy_propagate(void)
4931 glsl_to_tgsi_instruction
**acp
= rzalloc_array(mem_ctx
,
4932 glsl_to_tgsi_instruction
*,
4933 this->next_temp
* 4);
4934 int *acp_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
4937 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4938 assert(inst
->dst
[0].file
!= PROGRAM_TEMPORARY
4939 || inst
->dst
[0].index
< this->next_temp
);
4941 /* First, do any copy propagation possible into the src regs. */
4942 for (int r
= 0; r
< 3; r
++) {
4943 glsl_to_tgsi_instruction
*first
= NULL
;
4945 int acp_base
= inst
->src
[r
].index
* 4;
4947 if (inst
->src
[r
].file
!= PROGRAM_TEMPORARY
||
4948 inst
->src
[r
].reladdr
||
4949 inst
->src
[r
].reladdr2
)
4952 /* See if we can find entries in the ACP consisting of MOVs
4953 * from the same src register for all the swizzled channels
4954 * of this src register reference.
4956 for (int i
= 0; i
< 4; i
++) {
4957 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
4958 glsl_to_tgsi_instruction
*copy_chan
= acp
[acp_base
+ src_chan
];
4965 assert(acp_level
[acp_base
+ src_chan
] <= level
);
4970 if (first
->src
[0].file
!= copy_chan
->src
[0].file
||
4971 first
->src
[0].index
!= copy_chan
->src
[0].index
||
4972 first
->src
[0].double_reg2
!= copy_chan
->src
[0].double_reg2
||
4973 first
->src
[0].index2D
!= copy_chan
->src
[0].index2D
) {
4981 /* We've now validated that we can copy-propagate to
4982 * replace this src register reference. Do it.
4984 inst
->src
[r
].file
= first
->src
[0].file
;
4985 inst
->src
[r
].index
= first
->src
[0].index
;
4986 inst
->src
[r
].index2D
= first
->src
[0].index2D
;
4987 inst
->src
[r
].has_index2
= first
->src
[0].has_index2
;
4988 inst
->src
[r
].double_reg2
= first
->src
[0].double_reg2
;
4989 inst
->src
[r
].array_id
= first
->src
[0].array_id
;
4992 for (int i
= 0; i
< 4; i
++) {
4993 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
4994 glsl_to_tgsi_instruction
*copy_inst
= acp
[acp_base
+ src_chan
];
4995 swizzle
|= (GET_SWZ(copy_inst
->src
[0].swizzle
, src_chan
) << (3 * i
));
4997 inst
->src
[r
].swizzle
= swizzle
;
5002 case TGSI_OPCODE_BGNLOOP
:
5003 case TGSI_OPCODE_ENDLOOP
:
5004 /* End of a basic block, clear the ACP entirely. */
5005 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
5008 case TGSI_OPCODE_IF
:
5009 case TGSI_OPCODE_UIF
:
5013 case TGSI_OPCODE_ENDIF
:
5014 case TGSI_OPCODE_ELSE
:
5015 /* Clear all channels written inside the block from the ACP, but
5016 * leaving those that were not touched.
5018 for (int r
= 0; r
< this->next_temp
; r
++) {
5019 for (int c
= 0; c
< 4; c
++) {
5020 if (!acp
[4 * r
+ c
])
5023 if (acp_level
[4 * r
+ c
] >= level
)
5024 acp
[4 * r
+ c
] = NULL
;
5027 if (inst
->op
== TGSI_OPCODE_ENDIF
)
5032 /* Continuing the block, clear any written channels from
5035 for (int d
= 0; d
< 2; d
++) {
5036 if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
&& inst
->dst
[d
].reladdr
) {
5037 /* Any temporary might be written, so no copy propagation
5038 * across this instruction.
5040 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
5041 } else if (inst
->dst
[d
].file
== PROGRAM_OUTPUT
&&
5042 inst
->dst
[d
].reladdr
) {
5043 /* Any output might be written, so no copy propagation
5044 * from outputs across this instruction.
5046 for (int r
= 0; r
< this->next_temp
; r
++) {
5047 for (int c
= 0; c
< 4; c
++) {
5048 if (!acp
[4 * r
+ c
])
5051 if (acp
[4 * r
+ c
]->src
[0].file
== PROGRAM_OUTPUT
)
5052 acp
[4 * r
+ c
] = NULL
;
5055 } else if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
||
5056 inst
->dst
[d
].file
== PROGRAM_OUTPUT
) {
5057 /* Clear where it's used as dst. */
5058 if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
) {
5059 for (int c
= 0; c
< 4; c
++) {
5060 if (inst
->dst
[d
].writemask
& (1 << c
))
5061 acp
[4 * inst
->dst
[d
].index
+ c
] = NULL
;
5065 /* Clear where it's used as src. */
5066 for (int r
= 0; r
< this->next_temp
; r
++) {
5067 for (int c
= 0; c
< 4; c
++) {
5068 if (!acp
[4 * r
+ c
])
5071 int src_chan
= GET_SWZ(acp
[4 * r
+ c
]->src
[0].swizzle
, c
);
5073 if (acp
[4 * r
+ c
]->src
[0].file
== inst
->dst
[d
].file
&&
5074 acp
[4 * r
+ c
]->src
[0].index
== inst
->dst
[d
].index
&&
5075 inst
->dst
[d
].writemask
& (1 << src_chan
)) {
5076 acp
[4 * r
+ c
] = NULL
;
5085 /* If this is a copy, add it to the ACP. */
5086 if (inst
->op
== TGSI_OPCODE_MOV
&&
5087 inst
->dst
[0].file
== PROGRAM_TEMPORARY
&&
5088 !(inst
->dst
[0].file
== inst
->src
[0].file
&&
5089 inst
->dst
[0].index
== inst
->src
[0].index
) &&
5090 !inst
->dst
[0].reladdr
&&
5091 !inst
->dst
[0].reladdr2
&&
5093 inst
->src
[0].file
!= PROGRAM_ARRAY
&&
5094 !inst
->src
[0].reladdr
&&
5095 !inst
->src
[0].reladdr2
&&
5096 !inst
->src
[0].negate
&&
5097 !inst
->src
[0].abs
) {
5098 for (int i
= 0; i
< 4; i
++) {
5099 if (inst
->dst
[0].writemask
& (1 << i
)) {
5100 acp
[4 * inst
->dst
[0].index
+ i
] = inst
;
5101 acp_level
[4 * inst
->dst
[0].index
+ i
] = level
;
5107 ralloc_free(acp_level
);
5112 * On a basic block basis, tracks available PROGRAM_TEMPORARY registers for dead
5115 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
5116 * will occur. As an example, a TXP production after copy propagation but
5119 * 0: MOV TEMP[1], INPUT[4].xyyy;
5120 * 1: MOV TEMP[1].w, INPUT[4].wwww;
5121 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5123 * and after this pass:
5125 * 0: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5128 glsl_to_tgsi_visitor::eliminate_dead_code(void)
5130 glsl_to_tgsi_instruction
**writes
= rzalloc_array(mem_ctx
,
5131 glsl_to_tgsi_instruction
*,
5132 this->next_temp
* 4);
5133 int *write_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
5137 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5138 assert(inst
->dst
[0].file
!= PROGRAM_TEMPORARY
5139 || inst
->dst
[0].index
< this->next_temp
);
5142 case TGSI_OPCODE_BGNLOOP
:
5143 case TGSI_OPCODE_ENDLOOP
:
5144 case TGSI_OPCODE_CONT
:
5145 case TGSI_OPCODE_BRK
:
5146 /* End of a basic block, clear the write array entirely.
5148 * This keeps us from killing dead code when the writes are
5149 * on either side of a loop, even when the register isn't touched
5150 * inside the loop. However, glsl_to_tgsi_visitor doesn't seem to emit
5151 * dead code of this type, so it shouldn't make a difference as long as
5152 * the dead code elimination pass in the GLSL compiler does its job.
5154 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5157 case TGSI_OPCODE_ENDIF
:
5158 case TGSI_OPCODE_ELSE
:
5159 /* Promote the recorded level of all channels written inside the
5160 * preceding if or else block to the level above the if/else block.
5162 for (int r
= 0; r
< this->next_temp
; r
++) {
5163 for (int c
= 0; c
< 4; c
++) {
5164 if (!writes
[4 * r
+ c
])
5167 if (write_level
[4 * r
+ c
] == level
)
5168 write_level
[4 * r
+ c
] = level
-1;
5171 if(inst
->op
== TGSI_OPCODE_ENDIF
)
5175 case TGSI_OPCODE_IF
:
5176 case TGSI_OPCODE_UIF
:
5178 /* fallthrough to default case to mark the condition as read */
5180 /* Continuing the block, clear any channels from the write array that
5181 * are read by this instruction.
5183 for (unsigned i
= 0; i
< ARRAY_SIZE(inst
->src
); i
++) {
5184 if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
&& inst
->src
[i
].reladdr
){
5185 /* Any temporary might be read, so no dead code elimination
5186 * across this instruction.
5188 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5189 } else if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
) {
5190 /* Clear where it's used as src. */
5191 int src_chans
= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 0);
5192 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 1);
5193 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 2);
5194 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 3);
5196 for (int c
= 0; c
< 4; c
++) {
5197 if (src_chans
& (1 << c
))
5198 writes
[4 * inst
->src
[i
].index
+ c
] = NULL
;
5202 for (unsigned i
= 0; i
< inst
->tex_offset_num_offset
; i
++) {
5203 if (inst
->tex_offsets
[i
].file
== PROGRAM_TEMPORARY
&& inst
->tex_offsets
[i
].reladdr
){
5204 /* Any temporary might be read, so no dead code elimination
5205 * across this instruction.
5207 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5208 } else if (inst
->tex_offsets
[i
].file
== PROGRAM_TEMPORARY
) {
5209 /* Clear where it's used as src. */
5210 int src_chans
= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 0);
5211 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 1);
5212 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 2);
5213 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 3);
5215 for (int c
= 0; c
< 4; c
++) {
5216 if (src_chans
& (1 << c
))
5217 writes
[4 * inst
->tex_offsets
[i
].index
+ c
] = NULL
;
5224 /* If this instruction writes to a temporary, add it to the write array.
5225 * If there is already an instruction in the write array for one or more
5226 * of the channels, flag that channel write as dead.
5228 for (unsigned i
= 0; i
< ARRAY_SIZE(inst
->dst
); i
++) {
5229 if (inst
->dst
[i
].file
== PROGRAM_TEMPORARY
&&
5230 !inst
->dst
[i
].reladdr
) {
5231 for (int c
= 0; c
< 4; c
++) {
5232 if (inst
->dst
[i
].writemask
& (1 << c
)) {
5233 if (writes
[4 * inst
->dst
[i
].index
+ c
]) {
5234 if (write_level
[4 * inst
->dst
[i
].index
+ c
] < level
)
5237 writes
[4 * inst
->dst
[i
].index
+ c
]->dead_mask
|= (1 << c
);
5239 writes
[4 * inst
->dst
[i
].index
+ c
] = inst
;
5240 write_level
[4 * inst
->dst
[i
].index
+ c
] = level
;
5247 /* Anything still in the write array at this point is dead code. */
5248 for (int r
= 0; r
< this->next_temp
; r
++) {
5249 for (int c
= 0; c
< 4; c
++) {
5250 glsl_to_tgsi_instruction
*inst
= writes
[4 * r
+ c
];
5252 inst
->dead_mask
|= (1 << c
);
5256 /* Now actually remove the instructions that are completely dead and update
5257 * the writemask of other instructions with dead channels.
5259 foreach_in_list_safe(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5260 if (!inst
->dead_mask
|| !inst
->dst
[0].writemask
)
5262 /* No amount of dead masks should remove memory stores */
5263 if (inst
->info
->is_store
)
5266 if ((inst
->dst
[0].writemask
& ~inst
->dead_mask
) == 0) {
5271 if (glsl_base_type_is_64bit(inst
->dst
[0].type
)) {
5272 if (inst
->dead_mask
== WRITEMASK_XY
||
5273 inst
->dead_mask
== WRITEMASK_ZW
)
5274 inst
->dst
[0].writemask
&= ~(inst
->dead_mask
);
5276 inst
->dst
[0].writemask
&= ~(inst
->dead_mask
);
5280 ralloc_free(write_level
);
5281 ralloc_free(writes
);
5286 /* merge DFRACEXP instructions into one. */
5288 glsl_to_tgsi_visitor::merge_two_dsts(void)
5290 foreach_in_list_safe(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5291 glsl_to_tgsi_instruction
*inst2
;
5293 if (num_inst_dst_regs(inst
) != 2)
5296 if (inst
->dst
[0].file
!= PROGRAM_UNDEFINED
&&
5297 inst
->dst
[1].file
!= PROGRAM_UNDEFINED
)
5300 inst2
= (glsl_to_tgsi_instruction
*) inst
->next
;
5303 if (inst
->src
[0].file
== inst2
->src
[0].file
&&
5304 inst
->src
[0].index
== inst2
->src
[0].index
&&
5305 inst
->src
[0].type
== inst2
->src
[0].type
&&
5306 inst
->src
[0].swizzle
== inst2
->src
[0].swizzle
)
5308 inst2
= (glsl_to_tgsi_instruction
*) inst2
->next
;
5314 if (inst
->dst
[0].file
== PROGRAM_UNDEFINED
) {
5316 inst
->dst
[0] = inst2
->dst
[0];
5317 } else if (inst
->dst
[1].file
== PROGRAM_UNDEFINED
) {
5318 inst
->dst
[1] = inst2
->dst
[1];
5329 /* Merges temporary registers together where possible to reduce the number of
5330 * registers needed to run a program.
5332 * Produces optimal code only after copy propagation and dead code elimination
5335 glsl_to_tgsi_visitor::merge_registers(void)
5337 int *last_reads
= ralloc_array(mem_ctx
, int, this->next_temp
);
5338 int *first_writes
= ralloc_array(mem_ctx
, int, this->next_temp
);
5339 struct rename_reg_pair
*renames
= rzalloc_array(mem_ctx
, struct rename_reg_pair
, this->next_temp
);
5342 /* Read the indices of the last read and first write to each temp register
5343 * into an array so that we don't have to traverse the instruction list as
5345 for (i
= 0; i
< this->next_temp
; i
++) {
5347 first_writes
[i
] = -1;
5349 get_last_temp_read_first_temp_write(last_reads
, first_writes
);
5351 /* Start looking for registers with non-overlapping usages that can be
5352 * merged together. */
5353 for (i
= 0; i
< this->next_temp
; i
++) {
5354 /* Don't touch unused registers. */
5355 if (last_reads
[i
] < 0 || first_writes
[i
] < 0) continue;
5357 for (j
= 0; j
< this->next_temp
; j
++) {
5358 /* Don't touch unused registers. */
5359 if (last_reads
[j
] < 0 || first_writes
[j
] < 0) continue;
5361 /* We can merge the two registers if the first write to j is after or
5362 * in the same instruction as the last read from i. Note that the
5363 * register at index i will always be used earlier or at the same time
5364 * as the register at index j. */
5365 if (first_writes
[i
] <= first_writes
[j
] &&
5366 last_reads
[i
] <= first_writes
[j
]) {
5367 renames
[j
].new_reg
= i
;
5368 renames
[j
].valid
= true;
5370 /* Update the first_writes and last_reads arrays with the new
5371 * values for the merged register index, and mark the newly unused
5372 * register index as such. */
5373 assert(last_reads
[j
] >= last_reads
[i
]);
5374 last_reads
[i
] = last_reads
[j
];
5375 first_writes
[j
] = -1;
5381 rename_temp_registers(renames
);
5382 ralloc_free(renames
);
5383 ralloc_free(last_reads
);
5384 ralloc_free(first_writes
);
5387 /* Reassign indices to temporary registers by reusing unused indices created
5388 * by optimization passes. */
5390 glsl_to_tgsi_visitor::renumber_registers(void)
5394 int *first_writes
= ralloc_array(mem_ctx
, int, this->next_temp
);
5395 struct rename_reg_pair
*renames
= rzalloc_array(mem_ctx
, struct rename_reg_pair
, this->next_temp
);
5397 for (i
= 0; i
< this->next_temp
; i
++) {
5398 first_writes
[i
] = -1;
5400 get_first_temp_write(first_writes
);
5402 for (i
= 0; i
< this->next_temp
; i
++) {
5403 if (first_writes
[i
] < 0) continue;
5404 if (i
!= new_index
) {
5405 renames
[i
].new_reg
= new_index
;
5406 renames
[i
].valid
= true;
5411 rename_temp_registers(renames
);
5412 this->next_temp
= new_index
;
5413 ralloc_free(renames
);
5414 ralloc_free(first_writes
);
5417 /* ------------------------- TGSI conversion stuff -------------------------- */
5420 * Intermediate state used during shader translation.
5422 struct st_translate
{
5423 struct ureg_program
*ureg
;
5425 unsigned temps_size
;
5426 struct ureg_dst
*temps
;
5428 struct ureg_dst
*arrays
;
5429 unsigned num_temp_arrays
;
5430 struct ureg_src
*constants
;
5432 struct ureg_src
*immediates
;
5434 struct ureg_dst outputs
[PIPE_MAX_SHADER_OUTPUTS
];
5435 struct ureg_src inputs
[PIPE_MAX_SHADER_INPUTS
];
5436 struct ureg_dst address
[3];
5437 struct ureg_src samplers
[PIPE_MAX_SAMPLERS
];
5438 struct ureg_src buffers
[PIPE_MAX_SHADER_BUFFERS
];
5439 struct ureg_src images
[PIPE_MAX_SHADER_IMAGES
];
5440 struct ureg_src systemValues
[SYSTEM_VALUE_MAX
];
5441 struct ureg_src shared_memory
;
5442 unsigned *array_sizes
;
5443 struct inout_decl
*input_decls
;
5444 unsigned num_input_decls
;
5445 struct inout_decl
*output_decls
;
5446 unsigned num_output_decls
;
5448 const ubyte
*inputMapping
;
5449 const ubyte
*outputMapping
;
5451 unsigned procType
; /**< PIPE_SHADER_VERTEX/FRAGMENT */
5454 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
5456 _mesa_sysval_to_semantic(unsigned sysval
)
5460 case SYSTEM_VALUE_VERTEX_ID
:
5461 return TGSI_SEMANTIC_VERTEXID
;
5462 case SYSTEM_VALUE_INSTANCE_ID
:
5463 return TGSI_SEMANTIC_INSTANCEID
;
5464 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
5465 return TGSI_SEMANTIC_VERTEXID_NOBASE
;
5466 case SYSTEM_VALUE_BASE_VERTEX
:
5467 return TGSI_SEMANTIC_BASEVERTEX
;
5468 case SYSTEM_VALUE_BASE_INSTANCE
:
5469 return TGSI_SEMANTIC_BASEINSTANCE
;
5470 case SYSTEM_VALUE_DRAW_ID
:
5471 return TGSI_SEMANTIC_DRAWID
;
5473 /* Geometry shader */
5474 case SYSTEM_VALUE_INVOCATION_ID
:
5475 return TGSI_SEMANTIC_INVOCATIONID
;
5477 /* Fragment shader */
5478 case SYSTEM_VALUE_FRAG_COORD
:
5479 return TGSI_SEMANTIC_POSITION
;
5480 case SYSTEM_VALUE_FRONT_FACE
:
5481 return TGSI_SEMANTIC_FACE
;
5482 case SYSTEM_VALUE_SAMPLE_ID
:
5483 return TGSI_SEMANTIC_SAMPLEID
;
5484 case SYSTEM_VALUE_SAMPLE_POS
:
5485 return TGSI_SEMANTIC_SAMPLEPOS
;
5486 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
5487 return TGSI_SEMANTIC_SAMPLEMASK
;
5488 case SYSTEM_VALUE_HELPER_INVOCATION
:
5489 return TGSI_SEMANTIC_HELPER_INVOCATION
;
5491 /* Tessellation shader */
5492 case SYSTEM_VALUE_TESS_COORD
:
5493 return TGSI_SEMANTIC_TESSCOORD
;
5494 case SYSTEM_VALUE_VERTICES_IN
:
5495 return TGSI_SEMANTIC_VERTICESIN
;
5496 case SYSTEM_VALUE_PRIMITIVE_ID
:
5497 return TGSI_SEMANTIC_PRIMID
;
5498 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
5499 return TGSI_SEMANTIC_TESSOUTER
;
5500 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
5501 return TGSI_SEMANTIC_TESSINNER
;
5503 /* Compute shader */
5504 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
5505 return TGSI_SEMANTIC_THREAD_ID
;
5506 case SYSTEM_VALUE_WORK_GROUP_ID
:
5507 return TGSI_SEMANTIC_BLOCK_ID
;
5508 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
5509 return TGSI_SEMANTIC_GRID_SIZE
;
5510 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
5511 return TGSI_SEMANTIC_BLOCK_SIZE
;
5513 /* ARB_shader_ballot */
5514 case SYSTEM_VALUE_SUBGROUP_SIZE
:
5515 return TGSI_SEMANTIC_SUBGROUP_SIZE
;
5516 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
5517 return TGSI_SEMANTIC_SUBGROUP_INVOCATION
;
5518 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
5519 return TGSI_SEMANTIC_SUBGROUP_EQ_MASK
;
5520 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
5521 return TGSI_SEMANTIC_SUBGROUP_GE_MASK
;
5522 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
5523 return TGSI_SEMANTIC_SUBGROUP_GT_MASK
;
5524 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
5525 return TGSI_SEMANTIC_SUBGROUP_LE_MASK
;
5526 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
5527 return TGSI_SEMANTIC_SUBGROUP_LT_MASK
;
5530 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
5531 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
5532 case SYSTEM_VALUE_VERTEX_CNT
:
5534 assert(!"Unexpected SYSTEM_VALUE_ enum");
5535 return TGSI_SEMANTIC_COUNT
;
5540 * Map a glsl_to_tgsi constant/immediate to a TGSI immediate.
5542 static struct ureg_src
5543 emit_immediate(struct st_translate
*t
,
5544 gl_constant_value values
[4],
5547 struct ureg_program
*ureg
= t
->ureg
;
5552 return ureg_DECL_immediate(ureg
, &values
[0].f
, size
);
5554 return ureg_DECL_immediate_f64(ureg
, (double *)&values
[0].f
, size
);
5556 return ureg_DECL_immediate_int64(ureg
, (int64_t *)&values
[0].f
, size
);
5557 case GL_UNSIGNED_INT64_ARB
:
5558 return ureg_DECL_immediate_uint64(ureg
, (uint64_t *)&values
[0].f
, size
);
5560 return ureg_DECL_immediate_int(ureg
, &values
[0].i
, size
);
5561 case GL_UNSIGNED_INT
:
5563 return ureg_DECL_immediate_uint(ureg
, &values
[0].u
, size
);
5565 assert(!"should not get here - type must be float, int, uint, or bool");
5566 return ureg_src_undef();
5571 * Map a glsl_to_tgsi dst register to a TGSI ureg_dst register.
5573 static struct ureg_dst
5574 dst_register(struct st_translate
*t
, gl_register_file file
, unsigned index
,
5580 case PROGRAM_UNDEFINED
:
5581 return ureg_dst_undef();
5583 case PROGRAM_TEMPORARY
:
5584 /* Allocate space for temporaries on demand. */
5585 if (index
>= t
->temps_size
) {
5586 const int inc
= align(index
- t
->temps_size
+ 1, 4096);
5588 t
->temps
= (struct ureg_dst
*)
5590 (t
->temps_size
+ inc
) * sizeof(struct ureg_dst
));
5592 return ureg_dst_undef();
5594 memset(t
->temps
+ t
->temps_size
, 0, inc
* sizeof(struct ureg_dst
));
5595 t
->temps_size
+= inc
;
5598 if (ureg_dst_is_undef(t
->temps
[index
]))
5599 t
->temps
[index
] = ureg_DECL_local_temporary(t
->ureg
);
5601 return t
->temps
[index
];
5604 assert(array_id
&& array_id
<= t
->num_temp_arrays
);
5605 array
= array_id
- 1;
5607 if (ureg_dst_is_undef(t
->arrays
[array
]))
5608 t
->arrays
[array
] = ureg_DECL_array_temporary(
5609 t
->ureg
, t
->array_sizes
[array
], TRUE
);
5611 return ureg_dst_array_offset(t
->arrays
[array
], index
);
5613 case PROGRAM_OUTPUT
:
5615 if (t
->procType
== PIPE_SHADER_FRAGMENT
)
5616 assert(index
< 2 * FRAG_RESULT_MAX
);
5617 else if (t
->procType
== PIPE_SHADER_TESS_CTRL
||
5618 t
->procType
== PIPE_SHADER_TESS_EVAL
)
5619 assert(index
< VARYING_SLOT_TESS_MAX
);
5621 assert(index
< VARYING_SLOT_MAX
);
5623 assert(t
->outputMapping
[index
] < ARRAY_SIZE(t
->outputs
));
5624 assert(t
->outputs
[t
->outputMapping
[index
]].File
!= TGSI_FILE_NULL
);
5625 return t
->outputs
[t
->outputMapping
[index
]];
5628 struct inout_decl
*decl
= find_inout_array(t
->output_decls
, t
->num_output_decls
, array_id
);
5629 unsigned mesa_index
= decl
->mesa_index
;
5630 int slot
= t
->outputMapping
[mesa_index
];
5632 assert(slot
!= -1 && t
->outputs
[slot
].File
== TGSI_FILE_OUTPUT
);
5634 struct ureg_dst dst
= t
->outputs
[slot
];
5635 dst
.ArrayID
= array_id
;
5636 return ureg_dst_array_offset(dst
, index
- mesa_index
);
5639 case PROGRAM_ADDRESS
:
5640 return t
->address
[index
];
5643 assert(!"unknown dst register file");
5644 return ureg_dst_undef();
5649 * Map a glsl_to_tgsi src register to a TGSI ureg_src register.
5651 static struct ureg_src
5652 src_register(struct st_translate
*t
, const st_src_reg
*reg
)
5654 int index
= reg
->index
;
5655 int double_reg2
= reg
->double_reg2
? 1 : 0;
5658 case PROGRAM_UNDEFINED
:
5659 return ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
5661 case PROGRAM_TEMPORARY
:
5663 return ureg_src(dst_register(t
, reg
->file
, reg
->index
, reg
->array_id
));
5665 case PROGRAM_OUTPUT
: {
5666 struct ureg_dst dst
= dst_register(t
, reg
->file
, reg
->index
, reg
->array_id
);
5667 assert(dst
.WriteMask
!= 0);
5668 unsigned shift
= ffs(dst
.WriteMask
) - 1;
5669 return ureg_swizzle(ureg_src(dst
),
5673 MIN2(shift
+ 3, 3));
5676 case PROGRAM_UNIFORM
:
5677 assert(reg
->index
>= 0);
5678 return reg
->index
< t
->num_constants
?
5679 t
->constants
[reg
->index
] : ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
5680 case PROGRAM_STATE_VAR
:
5681 case PROGRAM_CONSTANT
: /* ie, immediate */
5682 if (reg
->has_index2
)
5683 return ureg_src_register(TGSI_FILE_CONSTANT
, reg
->index
);
5685 return reg
->index
>= 0 && reg
->index
< t
->num_constants
?
5686 t
->constants
[reg
->index
] : ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
5688 case PROGRAM_IMMEDIATE
:
5689 assert(reg
->index
>= 0 && reg
->index
< t
->num_immediates
);
5690 return t
->immediates
[reg
->index
];
5693 /* GLSL inputs are 64-bit containers, so we have to
5694 * map back to the original index and add the offset after
5696 index
-= double_reg2
;
5697 if (!reg
->array_id
) {
5698 assert(t
->inputMapping
[index
] < ARRAY_SIZE(t
->inputs
));
5699 assert(t
->inputs
[t
->inputMapping
[index
]].File
!= TGSI_FILE_NULL
);
5700 return t
->inputs
[t
->inputMapping
[index
] + double_reg2
];
5703 struct inout_decl
*decl
= find_inout_array(t
->input_decls
, t
->num_input_decls
, reg
->array_id
);
5704 unsigned mesa_index
= decl
->mesa_index
;
5705 int slot
= t
->inputMapping
[mesa_index
];
5707 assert(slot
!= -1 && t
->inputs
[slot
].File
== TGSI_FILE_INPUT
);
5709 struct ureg_src src
= t
->inputs
[slot
];
5710 src
.ArrayID
= reg
->array_id
;
5711 return ureg_src_array_offset(src
, index
+ double_reg2
- mesa_index
);
5714 case PROGRAM_ADDRESS
:
5715 return ureg_src(t
->address
[reg
->index
]);
5717 case PROGRAM_SYSTEM_VALUE
:
5718 assert(reg
->index
< (int) ARRAY_SIZE(t
->systemValues
));
5719 return t
->systemValues
[reg
->index
];
5722 assert(!"unknown src register file");
5723 return ureg_src_undef();
5728 * Create a TGSI ureg_dst register from an st_dst_reg.
5730 static struct ureg_dst
5731 translate_dst(struct st_translate
*t
,
5732 const st_dst_reg
*dst_reg
,
5735 struct ureg_dst dst
= dst_register(t
, dst_reg
->file
, dst_reg
->index
,
5738 if (dst
.File
== TGSI_FILE_NULL
)
5741 dst
= ureg_writemask(dst
, dst_reg
->writemask
);
5744 dst
= ureg_saturate(dst
);
5746 if (dst_reg
->reladdr
!= NULL
) {
5747 assert(dst_reg
->file
!= PROGRAM_TEMPORARY
);
5748 dst
= ureg_dst_indirect(dst
, ureg_src(t
->address
[0]));
5751 if (dst_reg
->has_index2
) {
5752 if (dst_reg
->reladdr2
)
5753 dst
= ureg_dst_dimension_indirect(dst
, ureg_src(t
->address
[1]),
5756 dst
= ureg_dst_dimension(dst
, dst_reg
->index2D
);
5763 * Create a TGSI ureg_src register from an st_src_reg.
5765 static struct ureg_src
5766 translate_src(struct st_translate
*t
, const st_src_reg
*src_reg
)
5768 struct ureg_src src
= src_register(t
, src_reg
);
5770 if (src_reg
->has_index2
) {
5771 /* 2D indexes occur with geometry shader inputs (attrib, vertex)
5772 * and UBO constant buffers (buffer, position).
5774 if (src_reg
->reladdr2
)
5775 src
= ureg_src_dimension_indirect(src
, ureg_src(t
->address
[1]),
5778 src
= ureg_src_dimension(src
, src_reg
->index2D
);
5781 src
= ureg_swizzle(src
,
5782 GET_SWZ(src_reg
->swizzle
, 0) & 0x3,
5783 GET_SWZ(src_reg
->swizzle
, 1) & 0x3,
5784 GET_SWZ(src_reg
->swizzle
, 2) & 0x3,
5785 GET_SWZ(src_reg
->swizzle
, 3) & 0x3);
5788 src
= ureg_abs(src
);
5790 if ((src_reg
->negate
& 0xf) == NEGATE_XYZW
)
5791 src
= ureg_negate(src
);
5793 if (src_reg
->reladdr
!= NULL
) {
5794 assert(src_reg
->file
!= PROGRAM_TEMPORARY
);
5795 src
= ureg_src_indirect(src
, ureg_src(t
->address
[0]));
5801 static struct tgsi_texture_offset
5802 translate_tex_offset(struct st_translate
*t
,
5803 const st_src_reg
*in_offset
)
5805 struct tgsi_texture_offset offset
;
5806 struct ureg_src src
= translate_src(t
, in_offset
);
5808 offset
.File
= src
.File
;
5809 offset
.Index
= src
.Index
;
5810 offset
.SwizzleX
= src
.SwizzleX
;
5811 offset
.SwizzleY
= src
.SwizzleY
;
5812 offset
.SwizzleZ
= src
.SwizzleZ
;
5815 assert(!src
.Indirect
);
5816 assert(!src
.DimIndirect
);
5817 assert(!src
.Dimension
);
5818 assert(!src
.Absolute
); /* those shouldn't be used with integers anyway */
5819 assert(!src
.Negate
);
5825 compile_tgsi_instruction(struct st_translate
*t
,
5826 const glsl_to_tgsi_instruction
*inst
)
5828 struct ureg_program
*ureg
= t
->ureg
;
5830 struct ureg_dst dst
[2];
5831 struct ureg_src src
[4];
5832 struct tgsi_texture_offset texoffsets
[MAX_GLSL_TEXTURE_OFFSET
];
5836 unsigned tex_target
= 0;
5838 num_dst
= num_inst_dst_regs(inst
);
5839 num_src
= num_inst_src_regs(inst
);
5841 for (i
= 0; i
< num_dst
; i
++)
5842 dst
[i
] = translate_dst(t
,
5846 for (i
= 0; i
< num_src
; i
++)
5847 src
[i
] = translate_src(t
, &inst
->src
[i
]);
5850 case TGSI_OPCODE_BGNLOOP
:
5851 case TGSI_OPCODE_ELSE
:
5852 case TGSI_OPCODE_ENDLOOP
:
5853 case TGSI_OPCODE_IF
:
5854 case TGSI_OPCODE_UIF
:
5855 assert(num_dst
== 0);
5856 ureg_insn(ureg
, inst
->op
, NULL
, 0, src
, num_src
);
5859 case TGSI_OPCODE_TEX
:
5860 case TGSI_OPCODE_TEX_LZ
:
5861 case TGSI_OPCODE_TXB
:
5862 case TGSI_OPCODE_TXD
:
5863 case TGSI_OPCODE_TXL
:
5864 case TGSI_OPCODE_TXP
:
5865 case TGSI_OPCODE_TXQ
:
5866 case TGSI_OPCODE_TXQS
:
5867 case TGSI_OPCODE_TXF
:
5868 case TGSI_OPCODE_TXF_LZ
:
5869 case TGSI_OPCODE_TEX2
:
5870 case TGSI_OPCODE_TXB2
:
5871 case TGSI_OPCODE_TXL2
:
5872 case TGSI_OPCODE_TG4
:
5873 case TGSI_OPCODE_LODQ
:
5874 src
[num_src
] = t
->samplers
[inst
->resource
.index
];
5875 assert(src
[num_src
].File
!= TGSI_FILE_NULL
);
5876 if (inst
->resource
.reladdr
)
5878 ureg_src_indirect(src
[num_src
], ureg_src(t
->address
[2]));
5880 for (i
= 0; i
< (int)inst
->tex_offset_num_offset
; i
++) {
5881 texoffsets
[i
] = translate_tex_offset(t
, &inst
->tex_offsets
[i
]);
5883 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
5889 st_translate_texture_type(inst
->tex_type
),
5890 texoffsets
, inst
->tex_offset_num_offset
,
5894 case TGSI_OPCODE_RESQ
:
5895 case TGSI_OPCODE_LOAD
:
5896 case TGSI_OPCODE_ATOMUADD
:
5897 case TGSI_OPCODE_ATOMXCHG
:
5898 case TGSI_OPCODE_ATOMCAS
:
5899 case TGSI_OPCODE_ATOMAND
:
5900 case TGSI_OPCODE_ATOMOR
:
5901 case TGSI_OPCODE_ATOMXOR
:
5902 case TGSI_OPCODE_ATOMUMIN
:
5903 case TGSI_OPCODE_ATOMUMAX
:
5904 case TGSI_OPCODE_ATOMIMIN
:
5905 case TGSI_OPCODE_ATOMIMAX
:
5906 for (i
= num_src
- 1; i
>= 0; i
--)
5907 src
[i
+ 1] = src
[i
];
5909 if (inst
->resource
.file
== PROGRAM_MEMORY
) {
5910 src
[0] = t
->shared_memory
;
5911 } else if (inst
->resource
.file
== PROGRAM_BUFFER
) {
5912 src
[0] = t
->buffers
[inst
->resource
.index
];
5914 src
[0] = t
->images
[inst
->resource
.index
];
5915 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
5917 if (inst
->resource
.reladdr
)
5918 src
[0] = ureg_src_indirect(src
[0], ureg_src(t
->address
[2]));
5919 assert(src
[0].File
!= TGSI_FILE_NULL
);
5920 ureg_memory_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
,
5921 inst
->buffer_access
,
5922 tex_target
, inst
->image_format
);
5925 case TGSI_OPCODE_STORE
:
5926 if (inst
->resource
.file
== PROGRAM_MEMORY
) {
5927 dst
[0] = ureg_dst(t
->shared_memory
);
5928 } else if (inst
->resource
.file
== PROGRAM_BUFFER
) {
5929 dst
[0] = ureg_dst(t
->buffers
[inst
->resource
.index
]);
5931 dst
[0] = ureg_dst(t
->images
[inst
->resource
.index
]);
5932 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
5934 dst
[0] = ureg_writemask(dst
[0], inst
->dst
[0].writemask
);
5935 if (inst
->resource
.reladdr
)
5936 dst
[0] = ureg_dst_indirect(dst
[0], ureg_src(t
->address
[2]));
5937 assert(dst
[0].File
!= TGSI_FILE_NULL
);
5938 ureg_memory_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
,
5939 inst
->buffer_access
,
5940 tex_target
, inst
->image_format
);
5943 case TGSI_OPCODE_SCS
:
5944 dst
[0] = ureg_writemask(dst
[0], TGSI_WRITEMASK_XY
);
5945 ureg_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
);
5958 * Emit the TGSI instructions for inverting and adjusting WPOS.
5959 * This code is unavoidable because it also depends on whether
5960 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
5963 emit_wpos_adjustment(struct gl_context
*ctx
,
5964 struct st_translate
*t
,
5965 int wpos_transform_const
,
5967 GLfloat adjX
, GLfloat adjY
[2])
5969 struct ureg_program
*ureg
= t
->ureg
;
5971 assert(wpos_transform_const
>= 0);
5973 /* Fragment program uses fragment position input.
5974 * Need to replace instances of INPUT[WPOS] with temp T
5975 * where T = INPUT[WPOS] is inverted by Y.
5977 struct ureg_src wpostrans
= ureg_DECL_constant(ureg
, wpos_transform_const
);
5978 struct ureg_dst wpos_temp
= ureg_DECL_temporary( ureg
);
5979 struct ureg_src
*wpos
=
5980 ctx
->Const
.GLSLFragCoordIsSysVal
?
5981 &t
->systemValues
[SYSTEM_VALUE_FRAG_COORD
] :
5982 &t
->inputs
[t
->inputMapping
[VARYING_SLOT_POS
]];
5983 struct ureg_src wpos_input
= *wpos
;
5985 /* First, apply the coordinate shift: */
5986 if (adjX
|| adjY
[0] || adjY
[1]) {
5987 if (adjY
[0] != adjY
[1]) {
5988 /* Adjust the y coordinate by adjY[1] or adjY[0] respectively
5989 * depending on whether inversion is actually going to be applied
5990 * or not, which is determined by testing against the inversion
5991 * state variable used below, which will be either +1 or -1.
5993 struct ureg_dst adj_temp
= ureg_DECL_local_temporary(ureg
);
5995 ureg_CMP(ureg
, adj_temp
,
5996 ureg_scalar(wpostrans
, invert
? 2 : 0),
5997 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
),
5998 ureg_imm4f(ureg
, adjX
, adjY
[1], 0.0f
, 0.0f
));
5999 ureg_ADD(ureg
, wpos_temp
, wpos_input
, ureg_src(adj_temp
));
6001 ureg_ADD(ureg
, wpos_temp
, wpos_input
,
6002 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
));
6004 wpos_input
= ureg_src(wpos_temp
);
6006 /* MOV wpos_temp, input[wpos]
6008 ureg_MOV( ureg
, wpos_temp
, wpos_input
);
6011 /* Now the conditional y flip: STATE_FB_WPOS_Y_TRANSFORM.xy/zw will be
6012 * inversion/identity, or the other way around if we're drawing to an FBO.
6015 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
6018 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
6020 ureg_scalar(wpostrans
, 0),
6021 ureg_scalar(wpostrans
, 1));
6023 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
6026 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
6028 ureg_scalar(wpostrans
, 2),
6029 ureg_scalar(wpostrans
, 3));
6032 /* Use wpos_temp as position input from here on:
6034 *wpos
= ureg_src(wpos_temp
);
6039 * Emit fragment position/ooordinate code.
6042 emit_wpos(struct st_context
*st
,
6043 struct st_translate
*t
,
6044 const struct gl_program
*program
,
6045 struct ureg_program
*ureg
,
6046 int wpos_transform_const
)
6048 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
6049 GLfloat adjX
= 0.0f
;
6050 GLfloat adjY
[2] = { 0.0f
, 0.0f
};
6051 boolean invert
= FALSE
;
6053 /* Query the pixel center conventions supported by the pipe driver and set
6054 * adjX, adjY to help out if it cannot handle the requested one internally.
6056 * The bias of the y-coordinate depends on whether y-inversion takes place
6057 * (adjY[1]) or not (adjY[0]), which is in turn dependent on whether we are
6058 * drawing to an FBO (causes additional inversion), and whether the pipe
6059 * driver origin and the requested origin differ (the latter condition is
6060 * stored in the 'invert' variable).
6062 * For height = 100 (i = integer, h = half-integer, l = lower, u = upper):
6064 * center shift only:
6069 * l,i -> u,i: ( 0.0 + 1.0) * -1 + 100 = 99
6070 * l,h -> u,h: ( 0.5 + 0.0) * -1 + 100 = 99.5
6071 * u,i -> l,i: (99.0 + 1.0) * -1 + 100 = 0
6072 * u,h -> l,h: (99.5 + 0.0) * -1 + 100 = 0.5
6074 * inversion and center shift:
6075 * l,i -> u,h: ( 0.0 + 0.5) * -1 + 100 = 99.5
6076 * l,h -> u,i: ( 0.5 + 0.5) * -1 + 100 = 99
6077 * u,i -> l,h: (99.0 + 0.5) * -1 + 100 = 0.5
6078 * u,h -> l,i: (99.5 + 0.5) * -1 + 100 = 0
6080 if (program
->OriginUpperLeft
) {
6081 /* Fragment shader wants origin in upper-left */
6082 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
)) {
6083 /* the driver supports upper-left origin */
6085 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
)) {
6086 /* the driver supports lower-left origin, need to invert Y */
6087 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_ORIGIN
,
6088 TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
6095 /* Fragment shader wants origin in lower-left */
6096 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
))
6097 /* the driver supports lower-left origin */
6098 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_ORIGIN
,
6099 TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
6100 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
))
6101 /* the driver supports upper-left origin, need to invert Y */
6107 if (program
->PixelCenterInteger
) {
6108 /* Fragment shader wants pixel center integer */
6109 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
6110 /* the driver supports pixel center integer */
6112 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
,
6113 TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
6115 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
6116 /* the driver supports pixel center half integer, need to bias X,Y */
6125 /* Fragment shader wants pixel center half integer */
6126 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
6127 /* the driver supports pixel center half integer */
6129 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
6130 /* the driver supports pixel center integer, need to bias X,Y */
6131 adjX
= adjY
[0] = adjY
[1] = 0.5f
;
6132 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
,
6133 TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
6139 /* we invert after adjustment so that we avoid the MOV to temporary,
6140 * and reuse the adjustment ADD instead */
6141 emit_wpos_adjustment(st
->ctx
, t
, wpos_transform_const
, invert
, adjX
, adjY
);
6145 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
6146 * TGSI uses +1 for front, -1 for back.
6147 * This function converts the TGSI value to the GL value. Simply clamping/
6148 * saturating the value to [0,1] does the job.
6151 emit_face_var(struct gl_context
*ctx
, struct st_translate
*t
)
6153 struct ureg_program
*ureg
= t
->ureg
;
6154 struct ureg_dst face_temp
= ureg_DECL_temporary(ureg
);
6155 struct ureg_src face_input
= t
->inputs
[t
->inputMapping
[VARYING_SLOT_FACE
]];
6157 if (ctx
->Const
.NativeIntegers
) {
6158 ureg_FSGE(ureg
, face_temp
, face_input
, ureg_imm1f(ureg
, 0));
6161 /* MOV_SAT face_temp, input[face] */
6162 ureg_MOV(ureg
, ureg_saturate(face_temp
), face_input
);
6165 /* Use face_temp as face input from here on: */
6166 t
->inputs
[t
->inputMapping
[VARYING_SLOT_FACE
]] = ureg_src(face_temp
);
6170 emit_compute_block_size(const struct gl_program
*prog
,
6171 struct ureg_program
*ureg
) {
6172 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
,
6173 prog
->info
.cs
.local_size
[0]);
6174 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
,
6175 prog
->info
.cs
.local_size
[1]);
6176 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
,
6177 prog
->info
.cs
.local_size
[2]);
6180 struct sort_inout_decls
{
6181 bool operator()(const struct inout_decl
&a
, const struct inout_decl
&b
) const {
6182 return mapping
[a
.mesa_index
] < mapping
[b
.mesa_index
];
6185 const ubyte
*mapping
;
6188 /* Sort the given array of decls by the corresponding slot (TGSI file index).
6190 * This is for the benefit of older drivers which are broken when the
6191 * declarations aren't sorted in this way.
6194 sort_inout_decls_by_slot(struct inout_decl
*decls
,
6196 const ubyte mapping
[])
6198 sort_inout_decls sorter
;
6199 sorter
.mapping
= mapping
;
6200 std::sort(decls
, decls
+ count
, sorter
);
6204 st_translate_interp(enum glsl_interp_mode glsl_qual
, GLuint varying
)
6206 switch (glsl_qual
) {
6207 case INTERP_MODE_NONE
:
6208 if (varying
== VARYING_SLOT_COL0
|| varying
== VARYING_SLOT_COL1
)
6209 return TGSI_INTERPOLATE_COLOR
;
6210 return TGSI_INTERPOLATE_PERSPECTIVE
;
6211 case INTERP_MODE_SMOOTH
:
6212 return TGSI_INTERPOLATE_PERSPECTIVE
;
6213 case INTERP_MODE_FLAT
:
6214 return TGSI_INTERPOLATE_CONSTANT
;
6215 case INTERP_MODE_NOPERSPECTIVE
:
6216 return TGSI_INTERPOLATE_LINEAR
;
6218 assert(0 && "unexpected interp mode in st_translate_interp()");
6219 return TGSI_INTERPOLATE_PERSPECTIVE
;
6224 * Translate intermediate IR (glsl_to_tgsi_instruction) to TGSI format.
6225 * \param program the program to translate
6226 * \param numInputs number of input registers used
6227 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
6229 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
6230 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
6232 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
6233 * \param numOutputs number of output registers used
6234 * \param outputMapping maps Mesa fragment program outputs to TGSI
6236 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
6237 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
6240 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
6242 extern "C" enum pipe_error
6243 st_translate_program(
6244 struct gl_context
*ctx
,
6246 struct ureg_program
*ureg
,
6247 glsl_to_tgsi_visitor
*program
,
6248 const struct gl_program
*proginfo
,
6250 const ubyte inputMapping
[],
6251 const ubyte inputSlotToAttr
[],
6252 const ubyte inputSemanticName
[],
6253 const ubyte inputSemanticIndex
[],
6254 const ubyte interpMode
[],
6256 const ubyte outputMapping
[],
6257 const ubyte outputSemanticName
[],
6258 const ubyte outputSemanticIndex
[])
6260 struct st_translate
*t
;
6262 struct gl_program_constants
*frag_const
=
6263 &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
6264 enum pipe_error ret
= PIPE_OK
;
6266 assert(numInputs
<= ARRAY_SIZE(t
->inputs
));
6267 assert(numOutputs
<= ARRAY_SIZE(t
->outputs
));
6269 t
= CALLOC_STRUCT(st_translate
);
6271 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
6275 t
->procType
= procType
;
6276 t
->inputMapping
= inputMapping
;
6277 t
->outputMapping
= outputMapping
;
6279 t
->num_temp_arrays
= program
->next_array
;
6280 if (t
->num_temp_arrays
)
6281 t
->arrays
= (struct ureg_dst
*)
6282 calloc(t
->num_temp_arrays
, sizeof(t
->arrays
[0]));
6285 * Declare input attributes.
6288 case PIPE_SHADER_FRAGMENT
:
6289 case PIPE_SHADER_GEOMETRY
:
6290 case PIPE_SHADER_TESS_EVAL
:
6291 case PIPE_SHADER_TESS_CTRL
:
6292 sort_inout_decls_by_slot(program
->inputs
, program
->num_inputs
, inputMapping
);
6294 for (i
= 0; i
< program
->num_inputs
; ++i
) {
6295 struct inout_decl
*decl
= &program
->inputs
[i
];
6296 unsigned slot
= inputMapping
[decl
->mesa_index
];
6297 struct ureg_src src
;
6298 ubyte tgsi_usage_mask
= decl
->usage_mask
;
6300 if (glsl_base_type_is_64bit(decl
->base_type
)) {
6301 if (tgsi_usage_mask
== 1)
6302 tgsi_usage_mask
= TGSI_WRITEMASK_XY
;
6303 else if (tgsi_usage_mask
== 2)
6304 tgsi_usage_mask
= TGSI_WRITEMASK_ZW
;
6306 tgsi_usage_mask
= TGSI_WRITEMASK_XYZW
;
6309 unsigned interp_mode
= 0;
6310 unsigned interp_location
= 0;
6311 if (procType
== PIPE_SHADER_FRAGMENT
) {
6313 interp_mode
= interpMode
[slot
] != TGSI_INTERPOLATE_COUNT
?
6315 st_translate_interp(decl
->interp
, inputSlotToAttr
[slot
]);
6317 interp_location
= decl
->interp_loc
;
6320 src
= ureg_DECL_fs_input_cyl_centroid_layout(ureg
,
6321 inputSemanticName
[slot
], inputSemanticIndex
[slot
],
6322 interp_mode
, 0, interp_location
, slot
, tgsi_usage_mask
,
6323 decl
->array_id
, decl
->size
);
6325 for (unsigned j
= 0; j
< decl
->size
; ++j
) {
6326 if (t
->inputs
[slot
+ j
].File
!= TGSI_FILE_INPUT
) {
6327 /* The ArrayID is set up in dst_register */
6328 t
->inputs
[slot
+ j
] = src
;
6329 t
->inputs
[slot
+ j
].ArrayID
= 0;
6330 t
->inputs
[slot
+ j
].Index
+= j
;
6335 case PIPE_SHADER_VERTEX
:
6336 for (i
= 0; i
< numInputs
; i
++) {
6337 t
->inputs
[i
] = ureg_DECL_vs_input(ureg
, i
);
6340 case PIPE_SHADER_COMPUTE
:
6347 * Declare output attributes.
6350 case PIPE_SHADER_FRAGMENT
:
6351 case PIPE_SHADER_COMPUTE
:
6353 case PIPE_SHADER_GEOMETRY
:
6354 case PIPE_SHADER_TESS_EVAL
:
6355 case PIPE_SHADER_TESS_CTRL
:
6356 case PIPE_SHADER_VERTEX
:
6357 sort_inout_decls_by_slot(program
->outputs
, program
->num_outputs
, outputMapping
);
6359 for (i
= 0; i
< program
->num_outputs
; ++i
) {
6360 struct inout_decl
*decl
= &program
->outputs
[i
];
6361 unsigned slot
= outputMapping
[decl
->mesa_index
];
6362 struct ureg_dst dst
;
6363 ubyte tgsi_usage_mask
= decl
->usage_mask
;
6365 if (glsl_base_type_is_64bit(decl
->base_type
)) {
6366 if (tgsi_usage_mask
== 1)
6367 tgsi_usage_mask
= TGSI_WRITEMASK_XY
;
6368 else if (tgsi_usage_mask
== 2)
6369 tgsi_usage_mask
= TGSI_WRITEMASK_ZW
;
6371 tgsi_usage_mask
= TGSI_WRITEMASK_XYZW
;
6374 dst
= ureg_DECL_output_layout(ureg
,
6375 outputSemanticName
[slot
], outputSemanticIndex
[slot
],
6376 decl
->gs_out_streams
,
6377 slot
, tgsi_usage_mask
, decl
->array_id
, decl
->size
);
6379 for (unsigned j
= 0; j
< decl
->size
; ++j
) {
6380 if (t
->outputs
[slot
+ j
].File
!= TGSI_FILE_OUTPUT
) {
6381 /* The ArrayID is set up in dst_register */
6382 t
->outputs
[slot
+ j
] = dst
;
6383 t
->outputs
[slot
+ j
].ArrayID
= 0;
6384 t
->outputs
[slot
+ j
].Index
+= j
;
6393 if (procType
== PIPE_SHADER_FRAGMENT
) {
6394 if (program
->shader
->Program
->info
.fs
.early_fragment_tests
||
6395 program
->shader
->Program
->info
.fs
.post_depth_coverage
) {
6396 ureg_property(ureg
, TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL
, 1);
6398 if (program
->shader
->Program
->info
.fs
.post_depth_coverage
)
6399 ureg_property(ureg
, TGSI_PROPERTY_FS_POST_DEPTH_COVERAGE
, 1);
6402 if (proginfo
->info
.inputs_read
& VARYING_BIT_POS
) {
6403 /* Must do this after setting up t->inputs. */
6404 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
,
6405 program
->wpos_transform_const
);
6408 if (proginfo
->info
.inputs_read
& VARYING_BIT_FACE
)
6409 emit_face_var(ctx
, t
);
6411 for (i
= 0; i
< numOutputs
; i
++) {
6412 switch (outputSemanticName
[i
]) {
6413 case TGSI_SEMANTIC_POSITION
:
6414 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6415 TGSI_SEMANTIC_POSITION
, /* Z/Depth */
6416 outputSemanticIndex
[i
]);
6417 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Z
);
6419 case TGSI_SEMANTIC_STENCIL
:
6420 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6421 TGSI_SEMANTIC_STENCIL
, /* Stencil */
6422 outputSemanticIndex
[i
]);
6423 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Y
);
6425 case TGSI_SEMANTIC_COLOR
:
6426 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6427 TGSI_SEMANTIC_COLOR
,
6428 outputSemanticIndex
[i
]);
6430 case TGSI_SEMANTIC_SAMPLEMASK
:
6431 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6432 TGSI_SEMANTIC_SAMPLEMASK
,
6433 outputSemanticIndex
[i
]);
6434 /* TODO: If we ever support more than 32 samples, this will have
6435 * to become an array.
6437 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_X
);
6440 assert(!"fragment shader outputs must be POSITION/STENCIL/COLOR");
6441 ret
= PIPE_ERROR_BAD_INPUT
;
6446 else if (procType
== PIPE_SHADER_VERTEX
) {
6447 for (i
= 0; i
< numOutputs
; i
++) {
6448 if (outputSemanticName
[i
] == TGSI_SEMANTIC_FOG
) {
6449 /* force register to contain a fog coordinate in the form (F, 0, 0, 1). */
6451 ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_YZW
),
6452 ureg_imm4f(ureg
, 0.0f
, 0.0f
, 0.0f
, 1.0f
));
6453 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_X
);
6458 if (procType
== PIPE_SHADER_COMPUTE
) {
6459 emit_compute_block_size(proginfo
, ureg
);
6462 /* Declare address register.
6464 if (program
->num_address_regs
> 0) {
6465 assert(program
->num_address_regs
<= 3);
6466 for (int i
= 0; i
< program
->num_address_regs
; i
++)
6467 t
->address
[i
] = ureg_DECL_address(ureg
);
6470 /* Declare misc input registers
6473 GLbitfield sysInputs
= proginfo
->info
.system_values_read
;
6475 for (i
= 0; sysInputs
; i
++) {
6476 if (sysInputs
& (1 << i
)) {
6477 unsigned semName
= _mesa_sysval_to_semantic(i
);
6479 t
->systemValues
[i
] = ureg_DECL_system_value(ureg
, semName
, 0);
6481 if (semName
== TGSI_SEMANTIC_INSTANCEID
||
6482 semName
== TGSI_SEMANTIC_VERTEXID
) {
6483 /* From Gallium perspective, these system values are always
6484 * integer, and require native integer support. However, if
6485 * native integer is supported on the vertex stage but not the
6486 * pixel stage (e.g, i915g + draw), Mesa will generate IR that
6487 * assumes these system values are floats. To resolve the
6488 * inconsistency, we insert a U2F.
6490 struct st_context
*st
= st_context(ctx
);
6491 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
6492 assert(procType
== PIPE_SHADER_VERTEX
);
6493 assert(pscreen
->get_shader_param(pscreen
, PIPE_SHADER_VERTEX
, PIPE_SHADER_CAP_INTEGERS
));
6495 if (!ctx
->Const
.NativeIntegers
) {
6496 struct ureg_dst temp
= ureg_DECL_local_temporary(t
->ureg
);
6497 ureg_U2F( t
->ureg
, ureg_writemask(temp
, TGSI_WRITEMASK_X
), t
->systemValues
[i
]);
6498 t
->systemValues
[i
] = ureg_scalar(ureg_src(temp
), 0);
6502 if (procType
== PIPE_SHADER_FRAGMENT
&&
6503 semName
== TGSI_SEMANTIC_POSITION
)
6504 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
,
6505 program
->wpos_transform_const
);
6507 sysInputs
&= ~(1 << i
);
6512 t
->array_sizes
= program
->array_sizes
;
6513 t
->input_decls
= program
->inputs
;
6514 t
->num_input_decls
= program
->num_inputs
;
6515 t
->output_decls
= program
->outputs
;
6516 t
->num_output_decls
= program
->num_outputs
;
6518 /* Emit constants and uniforms. TGSI uses a single index space for these,
6519 * so we put all the translated regs in t->constants.
6521 if (proginfo
->Parameters
) {
6522 t
->constants
= (struct ureg_src
*)
6523 calloc(proginfo
->Parameters
->NumParameters
, sizeof(t
->constants
[0]));
6524 if (t
->constants
== NULL
) {
6525 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
6528 t
->num_constants
= proginfo
->Parameters
->NumParameters
;
6530 for (i
= 0; i
< proginfo
->Parameters
->NumParameters
; i
++) {
6531 switch (proginfo
->Parameters
->Parameters
[i
].Type
) {
6532 case PROGRAM_STATE_VAR
:
6533 case PROGRAM_UNIFORM
:
6534 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
6537 /* Emit immediates for PROGRAM_CONSTANT only when there's no indirect
6538 * addressing of the const buffer.
6539 * FIXME: Be smarter and recognize param arrays:
6540 * indirect addressing is only valid within the referenced
6543 case PROGRAM_CONSTANT
:
6544 if (program
->indirect_addr_consts
)
6545 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
6547 t
->constants
[i
] = emit_immediate(t
,
6548 proginfo
->Parameters
->ParameterValues
[i
],
6549 proginfo
->Parameters
->Parameters
[i
].DataType
,
6558 for (i
= 0; i
< proginfo
->info
.num_ubos
; i
++) {
6559 unsigned size
= proginfo
->sh
.UniformBlocks
[i
]->UniformBufferSize
;
6560 unsigned num_const_vecs
= (size
+ 15) / 16;
6561 unsigned first
, last
;
6562 assert(num_const_vecs
> 0);
6564 last
= num_const_vecs
> 0 ? num_const_vecs
- 1 : 0;
6565 ureg_DECL_constant2D(t
->ureg
, first
, last
, i
+ 1);
6568 /* Emit immediate values.
6570 t
->immediates
= (struct ureg_src
*)
6571 calloc(program
->num_immediates
, sizeof(struct ureg_src
));
6572 if (t
->immediates
== NULL
) {
6573 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
6576 t
->num_immediates
= program
->num_immediates
;
6579 foreach_in_list(immediate_storage
, imm
, &program
->immediates
) {
6580 assert(i
< program
->num_immediates
);
6581 t
->immediates
[i
++] = emit_immediate(t
, imm
->values
, imm
->type
, imm
->size32
);
6583 assert(i
== program
->num_immediates
);
6585 /* texture samplers */
6586 for (i
= 0; i
< frag_const
->MaxTextureImageUnits
; i
++) {
6587 if (program
->samplers_used
& (1u << i
)) {
6588 unsigned type
= st_translate_texture_type(program
->sampler_types
[i
]);
6590 t
->samplers
[i
] = ureg_DECL_sampler(ureg
, i
);
6592 ureg_DECL_sampler_view( ureg
, i
, program
->sampler_targets
[i
],
6593 type
, type
, type
, type
);
6597 /* Declare atomic and shader storage buffers. */
6599 struct gl_program
*prog
= program
->prog
;
6601 for (i
= 0; i
< prog
->info
.num_abos
; i
++) {
6602 unsigned index
= prog
->sh
.AtomicBuffers
[i
]->Binding
;
6603 assert(index
< frag_const
->MaxAtomicBuffers
);
6604 t
->buffers
[index
] = ureg_DECL_buffer(ureg
, index
, true);
6607 assert(prog
->info
.num_ssbos
<= frag_const
->MaxShaderStorageBlocks
);
6608 for (i
= 0; i
< prog
->info
.num_ssbos
; i
++) {
6609 unsigned index
= frag_const
->MaxAtomicBuffers
+ i
;
6610 t
->buffers
[index
] = ureg_DECL_buffer(ureg
, index
, false);
6614 if (program
->use_shared_memory
)
6615 t
->shared_memory
= ureg_DECL_memory(ureg
, TGSI_MEMORY_TYPE_SHARED
);
6617 for (i
= 0; i
< program
->shader
->Program
->info
.num_images
; i
++) {
6618 if (program
->images_used
& (1 << i
)) {
6619 t
->images
[i
] = ureg_DECL_image(ureg
, i
,
6620 program
->image_targets
[i
],
6621 program
->image_formats
[i
],
6626 /* Emit each instruction in turn:
6628 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &program
->instructions
)
6629 compile_tgsi_instruction(t
, inst
);
6631 /* Set the next shader stage hint for VS and TES. */
6633 case PIPE_SHADER_VERTEX
:
6634 case PIPE_SHADER_TESS_EVAL
:
6635 if (program
->shader_program
->SeparateShader
)
6638 for (i
= program
->shader
->Stage
+1; i
<= MESA_SHADER_FRAGMENT
; i
++) {
6639 if (program
->shader_program
->_LinkedShaders
[i
]) {
6643 case MESA_SHADER_TESS_CTRL
:
6644 next
= PIPE_SHADER_TESS_CTRL
;
6646 case MESA_SHADER_TESS_EVAL
:
6647 next
= PIPE_SHADER_TESS_EVAL
;
6649 case MESA_SHADER_GEOMETRY
:
6650 next
= PIPE_SHADER_GEOMETRY
;
6652 case MESA_SHADER_FRAGMENT
:
6653 next
= PIPE_SHADER_FRAGMENT
;
6660 ureg_set_next_shader_processor(ureg
, next
);
6672 t
->num_constants
= 0;
6673 free(t
->immediates
);
6674 t
->num_immediates
= 0;
6680 /* ----------------------------- End TGSI code ------------------------------ */
6684 * Convert a shader's GLSL IR into a Mesa gl_program, although without
6685 * generating Mesa IR.
6687 static struct gl_program
*
6688 get_mesa_program_tgsi(struct gl_context
*ctx
,
6689 struct gl_shader_program
*shader_program
,
6690 struct gl_linked_shader
*shader
)
6692 glsl_to_tgsi_visitor
* v
;
6693 struct gl_program
*prog
;
6694 struct gl_shader_compiler_options
*options
=
6695 &ctx
->Const
.ShaderCompilerOptions
[shader
->Stage
];
6696 struct pipe_screen
*pscreen
= ctx
->st
->pipe
->screen
;
6697 enum pipe_shader_type ptarget
= st_shader_stage_to_ptarget(shader
->Stage
);
6698 unsigned skip_merge_registers
;
6700 validate_ir_tree(shader
->ir
);
6702 prog
= shader
->Program
;
6704 prog
->Parameters
= _mesa_new_parameter_list();
6705 v
= new glsl_to_tgsi_visitor();
6708 v
->shader_program
= shader_program
;
6710 v
->options
= options
;
6711 v
->glsl_version
= ctx
->Const
.GLSLVersion
;
6712 v
->native_integers
= ctx
->Const
.NativeIntegers
;
6714 v
->have_sqrt
= pscreen
->get_shader_param(pscreen
, ptarget
,
6715 PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED
);
6716 v
->have_fma
= pscreen
->get_shader_param(pscreen
, ptarget
,
6717 PIPE_SHADER_CAP_TGSI_FMA_SUPPORTED
);
6718 v
->has_tex_txf_lz
= pscreen
->get_param(pscreen
,
6719 PIPE_CAP_TGSI_TEX_TXF_LZ
);
6721 v
->variables
= _mesa_hash_table_create(v
->mem_ctx
, _mesa_hash_pointer
,
6722 _mesa_key_pointer_equal
);
6723 skip_merge_registers
=
6724 pscreen
->get_shader_param(pscreen
, ptarget
,
6725 PIPE_SHADER_CAP_TGSI_SKIP_MERGE_REGISTERS
);
6727 _mesa_generate_parameters_list_for_uniforms(shader_program
, shader
,
6730 /* Remove reads from output registers. */
6731 if (!pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_CAN_READ_OUTPUTS
))
6732 lower_output_reads(shader
->Stage
, shader
->ir
);
6734 /* Emit intermediate IR for main(). */
6735 visit_exec_list(shader
->ir
, v
);
6738 /* Print out some information (for debugging purposes) used by the
6739 * optimization passes. */
6742 int *first_writes
= ralloc_array(v
->mem_ctx
, int, v
->next_temp
);
6743 int *first_reads
= ralloc_array(v
->mem_ctx
, int, v
->next_temp
);
6744 int *last_writes
= ralloc_array(v
->mem_ctx
, int, v
->next_temp
);
6745 int *last_reads
= ralloc_array(v
->mem_ctx
, int, v
->next_temp
);
6747 for (i
= 0; i
< v
->next_temp
; i
++) {
6748 first_writes
[i
] = -1;
6749 first_reads
[i
] = -1;
6750 last_writes
[i
] = -1;
6753 v
->get_first_temp_read(first_reads
);
6754 v
->get_last_temp_read_first_temp_write(last_reads
, first_writes
);
6755 v
->get_last_temp_write(last_writes
);
6756 for (i
= 0; i
< v
->next_temp
; i
++)
6757 printf("Temp %d: FR=%3d FW=%3d LR=%3d LW=%3d\n", i
, first_reads
[i
],
6761 ralloc_free(first_writes
);
6762 ralloc_free(first_reads
);
6763 ralloc_free(last_writes
);
6764 ralloc_free(last_reads
);
6768 /* Perform optimizations on the instructions in the glsl_to_tgsi_visitor. */
6771 if (shader
->Stage
!= MESA_SHADER_TESS_CTRL
&&
6772 shader
->Stage
!= MESA_SHADER_TESS_EVAL
)
6773 v
->copy_propagate();
6775 while (v
->eliminate_dead_code());
6777 v
->merge_two_dsts();
6778 if (!skip_merge_registers
)
6779 v
->merge_registers();
6780 v
->renumber_registers();
6782 /* Write the END instruction. */
6783 v
->emit_asm(NULL
, TGSI_OPCODE_END
);
6785 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
6787 _mesa_log("GLSL IR for linked %s program %d:\n",
6788 _mesa_shader_stage_to_string(shader
->Stage
),
6789 shader_program
->Name
);
6790 _mesa_print_ir(_mesa_get_log_file(), shader
->ir
, NULL
);
6794 do_set_program_inouts(shader
->ir
, prog
, shader
->Stage
);
6795 _mesa_copy_linked_program_data(shader_program
, shader
);
6796 shrink_array_declarations(v
->inputs
, v
->num_inputs
,
6797 &prog
->info
.inputs_read
,
6798 prog
->info
.double_inputs_read
,
6799 &prog
->info
.patch_inputs_read
);
6800 shrink_array_declarations(v
->outputs
, v
->num_outputs
,
6801 &prog
->info
.outputs_written
, 0ULL,
6802 &prog
->info
.patch_outputs_written
);
6803 count_resources(v
, prog
);
6805 /* The GLSL IR won't be needed anymore. */
6806 ralloc_free(shader
->ir
);
6809 /* This must be done before the uniform storage is associated. */
6810 if (shader
->Stage
== MESA_SHADER_FRAGMENT
&&
6811 (prog
->info
.inputs_read
& VARYING_BIT_POS
||
6812 prog
->info
.system_values_read
& (1 << SYSTEM_VALUE_FRAG_COORD
))) {
6813 static const gl_state_index wposTransformState
[STATE_LENGTH
] = {
6814 STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
6817 v
->wpos_transform_const
= _mesa_add_state_reference(prog
->Parameters
,
6818 wposTransformState
);
6821 /* Avoid reallocation of the program parameter list, because the uniform
6822 * storage is only associated with the original parameter list.
6823 * This should be enough for Bitmap and DrawPixels constants.
6825 _mesa_reserve_parameter_storage(prog
->Parameters
, 8);
6827 /* This has to be done last. Any operation the can cause
6828 * prog->ParameterValues to get reallocated (e.g., anything that adds a
6829 * program constant) has to happen before creating this linkage.
6831 _mesa_associate_uniform_storage(ctx
, shader_program
, prog
->Parameters
,
6833 if (!shader_program
->data
->LinkStatus
) {
6834 free_glsl_to_tgsi_visitor(v
);
6835 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
6839 struct st_vertex_program
*stvp
;
6840 struct st_fragment_program
*stfp
;
6841 struct st_common_program
*stp
;
6842 struct st_compute_program
*stcp
;
6844 switch (shader
->Stage
) {
6845 case MESA_SHADER_VERTEX
:
6846 stvp
= (struct st_vertex_program
*)prog
;
6847 stvp
->glsl_to_tgsi
= v
;
6849 case MESA_SHADER_FRAGMENT
:
6850 stfp
= (struct st_fragment_program
*)prog
;
6851 stfp
->glsl_to_tgsi
= v
;
6853 case MESA_SHADER_TESS_CTRL
:
6854 case MESA_SHADER_TESS_EVAL
:
6855 case MESA_SHADER_GEOMETRY
:
6856 stp
= st_common_program(prog
);
6857 stp
->glsl_to_tgsi
= v
;
6859 case MESA_SHADER_COMPUTE
:
6860 stcp
= (struct st_compute_program
*)prog
;
6861 stcp
->glsl_to_tgsi
= v
;
6864 assert(!"should not be reached");
6871 /* See if there are unsupported control flow statements. */
6872 class ir_control_flow_info_visitor
: public ir_hierarchical_visitor
{
6874 const struct gl_shader_compiler_options
*options
;
6876 ir_control_flow_info_visitor(const struct gl_shader_compiler_options
*options
)
6882 virtual ir_visitor_status
visit_enter(ir_function
*ir
)
6884 /* Other functions are skipped (same as glsl_to_tgsi). */
6885 if (strcmp(ir
->name
, "main") == 0)
6886 return visit_continue
;
6888 return visit_continue_with_parent
;
6891 virtual ir_visitor_status
visit_enter(ir_call
*ir
)
6893 if (!ir
->callee
->is_intrinsic()) {
6894 unsupported
= true; /* it's a function call */
6897 return visit_continue
;
6900 virtual ir_visitor_status
visit_enter(ir_return
*ir
)
6902 if (options
->EmitNoMainReturn
) {
6906 return visit_continue
;
6913 has_unsupported_control_flow(exec_list
*ir
,
6914 const struct gl_shader_compiler_options
*options
)
6916 ir_control_flow_info_visitor
visitor(options
);
6917 visit_list_elements(&visitor
, ir
);
6918 return visitor
.unsupported
;
6925 * Called via ctx->Driver.LinkShader()
6926 * This actually involves converting GLSL IR into an intermediate TGSI-like IR
6927 * with code lowering and other optimizations.
6930 st_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
6932 /* Return early if we are loading the shader from on-disk cache */
6933 if (st_load_tgsi_from_disk_cache(ctx
, prog
)) {
6937 struct pipe_screen
*pscreen
= ctx
->st
->pipe
->screen
;
6938 assert(prog
->data
->LinkStatus
);
6940 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
6941 if (prog
->_LinkedShaders
[i
] == NULL
)
6944 struct gl_linked_shader
*shader
= prog
->_LinkedShaders
[i
];
6945 exec_list
*ir
= shader
->ir
;
6946 gl_shader_stage stage
= shader
->Stage
;
6947 const struct gl_shader_compiler_options
*options
=
6948 &ctx
->Const
.ShaderCompilerOptions
[stage
];
6949 enum pipe_shader_type ptarget
= st_shader_stage_to_ptarget(stage
);
6950 bool have_dround
= pscreen
->get_shader_param(pscreen
, ptarget
,
6951 PIPE_SHADER_CAP_TGSI_DROUND_SUPPORTED
);
6952 bool have_dfrexp
= pscreen
->get_shader_param(pscreen
, ptarget
,
6953 PIPE_SHADER_CAP_TGSI_DFRACEXP_DLDEXP_SUPPORTED
);
6954 unsigned if_threshold
= pscreen
->get_shader_param(pscreen
, ptarget
,
6955 PIPE_SHADER_CAP_LOWER_IF_THRESHOLD
);
6957 /* If there are forms of indirect addressing that the driver
6958 * cannot handle, perform the lowering pass.
6960 if (options
->EmitNoIndirectInput
|| options
->EmitNoIndirectOutput
||
6961 options
->EmitNoIndirectTemp
|| options
->EmitNoIndirectUniform
) {
6962 lower_variable_index_to_cond_assign(stage
, ir
,
6963 options
->EmitNoIndirectInput
,
6964 options
->EmitNoIndirectOutput
,
6965 options
->EmitNoIndirectTemp
,
6966 options
->EmitNoIndirectUniform
);
6969 if (!pscreen
->get_param(pscreen
, PIPE_CAP_INT64_DIVMOD
))
6970 lower_64bit_integer_instructions(ir
, DIV64
| MOD64
);
6972 if (ctx
->Extensions
.ARB_shading_language_packing
) {
6973 unsigned lower_inst
= LOWER_PACK_SNORM_2x16
|
6974 LOWER_UNPACK_SNORM_2x16
|
6975 LOWER_PACK_UNORM_2x16
|
6976 LOWER_UNPACK_UNORM_2x16
|
6977 LOWER_PACK_SNORM_4x8
|
6978 LOWER_UNPACK_SNORM_4x8
|
6979 LOWER_UNPACK_UNORM_4x8
|
6980 LOWER_PACK_UNORM_4x8
;
6982 if (ctx
->Extensions
.ARB_gpu_shader5
)
6983 lower_inst
|= LOWER_PACK_USE_BFI
|
6985 if (!ctx
->st
->has_half_float_packing
)
6986 lower_inst
|= LOWER_PACK_HALF_2x16
|
6987 LOWER_UNPACK_HALF_2x16
;
6989 lower_packing_builtins(ir
, lower_inst
);
6992 if (!pscreen
->get_param(pscreen
, PIPE_CAP_TEXTURE_GATHER_OFFSETS
))
6993 lower_offset_arrays(ir
);
6994 do_mat_op_to_vec(ir
);
6996 if (stage
== MESA_SHADER_FRAGMENT
)
6997 lower_blend_equation_advanced(shader
);
6999 lower_instructions(ir
,
7005 (have_dfrexp
? 0 : DFREXP_DLDEXP_TO_ARITH
) |
7008 (have_dround
? 0 : DOPS_TO_DFRAC
) |
7009 (options
->EmitNoPow
? POW_TO_EXP2
: 0) |
7010 (!ctx
->Const
.NativeIntegers
? INT_DIV_TO_MUL_RCP
: 0) |
7011 (options
->EmitNoSat
? SAT_TO_CLAMP
: 0) |
7012 (ctx
->Const
.ForceGLSLAbsSqrt
? SQRT_TO_ABS_SQRT
: 0) |
7013 /* Assume that if ARB_gpu_shader5 is not supported
7014 * then all of the extended integer functions need
7015 * lowering. It may be necessary to add some caps
7016 * for individual instructions.
7018 (!ctx
->Extensions
.ARB_gpu_shader5
7019 ? BIT_COUNT_TO_MATH
|
7023 FIND_LSB_TO_FLOAT_CAST
|
7024 FIND_MSB_TO_FLOAT_CAST
|
7028 do_vec_index_to_cond_assign(ir
);
7029 lower_vector_insert(ir
, true);
7030 lower_quadop_vector(ir
, false);
7032 if (options
->MaxIfDepth
== 0) {
7036 if (ctx
->Const
.GLSLOptimizeConservatively
) {
7037 /* Do it once and repeat only if there's unsupported control flow. */
7039 do_common_optimization(ir
, true, true, options
,
7040 ctx
->Const
.NativeIntegers
);
7041 lower_if_to_cond_assign((gl_shader_stage
)i
, ir
,
7042 options
->MaxIfDepth
, if_threshold
);
7043 } while (has_unsupported_control_flow(ir
, options
));
7045 /* Repeat it until it stops making changes. */
7048 progress
= do_common_optimization(ir
, true, true, options
,
7049 ctx
->Const
.NativeIntegers
);
7050 progress
|= lower_if_to_cond_assign((gl_shader_stage
)i
, ir
,
7051 options
->MaxIfDepth
, if_threshold
);
7055 validate_ir_tree(ir
);
7058 build_program_resource_list(ctx
, prog
);
7060 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
7061 struct gl_linked_shader
*shader
= prog
->_LinkedShaders
[i
];
7065 enum pipe_shader_type ptarget
=
7066 st_shader_stage_to_ptarget(shader
->Stage
);
7067 enum pipe_shader_ir preferred_ir
= (enum pipe_shader_ir
)
7068 pscreen
->get_shader_param(pscreen
, ptarget
,
7069 PIPE_SHADER_CAP_PREFERRED_IR
);
7071 struct gl_program
*linked_prog
= NULL
;
7072 if (preferred_ir
== PIPE_SHADER_IR_NIR
) {
7073 /* TODO only for GLSL VS/FS/CS for now: */
7074 switch (shader
->Stage
) {
7075 case MESA_SHADER_VERTEX
:
7076 case MESA_SHADER_FRAGMENT
:
7077 case MESA_SHADER_COMPUTE
:
7078 linked_prog
= st_nir_get_mesa_program(ctx
, prog
, shader
);
7083 linked_prog
= get_mesa_program_tgsi(ctx
, prog
, shader
);
7087 st_set_prog_affected_state_flags(linked_prog
);
7088 if (!ctx
->Driver
.ProgramStringNotify(ctx
,
7089 _mesa_shader_stage_to_program(i
),
7091 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
7101 st_translate_stream_output_info(glsl_to_tgsi_visitor
*glsl_to_tgsi
,
7102 const ubyte outputMapping
[],
7103 struct pipe_stream_output_info
*so
)
7105 if (!glsl_to_tgsi
->shader_program
->last_vert_prog
)
7108 struct gl_transform_feedback_info
*info
=
7109 glsl_to_tgsi
->shader_program
->last_vert_prog
->sh
.LinkedTransformFeedback
;
7110 st_translate_stream_output_info2(info
, outputMapping
, so
);
7114 st_translate_stream_output_info2(struct gl_transform_feedback_info
*info
,
7115 const ubyte outputMapping
[],
7116 struct pipe_stream_output_info
*so
)
7120 for (i
= 0; i
< info
->NumOutputs
; i
++) {
7121 so
->output
[i
].register_index
=
7122 outputMapping
[info
->Outputs
[i
].OutputRegister
];
7123 so
->output
[i
].start_component
= info
->Outputs
[i
].ComponentOffset
;
7124 so
->output
[i
].num_components
= info
->Outputs
[i
].NumComponents
;
7125 so
->output
[i
].output_buffer
= info
->Outputs
[i
].OutputBuffer
;
7126 so
->output
[i
].dst_offset
= info
->Outputs
[i
].DstOffset
;
7127 so
->output
[i
].stream
= info
->Outputs
[i
].StreamId
;
7130 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
7131 so
->stride
[i
] = info
->Buffers
[i
].Stride
;
7133 so
->num_outputs
= info
->NumOutputs
;