2 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
3 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
4 * Copyright © 2010 Intel Corporation
5 * Copyright © 2011 Bryan Cain
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
28 * \file glsl_to_tgsi.cpp
30 * Translate GLSL IR to TGSI.
33 #include "st_glsl_to_tgsi.h"
35 #include "compiler/glsl/glsl_parser_extras.h"
36 #include "compiler/glsl/ir_optimization.h"
37 #include "compiler/glsl/program.h"
39 #include "main/errors.h"
40 #include "main/shaderobj.h"
41 #include "main/uniforms.h"
42 #include "main/shaderapi.h"
43 #include "main/shaderimage.h"
44 #include "program/prog_instruction.h"
46 #include "pipe/p_context.h"
47 #include "pipe/p_screen.h"
48 #include "tgsi/tgsi_ureg.h"
49 #include "tgsi/tgsi_info.h"
50 #include "util/u_math.h"
51 #include "util/u_memory.h"
52 #include "st_program.h"
53 #include "st_mesa_to_tgsi.h"
54 #include "st_format.h"
55 #include "st_glsl_types.h"
57 #include "st_shader_cache.h"
61 #define PROGRAM_ANY_CONST ((1 << PROGRAM_STATE_VAR) | \
62 (1 << PROGRAM_CONSTANT) | \
63 (1 << PROGRAM_UNIFORM))
65 #define MAX_GLSL_TEXTURE_OFFSET 4
70 static int swizzle_for_size(int size
);
72 static int swizzle_for_type(const glsl_type
*type
, int component
= 0)
74 unsigned num_elements
= 4;
77 type
= type
->without_array();
78 if (type
->is_scalar() || type
->is_vector() || type
->is_matrix())
79 num_elements
= type
->vector_elements
;
82 int swizzle
= swizzle_for_size(num_elements
);
83 assert(num_elements
+ component
<= 4);
85 swizzle
+= component
* MAKE_SWIZZLE4(1, 1, 1, 1);
90 * This struct is a corresponding struct to TGSI ureg_src.
94 st_src_reg(gl_register_file file
, int index
, const glsl_type
*type
,
95 int component
= 0, unsigned array_id
= 0)
97 assert(file
!= PROGRAM_ARRAY
|| array_id
!= 0);
100 this->swizzle
= swizzle_for_type(type
, component
);
104 this->type
= type
? type
->base_type
: GLSL_TYPE_ERROR
;
105 this->reladdr
= NULL
;
106 this->reladdr2
= NULL
;
107 this->has_index2
= false;
108 this->double_reg2
= false;
109 this->array_id
= array_id
;
110 this->is_double_vertex_input
= false;
113 st_src_reg(gl_register_file file
, int index
, enum glsl_base_type type
)
115 assert(file
!= PROGRAM_ARRAY
); /* need array_id > 0 */
120 this->swizzle
= SWIZZLE_XYZW
;
123 this->reladdr
= NULL
;
124 this->reladdr2
= NULL
;
125 this->has_index2
= false;
126 this->double_reg2
= false;
128 this->is_double_vertex_input
= false;
131 st_src_reg(gl_register_file file
, int index
, enum glsl_base_type type
, int index2D
)
133 assert(file
!= PROGRAM_ARRAY
); /* need array_id > 0 */
137 this->index2D
= index2D
;
138 this->swizzle
= SWIZZLE_XYZW
;
141 this->reladdr
= NULL
;
142 this->reladdr2
= NULL
;
143 this->has_index2
= false;
144 this->double_reg2
= false;
146 this->is_double_vertex_input
= false;
151 this->type
= GLSL_TYPE_ERROR
;
152 this->file
= PROGRAM_UNDEFINED
;
158 this->reladdr
= NULL
;
159 this->reladdr2
= NULL
;
160 this->has_index2
= false;
161 this->double_reg2
= false;
163 this->is_double_vertex_input
= false;
166 explicit st_src_reg(st_dst_reg reg
);
168 int16_t index
; /**< temporary index, VERT_ATTRIB_*, VARYING_SLOT_*, etc. */
170 uint16_t swizzle
; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
171 int negate
:4; /**< NEGATE_XYZW mask from mesa */
173 enum glsl_base_type type
:5; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
174 unsigned has_index2
:1;
175 gl_register_file file
:5; /**< PROGRAM_* from Mesa */
177 * Is this the second half of a double register pair?
178 * currently used for input mapping only.
180 unsigned double_reg2
:1;
181 unsigned is_double_vertex_input
:1;
182 unsigned array_id
:10;
184 /** Register index should be offset by the integer in this reg. */
186 st_src_reg
*reladdr2
;
190 st_src_reg reg
= *this;
199 st_dst_reg(gl_register_file file
, int writemask
, enum glsl_base_type type
, int index
)
201 assert(file
!= PROGRAM_ARRAY
); /* need array_id > 0 */
205 this->writemask
= writemask
;
206 this->reladdr
= NULL
;
207 this->reladdr2
= NULL
;
208 this->has_index2
= false;
213 st_dst_reg(gl_register_file file
, int writemask
, enum glsl_base_type type
)
215 assert(file
!= PROGRAM_ARRAY
); /* need array_id > 0 */
219 this->writemask
= writemask
;
220 this->reladdr
= NULL
;
221 this->reladdr2
= NULL
;
222 this->has_index2
= false;
229 this->type
= GLSL_TYPE_ERROR
;
230 this->file
= PROGRAM_UNDEFINED
;
234 this->reladdr
= NULL
;
235 this->reladdr2
= NULL
;
236 this->has_index2
= false;
240 explicit st_dst_reg(st_src_reg reg
);
242 int16_t index
; /**< temporary index, VERT_ATTRIB_*, VARYING_SLOT_*, etc. */
244 gl_register_file file
:5; /**< PROGRAM_* from Mesa */
245 unsigned writemask
:4; /**< Bitfield of WRITEMASK_[XYZW] */
246 enum glsl_base_type type
:5; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
247 unsigned has_index2
:1;
248 unsigned array_id
:10;
250 /** Register index should be offset by the integer in this reg. */
252 st_src_reg
*reladdr2
;
255 st_src_reg::st_src_reg(st_dst_reg reg
)
257 this->type
= reg
.type
;
258 this->file
= reg
.file
;
259 this->index
= reg
.index
;
260 this->swizzle
= SWIZZLE_XYZW
;
263 this->reladdr
= reg
.reladdr
;
264 this->index2D
= reg
.index2D
;
265 this->reladdr2
= reg
.reladdr2
;
266 this->has_index2
= reg
.has_index2
;
267 this->double_reg2
= false;
268 this->array_id
= reg
.array_id
;
269 this->is_double_vertex_input
= false;
272 st_dst_reg::st_dst_reg(st_src_reg reg
)
274 this->type
= reg
.type
;
275 this->file
= reg
.file
;
276 this->index
= reg
.index
;
277 this->writemask
= WRITEMASK_XYZW
;
278 this->reladdr
= reg
.reladdr
;
279 this->index2D
= reg
.index2D
;
280 this->reladdr2
= reg
.reladdr2
;
281 this->has_index2
= reg
.has_index2
;
282 this->array_id
= reg
.array_id
;
285 class glsl_to_tgsi_instruction
: public exec_node
{
287 DECLARE_RALLOC_CXX_OPERATORS(glsl_to_tgsi_instruction
)
291 st_src_reg resource
; /**< sampler or buffer register */
292 st_src_reg
*tex_offsets
;
294 /** Pointer to the ir source this tree came from for debugging */
297 unsigned op
:8; /**< TGSI opcode */
299 unsigned is_64bit_expanded
:1;
300 unsigned sampler_base
:5;
301 unsigned sampler_array_size
:6; /**< 1-based size of sampler array, 1 if not array */
302 unsigned tex_target
:4; /**< One of TEXTURE_*_INDEX */
303 glsl_base_type tex_type
:5;
304 unsigned tex_shadow
:1;
305 unsigned image_format
:9;
306 unsigned tex_offset_num_offset
:3;
307 unsigned dead_mask
:4; /**< Used in dead code elimination */
308 unsigned buffer_access
:3; /**< buffer access type */
310 const struct tgsi_opcode_info
*info
;
313 class variable_storage
: public exec_node
{
315 variable_storage(ir_variable
*var
, gl_register_file file
, int index
,
316 unsigned array_id
= 0)
317 : file(file
), index(index
), component(0), var(var
), array_id(array_id
)
319 assert(file
!= PROGRAM_ARRAY
|| array_id
!= 0);
322 gl_register_file file
;
325 /* Explicit component location. This is given in terms of the GLSL-style
326 * swizzles where each double is a single component, i.e. for 64-bit types
327 * it can only be 0 or 1.
330 ir_variable
*var
; /* variable that maps to this, if any */
334 class immediate_storage
: public exec_node
{
336 immediate_storage(gl_constant_value
*values
, int size32
, int type
)
338 memcpy(this->values
, values
, size32
* sizeof(gl_constant_value
));
339 this->size32
= size32
;
343 /* doubles are stored across 2 gl_constant_values */
344 gl_constant_value values
[4];
345 int size32
; /**< Number of 32-bit components (1-4) */
346 int type
; /**< GL_DOUBLE, GL_FLOAT, GL_INT, GL_BOOL, or GL_UNSIGNED_INT */
349 static st_src_reg undef_src
= st_src_reg(PROGRAM_UNDEFINED
, 0, GLSL_TYPE_ERROR
);
350 static st_dst_reg undef_dst
= st_dst_reg(PROGRAM_UNDEFINED
, SWIZZLE_NOOP
, GLSL_TYPE_ERROR
);
354 unsigned array_id
; /* TGSI ArrayID; 1-based: 0 means not an array */
357 unsigned gs_out_streams
;
358 enum glsl_interp_mode interp
;
359 enum glsl_base_type base_type
;
360 ubyte usage_mask
; /* GLSL-style usage-mask, i.e. single bit per double */
363 static struct inout_decl
*
364 find_inout_array(struct inout_decl
*decls
, unsigned count
, unsigned array_id
)
366 assert(array_id
!= 0);
368 for (unsigned i
= 0; i
< count
; i
++) {
369 struct inout_decl
*decl
= &decls
[i
];
371 if (array_id
== decl
->array_id
) {
379 static enum glsl_base_type
380 find_array_type(struct inout_decl
*decls
, unsigned count
, unsigned array_id
)
383 return GLSL_TYPE_ERROR
;
384 struct inout_decl
*decl
= find_inout_array(decls
, count
, array_id
);
386 return decl
->base_type
;
387 return GLSL_TYPE_ERROR
;
390 struct rename_reg_pair
{
395 struct glsl_to_tgsi_visitor
: public ir_visitor
{
397 glsl_to_tgsi_visitor();
398 ~glsl_to_tgsi_visitor();
400 struct gl_context
*ctx
;
401 struct gl_program
*prog
;
402 struct gl_shader_program
*shader_program
;
403 struct gl_linked_shader
*shader
;
404 struct gl_shader_compiler_options
*options
;
408 unsigned *array_sizes
;
409 unsigned max_num_arrays
;
412 struct inout_decl inputs
[4 * PIPE_MAX_SHADER_INPUTS
];
414 unsigned num_input_arrays
;
415 struct inout_decl outputs
[4 * PIPE_MAX_SHADER_OUTPUTS
];
416 unsigned num_outputs
;
417 unsigned num_output_arrays
;
419 int num_address_regs
;
420 uint32_t samplers_used
;
421 glsl_base_type sampler_types
[PIPE_MAX_SAMPLERS
];
422 int sampler_targets
[PIPE_MAX_SAMPLERS
]; /**< One of TGSI_TEXTURE_* */
425 int image_targets
[PIPE_MAX_SHADER_IMAGES
];
426 unsigned image_formats
[PIPE_MAX_SHADER_IMAGES
];
427 bool indirect_addr_consts
;
428 int wpos_transform_const
;
431 bool native_integers
;
434 bool use_shared_memory
;
437 variable_storage
*find_variable_storage(ir_variable
*var
);
439 int add_constant(gl_register_file file
, gl_constant_value values
[8],
440 int size
, int datatype
, uint16_t *swizzle_out
);
442 st_src_reg
get_temp(const glsl_type
*type
);
443 void reladdr_to_temp(ir_instruction
*ir
, st_src_reg
*reg
, int *num_reladdr
);
445 st_src_reg
st_src_reg_for_double(double val
);
446 st_src_reg
st_src_reg_for_float(float val
);
447 st_src_reg
st_src_reg_for_int(int val
);
448 st_src_reg
st_src_reg_for_type(enum glsl_base_type type
, int val
);
451 * \name Visit methods
453 * As typical for the visitor pattern, there must be one \c visit method for
454 * each concrete subclass of \c ir_instruction. Virtual base classes within
455 * the hierarchy should not have \c visit methods.
458 virtual void visit(ir_variable
*);
459 virtual void visit(ir_loop
*);
460 virtual void visit(ir_loop_jump
*);
461 virtual void visit(ir_function_signature
*);
462 virtual void visit(ir_function
*);
463 virtual void visit(ir_expression
*);
464 virtual void visit(ir_swizzle
*);
465 virtual void visit(ir_dereference_variable
*);
466 virtual void visit(ir_dereference_array
*);
467 virtual void visit(ir_dereference_record
*);
468 virtual void visit(ir_assignment
*);
469 virtual void visit(ir_constant
*);
470 virtual void visit(ir_call
*);
471 virtual void visit(ir_return
*);
472 virtual void visit(ir_discard
*);
473 virtual void visit(ir_texture
*);
474 virtual void visit(ir_if
*);
475 virtual void visit(ir_emit_vertex
*);
476 virtual void visit(ir_end_primitive
*);
477 virtual void visit(ir_barrier
*);
480 void visit_expression(ir_expression
*, st_src_reg
*) ATTRIBUTE_NOINLINE
;
482 void visit_atomic_counter_intrinsic(ir_call
*);
483 void visit_ssbo_intrinsic(ir_call
*);
484 void visit_membar_intrinsic(ir_call
*);
485 void visit_shared_intrinsic(ir_call
*);
486 void visit_image_intrinsic(ir_call
*);
490 /** List of variable_storage */
493 /** List of immediate_storage */
494 exec_list immediates
;
495 unsigned num_immediates
;
497 /** List of glsl_to_tgsi_instruction */
498 exec_list instructions
;
500 glsl_to_tgsi_instruction
*emit_asm(ir_instruction
*ir
, unsigned op
,
501 st_dst_reg dst
= undef_dst
,
502 st_src_reg src0
= undef_src
,
503 st_src_reg src1
= undef_src
,
504 st_src_reg src2
= undef_src
,
505 st_src_reg src3
= undef_src
);
507 glsl_to_tgsi_instruction
*emit_asm(ir_instruction
*ir
, unsigned op
,
508 st_dst_reg dst
, st_dst_reg dst1
,
509 st_src_reg src0
= undef_src
,
510 st_src_reg src1
= undef_src
,
511 st_src_reg src2
= undef_src
,
512 st_src_reg src3
= undef_src
);
514 unsigned get_opcode(unsigned op
,
516 st_src_reg src0
, st_src_reg src1
);
519 * Emit the correct dot-product instruction for the type of arguments
521 glsl_to_tgsi_instruction
*emit_dp(ir_instruction
*ir
,
527 void emit_scalar(ir_instruction
*ir
, unsigned op
,
528 st_dst_reg dst
, st_src_reg src0
);
530 void emit_scalar(ir_instruction
*ir
, unsigned op
,
531 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
533 void emit_arl(ir_instruction
*ir
, st_dst_reg dst
, st_src_reg src0
);
535 void get_deref_offsets(ir_dereference
*ir
,
536 unsigned *array_size
,
541 void calc_deref_offsets(ir_dereference
*tail
,
542 unsigned *array_elements
,
544 st_src_reg
*indirect
,
546 st_src_reg
canonicalize_gather_offset(st_src_reg offset
);
548 bool try_emit_mad(ir_expression
*ir
,
550 bool try_emit_mad_for_and_not(ir_expression
*ir
,
553 void emit_swz(ir_expression
*ir
);
555 bool process_move_condition(ir_rvalue
*ir
);
557 void simplify_cmp(void);
559 void rename_temp_registers(int num_renames
, struct rename_reg_pair
*renames
);
560 void get_first_temp_read(int *first_reads
);
561 void get_last_temp_read_first_temp_write(int *last_reads
, int *first_writes
);
562 void get_last_temp_write(int *last_writes
);
564 void copy_propagate(void);
565 int eliminate_dead_code(void);
567 void merge_two_dsts(void);
568 void merge_registers(void);
569 void renumber_registers(void);
571 void emit_block_mov(ir_assignment
*ir
, const struct glsl_type
*type
,
572 st_dst_reg
*l
, st_src_reg
*r
,
573 st_src_reg
*cond
, bool cond_swap
);
578 static st_dst_reg address_reg
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
, 0);
579 static st_dst_reg address_reg2
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
, 1);
580 static st_dst_reg sampler_reladdr
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
, 2);
583 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...) PRINTFLIKE(2, 3);
586 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
590 ralloc_vasprintf_append(&prog
->data
->InfoLog
, fmt
, args
);
593 prog
->data
->LinkStatus
= linking_failure
;
597 swizzle_for_size(int size
)
599 static const int size_swizzles
[4] = {
600 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
601 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
602 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
603 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
606 assert((size
>= 1) && (size
<= 4));
607 return size_swizzles
[size
- 1];
611 is_resource_instruction(unsigned opcode
)
614 case TGSI_OPCODE_RESQ
:
615 case TGSI_OPCODE_LOAD
:
616 case TGSI_OPCODE_ATOMUADD
:
617 case TGSI_OPCODE_ATOMXCHG
:
618 case TGSI_OPCODE_ATOMCAS
:
619 case TGSI_OPCODE_ATOMAND
:
620 case TGSI_OPCODE_ATOMOR
:
621 case TGSI_OPCODE_ATOMXOR
:
622 case TGSI_OPCODE_ATOMUMIN
:
623 case TGSI_OPCODE_ATOMUMAX
:
624 case TGSI_OPCODE_ATOMIMIN
:
625 case TGSI_OPCODE_ATOMIMAX
:
633 num_inst_dst_regs(const glsl_to_tgsi_instruction
*op
)
635 return op
->info
->num_dst
;
639 num_inst_src_regs(const glsl_to_tgsi_instruction
*op
)
641 return op
->info
->is_tex
|| is_resource_instruction(op
->op
) ?
642 op
->info
->num_src
- 1 : op
->info
->num_src
;
645 glsl_to_tgsi_instruction
*
646 glsl_to_tgsi_visitor::emit_asm(ir_instruction
*ir
, unsigned op
,
647 st_dst_reg dst
, st_dst_reg dst1
,
648 st_src_reg src0
, st_src_reg src1
,
649 st_src_reg src2
, st_src_reg src3
)
651 glsl_to_tgsi_instruction
*inst
= new(mem_ctx
) glsl_to_tgsi_instruction();
652 int num_reladdr
= 0, i
, j
;
653 bool dst_is_64bit
[2];
655 op
= get_opcode(op
, dst
, src0
, src1
);
657 /* If we have to do relative addressing, we want to load the ARL
658 * reg directly for one of the regs, and preload the other reladdr
659 * sources into temps.
661 num_reladdr
+= dst
.reladdr
!= NULL
|| dst
.reladdr2
;
662 num_reladdr
+= dst1
.reladdr
!= NULL
|| dst1
.reladdr2
;
663 num_reladdr
+= src0
.reladdr
!= NULL
|| src0
.reladdr2
!= NULL
;
664 num_reladdr
+= src1
.reladdr
!= NULL
|| src1
.reladdr2
!= NULL
;
665 num_reladdr
+= src2
.reladdr
!= NULL
|| src2
.reladdr2
!= NULL
;
666 num_reladdr
+= src3
.reladdr
!= NULL
|| src3
.reladdr2
!= NULL
;
668 reladdr_to_temp(ir
, &src3
, &num_reladdr
);
669 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
670 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
671 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
673 if (dst
.reladdr
|| dst
.reladdr2
) {
675 emit_arl(ir
, address_reg
, *dst
.reladdr
);
677 emit_arl(ir
, address_reg2
, *dst
.reladdr2
);
681 emit_arl(ir
, address_reg
, *dst1
.reladdr
);
684 assert(num_reladdr
== 0);
686 /* inst->op has only 8 bits. */
687 STATIC_ASSERT(TGSI_OPCODE_LAST
<= 255);
690 inst
->info
= tgsi_get_opcode_info(op
);
697 inst
->is_64bit_expanded
= false;
700 inst
->tex_offsets
= NULL
;
701 inst
->tex_offset_num_offset
= 0;
703 inst
->tex_shadow
= 0;
704 /* default to float, for paths where this is not initialized
705 * (since 0==UINT which is likely wrong):
707 inst
->tex_type
= GLSL_TYPE_FLOAT
;
709 /* Update indirect addressing status used by TGSI */
710 if (dst
.reladdr
|| dst
.reladdr2
) {
712 case PROGRAM_STATE_VAR
:
713 case PROGRAM_CONSTANT
:
714 case PROGRAM_UNIFORM
:
715 this->indirect_addr_consts
= true;
717 case PROGRAM_IMMEDIATE
:
718 assert(!"immediates should not have indirect addressing");
725 for (i
= 0; i
< 4; i
++) {
726 if(inst
->src
[i
].reladdr
) {
727 switch(inst
->src
[i
].file
) {
728 case PROGRAM_STATE_VAR
:
729 case PROGRAM_CONSTANT
:
730 case PROGRAM_UNIFORM
:
731 this->indirect_addr_consts
= true;
733 case PROGRAM_IMMEDIATE
:
734 assert(!"immediates should not have indirect addressing");
744 * This section contains the double processing.
745 * GLSL just represents doubles as single channel values,
746 * however most HW and TGSI represent doubles as pairs of register channels.
748 * so we have to fixup destination writemask/index and src swizzle/indexes.
749 * dest writemasks need to translate from single channel write mask
750 * to a dual-channel writemask, but also need to modify the index,
751 * if we are touching the Z,W fields in the pre-translated writemask.
753 * src channels have similiar index modifications along with swizzle
754 * changes to we pick the XY, ZW pairs from the correct index.
756 * GLSL [0].x -> TGSI [0].xy
757 * GLSL [0].y -> TGSI [0].zw
758 * GLSL [0].z -> TGSI [1].xy
759 * GLSL [0].w -> TGSI [1].zw
761 for (j
= 0; j
< 2; j
++) {
762 dst_is_64bit
[j
] = glsl_base_type_is_64bit(inst
->dst
[j
].type
);
763 if (!dst_is_64bit
[j
] && inst
->dst
[j
].file
== PROGRAM_OUTPUT
&& inst
->dst
[j
].type
== GLSL_TYPE_ARRAY
) {
764 enum glsl_base_type type
= find_array_type(this->outputs
, this->num_outputs
, inst
->dst
[j
].array_id
);
765 if (glsl_base_type_is_64bit(type
))
766 dst_is_64bit
[j
] = true;
770 if (dst_is_64bit
[0] || dst_is_64bit
[1] ||
771 glsl_base_type_is_64bit(inst
->src
[0].type
)) {
772 glsl_to_tgsi_instruction
*dinst
= NULL
;
773 int initial_src_swz
[4], initial_src_idx
[4];
774 int initial_dst_idx
[2], initial_dst_writemask
[2];
775 /* select the writemask for dst0 or dst1 */
776 unsigned writemask
= inst
->dst
[1].file
== PROGRAM_UNDEFINED
? inst
->dst
[0].writemask
: inst
->dst
[1].writemask
;
778 /* copy out the writemask, index and swizzles for all src/dsts. */
779 for (j
= 0; j
< 2; j
++) {
780 initial_dst_writemask
[j
] = inst
->dst
[j
].writemask
;
781 initial_dst_idx
[j
] = inst
->dst
[j
].index
;
784 for (j
= 0; j
< 4; j
++) {
785 initial_src_swz
[j
] = inst
->src
[j
].swizzle
;
786 initial_src_idx
[j
] = inst
->src
[j
].index
;
790 * scan all the components in the dst writemask
791 * generate an instruction for each of them if required.
796 int i
= u_bit_scan(&writemask
);
798 /* before emitting the instruction, see if we have to adjust load / store
800 if (i
> 1 && (inst
->op
== TGSI_OPCODE_LOAD
|| inst
->op
== TGSI_OPCODE_STORE
) &&
801 addr
.file
== PROGRAM_UNDEFINED
) {
802 /* We have to advance the buffer address by 16 */
803 addr
= get_temp(glsl_type::uint_type
);
804 emit_asm(ir
, TGSI_OPCODE_UADD
, st_dst_reg(addr
),
805 inst
->src
[0], st_src_reg_for_int(16));
808 /* first time use previous instruction */
812 /* create a new instructions for subsequent attempts */
813 dinst
= new(mem_ctx
) glsl_to_tgsi_instruction();
818 this->instructions
.push_tail(dinst
);
819 dinst
->is_64bit_expanded
= true;
821 /* modify the destination if we are splitting */
822 for (j
= 0; j
< 2; j
++) {
823 if (dst_is_64bit
[j
]) {
824 dinst
->dst
[j
].writemask
= (i
& 1) ? WRITEMASK_ZW
: WRITEMASK_XY
;
825 dinst
->dst
[j
].index
= initial_dst_idx
[j
];
827 if (dinst
->op
== TGSI_OPCODE_LOAD
|| dinst
->op
== TGSI_OPCODE_STORE
)
828 dinst
->src
[0] = addr
;
829 if (dinst
->op
!= TGSI_OPCODE_STORE
)
830 dinst
->dst
[j
].index
++;
833 /* if we aren't writing to a double, just get the bit of the initial writemask
835 dinst
->dst
[j
].writemask
= initial_dst_writemask
[j
] & (1 << i
);
839 /* modify the src registers */
840 for (j
= 0; j
< 4; j
++) {
841 int swz
= GET_SWZ(initial_src_swz
[j
], i
);
843 if (glsl_base_type_is_64bit(dinst
->src
[j
].type
)) {
844 dinst
->src
[j
].index
= initial_src_idx
[j
];
846 dinst
->src
[j
].double_reg2
= true;
847 dinst
->src
[j
].index
++;
851 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_W
, SWIZZLE_Z
, SWIZZLE_W
);
853 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
856 /* some opcodes are special case in what they use as sources
857 - [FUI]2D/[UI]2I64 is a float/[u]int src0, DLDEXP is integer src1 */
858 if (op
== TGSI_OPCODE_F2D
|| op
== TGSI_OPCODE_U2D
|| op
== TGSI_OPCODE_I2D
||
859 op
== TGSI_OPCODE_I2I64
|| op
== TGSI_OPCODE_U2I64
||
860 op
== TGSI_OPCODE_DLDEXP
||
861 (op
== TGSI_OPCODE_UCMP
&& dst_is_64bit
[0])) {
862 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(swz
, swz
, swz
, swz
);
869 this->instructions
.push_tail(inst
);
876 glsl_to_tgsi_instruction
*
877 glsl_to_tgsi_visitor::emit_asm(ir_instruction
*ir
, unsigned op
,
879 st_src_reg src0
, st_src_reg src1
,
880 st_src_reg src2
, st_src_reg src3
)
882 return emit_asm(ir
, op
, dst
, undef_dst
, src0
, src1
, src2
, src3
);
886 * Determines whether to use an integer, unsigned integer, or float opcode
887 * based on the operands and input opcode, then emits the result.
890 glsl_to_tgsi_visitor::get_opcode(unsigned op
,
892 st_src_reg src0
, st_src_reg src1
)
894 enum glsl_base_type type
= GLSL_TYPE_FLOAT
;
896 if (op
== TGSI_OPCODE_MOV
)
899 assert(src0
.type
!= GLSL_TYPE_ARRAY
);
900 assert(src0
.type
!= GLSL_TYPE_STRUCT
);
901 assert(src1
.type
!= GLSL_TYPE_ARRAY
);
902 assert(src1
.type
!= GLSL_TYPE_STRUCT
);
904 if (is_resource_instruction(op
))
906 else if (src0
.type
== GLSL_TYPE_INT64
|| src1
.type
== GLSL_TYPE_INT64
)
907 type
= GLSL_TYPE_INT64
;
908 else if (src0
.type
== GLSL_TYPE_UINT64
|| src1
.type
== GLSL_TYPE_UINT64
)
909 type
= GLSL_TYPE_UINT64
;
910 else if (src0
.type
== GLSL_TYPE_DOUBLE
|| src1
.type
== GLSL_TYPE_DOUBLE
)
911 type
= GLSL_TYPE_DOUBLE
;
912 else if (src0
.type
== GLSL_TYPE_FLOAT
|| src1
.type
== GLSL_TYPE_FLOAT
)
913 type
= GLSL_TYPE_FLOAT
;
914 else if (native_integers
)
915 type
= src0
.type
== GLSL_TYPE_BOOL
? GLSL_TYPE_INT
: src0
.type
;
917 #define case7(c, f, i, u, d, i64, ui64) \
918 case TGSI_OPCODE_##c: \
919 if (type == GLSL_TYPE_UINT64) \
920 op = TGSI_OPCODE_##ui64; \
921 else if (type == GLSL_TYPE_INT64) \
922 op = TGSI_OPCODE_##i64; \
923 else if (type == GLSL_TYPE_DOUBLE) \
924 op = TGSI_OPCODE_##d; \
925 else if (type == GLSL_TYPE_INT) \
926 op = TGSI_OPCODE_##i; \
927 else if (type == GLSL_TYPE_UINT) \
928 op = TGSI_OPCODE_##u; \
930 op = TGSI_OPCODE_##f; \
932 #define case5(c, f, i, u, d) \
933 case TGSI_OPCODE_##c: \
934 if (type == GLSL_TYPE_DOUBLE) \
935 op = TGSI_OPCODE_##d; \
936 else if (type == GLSL_TYPE_INT) \
937 op = TGSI_OPCODE_##i; \
938 else if (type == GLSL_TYPE_UINT) \
939 op = TGSI_OPCODE_##u; \
941 op = TGSI_OPCODE_##f; \
944 #define case4(c, f, i, u) \
945 case TGSI_OPCODE_##c: \
946 if (type == GLSL_TYPE_INT) \
947 op = TGSI_OPCODE_##i; \
948 else if (type == GLSL_TYPE_UINT) \
949 op = TGSI_OPCODE_##u; \
951 op = TGSI_OPCODE_##f; \
954 #define case3(f, i, u) case4(f, f, i, u)
955 #define case6d(f, i, u, d, i64, u64) case7(f, f, i, u, d, i64, u64)
956 #define case3fid(f, i, d) case5(f, f, i, i, d)
957 #define case3fid64(f, i, d, i64) case7(f, f, i, i, d, i64, i64)
958 #define case2fi(f, i) case4(f, f, i, i)
959 #define case2iu(i, u) case4(i, LAST, i, u)
961 #define case2iu64(i, i64) case7(i, LAST, i, i, LAST, i64, i64)
962 #define case4iu64(i, u, i64, u64) case7(i, LAST, i, u, LAST, i64, u64)
964 #define casecomp(c, f, i, u, d, i64, ui64) \
965 case TGSI_OPCODE_##c: \
966 if (type == GLSL_TYPE_INT64) \
967 op = TGSI_OPCODE_##i64; \
968 else if (type == GLSL_TYPE_UINT64) \
969 op = TGSI_OPCODE_##ui64; \
970 else if (type == GLSL_TYPE_DOUBLE) \
971 op = TGSI_OPCODE_##d; \
972 else if (type == GLSL_TYPE_INT || type == GLSL_TYPE_SUBROUTINE) \
973 op = TGSI_OPCODE_##i; \
974 else if (type == GLSL_TYPE_UINT) \
975 op = TGSI_OPCODE_##u; \
976 else if (native_integers) \
977 op = TGSI_OPCODE_##f; \
979 op = TGSI_OPCODE_##c; \
983 case3fid64(ADD
, UADD
, DADD
, U64ADD
);
984 case3fid64(MUL
, UMUL
, DMUL
, U64MUL
);
985 case3fid(MAD
, UMAD
, DMAD
);
986 case3fid(FMA
, UMAD
, DFMA
);
987 case6d(DIV
, IDIV
, UDIV
, DDIV
, I64DIV
, U64DIV
);
988 case6d(MAX
, IMAX
, UMAX
, DMAX
, I64MAX
, U64MAX
);
989 case6d(MIN
, IMIN
, UMIN
, DMIN
, I64MIN
, U64MIN
);
990 case4iu64(MOD
, UMOD
, I64MOD
, U64MOD
);
992 casecomp(SEQ
, FSEQ
, USEQ
, USEQ
, DSEQ
, U64SEQ
, U64SEQ
);
993 casecomp(SNE
, FSNE
, USNE
, USNE
, DSNE
, U64SNE
, U64SNE
);
994 casecomp(SGE
, FSGE
, ISGE
, USGE
, DSGE
, I64SGE
, U64SGE
);
995 casecomp(SLT
, FSLT
, ISLT
, USLT
, DSLT
, I64SLT
, U64SLT
);
997 case2iu64(SHL
, U64SHL
);
998 case4iu64(ISHR
, USHR
, I64SHR
, U64SHR
);
1000 case3fid64(SSG
, ISSG
, DSSG
, I64SSG
);
1002 case2iu(IBFE
, UBFE
);
1003 case2iu(IMSB
, UMSB
);
1004 case2iu(IMUL_HI
, UMUL_HI
);
1006 case3fid(SQRT
, SQRT
, DSQRT
);
1008 case3fid(RCP
, RCP
, DRCP
);
1009 case3fid(RSQ
, RSQ
, DRSQ
);
1011 case3fid(FRC
, FRC
, DFRAC
);
1012 case3fid(TRUNC
, TRUNC
, DTRUNC
);
1013 case3fid(CEIL
, CEIL
, DCEIL
);
1014 case3fid(FLR
, FLR
, DFLR
);
1015 case3fid(ROUND
, ROUND
, DROUND
);
1017 case2iu(ATOMIMAX
, ATOMUMAX
);
1018 case2iu(ATOMIMIN
, ATOMUMIN
);
1023 assert(op
!= TGSI_OPCODE_LAST
);
1027 glsl_to_tgsi_instruction
*
1028 glsl_to_tgsi_visitor::emit_dp(ir_instruction
*ir
,
1029 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
,
1032 static const unsigned dot_opcodes
[] = {
1033 TGSI_OPCODE_DP2
, TGSI_OPCODE_DP3
, TGSI_OPCODE_DP4
1036 return emit_asm(ir
, dot_opcodes
[elements
- 2], dst
, src0
, src1
);
1040 * Emits TGSI scalar opcodes to produce unique answers across channels.
1042 * Some TGSI opcodes are scalar-only, like ARB_fp/vp. The src X
1043 * channel determines the result across all channels. So to do a vec4
1044 * of this operation, we want to emit a scalar per source channel used
1045 * to produce dest channels.
1048 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
1050 st_src_reg orig_src0
, st_src_reg orig_src1
)
1053 int done_mask
= ~dst
.writemask
;
1055 /* TGSI RCP is a scalar operation splatting results to all channels,
1056 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
1059 for (i
= 0; i
< 4; i
++) {
1060 GLuint this_mask
= (1 << i
);
1061 st_src_reg src0
= orig_src0
;
1062 st_src_reg src1
= orig_src1
;
1064 if (done_mask
& this_mask
)
1067 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
1068 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
1069 for (j
= i
+ 1; j
< 4; j
++) {
1070 /* If there is another enabled component in the destination that is
1071 * derived from the same inputs, generate its value on this pass as
1074 if (!(done_mask
& (1 << j
)) &&
1075 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
1076 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
1077 this_mask
|= (1 << j
);
1080 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
1081 src0_swiz
, src0_swiz
);
1082 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
1083 src1_swiz
, src1_swiz
);
1085 dst
.writemask
= this_mask
;
1086 emit_asm(ir
, op
, dst
, src0
, src1
);
1087 done_mask
|= this_mask
;
1092 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
1093 st_dst_reg dst
, st_src_reg src0
)
1095 st_src_reg undef
= undef_src
;
1097 undef
.swizzle
= SWIZZLE_XXXX
;
1099 emit_scalar(ir
, op
, dst
, src0
, undef
);
1103 glsl_to_tgsi_visitor::emit_arl(ir_instruction
*ir
,
1104 st_dst_reg dst
, st_src_reg src0
)
1106 int op
= TGSI_OPCODE_ARL
;
1108 if (src0
.type
== GLSL_TYPE_INT
|| src0
.type
== GLSL_TYPE_UINT
)
1109 op
= TGSI_OPCODE_UARL
;
1111 assert(dst
.file
== PROGRAM_ADDRESS
);
1112 if (dst
.index
>= this->num_address_regs
)
1113 this->num_address_regs
= dst
.index
+ 1;
1115 emit_asm(NULL
, op
, dst
, src0
);
1119 glsl_to_tgsi_visitor::add_constant(gl_register_file file
,
1120 gl_constant_value values
[8], int size
, int datatype
,
1121 uint16_t *swizzle_out
)
1123 if (file
== PROGRAM_CONSTANT
) {
1124 GLuint swizzle
= swizzle_out
? *swizzle_out
: 0;
1125 int result
= _mesa_add_typed_unnamed_constant(this->prog
->Parameters
, values
,
1126 size
, datatype
, &swizzle
);
1128 *swizzle_out
= swizzle
;
1132 assert(file
== PROGRAM_IMMEDIATE
);
1135 immediate_storage
*entry
;
1136 int size32
= size
* ((datatype
== GL_DOUBLE
||
1137 datatype
== GL_INT64_ARB
||
1138 datatype
== GL_UNSIGNED_INT64_ARB
)? 2 : 1);
1141 /* Search immediate storage to see if we already have an identical
1142 * immediate that we can use instead of adding a duplicate entry.
1144 foreach_in_list(immediate_storage
, entry
, &this->immediates
) {
1145 immediate_storage
*tmp
= entry
;
1147 for (i
= 0; i
* 4 < size32
; i
++) {
1148 int slot_size
= MIN2(size32
- (i
* 4), 4);
1149 if (tmp
->type
!= datatype
|| tmp
->size32
!= slot_size
)
1151 if (memcmp(tmp
->values
, &values
[i
* 4],
1152 slot_size
* sizeof(gl_constant_value
)))
1155 /* Everything matches, keep going until the full size is matched */
1156 tmp
= (immediate_storage
*)tmp
->next
;
1159 /* The full value matched */
1160 if (i
* 4 >= size32
)
1166 for (i
= 0; i
* 4 < size32
; i
++) {
1167 int slot_size
= MIN2(size32
- (i
* 4), 4);
1168 /* Add this immediate to the list. */
1169 entry
= new(mem_ctx
) immediate_storage(&values
[i
* 4], slot_size
, datatype
);
1170 this->immediates
.push_tail(entry
);
1171 this->num_immediates
++;
1177 glsl_to_tgsi_visitor::st_src_reg_for_float(float val
)
1179 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_FLOAT
);
1180 union gl_constant_value uval
;
1183 src
.index
= add_constant(src
.file
, &uval
, 1, GL_FLOAT
, &src
.swizzle
);
1189 glsl_to_tgsi_visitor::st_src_reg_for_double(double val
)
1191 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_DOUBLE
);
1192 union gl_constant_value uval
[2];
1194 memcpy(uval
, &val
, sizeof(uval
));
1195 src
.index
= add_constant(src
.file
, uval
, 1, GL_DOUBLE
, &src
.swizzle
);
1196 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
1201 glsl_to_tgsi_visitor::st_src_reg_for_int(int val
)
1203 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_INT
);
1204 union gl_constant_value uval
;
1206 assert(native_integers
);
1209 src
.index
= add_constant(src
.file
, &uval
, 1, GL_INT
, &src
.swizzle
);
1215 glsl_to_tgsi_visitor::st_src_reg_for_type(enum glsl_base_type type
, int val
)
1217 if (native_integers
)
1218 return type
== GLSL_TYPE_FLOAT
? st_src_reg_for_float(val
) :
1219 st_src_reg_for_int(val
);
1221 return st_src_reg_for_float(val
);
1225 attrib_type_size(const struct glsl_type
*type
, bool is_vs_input
)
1227 return st_glsl_attrib_type_size(type
, is_vs_input
);
1231 type_size(const struct glsl_type
*type
)
1233 return st_glsl_type_size(type
);
1237 * If the given GLSL type is an array or matrix or a structure containing
1238 * an array/matrix member, return true. Else return false.
1240 * This is used to determine which kind of temp storage (PROGRAM_TEMPORARY
1241 * or PROGRAM_ARRAY) should be used for variables of this type. Anytime
1242 * we have an array that might be indexed with a variable, we need to use
1243 * the later storage type.
1246 type_has_array_or_matrix(const glsl_type
*type
)
1248 if (type
->is_array() || type
->is_matrix())
1251 if (type
->is_record()) {
1252 for (unsigned i
= 0; i
< type
->length
; i
++) {
1253 if (type_has_array_or_matrix(type
->fields
.structure
[i
].type
)) {
1264 * In the initial pass of codegen, we assign temporary numbers to
1265 * intermediate results. (not SSA -- variable assignments will reuse
1269 glsl_to_tgsi_visitor::get_temp(const glsl_type
*type
)
1273 src
.type
= native_integers
? type
->base_type
: GLSL_TYPE_FLOAT
;
1278 if (!options
->EmitNoIndirectTemp
&& type_has_array_or_matrix(type
)) {
1279 if (next_array
>= max_num_arrays
) {
1280 max_num_arrays
+= 32;
1281 array_sizes
= (unsigned*)
1282 realloc(array_sizes
, sizeof(array_sizes
[0]) * max_num_arrays
);
1285 src
.file
= PROGRAM_ARRAY
;
1287 src
.array_id
= next_array
+ 1;
1288 array_sizes
[next_array
] = type_size(type
);
1292 src
.file
= PROGRAM_TEMPORARY
;
1293 src
.index
= next_temp
;
1294 next_temp
+= type_size(type
);
1297 if (type
->is_array() || type
->is_record()) {
1298 src
.swizzle
= SWIZZLE_NOOP
;
1300 src
.swizzle
= swizzle_for_size(type
->vector_elements
);
1307 glsl_to_tgsi_visitor::find_variable_storage(ir_variable
*var
)
1310 foreach_in_list(variable_storage
, entry
, &this->variables
) {
1311 if (entry
->var
== var
)
1319 glsl_to_tgsi_visitor::visit(ir_variable
*ir
)
1321 if (strcmp(ir
->name
, "gl_FragCoord") == 0) {
1322 this->prog
->OriginUpperLeft
= ir
->data
.origin_upper_left
;
1323 this->prog
->PixelCenterInteger
= ir
->data
.pixel_center_integer
;
1326 if (ir
->data
.mode
== ir_var_uniform
&& strncmp(ir
->name
, "gl_", 3) == 0) {
1328 const ir_state_slot
*const slots
= ir
->get_state_slots();
1329 assert(slots
!= NULL
);
1331 /* Check if this statevar's setup in the STATE file exactly
1332 * matches how we'll want to reference it as a
1333 * struct/array/whatever. If not, then we need to move it into
1334 * temporary storage and hope that it'll get copy-propagated
1337 for (i
= 0; i
< ir
->get_num_state_slots(); i
++) {
1338 if (slots
[i
].swizzle
!= SWIZZLE_XYZW
) {
1343 variable_storage
*storage
;
1345 if (i
== ir
->get_num_state_slots()) {
1346 /* We'll set the index later. */
1347 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_STATE_VAR
, -1);
1348 this->variables
.push_tail(storage
);
1352 /* The variable_storage constructor allocates slots based on the size
1353 * of the type. However, this had better match the number of state
1354 * elements that we're going to copy into the new temporary.
1356 assert((int) ir
->get_num_state_slots() == type_size(ir
->type
));
1358 dst
= st_dst_reg(get_temp(ir
->type
));
1360 storage
= new(mem_ctx
) variable_storage(ir
, dst
.file
, dst
.index
,
1363 this->variables
.push_tail(storage
);
1367 for (unsigned int i
= 0; i
< ir
->get_num_state_slots(); i
++) {
1368 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
1369 (gl_state_index
*)slots
[i
].tokens
);
1371 if (storage
->file
== PROGRAM_STATE_VAR
) {
1372 if (storage
->index
== -1) {
1373 storage
->index
= index
;
1375 assert(index
== storage
->index
+ (int)i
);
1378 /* We use GLSL_TYPE_FLOAT here regardless of the actual type of
1379 * the data being moved since MOV does not care about the type of
1380 * data it is moving, and we don't want to declare registers with
1381 * array or struct types.
1383 st_src_reg
src(PROGRAM_STATE_VAR
, index
, GLSL_TYPE_FLOAT
);
1384 src
.swizzle
= slots
[i
].swizzle
;
1385 emit_asm(ir
, TGSI_OPCODE_MOV
, dst
, src
);
1386 /* even a float takes up a whole vec4 reg in a struct/array. */
1391 if (storage
->file
== PROGRAM_TEMPORARY
&&
1392 dst
.index
!= storage
->index
+ (int) ir
->get_num_state_slots()) {
1393 fail_link(this->shader_program
,
1394 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
1395 ir
->name
, dst
.index
- storage
->index
,
1396 type_size(ir
->type
));
1402 glsl_to_tgsi_visitor::visit(ir_loop
*ir
)
1404 emit_asm(NULL
, TGSI_OPCODE_BGNLOOP
);
1406 visit_exec_list(&ir
->body_instructions
, this);
1408 emit_asm(NULL
, TGSI_OPCODE_ENDLOOP
);
1412 glsl_to_tgsi_visitor::visit(ir_loop_jump
*ir
)
1415 case ir_loop_jump::jump_break
:
1416 emit_asm(NULL
, TGSI_OPCODE_BRK
);
1418 case ir_loop_jump::jump_continue
:
1419 emit_asm(NULL
, TGSI_OPCODE_CONT
);
1426 glsl_to_tgsi_visitor::visit(ir_function_signature
*ir
)
1433 glsl_to_tgsi_visitor::visit(ir_function
*ir
)
1435 /* Ignore function bodies other than main() -- we shouldn't see calls to
1436 * them since they should all be inlined before we get to glsl_to_tgsi.
1438 if (strcmp(ir
->name
, "main") == 0) {
1439 const ir_function_signature
*sig
;
1442 sig
= ir
->matching_signature(NULL
, &empty
, false);
1446 foreach_in_list(ir_instruction
, ir
, &sig
->body
) {
1453 glsl_to_tgsi_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
1455 int nonmul_operand
= 1 - mul_operand
;
1457 st_dst_reg result_dst
;
1459 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
1460 if (!expr
|| expr
->operation
!= ir_binop_mul
)
1463 expr
->operands
[0]->accept(this);
1465 expr
->operands
[1]->accept(this);
1467 ir
->operands
[nonmul_operand
]->accept(this);
1470 this->result
= get_temp(ir
->type
);
1471 result_dst
= st_dst_reg(this->result
);
1472 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1473 emit_asm(ir
, TGSI_OPCODE_MAD
, result_dst
, a
, b
, c
);
1479 * Emit MAD(a, -b, a) instead of AND(a, NOT(b))
1481 * The logic values are 1.0 for true and 0.0 for false. Logical-and is
1482 * implemented using multiplication, and logical-or is implemented using
1483 * addition. Logical-not can be implemented as (true - x), or (1.0 - x).
1484 * As result, the logical expression (a & !b) can be rewritten as:
1488 * - (a * 1) - (a * b)
1492 * This final expression can be implemented as a single MAD(a, -b, a)
1496 glsl_to_tgsi_visitor::try_emit_mad_for_and_not(ir_expression
*ir
, int try_operand
)
1498 const int other_operand
= 1 - try_operand
;
1501 ir_expression
*expr
= ir
->operands
[try_operand
]->as_expression();
1502 if (!expr
|| expr
->operation
!= ir_unop_logic_not
)
1505 ir
->operands
[other_operand
]->accept(this);
1507 expr
->operands
[0]->accept(this);
1510 b
.negate
= ~b
.negate
;
1512 this->result
= get_temp(ir
->type
);
1513 emit_asm(ir
, TGSI_OPCODE_MAD
, st_dst_reg(this->result
), a
, b
, a
);
1519 glsl_to_tgsi_visitor::reladdr_to_temp(ir_instruction
*ir
,
1520 st_src_reg
*reg
, int *num_reladdr
)
1522 if (!reg
->reladdr
&& !reg
->reladdr2
)
1525 if (reg
->reladdr
) emit_arl(ir
, address_reg
, *reg
->reladdr
);
1526 if (reg
->reladdr2
) emit_arl(ir
, address_reg2
, *reg
->reladdr2
);
1528 if (*num_reladdr
!= 1) {
1529 st_src_reg temp
= get_temp(reg
->type
== GLSL_TYPE_DOUBLE
? glsl_type::dvec4_type
: glsl_type::vec4_type
);
1531 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), *reg
);
1539 glsl_to_tgsi_visitor::visit(ir_expression
*ir
)
1541 st_src_reg op
[ARRAY_SIZE(ir
->operands
)];
1543 /* Quick peephole: Emit MAD(a, b, c) instead of ADD(MUL(a, b), c)
1545 if (ir
->operation
== ir_binop_add
) {
1546 if (try_emit_mad(ir
, 1))
1548 if (try_emit_mad(ir
, 0))
1552 /* Quick peephole: Emit OPCODE_MAD(-a, -b, a) instead of AND(a, NOT(b))
1554 if (!native_integers
&& ir
->operation
== ir_binop_logic_and
) {
1555 if (try_emit_mad_for_and_not(ir
, 1))
1557 if (try_emit_mad_for_and_not(ir
, 0))
1561 if (ir
->operation
== ir_quadop_vector
)
1562 assert(!"ir_quadop_vector should have been lowered");
1564 for (unsigned int operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1565 this->result
.file
= PROGRAM_UNDEFINED
;
1566 ir
->operands
[operand
]->accept(this);
1567 if (this->result
.file
== PROGRAM_UNDEFINED
) {
1568 printf("Failed to get tree for expression operand:\n");
1569 ir
->operands
[operand
]->print();
1573 op
[operand
] = this->result
;
1575 /* Matrix expression operands should have been broken down to vector
1576 * operations already.
1578 assert(!ir
->operands
[operand
]->type
->is_matrix());
1581 visit_expression(ir
, op
);
1584 /* The non-recursive part of the expression visitor lives in a separate
1585 * function and should be prevented from being inlined, to avoid a stack
1586 * explosion when deeply nested expressions are visited.
1589 glsl_to_tgsi_visitor::visit_expression(ir_expression
* ir
, st_src_reg
*op
)
1591 st_src_reg result_src
;
1592 st_dst_reg result_dst
;
1594 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1595 if (ir
->operands
[1]) {
1596 vector_elements
= MAX2(vector_elements
,
1597 ir
->operands
[1]->type
->vector_elements
);
1600 this->result
.file
= PROGRAM_UNDEFINED
;
1602 /* Storage for our result. Ideally for an assignment we'd be using
1603 * the actual storage for the result here, instead.
1605 result_src
= get_temp(ir
->type
);
1606 /* convenience for the emit functions below. */
1607 result_dst
= st_dst_reg(result_src
);
1608 /* Limit writes to the channels that will be used by result_src later.
1609 * This does limit this temp's use as a temporary for multi-instruction
1612 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1614 switch (ir
->operation
) {
1615 case ir_unop_logic_not
:
1616 if (result_dst
.type
!= GLSL_TYPE_FLOAT
)
1617 emit_asm(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1619 /* Previously 'SEQ dst, src, 0.0' was used for this. However, many
1620 * older GPUs implement SEQ using multiple instructions (i915 uses two
1621 * SGE instructions and a MUL instruction). Since our logic values are
1622 * 0.0 and 1.0, 1-x also implements !x.
1624 op
[0].negate
= ~op
[0].negate
;
1625 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], st_src_reg_for_float(1.0));
1629 if (result_dst
.type
== GLSL_TYPE_INT64
|| result_dst
.type
== GLSL_TYPE_UINT64
)
1630 emit_asm(ir
, TGSI_OPCODE_I64NEG
, result_dst
, op
[0]);
1631 else if (result_dst
.type
== GLSL_TYPE_INT
|| result_dst
.type
== GLSL_TYPE_UINT
)
1632 emit_asm(ir
, TGSI_OPCODE_INEG
, result_dst
, op
[0]);
1633 else if (result_dst
.type
== GLSL_TYPE_DOUBLE
)
1634 emit_asm(ir
, TGSI_OPCODE_DNEG
, result_dst
, op
[0]);
1636 op
[0].negate
= ~op
[0].negate
;
1640 case ir_unop_subroutine_to_int
:
1641 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
1644 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1645 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0].get_abs());
1646 else if (result_dst
.type
== GLSL_TYPE_DOUBLE
)
1647 emit_asm(ir
, TGSI_OPCODE_DABS
, result_dst
, op
[0]);
1648 else if (result_dst
.type
== GLSL_TYPE_INT64
|| result_dst
.type
== GLSL_TYPE_UINT64
)
1649 emit_asm(ir
, TGSI_OPCODE_I64ABS
, result_dst
, op
[0]);
1651 emit_asm(ir
, TGSI_OPCODE_IABS
, result_dst
, op
[0]);
1654 emit_asm(ir
, TGSI_OPCODE_SSG
, result_dst
, op
[0]);
1657 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, op
[0]);
1661 emit_scalar(ir
, TGSI_OPCODE_EX2
, result_dst
, op
[0]);
1665 assert(!"not reached: should be handled by ir_explog_to_explog2");
1668 emit_scalar(ir
, TGSI_OPCODE_LG2
, result_dst
, op
[0]);
1671 emit_scalar(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1674 emit_scalar(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1676 case ir_unop_saturate
: {
1677 glsl_to_tgsi_instruction
*inst
;
1678 inst
= emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
1679 inst
->saturate
= true;
1684 case ir_unop_dFdx_coarse
:
1685 emit_asm(ir
, TGSI_OPCODE_DDX
, result_dst
, op
[0]);
1687 case ir_unop_dFdx_fine
:
1688 emit_asm(ir
, TGSI_OPCODE_DDX_FINE
, result_dst
, op
[0]);
1691 case ir_unop_dFdy_coarse
:
1692 case ir_unop_dFdy_fine
:
1694 /* The X component contains 1 or -1 depending on whether the framebuffer
1695 * is a FBO or the window system buffer, respectively.
1696 * It is then multiplied with the source operand of DDY.
1698 static const gl_state_index transform_y_state
[STATE_LENGTH
]
1699 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
};
1701 unsigned transform_y_index
=
1702 _mesa_add_state_reference(this->prog
->Parameters
,
1705 st_src_reg transform_y
= st_src_reg(PROGRAM_STATE_VAR
,
1707 glsl_type::vec4_type
);
1708 transform_y
.swizzle
= SWIZZLE_XXXX
;
1710 st_src_reg temp
= get_temp(glsl_type::vec4_type
);
1712 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(temp
), transform_y
, op
[0]);
1713 emit_asm(ir
, ir
->operation
== ir_unop_dFdy_fine
?
1714 TGSI_OPCODE_DDY_FINE
: TGSI_OPCODE_DDY
, result_dst
, temp
);
1718 case ir_unop_frexp_sig
:
1719 emit_asm(ir
, TGSI_OPCODE_DFRACEXP
, result_dst
, undef_dst
, op
[0]);
1722 case ir_unop_frexp_exp
:
1723 emit_asm(ir
, TGSI_OPCODE_DFRACEXP
, undef_dst
, result_dst
, op
[0]);
1726 case ir_unop_noise
: {
1727 /* At some point, a motivated person could add a better
1728 * implementation of noise. Currently not even the nvidia
1729 * binary drivers do anything more than this. In any case, the
1730 * place to do this is in the GL state tracker, not the poor
1733 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, st_src_reg_for_float(0.5));
1738 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1741 op
[1].negate
= ~op
[1].negate
;
1742 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1746 emit_asm(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1749 emit_asm(ir
, TGSI_OPCODE_DIV
, result_dst
, op
[0], op
[1]);
1752 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1753 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1755 emit_asm(ir
, TGSI_OPCODE_MOD
, result_dst
, op
[0], op
[1]);
1759 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[0], op
[1]);
1761 case ir_binop_greater
:
1762 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[1], op
[0]);
1764 case ir_binop_lequal
:
1765 emit_asm(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[1], op
[0]);
1767 case ir_binop_gequal
:
1768 emit_asm(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[0], op
[1]);
1770 case ir_binop_equal
:
1771 emit_asm(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1773 case ir_binop_nequal
:
1774 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1776 case ir_binop_all_equal
:
1777 /* "==" operator producing a scalar boolean. */
1778 if (ir
->operands
[0]->type
->is_vector() ||
1779 ir
->operands
[1]->type
->is_vector()) {
1780 st_src_reg temp
= get_temp(native_integers
?
1781 glsl_type::uvec4_type
:
1782 glsl_type::vec4_type
);
1784 if (native_integers
) {
1785 st_dst_reg temp_dst
= st_dst_reg(temp
);
1786 st_src_reg temp1
= st_src_reg(temp
), temp2
= st_src_reg(temp
);
1788 if (ir
->operands
[0]->type
->is_boolean() &&
1789 ir
->operands
[1]->as_constant() &&
1790 ir
->operands
[1]->as_constant()->is_one()) {
1791 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), op
[0]);
1793 emit_asm(ir
, TGSI_OPCODE_SEQ
, st_dst_reg(temp
), op
[0], op
[1]);
1796 /* Emit 1-3 AND operations to combine the SEQ results. */
1797 switch (ir
->operands
[0]->type
->vector_elements
) {
1801 temp_dst
.writemask
= WRITEMASK_Y
;
1802 temp1
.swizzle
= SWIZZLE_YYYY
;
1803 temp2
.swizzle
= SWIZZLE_ZZZZ
;
1804 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1807 temp_dst
.writemask
= WRITEMASK_X
;
1808 temp1
.swizzle
= SWIZZLE_XXXX
;
1809 temp2
.swizzle
= SWIZZLE_YYYY
;
1810 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1811 temp_dst
.writemask
= WRITEMASK_Y
;
1812 temp1
.swizzle
= SWIZZLE_ZZZZ
;
1813 temp2
.swizzle
= SWIZZLE_WWWW
;
1814 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1817 temp1
.swizzle
= SWIZZLE_XXXX
;
1818 temp2
.swizzle
= SWIZZLE_YYYY
;
1819 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, temp1
, temp2
);
1821 emit_asm(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1823 /* After the dot-product, the value will be an integer on the
1824 * range [0,4]. Zero becomes 1.0, and positive values become zero.
1826 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1828 /* Negating the result of the dot-product gives values on the range
1829 * [-4, 0]. Zero becomes 1.0, and negative values become zero.
1830 * This is achieved using SGE.
1832 st_src_reg sge_src
= result_src
;
1833 sge_src
.negate
= ~sge_src
.negate
;
1834 emit_asm(ir
, TGSI_OPCODE_SGE
, result_dst
, sge_src
, st_src_reg_for_float(0.0));
1837 emit_asm(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1840 case ir_binop_any_nequal
:
1841 /* "!=" operator producing a scalar boolean. */
1842 if (ir
->operands
[0]->type
->is_vector() ||
1843 ir
->operands
[1]->type
->is_vector()) {
1844 st_src_reg temp
= get_temp(native_integers
?
1845 glsl_type::uvec4_type
:
1846 glsl_type::vec4_type
);
1847 if (ir
->operands
[0]->type
->is_boolean() &&
1848 ir
->operands
[1]->as_constant() &&
1849 ir
->operands
[1]->as_constant()->is_zero()) {
1850 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), op
[0]);
1852 emit_asm(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1855 if (native_integers
) {
1856 st_dst_reg temp_dst
= st_dst_reg(temp
);
1857 st_src_reg temp1
= st_src_reg(temp
), temp2
= st_src_reg(temp
);
1859 /* Emit 1-3 OR operations to combine the SNE results. */
1860 switch (ir
->operands
[0]->type
->vector_elements
) {
1864 temp_dst
.writemask
= WRITEMASK_Y
;
1865 temp1
.swizzle
= SWIZZLE_YYYY
;
1866 temp2
.swizzle
= SWIZZLE_ZZZZ
;
1867 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1870 temp_dst
.writemask
= WRITEMASK_X
;
1871 temp1
.swizzle
= SWIZZLE_XXXX
;
1872 temp2
.swizzle
= SWIZZLE_YYYY
;
1873 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1874 temp_dst
.writemask
= WRITEMASK_Y
;
1875 temp1
.swizzle
= SWIZZLE_ZZZZ
;
1876 temp2
.swizzle
= SWIZZLE_WWWW
;
1877 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1880 temp1
.swizzle
= SWIZZLE_XXXX
;
1881 temp2
.swizzle
= SWIZZLE_YYYY
;
1882 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, temp1
, temp2
);
1884 /* After the dot-product, the value will be an integer on the
1885 * range [0,4]. Zero stays zero, and positive values become 1.0.
1887 glsl_to_tgsi_instruction
*const dp
=
1888 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1889 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1890 /* The clamping to [0,1] can be done for free in the fragment
1891 * shader with a saturate.
1893 dp
->saturate
= true;
1895 /* Negating the result of the dot-product gives values on the range
1896 * [-4, 0]. Zero stays zero, and negative values become 1.0. This
1897 * achieved using SLT.
1899 st_src_reg slt_src
= result_src
;
1900 slt_src
.negate
= ~slt_src
.negate
;
1901 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1905 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1909 case ir_binop_logic_xor
:
1910 if (native_integers
)
1911 emit_asm(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0], op
[1]);
1913 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1916 case ir_binop_logic_or
: {
1917 if (native_integers
) {
1918 /* If integers are used as booleans, we can use an actual "or"
1921 assert(native_integers
);
1922 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0], op
[1]);
1924 /* After the addition, the value will be an integer on the
1925 * range [0,2]. Zero stays zero, and positive values become 1.0.
1927 glsl_to_tgsi_instruction
*add
=
1928 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1929 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1930 /* The clamping to [0,1] can be done for free in the fragment
1931 * shader with a saturate if floats are being used as boolean values.
1933 add
->saturate
= true;
1935 /* Negating the result of the addition gives values on the range
1936 * [-2, 0]. Zero stays zero, and negative values become 1.0. This
1937 * is achieved using SLT.
1939 st_src_reg slt_src
= result_src
;
1940 slt_src
.negate
= ~slt_src
.negate
;
1941 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1947 case ir_binop_logic_and
:
1948 /* If native integers are disabled, the bool args are stored as float 0.0
1949 * or 1.0, so "mul" gives us "and". If they're enabled, just use the
1950 * actual AND opcode.
1952 if (native_integers
)
1953 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], op
[1]);
1955 emit_asm(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1959 assert(ir
->operands
[0]->type
->is_vector());
1960 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1961 emit_dp(ir
, result_dst
, op
[0], op
[1],
1962 ir
->operands
[0]->type
->vector_elements
);
1967 emit_scalar(ir
, TGSI_OPCODE_SQRT
, result_dst
, op
[0]);
1969 /* This is the only instruction sequence that makes the game "Risen"
1970 * render correctly. ABS is not required for the game, but since GLSL
1971 * declares negative values as "undefined", allowing us to do whatever
1972 * we want, I choose to use ABS to match DX9 and pre-GLSL RSQ
1975 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0].get_abs());
1976 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, result_src
);
1980 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1983 if (native_integers
) {
1984 emit_asm(ir
, TGSI_OPCODE_I2F
, result_dst
, op
[0]);
1987 /* fallthrough to next case otherwise */
1989 if (native_integers
) {
1990 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], st_src_reg_for_float(1.0));
1993 /* fallthrough to next case otherwise */
1996 case ir_unop_i642u64
:
1997 case ir_unop_u642i64
:
1998 /* Converting between signed and unsigned integers is a no-op. */
2000 result_src
.type
= result_dst
.type
;
2003 if (native_integers
) {
2004 /* Booleans are stored as integers using ~0 for true and 0 for false.
2005 * GLSL requires that int(bool) return 1 for true and 0 for false.
2006 * This conversion is done with AND, but it could be done with NEG.
2008 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], st_src_reg_for_int(1));
2010 /* Booleans and integers are both stored as floats when native
2011 * integers are disabled.
2017 if (native_integers
)
2018 emit_asm(ir
, TGSI_OPCODE_F2I
, result_dst
, op
[0]);
2020 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
2023 if (native_integers
)
2024 emit_asm(ir
, TGSI_OPCODE_F2U
, result_dst
, op
[0]);
2026 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
2028 case ir_unop_bitcast_f2i
:
2029 case ir_unop_bitcast_f2u
:
2030 /* Make sure we don't propagate the negate modifier to integer opcodes. */
2031 if (op
[0].negate
|| op
[0].abs
)
2032 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
2035 result_src
.type
= ir
->operation
== ir_unop_bitcast_f2i
? GLSL_TYPE_INT
:
2038 case ir_unop_bitcast_i2f
:
2039 case ir_unop_bitcast_u2f
:
2041 result_src
.type
= GLSL_TYPE_FLOAT
;
2044 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_float(0.0));
2047 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_double(0.0));
2050 if (native_integers
)
2051 emit_asm(ir
, TGSI_OPCODE_USNE
, result_dst
, op
[0], st_src_reg_for_int(0));
2053 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_float(0.0));
2055 case ir_unop_bitcast_u642d
:
2056 case ir_unop_bitcast_i642d
:
2058 result_src
.type
= GLSL_TYPE_DOUBLE
;
2060 case ir_unop_bitcast_d2i64
:
2062 result_src
.type
= GLSL_TYPE_INT64
;
2064 case ir_unop_bitcast_d2u64
:
2066 result_src
.type
= GLSL_TYPE_UINT64
;
2069 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
2072 emit_asm(ir
, TGSI_OPCODE_CEIL
, result_dst
, op
[0]);
2075 emit_asm(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
2077 case ir_unop_round_even
:
2078 emit_asm(ir
, TGSI_OPCODE_ROUND
, result_dst
, op
[0]);
2081 emit_asm(ir
, TGSI_OPCODE_FRC
, result_dst
, op
[0]);
2085 emit_asm(ir
, TGSI_OPCODE_MIN
, result_dst
, op
[0], op
[1]);
2088 emit_asm(ir
, TGSI_OPCODE_MAX
, result_dst
, op
[0], op
[1]);
2091 emit_scalar(ir
, TGSI_OPCODE_POW
, result_dst
, op
[0], op
[1]);
2094 case ir_unop_bit_not
:
2095 if (native_integers
) {
2096 emit_asm(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
2100 if (native_integers
) {
2101 emit_asm(ir
, TGSI_OPCODE_U2F
, result_dst
, op
[0]);
2104 case ir_binop_lshift
:
2105 case ir_binop_rshift
:
2106 if (native_integers
) {
2107 unsigned opcode
= ir
->operation
== ir_binop_lshift
? TGSI_OPCODE_SHL
2111 if (glsl_base_type_is_64bit(op
[0].type
)) {
2112 /* GLSL shift operations have 32-bit shift counts, but TGSI uses
2115 count
= get_temp(glsl_type::u64vec(ir
->operands
[1]->type
->components()));
2116 emit_asm(ir
, TGSI_OPCODE_U2I64
, st_dst_reg(count
), op
[1]);
2121 emit_asm(ir
, opcode
, result_dst
, op
[0], count
);
2124 case ir_binop_bit_and
:
2125 if (native_integers
) {
2126 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], op
[1]);
2129 case ir_binop_bit_xor
:
2130 if (native_integers
) {
2131 emit_asm(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0], op
[1]);
2134 case ir_binop_bit_or
:
2135 if (native_integers
) {
2136 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0], op
[1]);
2140 assert(!"GLSL 1.30 features unsupported");
2143 case ir_binop_ubo_load
: {
2144 ir_constant
*const_uniform_block
= ir
->operands
[0]->as_constant();
2145 ir_constant
*const_offset_ir
= ir
->operands
[1]->as_constant();
2146 unsigned const_offset
= const_offset_ir
? const_offset_ir
->value
.u
[0] : 0;
2147 unsigned const_block
= const_uniform_block
? const_uniform_block
->value
.u
[0] + 1 : 0;
2148 st_src_reg index_reg
= get_temp(glsl_type::uint_type
);
2151 cbuf
.type
= ir
->type
->base_type
;
2152 cbuf
.file
= PROGRAM_CONSTANT
;
2154 cbuf
.reladdr
= NULL
;
2158 assert(ir
->type
->is_vector() || ir
->type
->is_scalar());
2160 if (const_offset_ir
) {
2161 /* Constant index into constant buffer */
2162 cbuf
.reladdr
= NULL
;
2163 cbuf
.index
= const_offset
/ 16;
2166 ir_expression
*offset_expr
= ir
->operands
[1]->as_expression();
2167 st_src_reg offset
= op
[1];
2169 /* The OpenGL spec is written in such a way that accesses with
2170 * non-constant offset are almost always vec4-aligned. The only
2171 * exception to this are members of structs in arrays of structs:
2172 * each struct in an array of structs is at least vec4-aligned,
2173 * but single-element and [ui]vec2 members of the struct may be at
2174 * an offset that is not a multiple of 16 bytes.
2176 * Here, we extract that offset, relying on previous passes to always
2177 * generate offset expressions of the form (+ expr constant_offset).
2179 * Note that the std430 layout, which allows more cases of alignment
2180 * less than vec4 in arrays, is not supported for uniform blocks, so
2181 * we do not have to deal with it here.
2183 if (offset_expr
&& offset_expr
->operation
== ir_binop_add
) {
2184 const_offset_ir
= offset_expr
->operands
[1]->as_constant();
2185 if (const_offset_ir
) {
2186 const_offset
= const_offset_ir
->value
.u
[0];
2187 cbuf
.index
= const_offset
/ 16;
2188 offset_expr
->operands
[0]->accept(this);
2189 offset
= this->result
;
2193 /* Relative/variable index into constant buffer */
2194 emit_asm(ir
, TGSI_OPCODE_USHR
, st_dst_reg(index_reg
), offset
,
2195 st_src_reg_for_int(4));
2196 cbuf
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2197 memcpy(cbuf
.reladdr
, &index_reg
, sizeof(index_reg
));
2200 if (const_uniform_block
) {
2201 /* Constant constant buffer */
2202 cbuf
.reladdr2
= NULL
;
2203 cbuf
.index2D
= const_block
;
2204 cbuf
.has_index2
= true;
2207 /* Relative/variable constant buffer */
2208 cbuf
.reladdr2
= ralloc(mem_ctx
, st_src_reg
);
2210 memcpy(cbuf
.reladdr2
, &op
[0], sizeof(st_src_reg
));
2211 cbuf
.has_index2
= true;
2214 cbuf
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
2215 if (glsl_base_type_is_64bit(cbuf
.type
))
2216 cbuf
.swizzle
+= MAKE_SWIZZLE4(const_offset
% 16 / 8,
2217 const_offset
% 16 / 8,
2218 const_offset
% 16 / 8,
2219 const_offset
% 16 / 8);
2221 cbuf
.swizzle
+= MAKE_SWIZZLE4(const_offset
% 16 / 4,
2222 const_offset
% 16 / 4,
2223 const_offset
% 16 / 4,
2224 const_offset
% 16 / 4);
2226 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
2227 emit_asm(ir
, TGSI_OPCODE_USNE
, result_dst
, cbuf
, st_src_reg_for_int(0));
2229 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, cbuf
);
2234 /* note: we have to reorder the three args here */
2235 emit_asm(ir
, TGSI_OPCODE_LRP
, result_dst
, op
[2], op
[1], op
[0]);
2238 if (this->ctx
->Const
.NativeIntegers
)
2239 emit_asm(ir
, TGSI_OPCODE_UCMP
, result_dst
, op
[0], op
[1], op
[2]);
2241 op
[0].negate
= ~op
[0].negate
;
2242 emit_asm(ir
, TGSI_OPCODE_CMP
, result_dst
, op
[0], op
[1], op
[2]);
2245 case ir_triop_bitfield_extract
:
2246 emit_asm(ir
, TGSI_OPCODE_IBFE
, result_dst
, op
[0], op
[1], op
[2]);
2248 case ir_quadop_bitfield_insert
:
2249 emit_asm(ir
, TGSI_OPCODE_BFI
, result_dst
, op
[0], op
[1], op
[2], op
[3]);
2251 case ir_unop_bitfield_reverse
:
2252 emit_asm(ir
, TGSI_OPCODE_BREV
, result_dst
, op
[0]);
2254 case ir_unop_bit_count
:
2255 emit_asm(ir
, TGSI_OPCODE_POPC
, result_dst
, op
[0]);
2257 case ir_unop_find_msb
:
2258 emit_asm(ir
, TGSI_OPCODE_IMSB
, result_dst
, op
[0]);
2260 case ir_unop_find_lsb
:
2261 emit_asm(ir
, TGSI_OPCODE_LSB
, result_dst
, op
[0]);
2263 case ir_binop_imul_high
:
2264 emit_asm(ir
, TGSI_OPCODE_IMUL_HI
, result_dst
, op
[0], op
[1]);
2267 /* In theory, MAD is incorrect here. */
2269 emit_asm(ir
, TGSI_OPCODE_FMA
, result_dst
, op
[0], op
[1], op
[2]);
2271 emit_asm(ir
, TGSI_OPCODE_MAD
, result_dst
, op
[0], op
[1], op
[2]);
2273 case ir_unop_interpolate_at_centroid
:
2274 emit_asm(ir
, TGSI_OPCODE_INTERP_CENTROID
, result_dst
, op
[0]);
2276 case ir_binop_interpolate_at_offset
: {
2277 /* The y coordinate needs to be flipped for the default fb */
2278 static const gl_state_index transform_y_state
[STATE_LENGTH
]
2279 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
};
2281 unsigned transform_y_index
=
2282 _mesa_add_state_reference(this->prog
->Parameters
,
2285 st_src_reg transform_y
= st_src_reg(PROGRAM_STATE_VAR
,
2287 glsl_type::vec4_type
);
2288 transform_y
.swizzle
= SWIZZLE_XXXX
;
2290 st_src_reg temp
= get_temp(glsl_type::vec2_type
);
2291 st_dst_reg temp_dst
= st_dst_reg(temp
);
2293 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[1]);
2294 temp_dst
.writemask
= WRITEMASK_Y
;
2295 emit_asm(ir
, TGSI_OPCODE_MUL
, temp_dst
, transform_y
, op
[1]);
2296 emit_asm(ir
, TGSI_OPCODE_INTERP_OFFSET
, result_dst
, op
[0], temp
);
2299 case ir_binop_interpolate_at_sample
:
2300 emit_asm(ir
, TGSI_OPCODE_INTERP_SAMPLE
, result_dst
, op
[0], op
[1]);
2304 emit_asm(ir
, TGSI_OPCODE_D2F
, result_dst
, op
[0]);
2307 emit_asm(ir
, TGSI_OPCODE_F2D
, result_dst
, op
[0]);
2310 emit_asm(ir
, TGSI_OPCODE_D2I
, result_dst
, op
[0]);
2313 emit_asm(ir
, TGSI_OPCODE_I2D
, result_dst
, op
[0]);
2316 emit_asm(ir
, TGSI_OPCODE_D2U
, result_dst
, op
[0]);
2319 emit_asm(ir
, TGSI_OPCODE_U2D
, result_dst
, op
[0]);
2321 case ir_unop_unpack_double_2x32
:
2322 case ir_unop_pack_double_2x32
:
2323 case ir_unop_unpack_int_2x32
:
2324 case ir_unop_pack_int_2x32
:
2325 case ir_unop_unpack_uint_2x32
:
2326 case ir_unop_pack_uint_2x32
:
2327 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
2330 case ir_binop_ldexp
:
2331 if (ir
->operands
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
) {
2332 emit_asm(ir
, TGSI_OPCODE_DLDEXP
, result_dst
, op
[0], op
[1]);
2334 assert(!"Invalid ldexp for non-double opcode in glsl_to_tgsi_visitor::visit()");
2338 case ir_unop_pack_half_2x16
:
2339 emit_asm(ir
, TGSI_OPCODE_PK2H
, result_dst
, op
[0]);
2341 case ir_unop_unpack_half_2x16
:
2342 emit_asm(ir
, TGSI_OPCODE_UP2H
, result_dst
, op
[0]);
2345 case ir_unop_get_buffer_size
: {
2346 ir_constant
*const_offset
= ir
->operands
[0]->as_constant();
2349 ctx
->Const
.Program
[shader
->Stage
].MaxAtomicBuffers
+
2350 (const_offset
? const_offset
->value
.u
[0] : 0),
2352 if (!const_offset
) {
2353 buffer
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2354 *buffer
.reladdr
= op
[0];
2355 emit_arl(ir
, sampler_reladdr
, op
[0]);
2357 emit_asm(ir
, TGSI_OPCODE_RESQ
, result_dst
)->resource
= buffer
;
2361 case ir_unop_vote_any
:
2362 emit_asm(ir
, TGSI_OPCODE_VOTE_ANY
, result_dst
, op
[0]);
2364 case ir_unop_vote_all
:
2365 emit_asm(ir
, TGSI_OPCODE_VOTE_ALL
, result_dst
, op
[0]);
2367 case ir_unop_vote_eq
:
2368 emit_asm(ir
, TGSI_OPCODE_VOTE_EQ
, result_dst
, op
[0]);
2372 case ir_unop_b2i64
: {
2373 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2374 st_dst_reg temp_dst
= st_dst_reg(temp
);
2375 unsigned orig_swz
= op
[0].swizzle
;
2377 * To convert unsigned to 64-bit:
2378 * zero Y channel, copy X channel.
2380 temp_dst
.writemask
= WRITEMASK_Y
;
2381 if (vector_elements
> 1)
2382 temp_dst
.writemask
|= WRITEMASK_W
;
2383 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, st_src_reg_for_int(0));
2384 temp_dst
.writemask
= WRITEMASK_X
;
2385 if (vector_elements
> 1)
2386 temp_dst
.writemask
|= WRITEMASK_Z
;
2387 op
[0].swizzle
= MAKE_SWIZZLE4(GET_SWZ(orig_swz
, 0), GET_SWZ(orig_swz
, 0),
2388 GET_SWZ(orig_swz
, 1), GET_SWZ(orig_swz
, 1));
2389 if (ir
->operation
== ir_unop_u2i64
|| ir
->operation
== ir_unop_u2u64
)
2390 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2392 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, op
[0], st_src_reg_for_int(1));
2394 result_src
.type
= GLSL_TYPE_UINT64
;
2395 if (vector_elements
> 2) {
2396 /* Subtle: We rely on the fact that get_temp here returns the next
2397 * TGSI temporary register directly after the temp register used for
2398 * the first two components, so that the result gets picked up
2401 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2402 st_dst_reg temp_dst
= st_dst_reg(temp
);
2403 temp_dst
.writemask
= WRITEMASK_Y
;
2404 if (vector_elements
> 3)
2405 temp_dst
.writemask
|= WRITEMASK_W
;
2406 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, st_src_reg_for_int(0));
2408 temp_dst
.writemask
= WRITEMASK_X
;
2409 if (vector_elements
> 3)
2410 temp_dst
.writemask
|= WRITEMASK_Z
;
2411 op
[0].swizzle
= MAKE_SWIZZLE4(GET_SWZ(orig_swz
, 2), GET_SWZ(orig_swz
, 2),
2412 GET_SWZ(orig_swz
, 3), GET_SWZ(orig_swz
, 3));
2413 if (ir
->operation
== ir_unop_u2i64
|| ir
->operation
== ir_unop_u2u64
)
2414 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2416 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, op
[0], st_src_reg_for_int(1));
2423 case ir_unop_i642u
: {
2424 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2425 st_dst_reg temp_dst
= st_dst_reg(temp
);
2426 unsigned orig_swz
= op
[0].swizzle
;
2427 unsigned orig_idx
= op
[0].index
;
2429 temp_dst
.writemask
= WRITEMASK_X
;
2431 for (el
= 0; el
< vector_elements
; el
++) {
2432 unsigned swz
= GET_SWZ(orig_swz
, el
);
2434 op
[0].swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_Z
, SWIZZLE_Z
, SWIZZLE_Z
);
2436 op
[0].swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
);
2438 op
[0].index
= orig_idx
+ 1;
2439 op
[0].type
= GLSL_TYPE_UINT
;
2440 temp_dst
.writemask
= WRITEMASK_X
<< el
;
2441 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2444 if (ir
->operation
== ir_unop_u642u
|| ir
->operation
== ir_unop_i642u
)
2445 result_src
.type
= GLSL_TYPE_UINT
;
2447 result_src
.type
= GLSL_TYPE_INT
;
2451 emit_asm(ir
, TGSI_OPCODE_U64SNE
, result_dst
, op
[0], st_src_reg_for_int(0));
2454 emit_asm(ir
, TGSI_OPCODE_I642F
, result_dst
, op
[0]);
2457 emit_asm(ir
, TGSI_OPCODE_U642F
, result_dst
, op
[0]);
2460 emit_asm(ir
, TGSI_OPCODE_I642D
, result_dst
, op
[0]);
2463 emit_asm(ir
, TGSI_OPCODE_U642D
, result_dst
, op
[0]);
2466 emit_asm(ir
, TGSI_OPCODE_I2I64
, result_dst
, op
[0]);
2469 emit_asm(ir
, TGSI_OPCODE_F2I64
, result_dst
, op
[0]);
2472 emit_asm(ir
, TGSI_OPCODE_D2I64
, result_dst
, op
[0]);
2475 emit_asm(ir
, TGSI_OPCODE_I2I64
, result_dst
, op
[0]);
2478 emit_asm(ir
, TGSI_OPCODE_F2U64
, result_dst
, op
[0]);
2481 emit_asm(ir
, TGSI_OPCODE_D2U64
, result_dst
, op
[0]);
2483 /* these might be needed */
2484 case ir_unop_pack_snorm_2x16
:
2485 case ir_unop_pack_unorm_2x16
:
2486 case ir_unop_pack_snorm_4x8
:
2487 case ir_unop_pack_unorm_4x8
:
2489 case ir_unop_unpack_snorm_2x16
:
2490 case ir_unop_unpack_unorm_2x16
:
2491 case ir_unop_unpack_snorm_4x8
:
2492 case ir_unop_unpack_unorm_4x8
:
2494 case ir_quadop_vector
:
2495 case ir_binop_vector_extract
:
2496 case ir_triop_vector_insert
:
2497 case ir_binop_carry
:
2498 case ir_binop_borrow
:
2499 case ir_unop_ssbo_unsized_array_length
:
2500 /* This operation is not supported, or should have already been handled.
2502 assert(!"Invalid ir opcode in glsl_to_tgsi_visitor::visit()");
2506 this->result
= result_src
;
2511 glsl_to_tgsi_visitor::visit(ir_swizzle
*ir
)
2517 /* Note that this is only swizzles in expressions, not those on the left
2518 * hand side of an assignment, which do write masking. See ir_assignment
2522 ir
->val
->accept(this);
2524 assert(src
.file
!= PROGRAM_UNDEFINED
);
2525 assert(ir
->type
->vector_elements
> 0);
2527 for (i
= 0; i
< 4; i
++) {
2528 if (i
< ir
->type
->vector_elements
) {
2531 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.x
);
2534 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.y
);
2537 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.z
);
2540 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.w
);
2544 /* If the type is smaller than a vec4, replicate the last
2547 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
2551 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2556 /* Test if the variable is an array. Note that geometry and
2557 * tessellation shader inputs are outputs are always arrays (except
2558 * for patch inputs), so only the array element type is considered.
2561 is_inout_array(unsigned stage
, ir_variable
*var
, bool *remove_array
)
2563 const glsl_type
*type
= var
->type
;
2565 *remove_array
= false;
2567 if ((stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_in
) ||
2568 (stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_out
))
2571 if (((stage
== MESA_SHADER_GEOMETRY
&& var
->data
.mode
== ir_var_shader_in
) ||
2572 (stage
== MESA_SHADER_TESS_EVAL
&& var
->data
.mode
== ir_var_shader_in
) ||
2573 stage
== MESA_SHADER_TESS_CTRL
) &&
2575 if (!var
->type
->is_array())
2576 return false; /* a system value probably */
2578 type
= var
->type
->fields
.array
;
2579 *remove_array
= true;
2582 return type
->is_array() || type
->is_matrix();
2586 st_translate_interp_loc(ir_variable
*var
)
2588 if (var
->data
.centroid
)
2589 return TGSI_INTERPOLATE_LOC_CENTROID
;
2590 else if (var
->data
.sample
)
2591 return TGSI_INTERPOLATE_LOC_SAMPLE
;
2593 return TGSI_INTERPOLATE_LOC_CENTER
;
2597 glsl_to_tgsi_visitor::visit(ir_dereference_variable
*ir
)
2599 variable_storage
*entry
= find_variable_storage(ir
->var
);
2600 ir_variable
*var
= ir
->var
;
2604 switch (var
->data
.mode
) {
2605 case ir_var_uniform
:
2606 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_UNIFORM
,
2607 var
->data
.param_index
);
2608 this->variables
.push_tail(entry
);
2610 case ir_var_shader_in
: {
2611 /* The linker assigns locations for varyings and attributes,
2612 * including deprecated builtins (like gl_Color), user-assign
2613 * generic attributes (glBindVertexLocation), and
2614 * user-defined varyings.
2616 assert(var
->data
.location
!= -1);
2618 const glsl_type
*type_without_array
= var
->type
->without_array();
2619 struct inout_decl
*decl
= &inputs
[num_inputs
];
2620 unsigned component
= var
->data
.location_frac
;
2621 unsigned num_components
;
2624 if (type_without_array
->is_64bit())
2625 component
= component
/ 2;
2626 if (type_without_array
->vector_elements
)
2627 num_components
= type_without_array
->vector_elements
;
2631 decl
->mesa_index
= var
->data
.location
;
2632 decl
->interp
= (glsl_interp_mode
) var
->data
.interpolation
;
2633 decl
->interp_loc
= st_translate_interp_loc(var
);
2634 decl
->base_type
= type_without_array
->base_type
;
2635 decl
->usage_mask
= u_bit_consecutive(component
, num_components
);
2637 if (is_inout_array(shader
->Stage
, var
, &remove_array
)) {
2638 decl
->array_id
= num_input_arrays
+ 1;
2645 decl
->size
= type_size(var
->type
->fields
.array
);
2647 decl
->size
= type_size(var
->type
);
2649 entry
= new(mem_ctx
) variable_storage(var
,
2653 entry
->component
= component
;
2655 this->variables
.push_tail(entry
);
2658 case ir_var_shader_out
: {
2659 assert(var
->data
.location
!= -1);
2661 const glsl_type
*type_without_array
= var
->type
->without_array();
2662 struct inout_decl
*decl
= &outputs
[num_outputs
];
2663 unsigned component
= var
->data
.location_frac
;
2664 unsigned num_components
;
2667 if (type_without_array
->is_64bit())
2668 component
= component
/ 2;
2669 if (type_without_array
->vector_elements
)
2670 num_components
= type_without_array
->vector_elements
;
2674 decl
->mesa_index
= var
->data
.location
+ FRAG_RESULT_MAX
* var
->data
.index
;
2675 decl
->base_type
= type_without_array
->base_type
;
2676 decl
->usage_mask
= u_bit_consecutive(component
, num_components
);
2677 if (var
->data
.stream
& (1u << 31)) {
2678 decl
->gs_out_streams
= var
->data
.stream
& ~(1u << 31);
2680 assert(var
->data
.stream
< 4);
2681 decl
->gs_out_streams
= 0;
2682 for (unsigned i
= 0; i
< num_components
; ++i
)
2683 decl
->gs_out_streams
|= var
->data
.stream
<< (2 * (component
+ i
));
2686 if (is_inout_array(shader
->Stage
, var
, &remove_array
)) {
2687 decl
->array_id
= num_output_arrays
+ 1;
2688 num_output_arrays
++;
2694 decl
->size
= type_size(var
->type
->fields
.array
);
2696 decl
->size
= type_size(var
->type
);
2698 if (var
->data
.fb_fetch_output
) {
2699 st_dst_reg dst
= st_dst_reg(get_temp(var
->type
));
2700 st_src_reg src
= st_src_reg(PROGRAM_OUTPUT
, decl
->mesa_index
,
2701 var
->type
, component
, decl
->array_id
);
2702 emit_asm(NULL
, TGSI_OPCODE_FBFETCH
, dst
, src
);
2703 entry
= new(mem_ctx
) variable_storage(var
, dst
.file
, dst
.index
,
2706 entry
= new(mem_ctx
) variable_storage(var
,
2711 entry
->component
= component
;
2713 this->variables
.push_tail(entry
);
2716 case ir_var_system_value
:
2717 entry
= new(mem_ctx
) variable_storage(var
,
2718 PROGRAM_SYSTEM_VALUE
,
2719 var
->data
.location
);
2722 case ir_var_temporary
:
2723 st_src_reg src
= get_temp(var
->type
);
2725 entry
= new(mem_ctx
) variable_storage(var
, src
.file
, src
.index
,
2727 this->variables
.push_tail(entry
);
2733 printf("Failed to make storage for %s\n", var
->name
);
2738 this->result
= st_src_reg(entry
->file
, entry
->index
, var
->type
,
2739 entry
->component
, entry
->array_id
);
2740 if (this->shader
->Stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_in
&& var
->type
->is_double())
2741 this->result
.is_double_vertex_input
= true;
2742 if (!native_integers
)
2743 this->result
.type
= GLSL_TYPE_FLOAT
;
2747 shrink_array_declarations(struct inout_decl
*decls
, unsigned count
,
2748 GLbitfield64
* usage_mask
,
2749 GLbitfield64 double_usage_mask
,
2750 GLbitfield
* patch_usage_mask
)
2755 /* Fix array declarations by removing unused array elements at both ends
2756 * of the arrays. For example, mat4[3] where only mat[1] is used.
2758 for (i
= 0; i
< count
; i
++) {
2759 struct inout_decl
*decl
= &decls
[i
];
2760 if (!decl
->array_id
)
2763 /* Shrink the beginning. */
2764 for (j
= 0; j
< (int)decl
->size
; j
++) {
2765 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
) {
2766 if (*patch_usage_mask
&
2767 BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
))
2771 if (*usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
))
2773 if (double_usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
-1))
2782 /* Shrink the end. */
2783 for (j
= decl
->size
-1; j
>= 0; j
--) {
2784 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
) {
2785 if (*patch_usage_mask
&
2786 BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
))
2790 if (*usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
))
2792 if (double_usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
-1))
2799 /* When not all entries of an array are accessed, we mark them as used
2800 * here anyway, to ensure that the input/output mapping logic doesn't get
2803 * TODO This happens when an array isn't used via indirect access, which
2804 * some game ports do (at least eON-based). There is an optimization
2805 * opportunity here by replacing the array declaration with non-array
2806 * declarations of those slots that are actually used.
2808 for (j
= 1; j
< (int)decl
->size
; ++j
) {
2809 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
)
2810 *patch_usage_mask
|= BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
);
2812 *usage_mask
|= BITFIELD64_BIT(decl
->mesa_index
+ j
);
2818 glsl_to_tgsi_visitor::visit(ir_dereference_array
*ir
)
2822 int element_size
= type_size(ir
->type
);
2825 index
= ir
->array_index
->constant_expression_value();
2827 ir
->array
->accept(this);
2830 if (ir
->array
->ir_type
!= ir_type_dereference_array
) {
2831 switch (this->prog
->Target
) {
2832 case GL_TESS_CONTROL_PROGRAM_NV
:
2833 is_2D
= (src
.file
== PROGRAM_INPUT
|| src
.file
== PROGRAM_OUTPUT
) &&
2834 !ir
->variable_referenced()->data
.patch
;
2836 case GL_TESS_EVALUATION_PROGRAM_NV
:
2837 is_2D
= src
.file
== PROGRAM_INPUT
&&
2838 !ir
->variable_referenced()->data
.patch
;
2840 case GL_GEOMETRY_PROGRAM_NV
:
2841 is_2D
= src
.file
== PROGRAM_INPUT
;
2851 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
2852 src
.file
== PROGRAM_INPUT
)
2853 element_size
= attrib_type_size(ir
->type
, true);
2855 src
.index2D
= index
->value
.i
[0];
2856 src
.has_index2
= true;
2858 src
.index
+= index
->value
.i
[0] * element_size
;
2860 /* Variable index array dereference. It eats the "vec4" of the
2861 * base of the array and an index that offsets the TGSI register
2864 ir
->array_index
->accept(this);
2866 st_src_reg index_reg
;
2868 if (element_size
== 1) {
2869 index_reg
= this->result
;
2871 index_reg
= get_temp(native_integers
?
2872 glsl_type::int_type
: glsl_type::float_type
);
2874 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(index_reg
),
2875 this->result
, st_src_reg_for_type(index_reg
.type
, element_size
));
2878 /* If there was already a relative address register involved, add the
2879 * new and the old together to get the new offset.
2881 if (!is_2D
&& src
.reladdr
!= NULL
) {
2882 st_src_reg accum_reg
= get_temp(native_integers
?
2883 glsl_type::int_type
: glsl_type::float_type
);
2885 emit_asm(ir
, TGSI_OPCODE_ADD
, st_dst_reg(accum_reg
),
2886 index_reg
, *src
.reladdr
);
2888 index_reg
= accum_reg
;
2892 src
.reladdr2
= ralloc(mem_ctx
, st_src_reg
);
2893 memcpy(src
.reladdr2
, &index_reg
, sizeof(index_reg
));
2895 src
.has_index2
= true;
2897 src
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2898 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
2902 /* Change the register type to the element type of the array. */
2903 src
.type
= ir
->type
->base_type
;
2909 glsl_to_tgsi_visitor::visit(ir_dereference_record
*ir
)
2912 const glsl_type
*struct_type
= ir
->record
->type
;
2915 ir
->record
->accept(this);
2917 for (i
= 0; i
< struct_type
->length
; i
++) {
2918 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
2920 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
2923 /* If the type is smaller than a vec4, replicate the last channel out. */
2924 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
2925 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
2927 this->result
.swizzle
= SWIZZLE_NOOP
;
2929 this->result
.index
+= offset
;
2930 this->result
.type
= ir
->type
->base_type
;
2934 * We want to be careful in assignment setup to hit the actual storage
2935 * instead of potentially using a temporary like we might with the
2936 * ir_dereference handler.
2939 get_assignment_lhs(ir_dereference
*ir
, glsl_to_tgsi_visitor
*v
, int *component
)
2941 /* The LHS must be a dereference. If the LHS is a variable indexed array
2942 * access of a vector, it must be separated into a series conditional moves
2943 * before reaching this point (see ir_vec_index_to_cond_assign).
2945 assert(ir
->as_dereference());
2946 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
2948 assert(!deref_array
->array
->type
->is_vector());
2951 /* Use the rvalue deref handler for the most part. We write swizzles using
2952 * the writemask, but we do extract the base component for enhanced layouts
2953 * from the source swizzle.
2956 *component
= GET_SWZ(v
->result
.swizzle
, 0);
2957 return st_dst_reg(v
->result
);
2961 * Process the condition of a conditional assignment
2963 * Examines the condition of a conditional assignment to generate the optimal
2964 * first operand of a \c CMP instruction. If the condition is a relational
2965 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
2966 * used as the source for the \c CMP instruction. Otherwise the comparison
2967 * is processed to a boolean result, and the boolean result is used as the
2968 * operand to the CMP instruction.
2971 glsl_to_tgsi_visitor::process_move_condition(ir_rvalue
*ir
)
2973 ir_rvalue
*src_ir
= ir
;
2975 bool switch_order
= false;
2977 ir_expression
*const expr
= ir
->as_expression();
2979 if (native_integers
) {
2980 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
2981 enum glsl_base_type type
= expr
->operands
[0]->type
->base_type
;
2982 if (type
== GLSL_TYPE_INT
|| type
== GLSL_TYPE_UINT
||
2983 type
== GLSL_TYPE_BOOL
) {
2984 if (expr
->operation
== ir_binop_equal
) {
2985 if (expr
->operands
[0]->is_zero()) {
2986 src_ir
= expr
->operands
[1];
2987 switch_order
= true;
2989 else if (expr
->operands
[1]->is_zero()) {
2990 src_ir
= expr
->operands
[0];
2991 switch_order
= true;
2994 else if (expr
->operation
== ir_binop_nequal
) {
2995 if (expr
->operands
[0]->is_zero()) {
2996 src_ir
= expr
->operands
[1];
2998 else if (expr
->operands
[1]->is_zero()) {
2999 src_ir
= expr
->operands
[0];
3005 src_ir
->accept(this);
3006 return switch_order
;
3009 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
3010 bool zero_on_left
= false;
3012 if (expr
->operands
[0]->is_zero()) {
3013 src_ir
= expr
->operands
[1];
3014 zero_on_left
= true;
3015 } else if (expr
->operands
[1]->is_zero()) {
3016 src_ir
= expr
->operands
[0];
3017 zero_on_left
= false;
3021 * (a < 0) T F F ( a < 0) T F F
3022 * (0 < a) F F T (-a < 0) F F T
3023 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
3024 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
3025 * (a > 0) F F T (-a < 0) F F T
3026 * (0 > a) T F F ( a < 0) T F F
3027 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
3028 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
3030 * Note that exchanging the order of 0 and 'a' in the comparison simply
3031 * means that the value of 'a' should be negated.
3034 switch (expr
->operation
) {
3036 switch_order
= false;
3037 negate
= zero_on_left
;
3040 case ir_binop_greater
:
3041 switch_order
= false;
3042 negate
= !zero_on_left
;
3045 case ir_binop_lequal
:
3046 switch_order
= true;
3047 negate
= !zero_on_left
;
3050 case ir_binop_gequal
:
3051 switch_order
= true;
3052 negate
= zero_on_left
;
3056 /* This isn't the right kind of comparison afterall, so make sure
3057 * the whole condition is visited.
3065 src_ir
->accept(this);
3067 /* We use the TGSI_OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
3068 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
3069 * choose which value TGSI_OPCODE_CMP produces without an extra instruction
3070 * computing the condition.
3073 this->result
.negate
= ~this->result
.negate
;
3075 return switch_order
;
3079 glsl_to_tgsi_visitor::emit_block_mov(ir_assignment
*ir
, const struct glsl_type
*type
,
3080 st_dst_reg
*l
, st_src_reg
*r
,
3081 st_src_reg
*cond
, bool cond_swap
)
3083 if (type
->base_type
== GLSL_TYPE_STRUCT
) {
3084 for (unsigned int i
= 0; i
< type
->length
; i
++) {
3085 emit_block_mov(ir
, type
->fields
.structure
[i
].type
, l
, r
,
3091 if (type
->is_array()) {
3092 for (unsigned int i
= 0; i
< type
->length
; i
++) {
3093 emit_block_mov(ir
, type
->fields
.array
, l
, r
, cond
, cond_swap
);
3098 if (type
->is_matrix()) {
3099 const struct glsl_type
*vec_type
;
3101 vec_type
= glsl_type::get_instance(type
->is_double() ? GLSL_TYPE_DOUBLE
: GLSL_TYPE_FLOAT
,
3102 type
->vector_elements
, 1);
3104 for (int i
= 0; i
< type
->matrix_columns
; i
++) {
3105 emit_block_mov(ir
, vec_type
, l
, r
, cond
, cond_swap
);
3110 assert(type
->is_scalar() || type
->is_vector());
3112 l
->type
= type
->base_type
;
3113 r
->type
= type
->base_type
;
3115 st_src_reg l_src
= st_src_reg(*l
);
3116 l_src
.swizzle
= swizzle_for_size(type
->vector_elements
);
3118 if (native_integers
) {
3119 emit_asm(ir
, TGSI_OPCODE_UCMP
, *l
, *cond
,
3120 cond_swap
? l_src
: *r
,
3121 cond_swap
? *r
: l_src
);
3123 emit_asm(ir
, TGSI_OPCODE_CMP
, *l
, *cond
,
3124 cond_swap
? l_src
: *r
,
3125 cond_swap
? *r
: l_src
);
3128 emit_asm(ir
, TGSI_OPCODE_MOV
, *l
, *r
);
3132 if (type
->is_dual_slot()) {
3134 if (r
->is_double_vertex_input
== false)
3140 glsl_to_tgsi_visitor::visit(ir_assignment
*ir
)
3146 ir
->rhs
->accept(this);
3149 l
= get_assignment_lhs(ir
->lhs
, this, &dst_component
);
3153 int first_enabled_chan
= 0;
3155 ir_variable
*variable
= ir
->lhs
->variable_referenced();
3157 if (shader
->Stage
== MESA_SHADER_FRAGMENT
&&
3158 variable
->data
.mode
== ir_var_shader_out
&&
3159 (variable
->data
.location
== FRAG_RESULT_DEPTH
||
3160 variable
->data
.location
== FRAG_RESULT_STENCIL
)) {
3161 assert(ir
->lhs
->type
->is_scalar());
3162 assert(ir
->write_mask
== WRITEMASK_X
);
3164 if (variable
->data
.location
== FRAG_RESULT_DEPTH
)
3165 l
.writemask
= WRITEMASK_Z
;
3167 assert(variable
->data
.location
== FRAG_RESULT_STENCIL
);
3168 l
.writemask
= WRITEMASK_Y
;
3170 } else if (ir
->write_mask
== 0) {
3171 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
3173 unsigned num_elements
= ir
->lhs
->type
->without_array()->vector_elements
;
3176 l
.writemask
= u_bit_consecutive(0, num_elements
);
3178 /* The type is a struct or an array of (array of) structs. */
3179 l
.writemask
= WRITEMASK_XYZW
;
3182 l
.writemask
= ir
->write_mask
;
3185 for (int i
= 0; i
< 4; i
++) {
3186 if (l
.writemask
& (1 << i
)) {
3187 first_enabled_chan
= GET_SWZ(r
.swizzle
, i
);
3192 l
.writemask
= l
.writemask
<< dst_component
;
3194 /* Swizzle a small RHS vector into the channels being written.
3196 * glsl ir treats write_mask as dictating how many channels are
3197 * present on the RHS while TGSI treats write_mask as just
3198 * showing which channels of the vec4 RHS get written.
3200 for (int i
= 0; i
< 4; i
++) {
3201 if (l
.writemask
& (1 << i
))
3202 swizzles
[i
] = GET_SWZ(r
.swizzle
, rhs_chan
++);
3204 swizzles
[i
] = first_enabled_chan
;
3206 r
.swizzle
= MAKE_SWIZZLE4(swizzles
[0], swizzles
[1],
3207 swizzles
[2], swizzles
[3]);
3210 assert(l
.file
!= PROGRAM_UNDEFINED
);
3211 assert(r
.file
!= PROGRAM_UNDEFINED
);
3213 if (ir
->condition
) {
3214 const bool switch_order
= this->process_move_condition(ir
->condition
);
3215 st_src_reg condition
= this->result
;
3217 emit_block_mov(ir
, ir
->lhs
->type
, &l
, &r
, &condition
, switch_order
);
3218 } else if (ir
->rhs
->as_expression() &&
3219 this->instructions
.get_tail() &&
3220 ir
->rhs
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->ir
&&
3221 !((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->is_64bit_expanded
&&
3222 type_size(ir
->lhs
->type
) == 1 &&
3223 l
.writemask
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->dst
[0].writemask
) {
3224 /* To avoid emitting an extra MOV when assigning an expression to a
3225 * variable, emit the last instruction of the expression again, but
3226 * replace the destination register with the target of the assignment.
3227 * Dead code elimination will remove the original instruction.
3229 glsl_to_tgsi_instruction
*inst
, *new_inst
;
3230 inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
3231 new_inst
= emit_asm(ir
, inst
->op
, l
, inst
->src
[0], inst
->src
[1], inst
->src
[2], inst
->src
[3]);
3232 new_inst
->saturate
= inst
->saturate
;
3233 inst
->dead_mask
= inst
->dst
[0].writemask
;
3235 emit_block_mov(ir
, ir
->rhs
->type
, &l
, &r
, NULL
, false);
3241 glsl_to_tgsi_visitor::visit(ir_constant
*ir
)
3244 GLdouble stack_vals
[4] = { 0 };
3245 gl_constant_value
*values
= (gl_constant_value
*) stack_vals
;
3246 GLenum gl_type
= GL_NONE
;
3248 static int in_array
= 0;
3249 gl_register_file file
= in_array
? PROGRAM_CONSTANT
: PROGRAM_IMMEDIATE
;
3251 /* Unfortunately, 4 floats is all we can get into
3252 * _mesa_add_typed_unnamed_constant. So, make a temp to store an
3253 * aggregate constant and move each constant value into it. If we
3254 * get lucky, copy propagation will eliminate the extra moves.
3256 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
3257 st_src_reg temp_base
= get_temp(ir
->type
);
3258 st_dst_reg temp
= st_dst_reg(temp_base
);
3260 foreach_in_list(ir_constant
, field_value
, &ir
->components
) {
3261 int size
= type_size(field_value
->type
);
3265 field_value
->accept(this);
3268 for (i
= 0; i
< (unsigned int)size
; i
++) {
3269 emit_asm(ir
, TGSI_OPCODE_MOV
, temp
, src
);
3275 this->result
= temp_base
;
3279 if (ir
->type
->is_array()) {
3280 st_src_reg temp_base
= get_temp(ir
->type
);
3281 st_dst_reg temp
= st_dst_reg(temp_base
);
3282 int size
= type_size(ir
->type
->fields
.array
);
3287 for (i
= 0; i
< ir
->type
->length
; i
++) {
3288 ir
->array_elements
[i
]->accept(this);
3290 for (int j
= 0; j
< size
; j
++) {
3291 emit_asm(ir
, TGSI_OPCODE_MOV
, temp
, src
);
3297 this->result
= temp_base
;
3302 if (ir
->type
->is_matrix()) {
3303 st_src_reg mat
= get_temp(ir
->type
);
3304 st_dst_reg mat_column
= st_dst_reg(mat
);
3306 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
3307 switch (ir
->type
->base_type
) {
3308 case GLSL_TYPE_FLOAT
:
3309 values
= (gl_constant_value
*) &ir
->value
.f
[i
* ir
->type
->vector_elements
];
3311 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
3312 src
.index
= add_constant(file
,
3314 ir
->type
->vector_elements
,
3317 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3319 case GLSL_TYPE_DOUBLE
:
3320 values
= (gl_constant_value
*) &ir
->value
.d
[i
* ir
->type
->vector_elements
];
3321 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
3322 src
.index
= add_constant(file
,
3324 ir
->type
->vector_elements
,
3327 if (ir
->type
->vector_elements
>= 2) {
3328 mat_column
.writemask
= WRITEMASK_XY
;
3329 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
3330 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3332 mat_column
.writemask
= WRITEMASK_X
;
3333 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
);
3334 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3337 if (ir
->type
->vector_elements
> 2) {
3338 if (ir
->type
->vector_elements
== 4) {
3339 mat_column
.writemask
= WRITEMASK_ZW
;
3340 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
3341 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3343 mat_column
.writemask
= WRITEMASK_Z
;
3344 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
);
3345 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3346 mat_column
.writemask
= WRITEMASK_XYZW
;
3347 src
.swizzle
= SWIZZLE_XYZW
;
3353 unreachable("Illegal matrix constant type.\n");
3362 switch (ir
->type
->base_type
) {
3363 case GLSL_TYPE_FLOAT
:
3365 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3366 values
[i
].f
= ir
->value
.f
[i
];
3369 case GLSL_TYPE_DOUBLE
:
3370 gl_type
= GL_DOUBLE
;
3371 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3372 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(double));
3375 case GLSL_TYPE_INT64
:
3376 gl_type
= GL_INT64_ARB
;
3377 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3378 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(int64_t));
3381 case GLSL_TYPE_UINT64
:
3382 gl_type
= GL_UNSIGNED_INT64_ARB
;
3383 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3384 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(uint64_t));
3387 case GLSL_TYPE_UINT
:
3388 gl_type
= native_integers
? GL_UNSIGNED_INT
: GL_FLOAT
;
3389 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3390 if (native_integers
)
3391 values
[i
].u
= ir
->value
.u
[i
];
3393 values
[i
].f
= ir
->value
.u
[i
];
3397 gl_type
= native_integers
? GL_INT
: GL_FLOAT
;
3398 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3399 if (native_integers
)
3400 values
[i
].i
= ir
->value
.i
[i
];
3402 values
[i
].f
= ir
->value
.i
[i
];
3405 case GLSL_TYPE_BOOL
:
3406 gl_type
= native_integers
? GL_BOOL
: GL_FLOAT
;
3407 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3408 values
[i
].u
= ir
->value
.b
[i
] ? ctx
->Const
.UniformBooleanTrue
: 0;
3412 assert(!"Non-float/uint/int/bool constant");
3415 this->result
= st_src_reg(file
, -1, ir
->type
);
3416 this->result
.index
= add_constant(file
,
3418 ir
->type
->vector_elements
,
3420 &this->result
.swizzle
);
3424 glsl_to_tgsi_visitor::visit_atomic_counter_intrinsic(ir_call
*ir
)
3426 exec_node
*param
= ir
->actual_parameters
.get_head();
3427 ir_dereference
*deref
= static_cast<ir_dereference
*>(param
);
3428 ir_variable
*location
= deref
->variable_referenced();
3431 PROGRAM_BUFFER
, location
->data
.binding
, GLSL_TYPE_ATOMIC_UINT
);
3433 /* Calculate the surface offset */
3435 unsigned array_size
= 0, base
= 0;
3438 get_deref_offsets(deref
, &array_size
, &base
, &index
, &offset
, false);
3440 if (offset
.file
!= PROGRAM_UNDEFINED
) {
3441 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(offset
),
3442 offset
, st_src_reg_for_int(ATOMIC_COUNTER_SIZE
));
3443 emit_asm(ir
, TGSI_OPCODE_ADD
, st_dst_reg(offset
),
3444 offset
, st_src_reg_for_int(location
->data
.offset
+ index
* ATOMIC_COUNTER_SIZE
));
3446 offset
= st_src_reg_for_int(location
->data
.offset
+ index
* ATOMIC_COUNTER_SIZE
);
3449 ir
->return_deref
->accept(this);
3450 st_dst_reg
dst(this->result
);
3451 dst
.writemask
= WRITEMASK_X
;
3453 glsl_to_tgsi_instruction
*inst
;
3455 if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_read
) {
3456 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, offset
);
3457 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_increment
) {
3458 inst
= emit_asm(ir
, TGSI_OPCODE_ATOMUADD
, dst
, offset
,
3459 st_src_reg_for_int(1));
3460 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_predecrement
) {
3461 inst
= emit_asm(ir
, TGSI_OPCODE_ATOMUADD
, dst
, offset
,
3462 st_src_reg_for_int(-1));
3463 emit_asm(ir
, TGSI_OPCODE_ADD
, dst
, this->result
, st_src_reg_for_int(-1));
3465 param
= param
->get_next();
3466 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3469 st_src_reg data
= this->result
, data2
= undef_src
;
3471 switch (ir
->callee
->intrinsic_id
) {
3472 case ir_intrinsic_atomic_counter_add
:
3473 opcode
= TGSI_OPCODE_ATOMUADD
;
3475 case ir_intrinsic_atomic_counter_min
:
3476 opcode
= TGSI_OPCODE_ATOMIMIN
;
3478 case ir_intrinsic_atomic_counter_max
:
3479 opcode
= TGSI_OPCODE_ATOMIMAX
;
3481 case ir_intrinsic_atomic_counter_and
:
3482 opcode
= TGSI_OPCODE_ATOMAND
;
3484 case ir_intrinsic_atomic_counter_or
:
3485 opcode
= TGSI_OPCODE_ATOMOR
;
3487 case ir_intrinsic_atomic_counter_xor
:
3488 opcode
= TGSI_OPCODE_ATOMXOR
;
3490 case ir_intrinsic_atomic_counter_exchange
:
3491 opcode
= TGSI_OPCODE_ATOMXCHG
;
3493 case ir_intrinsic_atomic_counter_comp_swap
: {
3494 opcode
= TGSI_OPCODE_ATOMCAS
;
3495 param
= param
->get_next();
3496 val
= ((ir_instruction
*)param
)->as_rvalue();
3498 data2
= this->result
;
3502 assert(!"Unexpected intrinsic");
3506 inst
= emit_asm(ir
, opcode
, dst
, offset
, data
, data2
);
3509 inst
->resource
= buffer
;
3513 glsl_to_tgsi_visitor::visit_ssbo_intrinsic(ir_call
*ir
)
3515 exec_node
*param
= ir
->actual_parameters
.get_head();
3517 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
3519 param
= param
->get_next();
3520 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
3522 ir_constant
*const_block
= block
->as_constant();
3526 ctx
->Const
.Program
[shader
->Stage
].MaxAtomicBuffers
+
3527 (const_block
? const_block
->value
.u
[0] : 0),
3531 block
->accept(this);
3532 buffer
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
3533 *buffer
.reladdr
= this->result
;
3534 emit_arl(ir
, sampler_reladdr
, this->result
);
3537 /* Calculate the surface offset */
3538 offset
->accept(this);
3539 st_src_reg off
= this->result
;
3541 st_dst_reg dst
= undef_dst
;
3542 if (ir
->return_deref
) {
3543 ir
->return_deref
->accept(this);
3544 dst
= st_dst_reg(this->result
);
3545 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3548 glsl_to_tgsi_instruction
*inst
;
3550 if (ir
->callee
->intrinsic_id
== ir_intrinsic_ssbo_load
) {
3551 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, off
);
3552 if (dst
.type
== GLSL_TYPE_BOOL
)
3553 emit_asm(ir
, TGSI_OPCODE_USNE
, dst
, st_src_reg(dst
), st_src_reg_for_int(0));
3554 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_ssbo_store
) {
3555 param
= param
->get_next();
3556 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3559 param
= param
->get_next();
3560 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
3562 dst
.writemask
= write_mask
->value
.u
[0];
3564 dst
.type
= this->result
.type
;
3565 inst
= emit_asm(ir
, TGSI_OPCODE_STORE
, dst
, off
, this->result
);
3567 param
= param
->get_next();
3568 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3571 st_src_reg data
= this->result
, data2
= undef_src
;
3573 switch (ir
->callee
->intrinsic_id
) {
3574 case ir_intrinsic_ssbo_atomic_add
:
3575 opcode
= TGSI_OPCODE_ATOMUADD
;
3577 case ir_intrinsic_ssbo_atomic_min
:
3578 opcode
= TGSI_OPCODE_ATOMIMIN
;
3580 case ir_intrinsic_ssbo_atomic_max
:
3581 opcode
= TGSI_OPCODE_ATOMIMAX
;
3583 case ir_intrinsic_ssbo_atomic_and
:
3584 opcode
= TGSI_OPCODE_ATOMAND
;
3586 case ir_intrinsic_ssbo_atomic_or
:
3587 opcode
= TGSI_OPCODE_ATOMOR
;
3589 case ir_intrinsic_ssbo_atomic_xor
:
3590 opcode
= TGSI_OPCODE_ATOMXOR
;
3592 case ir_intrinsic_ssbo_atomic_exchange
:
3593 opcode
= TGSI_OPCODE_ATOMXCHG
;
3595 case ir_intrinsic_ssbo_atomic_comp_swap
:
3596 opcode
= TGSI_OPCODE_ATOMCAS
;
3597 param
= param
->get_next();
3598 val
= ((ir_instruction
*)param
)->as_rvalue();
3600 data2
= this->result
;
3603 assert(!"Unexpected intrinsic");
3607 inst
= emit_asm(ir
, opcode
, dst
, off
, data
, data2
);
3610 param
= param
->get_next();
3611 ir_constant
*access
= NULL
;
3612 if (!param
->is_tail_sentinel()) {
3613 access
= ((ir_instruction
*)param
)->as_constant();
3617 /* The emit_asm() might have actually split the op into pieces, e.g. for
3618 * double stores. We have to go back and fix up all the generated ops.
3620 unsigned op
= inst
->op
;
3622 inst
->resource
= buffer
;
3624 inst
->buffer_access
= access
->value
.u
[0];
3626 if (inst
== this->instructions
.get_head_raw())
3628 inst
= (glsl_to_tgsi_instruction
*)inst
->get_prev();
3630 if (inst
->op
== TGSI_OPCODE_UADD
) {
3631 if (inst
== this->instructions
.get_head_raw())
3633 inst
= (glsl_to_tgsi_instruction
*)inst
->get_prev();
3635 } while (inst
->op
== op
&& inst
->resource
.file
== PROGRAM_UNDEFINED
);
3639 glsl_to_tgsi_visitor::visit_membar_intrinsic(ir_call
*ir
)
3641 switch (ir
->callee
->intrinsic_id
) {
3642 case ir_intrinsic_memory_barrier
:
3643 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3644 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
|
3645 TGSI_MEMBAR_ATOMIC_BUFFER
|
3646 TGSI_MEMBAR_SHADER_IMAGE
|
3647 TGSI_MEMBAR_SHARED
));
3649 case ir_intrinsic_memory_barrier_atomic_counter
:
3650 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3651 st_src_reg_for_int(TGSI_MEMBAR_ATOMIC_BUFFER
));
3653 case ir_intrinsic_memory_barrier_buffer
:
3654 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3655 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
));
3657 case ir_intrinsic_memory_barrier_image
:
3658 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3659 st_src_reg_for_int(TGSI_MEMBAR_SHADER_IMAGE
));
3661 case ir_intrinsic_memory_barrier_shared
:
3662 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3663 st_src_reg_for_int(TGSI_MEMBAR_SHARED
));
3665 case ir_intrinsic_group_memory_barrier
:
3666 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3667 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
|
3668 TGSI_MEMBAR_ATOMIC_BUFFER
|
3669 TGSI_MEMBAR_SHADER_IMAGE
|
3670 TGSI_MEMBAR_SHARED
|
3671 TGSI_MEMBAR_THREAD_GROUP
));
3674 assert(!"Unexpected memory barrier intrinsic");
3679 glsl_to_tgsi_visitor::visit_shared_intrinsic(ir_call
*ir
)
3681 exec_node
*param
= ir
->actual_parameters
.get_head();
3683 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
3685 st_src_reg
buffer(PROGRAM_MEMORY
, 0, GLSL_TYPE_UINT
);
3687 /* Calculate the surface offset */
3688 offset
->accept(this);
3689 st_src_reg off
= this->result
;
3691 st_dst_reg dst
= undef_dst
;
3692 if (ir
->return_deref
) {
3693 ir
->return_deref
->accept(this);
3694 dst
= st_dst_reg(this->result
);
3695 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3698 glsl_to_tgsi_instruction
*inst
;
3700 if (ir
->callee
->intrinsic_id
== ir_intrinsic_shared_load
) {
3701 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, off
);
3702 inst
->resource
= buffer
;
3703 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_shared_store
) {
3704 param
= param
->get_next();
3705 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3708 param
= param
->get_next();
3709 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
3711 dst
.writemask
= write_mask
->value
.u
[0];
3713 dst
.type
= this->result
.type
;
3714 inst
= emit_asm(ir
, TGSI_OPCODE_STORE
, dst
, off
, this->result
);
3715 inst
->resource
= buffer
;
3717 param
= param
->get_next();
3718 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3721 st_src_reg data
= this->result
, data2
= undef_src
;
3723 switch (ir
->callee
->intrinsic_id
) {
3724 case ir_intrinsic_shared_atomic_add
:
3725 opcode
= TGSI_OPCODE_ATOMUADD
;
3727 case ir_intrinsic_shared_atomic_min
:
3728 opcode
= TGSI_OPCODE_ATOMIMIN
;
3730 case ir_intrinsic_shared_atomic_max
:
3731 opcode
= TGSI_OPCODE_ATOMIMAX
;
3733 case ir_intrinsic_shared_atomic_and
:
3734 opcode
= TGSI_OPCODE_ATOMAND
;
3736 case ir_intrinsic_shared_atomic_or
:
3737 opcode
= TGSI_OPCODE_ATOMOR
;
3739 case ir_intrinsic_shared_atomic_xor
:
3740 opcode
= TGSI_OPCODE_ATOMXOR
;
3742 case ir_intrinsic_shared_atomic_exchange
:
3743 opcode
= TGSI_OPCODE_ATOMXCHG
;
3745 case ir_intrinsic_shared_atomic_comp_swap
:
3746 opcode
= TGSI_OPCODE_ATOMCAS
;
3747 param
= param
->get_next();
3748 val
= ((ir_instruction
*)param
)->as_rvalue();
3750 data2
= this->result
;
3753 assert(!"Unexpected intrinsic");
3757 inst
= emit_asm(ir
, opcode
, dst
, off
, data
, data2
);
3758 inst
->resource
= buffer
;
3763 glsl_to_tgsi_visitor::visit_image_intrinsic(ir_call
*ir
)
3765 exec_node
*param
= ir
->actual_parameters
.get_head();
3767 ir_dereference
*img
= (ir_dereference
*)param
;
3768 const ir_variable
*imgvar
= img
->variable_referenced();
3769 const glsl_type
*type
= imgvar
->type
->without_array();
3770 unsigned sampler_array_size
= 1, sampler_base
= 0;
3773 st_src_reg
image(PROGRAM_IMAGE
, 0, GLSL_TYPE_UINT
);
3775 get_deref_offsets(img
, &sampler_array_size
, &sampler_base
,
3776 (uint16_t*)&image
.index
, &reladdr
, true);
3778 if (reladdr
.file
!= PROGRAM_UNDEFINED
) {
3779 image
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
3780 *image
.reladdr
= reladdr
;
3781 emit_arl(ir
, sampler_reladdr
, reladdr
);
3784 st_dst_reg dst
= undef_dst
;
3785 if (ir
->return_deref
) {
3786 ir
->return_deref
->accept(this);
3787 dst
= st_dst_reg(this->result
);
3788 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3791 glsl_to_tgsi_instruction
*inst
;
3793 if (ir
->callee
->intrinsic_id
== ir_intrinsic_image_size
) {
3794 dst
.writemask
= WRITEMASK_XYZ
;
3795 inst
= emit_asm(ir
, TGSI_OPCODE_RESQ
, dst
);
3796 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_image_samples
) {
3797 st_src_reg res
= get_temp(glsl_type::ivec4_type
);
3798 st_dst_reg dstres
= st_dst_reg(res
);
3799 dstres
.writemask
= WRITEMASK_W
;
3800 inst
= emit_asm(ir
, TGSI_OPCODE_RESQ
, dstres
);
3801 res
.swizzle
= SWIZZLE_WWWW
;
3802 emit_asm(ir
, TGSI_OPCODE_MOV
, dst
, res
);
3804 st_src_reg arg1
= undef_src
, arg2
= undef_src
;
3806 st_dst_reg coord_dst
;
3807 coord
= get_temp(glsl_type::ivec4_type
);
3808 coord_dst
= st_dst_reg(coord
);
3809 coord_dst
.writemask
= (1 << type
->coordinate_components()) - 1;
3810 param
= param
->get_next();
3811 ((ir_dereference
*)param
)->accept(this);
3812 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
3813 coord
.swizzle
= SWIZZLE_XXXX
;
3814 switch (type
->coordinate_components()) {
3815 case 4: assert(!"unexpected coord count");
3817 case 3: coord
.swizzle
|= SWIZZLE_Z
<< 6;
3819 case 2: coord
.swizzle
|= SWIZZLE_Y
<< 3;
3822 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
3823 param
= param
->get_next();
3824 ((ir_dereference
*)param
)->accept(this);
3825 st_src_reg sample
= this->result
;
3826 sample
.swizzle
= SWIZZLE_XXXX
;
3827 coord_dst
.writemask
= WRITEMASK_W
;
3828 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, sample
);
3829 coord
.swizzle
|= SWIZZLE_W
<< 9;
3832 param
= param
->get_next();
3833 if (!param
->is_tail_sentinel()) {
3834 ((ir_dereference
*)param
)->accept(this);
3835 arg1
= this->result
;
3836 param
= param
->get_next();
3839 if (!param
->is_tail_sentinel()) {
3840 ((ir_dereference
*)param
)->accept(this);
3841 arg2
= this->result
;
3842 param
= param
->get_next();
3845 assert(param
->is_tail_sentinel());
3848 switch (ir
->callee
->intrinsic_id
) {
3849 case ir_intrinsic_image_load
:
3850 opcode
= TGSI_OPCODE_LOAD
;
3852 case ir_intrinsic_image_store
:
3853 opcode
= TGSI_OPCODE_STORE
;
3855 case ir_intrinsic_image_atomic_add
:
3856 opcode
= TGSI_OPCODE_ATOMUADD
;
3858 case ir_intrinsic_image_atomic_min
:
3859 opcode
= TGSI_OPCODE_ATOMIMIN
;
3861 case ir_intrinsic_image_atomic_max
:
3862 opcode
= TGSI_OPCODE_ATOMIMAX
;
3864 case ir_intrinsic_image_atomic_and
:
3865 opcode
= TGSI_OPCODE_ATOMAND
;
3867 case ir_intrinsic_image_atomic_or
:
3868 opcode
= TGSI_OPCODE_ATOMOR
;
3870 case ir_intrinsic_image_atomic_xor
:
3871 opcode
= TGSI_OPCODE_ATOMXOR
;
3873 case ir_intrinsic_image_atomic_exchange
:
3874 opcode
= TGSI_OPCODE_ATOMXCHG
;
3876 case ir_intrinsic_image_atomic_comp_swap
:
3877 opcode
= TGSI_OPCODE_ATOMCAS
;
3880 assert(!"Unexpected intrinsic");
3884 inst
= emit_asm(ir
, opcode
, dst
, coord
, arg1
, arg2
);
3885 if (opcode
== TGSI_OPCODE_STORE
)
3886 inst
->dst
[0].writemask
= WRITEMASK_XYZW
;
3889 inst
->resource
= image
;
3890 inst
->sampler_array_size
= sampler_array_size
;
3891 inst
->sampler_base
= sampler_base
;
3893 inst
->tex_target
= type
->sampler_index();
3894 inst
->image_format
= st_mesa_format_to_pipe_format(st_context(ctx
),
3895 _mesa_get_shader_image_format(imgvar
->data
.image_format
));
3897 if (imgvar
->data
.image_coherent
)
3898 inst
->buffer_access
|= TGSI_MEMORY_COHERENT
;
3899 if (imgvar
->data
.image_restrict
)
3900 inst
->buffer_access
|= TGSI_MEMORY_RESTRICT
;
3901 if (imgvar
->data
.image_volatile
)
3902 inst
->buffer_access
|= TGSI_MEMORY_VOLATILE
;
3906 glsl_to_tgsi_visitor::visit(ir_call
*ir
)
3908 ir_function_signature
*sig
= ir
->callee
;
3910 /* Filter out intrinsics */
3911 switch (sig
->intrinsic_id
) {
3912 case ir_intrinsic_atomic_counter_read
:
3913 case ir_intrinsic_atomic_counter_increment
:
3914 case ir_intrinsic_atomic_counter_predecrement
:
3915 case ir_intrinsic_atomic_counter_add
:
3916 case ir_intrinsic_atomic_counter_min
:
3917 case ir_intrinsic_atomic_counter_max
:
3918 case ir_intrinsic_atomic_counter_and
:
3919 case ir_intrinsic_atomic_counter_or
:
3920 case ir_intrinsic_atomic_counter_xor
:
3921 case ir_intrinsic_atomic_counter_exchange
:
3922 case ir_intrinsic_atomic_counter_comp_swap
:
3923 visit_atomic_counter_intrinsic(ir
);
3926 case ir_intrinsic_ssbo_load
:
3927 case ir_intrinsic_ssbo_store
:
3928 case ir_intrinsic_ssbo_atomic_add
:
3929 case ir_intrinsic_ssbo_atomic_min
:
3930 case ir_intrinsic_ssbo_atomic_max
:
3931 case ir_intrinsic_ssbo_atomic_and
:
3932 case ir_intrinsic_ssbo_atomic_or
:
3933 case ir_intrinsic_ssbo_atomic_xor
:
3934 case ir_intrinsic_ssbo_atomic_exchange
:
3935 case ir_intrinsic_ssbo_atomic_comp_swap
:
3936 visit_ssbo_intrinsic(ir
);
3939 case ir_intrinsic_memory_barrier
:
3940 case ir_intrinsic_memory_barrier_atomic_counter
:
3941 case ir_intrinsic_memory_barrier_buffer
:
3942 case ir_intrinsic_memory_barrier_image
:
3943 case ir_intrinsic_memory_barrier_shared
:
3944 case ir_intrinsic_group_memory_barrier
:
3945 visit_membar_intrinsic(ir
);
3948 case ir_intrinsic_shared_load
:
3949 case ir_intrinsic_shared_store
:
3950 case ir_intrinsic_shared_atomic_add
:
3951 case ir_intrinsic_shared_atomic_min
:
3952 case ir_intrinsic_shared_atomic_max
:
3953 case ir_intrinsic_shared_atomic_and
:
3954 case ir_intrinsic_shared_atomic_or
:
3955 case ir_intrinsic_shared_atomic_xor
:
3956 case ir_intrinsic_shared_atomic_exchange
:
3957 case ir_intrinsic_shared_atomic_comp_swap
:
3958 visit_shared_intrinsic(ir
);
3961 case ir_intrinsic_image_load
:
3962 case ir_intrinsic_image_store
:
3963 case ir_intrinsic_image_atomic_add
:
3964 case ir_intrinsic_image_atomic_min
:
3965 case ir_intrinsic_image_atomic_max
:
3966 case ir_intrinsic_image_atomic_and
:
3967 case ir_intrinsic_image_atomic_or
:
3968 case ir_intrinsic_image_atomic_xor
:
3969 case ir_intrinsic_image_atomic_exchange
:
3970 case ir_intrinsic_image_atomic_comp_swap
:
3971 case ir_intrinsic_image_size
:
3972 case ir_intrinsic_image_samples
:
3973 visit_image_intrinsic(ir
);
3976 case ir_intrinsic_shader_clock
: {
3977 ir
->return_deref
->accept(this);
3979 st_dst_reg dst
= st_dst_reg(this->result
);
3980 dst
.writemask
= TGSI_WRITEMASK_XY
;
3982 emit_asm(ir
, TGSI_OPCODE_CLOCK
, dst
);
3986 case ir_intrinsic_invalid
:
3987 case ir_intrinsic_generic_load
:
3988 case ir_intrinsic_generic_store
:
3989 case ir_intrinsic_generic_atomic_add
:
3990 case ir_intrinsic_generic_atomic_and
:
3991 case ir_intrinsic_generic_atomic_or
:
3992 case ir_intrinsic_generic_atomic_xor
:
3993 case ir_intrinsic_generic_atomic_min
:
3994 case ir_intrinsic_generic_atomic_max
:
3995 case ir_intrinsic_generic_atomic_exchange
:
3996 case ir_intrinsic_generic_atomic_comp_swap
:
3997 unreachable("Invalid intrinsic");
4002 glsl_to_tgsi_visitor::calc_deref_offsets(ir_dereference
*tail
,
4003 unsigned *array_elements
,
4005 st_src_reg
*indirect
,
4008 switch (tail
->ir_type
) {
4009 case ir_type_dereference_record
: {
4010 ir_dereference_record
*deref_record
= tail
->as_dereference_record();
4011 const glsl_type
*struct_type
= deref_record
->record
->type
;
4012 int field_index
= deref_record
->record
->type
->field_index(deref_record
->field
);
4014 calc_deref_offsets(deref_record
->record
->as_dereference(), array_elements
, index
, indirect
, location
);
4016 assert(field_index
>= 0);
4017 *location
+= struct_type
->record_location_offset(field_index
);
4021 case ir_type_dereference_array
: {
4022 ir_dereference_array
*deref_arr
= tail
->as_dereference_array();
4023 ir_constant
*array_index
= deref_arr
->array_index
->constant_expression_value();
4026 st_src_reg temp_reg
;
4027 st_dst_reg temp_dst
;
4029 temp_reg
= get_temp(glsl_type::uint_type
);
4030 temp_dst
= st_dst_reg(temp_reg
);
4031 temp_dst
.writemask
= 1;
4033 deref_arr
->array_index
->accept(this);
4034 if (*array_elements
!= 1)
4035 emit_asm(NULL
, TGSI_OPCODE_MUL
, temp_dst
, this->result
, st_src_reg_for_int(*array_elements
));
4037 emit_asm(NULL
, TGSI_OPCODE_MOV
, temp_dst
, this->result
);
4039 if (indirect
->file
== PROGRAM_UNDEFINED
)
4040 *indirect
= temp_reg
;
4042 temp_dst
= st_dst_reg(*indirect
);
4043 temp_dst
.writemask
= 1;
4044 emit_asm(NULL
, TGSI_OPCODE_ADD
, temp_dst
, *indirect
, temp_reg
);
4047 *index
+= array_index
->value
.u
[0] * *array_elements
;
4049 *array_elements
*= deref_arr
->array
->type
->length
;
4051 calc_deref_offsets(deref_arr
->array
->as_dereference(), array_elements
, index
, indirect
, location
);
4060 glsl_to_tgsi_visitor::get_deref_offsets(ir_dereference
*ir
,
4061 unsigned *array_size
,
4064 st_src_reg
*reladdr
,
4067 GLuint shader
= _mesa_program_enum_to_shader_stage(this->prog
->Target
);
4068 unsigned location
= 0;
4069 ir_variable
*var
= ir
->variable_referenced();
4071 memset(reladdr
, 0, sizeof(*reladdr
));
4072 reladdr
->file
= PROGRAM_UNDEFINED
;
4078 location
= var
->data
.location
;
4079 calc_deref_offsets(ir
, array_size
, index
, reladdr
, &location
);
4082 * If we end up with no indirect then adjust the base to the index,
4083 * and set the array size to 1.
4085 if (reladdr
->file
== PROGRAM_UNDEFINED
) {
4091 assert(location
!= 0xffffffff);
4092 *base
+= this->shader_program
->data
->UniformStorage
[location
].opaque
[shader
].index
;
4093 *index
+= this->shader_program
->data
->UniformStorage
[location
].opaque
[shader
].index
;
4098 glsl_to_tgsi_visitor::canonicalize_gather_offset(st_src_reg offset
)
4100 if (offset
.reladdr
|| offset
.reladdr2
) {
4101 st_src_reg tmp
= get_temp(glsl_type::ivec2_type
);
4102 st_dst_reg tmp_dst
= st_dst_reg(tmp
);
4103 tmp_dst
.writemask
= WRITEMASK_XY
;
4104 emit_asm(NULL
, TGSI_OPCODE_MOV
, tmp_dst
, offset
);
4112 glsl_to_tgsi_visitor::visit(ir_texture
*ir
)
4114 st_src_reg result_src
, coord
, cube_sc
, lod_info
, projector
, dx
, dy
;
4115 st_src_reg offset
[MAX_GLSL_TEXTURE_OFFSET
], sample_index
, component
;
4116 st_src_reg levels_src
, reladdr
;
4117 st_dst_reg result_dst
, coord_dst
, cube_sc_dst
;
4118 glsl_to_tgsi_instruction
*inst
= NULL
;
4119 unsigned opcode
= TGSI_OPCODE_NOP
;
4120 const glsl_type
*sampler_type
= ir
->sampler
->type
;
4121 unsigned sampler_array_size
= 1, sampler_base
= 0;
4122 uint16_t sampler_index
= 0;
4123 bool is_cube_array
= false, is_cube_shadow
= false;
4126 /* if we are a cube array sampler or a cube shadow */
4127 if (sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
) {
4128 is_cube_array
= sampler_type
->sampler_array
;
4129 is_cube_shadow
= sampler_type
->sampler_shadow
;
4132 if (ir
->coordinate
) {
4133 ir
->coordinate
->accept(this);
4135 /* Put our coords in a temp. We'll need to modify them for shadow,
4136 * projection, or LOD, so the only case we'd use it as-is is if
4137 * we're doing plain old texturing. The optimization passes on
4138 * glsl_to_tgsi_visitor should handle cleaning up our mess in that case.
4140 coord
= get_temp(glsl_type::vec4_type
);
4141 coord_dst
= st_dst_reg(coord
);
4142 coord_dst
.writemask
= (1 << ir
->coordinate
->type
->vector_elements
) - 1;
4143 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
4146 if (ir
->projector
) {
4147 ir
->projector
->accept(this);
4148 projector
= this->result
;
4151 /* Storage for our result. Ideally for an assignment we'd be using
4152 * the actual storage for the result here, instead.
4154 result_src
= get_temp(ir
->type
);
4155 result_dst
= st_dst_reg(result_src
);
4156 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
4160 opcode
= (is_cube_array
&& ir
->shadow_comparator
) ? TGSI_OPCODE_TEX2
: TGSI_OPCODE_TEX
;
4162 ir
->offset
->accept(this);
4163 offset
[0] = this->result
;
4167 if (is_cube_array
|| is_cube_shadow
) {
4168 opcode
= TGSI_OPCODE_TXB2
;
4171 opcode
= TGSI_OPCODE_TXB
;
4173 ir
->lod_info
.bias
->accept(this);
4174 lod_info
= this->result
;
4176 ir
->offset
->accept(this);
4177 offset
[0] = this->result
;
4181 if (this->has_tex_txf_lz
&& ir
->lod_info
.lod
->is_zero()) {
4182 opcode
= TGSI_OPCODE_TEX_LZ
;
4184 opcode
= is_cube_array
? TGSI_OPCODE_TXL2
: TGSI_OPCODE_TXL
;
4185 ir
->lod_info
.lod
->accept(this);
4186 lod_info
= this->result
;
4189 ir
->offset
->accept(this);
4190 offset
[0] = this->result
;
4194 opcode
= TGSI_OPCODE_TXD
;
4195 ir
->lod_info
.grad
.dPdx
->accept(this);
4197 ir
->lod_info
.grad
.dPdy
->accept(this);
4200 ir
->offset
->accept(this);
4201 offset
[0] = this->result
;
4205 opcode
= TGSI_OPCODE_TXQ
;
4206 ir
->lod_info
.lod
->accept(this);
4207 lod_info
= this->result
;
4209 case ir_query_levels
:
4210 opcode
= TGSI_OPCODE_TXQ
;
4211 lod_info
= undef_src
;
4212 levels_src
= get_temp(ir
->type
);
4215 if (this->has_tex_txf_lz
&& ir
->lod_info
.lod
->is_zero()) {
4216 opcode
= TGSI_OPCODE_TXF_LZ
;
4218 opcode
= TGSI_OPCODE_TXF
;
4219 ir
->lod_info
.lod
->accept(this);
4220 lod_info
= this->result
;
4223 ir
->offset
->accept(this);
4224 offset
[0] = this->result
;
4228 opcode
= TGSI_OPCODE_TXF
;
4229 ir
->lod_info
.sample_index
->accept(this);
4230 sample_index
= this->result
;
4233 opcode
= TGSI_OPCODE_TG4
;
4234 ir
->lod_info
.component
->accept(this);
4235 component
= this->result
;
4237 ir
->offset
->accept(this);
4238 if (ir
->offset
->type
->base_type
== GLSL_TYPE_ARRAY
) {
4239 const glsl_type
*elt_type
= ir
->offset
->type
->fields
.array
;
4240 for (i
= 0; i
< ir
->offset
->type
->length
; i
++) {
4241 offset
[i
] = this->result
;
4242 offset
[i
].index
+= i
* type_size(elt_type
);
4243 offset
[i
].type
= elt_type
->base_type
;
4244 offset
[i
].swizzle
= swizzle_for_size(elt_type
->vector_elements
);
4245 offset
[i
] = canonicalize_gather_offset(offset
[i
]);
4248 offset
[0] = canonicalize_gather_offset(this->result
);
4253 opcode
= TGSI_OPCODE_LODQ
;
4255 case ir_texture_samples
:
4256 opcode
= TGSI_OPCODE_TXQS
;
4258 case ir_samples_identical
:
4259 unreachable("Unexpected ir_samples_identical opcode");
4262 if (ir
->projector
) {
4263 if (opcode
== TGSI_OPCODE_TEX
) {
4264 /* Slot the projector in as the last component of the coord. */
4265 coord_dst
.writemask
= WRITEMASK_W
;
4266 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, projector
);
4267 coord_dst
.writemask
= WRITEMASK_XYZW
;
4268 opcode
= TGSI_OPCODE_TXP
;
4270 st_src_reg coord_w
= coord
;
4271 coord_w
.swizzle
= SWIZZLE_WWWW
;
4273 /* For the other TEX opcodes there's no projective version
4274 * since the last slot is taken up by LOD info. Do the
4275 * projective divide now.
4277 coord_dst
.writemask
= WRITEMASK_W
;
4278 emit_asm(ir
, TGSI_OPCODE_RCP
, coord_dst
, projector
);
4280 /* In the case where we have to project the coordinates "by hand,"
4281 * the shadow comparator value must also be projected.
4283 st_src_reg tmp_src
= coord
;
4284 if (ir
->shadow_comparator
) {
4285 /* Slot the shadow value in as the second to last component of the
4288 ir
->shadow_comparator
->accept(this);
4290 tmp_src
= get_temp(glsl_type::vec4_type
);
4291 st_dst_reg tmp_dst
= st_dst_reg(tmp_src
);
4293 /* Projective division not allowed for array samplers. */
4294 assert(!sampler_type
->sampler_array
);
4296 tmp_dst
.writemask
= WRITEMASK_Z
;
4297 emit_asm(ir
, TGSI_OPCODE_MOV
, tmp_dst
, this->result
);
4299 tmp_dst
.writemask
= WRITEMASK_XY
;
4300 emit_asm(ir
, TGSI_OPCODE_MOV
, tmp_dst
, coord
);
4303 coord_dst
.writemask
= WRITEMASK_XYZ
;
4304 emit_asm(ir
, TGSI_OPCODE_MUL
, coord_dst
, tmp_src
, coord_w
);
4306 coord_dst
.writemask
= WRITEMASK_XYZW
;
4307 coord
.swizzle
= SWIZZLE_XYZW
;
4311 /* If projection is done and the opcode is not TGSI_OPCODE_TXP, then the shadow
4312 * comparator was put in the correct place (and projected) by the code,
4313 * above, that handles by-hand projection.
4315 if (ir
->shadow_comparator
&& (!ir
->projector
|| opcode
== TGSI_OPCODE_TXP
)) {
4316 /* Slot the shadow value in as the second to last component of the
4319 ir
->shadow_comparator
->accept(this);
4321 if (is_cube_array
) {
4322 cube_sc
= get_temp(glsl_type::float_type
);
4323 cube_sc_dst
= st_dst_reg(cube_sc
);
4324 cube_sc_dst
.writemask
= WRITEMASK_X
;
4325 emit_asm(ir
, TGSI_OPCODE_MOV
, cube_sc_dst
, this->result
);
4326 cube_sc_dst
.writemask
= WRITEMASK_X
;
4329 if ((sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_2D
&&
4330 sampler_type
->sampler_array
) ||
4331 sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
) {
4332 coord_dst
.writemask
= WRITEMASK_W
;
4334 coord_dst
.writemask
= WRITEMASK_Z
;
4336 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
4337 coord_dst
.writemask
= WRITEMASK_XYZW
;
4341 if (ir
->op
== ir_txf_ms
) {
4342 coord_dst
.writemask
= WRITEMASK_W
;
4343 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, sample_index
);
4344 coord_dst
.writemask
= WRITEMASK_XYZW
;
4345 } else if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXB
||
4346 opcode
== TGSI_OPCODE_TXF
) {
4347 /* TGSI stores LOD or LOD bias in the last channel of the coords. */
4348 coord_dst
.writemask
= WRITEMASK_W
;
4349 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, lod_info
);
4350 coord_dst
.writemask
= WRITEMASK_XYZW
;
4353 get_deref_offsets(ir
->sampler
, &sampler_array_size
, &sampler_base
,
4354 &sampler_index
, &reladdr
, true);
4355 if (reladdr
.file
!= PROGRAM_UNDEFINED
)
4356 emit_arl(ir
, sampler_reladdr
, reladdr
);
4358 if (opcode
== TGSI_OPCODE_TXD
)
4359 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, dx
, dy
);
4360 else if (opcode
== TGSI_OPCODE_TXQ
) {
4361 if (ir
->op
== ir_query_levels
) {
4362 /* the level is stored in W */
4363 inst
= emit_asm(ir
, opcode
, st_dst_reg(levels_src
), lod_info
);
4364 result_dst
.writemask
= WRITEMASK_X
;
4365 levels_src
.swizzle
= SWIZZLE_WWWW
;
4366 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, levels_src
);
4368 inst
= emit_asm(ir
, opcode
, result_dst
, lod_info
);
4369 } else if (opcode
== TGSI_OPCODE_TXQS
) {
4370 inst
= emit_asm(ir
, opcode
, result_dst
);
4371 } else if (opcode
== TGSI_OPCODE_TXL2
|| opcode
== TGSI_OPCODE_TXB2
) {
4372 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, lod_info
);
4373 } else if (opcode
== TGSI_OPCODE_TEX2
) {
4374 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, cube_sc
);
4375 } else if (opcode
== TGSI_OPCODE_TG4
) {
4376 if (is_cube_array
&& ir
->shadow_comparator
) {
4377 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, cube_sc
);
4379 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, component
);
4382 inst
= emit_asm(ir
, opcode
, result_dst
, coord
);
4384 if (ir
->shadow_comparator
)
4385 inst
->tex_shadow
= GL_TRUE
;
4387 inst
->resource
.index
= sampler_index
;
4388 inst
->sampler_array_size
= sampler_array_size
;
4389 inst
->sampler_base
= sampler_base
;
4391 if (reladdr
.file
!= PROGRAM_UNDEFINED
) {
4392 inst
->resource
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
4393 memcpy(inst
->resource
.reladdr
, &reladdr
, sizeof(reladdr
));
4397 if (!inst
->tex_offsets
)
4398 inst
->tex_offsets
= rzalloc_array(inst
, st_src_reg
, MAX_GLSL_TEXTURE_OFFSET
);
4400 for (i
= 0; i
< MAX_GLSL_TEXTURE_OFFSET
&& offset
[i
].file
!= PROGRAM_UNDEFINED
; i
++)
4401 inst
->tex_offsets
[i
] = offset
[i
];
4402 inst
->tex_offset_num_offset
= i
;
4405 inst
->tex_target
= sampler_type
->sampler_index();
4406 inst
->tex_type
= ir
->type
->base_type
;
4408 this->result
= result_src
;
4412 glsl_to_tgsi_visitor::visit(ir_return
*ir
)
4414 assert(!ir
->get_value());
4416 emit_asm(ir
, TGSI_OPCODE_RET
);
4420 glsl_to_tgsi_visitor::visit(ir_discard
*ir
)
4422 if (ir
->condition
) {
4423 ir
->condition
->accept(this);
4424 st_src_reg condition
= this->result
;
4426 /* Convert the bool condition to a float so we can negate. */
4427 if (native_integers
) {
4428 st_src_reg temp
= get_temp(ir
->condition
->type
);
4429 emit_asm(ir
, TGSI_OPCODE_AND
, st_dst_reg(temp
),
4430 condition
, st_src_reg_for_float(1.0));
4434 condition
.negate
= ~condition
.negate
;
4435 emit_asm(ir
, TGSI_OPCODE_KILL_IF
, undef_dst
, condition
);
4437 /* unconditional kil */
4438 emit_asm(ir
, TGSI_OPCODE_KILL
);
4443 glsl_to_tgsi_visitor::visit(ir_if
*ir
)
4446 glsl_to_tgsi_instruction
*if_inst
;
4448 ir
->condition
->accept(this);
4449 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
4451 if_opcode
= native_integers
? TGSI_OPCODE_UIF
: TGSI_OPCODE_IF
;
4453 if_inst
= emit_asm(ir
->condition
, if_opcode
, undef_dst
, this->result
);
4455 this->instructions
.push_tail(if_inst
);
4457 visit_exec_list(&ir
->then_instructions
, this);
4459 if (!ir
->else_instructions
.is_empty()) {
4460 emit_asm(ir
->condition
, TGSI_OPCODE_ELSE
);
4461 visit_exec_list(&ir
->else_instructions
, this);
4464 if_inst
= emit_asm(ir
->condition
, TGSI_OPCODE_ENDIF
);
4469 glsl_to_tgsi_visitor::visit(ir_emit_vertex
*ir
)
4471 assert(this->prog
->Target
== GL_GEOMETRY_PROGRAM_NV
);
4473 ir
->stream
->accept(this);
4474 emit_asm(ir
, TGSI_OPCODE_EMIT
, undef_dst
, this->result
);
4478 glsl_to_tgsi_visitor::visit(ir_end_primitive
*ir
)
4480 assert(this->prog
->Target
== GL_GEOMETRY_PROGRAM_NV
);
4482 ir
->stream
->accept(this);
4483 emit_asm(ir
, TGSI_OPCODE_ENDPRIM
, undef_dst
, this->result
);
4487 glsl_to_tgsi_visitor::visit(ir_barrier
*ir
)
4489 assert(this->prog
->Target
== GL_TESS_CONTROL_PROGRAM_NV
||
4490 this->prog
->Target
== GL_COMPUTE_PROGRAM_NV
);
4492 emit_asm(ir
, TGSI_OPCODE_BARRIER
);
4495 glsl_to_tgsi_visitor::glsl_to_tgsi_visitor()
4497 STATIC_ASSERT(sizeof(samplers_used
) * 8 >= PIPE_MAX_SAMPLERS
);
4499 result
.file
= PROGRAM_UNDEFINED
;
4506 num_input_arrays
= 0;
4507 num_output_arrays
= 0;
4509 num_address_regs
= 0;
4513 indirect_addr_consts
= false;
4514 wpos_transform_const
= -1;
4516 native_integers
= false;
4517 mem_ctx
= ralloc_context(NULL
);
4520 shader_program
= NULL
;
4525 use_shared_memory
= false;
4526 has_tex_txf_lz
= false;
4529 glsl_to_tgsi_visitor::~glsl_to_tgsi_visitor()
4532 ralloc_free(mem_ctx
);
4535 extern "C" void free_glsl_to_tgsi_visitor(glsl_to_tgsi_visitor
*v
)
4542 * Count resources used by the given gpu program (number of texture
4546 count_resources(glsl_to_tgsi_visitor
*v
, gl_program
*prog
)
4548 v
->samplers_used
= 0;
4549 v
->buffers_used
= 0;
4552 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &v
->instructions
) {
4553 if (inst
->info
->is_tex
) {
4554 for (int i
= 0; i
< inst
->sampler_array_size
; i
++) {
4555 unsigned idx
= inst
->sampler_base
+ i
;
4556 v
->samplers_used
|= 1u << idx
;
4558 debug_assert(idx
< (int)ARRAY_SIZE(v
->sampler_types
));
4559 v
->sampler_types
[idx
] = inst
->tex_type
;
4560 v
->sampler_targets
[idx
] =
4561 st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
4563 if (inst
->tex_shadow
) {
4564 prog
->ShadowSamplers
|= 1 << (inst
->resource
.index
+ i
);
4569 if (inst
->tex_target
== TEXTURE_EXTERNAL_INDEX
)
4570 prog
->ExternalSamplersUsed
|= 1 << inst
->resource
.index
;
4572 if (inst
->resource
.file
!= PROGRAM_UNDEFINED
&& (
4573 is_resource_instruction(inst
->op
) ||
4574 inst
->op
== TGSI_OPCODE_STORE
)) {
4575 if (inst
->resource
.file
== PROGRAM_BUFFER
) {
4576 v
->buffers_used
|= 1 << inst
->resource
.index
;
4577 } else if (inst
->resource
.file
== PROGRAM_MEMORY
) {
4578 v
->use_shared_memory
= true;
4580 assert(inst
->resource
.file
== PROGRAM_IMAGE
);
4581 for (int i
= 0; i
< inst
->sampler_array_size
; i
++) {
4582 unsigned idx
= inst
->sampler_base
+ i
;
4583 v
->images_used
|= 1 << idx
;
4584 v
->image_targets
[idx
] =
4585 st_translate_texture_target(inst
->tex_target
, false);
4586 v
->image_formats
[idx
] = inst
->image_format
;
4591 prog
->SamplersUsed
= v
->samplers_used
;
4593 if (v
->shader_program
!= NULL
)
4594 _mesa_update_shader_textures_used(v
->shader_program
, prog
);
4598 * Returns the mask of channels (bitmask of WRITEMASK_X,Y,Z,W) which
4599 * are read from the given src in this instruction
4602 get_src_arg_mask(st_dst_reg dst
, st_src_reg src
)
4604 int read_mask
= 0, comp
;
4606 /* Now, given the src swizzle and the written channels, find which
4607 * components are actually read
4609 for (comp
= 0; comp
< 4; ++comp
) {
4610 const unsigned coord
= GET_SWZ(src
.swizzle
, comp
);
4612 if (dst
.writemask
& (1 << comp
) && coord
<= SWIZZLE_W
)
4613 read_mask
|= 1 << coord
;
4620 * This pass replaces CMP T0, T1 T2 T0 with MOV T0, T2 when the CMP
4621 * instruction is the first instruction to write to register T0. There are
4622 * several lowering passes done in GLSL IR (e.g. branches and
4623 * relative addressing) that create a large number of conditional assignments
4624 * that ir_to_mesa converts to CMP instructions like the one mentioned above.
4626 * Here is why this conversion is safe:
4627 * CMP T0, T1 T2 T0 can be expanded to:
4633 * If (T1 < 0.0) evaluates to true then our replacement MOV T0, T2 is the same
4634 * as the original program. If (T1 < 0.0) evaluates to false, executing
4635 * MOV T0, T0 will store a garbage value in T0 since T0 is uninitialized.
4636 * Therefore, it doesn't matter that we are replacing MOV T0, T0 with MOV T0, T2
4637 * because any instruction that was going to read from T0 after this was going
4638 * to read a garbage value anyway.
4641 glsl_to_tgsi_visitor::simplify_cmp(void)
4643 int tempWritesSize
= 0;
4644 unsigned *tempWrites
= NULL
;
4645 unsigned outputWrites
[VARYING_SLOT_TESS_MAX
];
4647 memset(outputWrites
, 0, sizeof(outputWrites
));
4649 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4650 unsigned prevWriteMask
= 0;
4652 /* Give up if we encounter relative addressing or flow control. */
4653 if (inst
->dst
[0].reladdr
|| inst
->dst
[0].reladdr2
||
4654 inst
->dst
[1].reladdr
|| inst
->dst
[1].reladdr2
||
4655 tgsi_get_opcode_info(inst
->op
)->is_branch
||
4656 inst
->op
== TGSI_OPCODE_CONT
||
4657 inst
->op
== TGSI_OPCODE_END
||
4658 inst
->op
== TGSI_OPCODE_RET
) {
4662 if (inst
->dst
[0].file
== PROGRAM_OUTPUT
) {
4663 assert(inst
->dst
[0].index
< (signed)ARRAY_SIZE(outputWrites
));
4664 prevWriteMask
= outputWrites
[inst
->dst
[0].index
];
4665 outputWrites
[inst
->dst
[0].index
] |= inst
->dst
[0].writemask
;
4666 } else if (inst
->dst
[0].file
== PROGRAM_TEMPORARY
) {
4667 if (inst
->dst
[0].index
>= tempWritesSize
) {
4668 const int inc
= 4096;
4670 tempWrites
= (unsigned*)
4672 (tempWritesSize
+ inc
) * sizeof(unsigned));
4676 memset(tempWrites
+ tempWritesSize
, 0, inc
* sizeof(unsigned));
4677 tempWritesSize
+= inc
;
4680 prevWriteMask
= tempWrites
[inst
->dst
[0].index
];
4681 tempWrites
[inst
->dst
[0].index
] |= inst
->dst
[0].writemask
;
4685 /* For a CMP to be considered a conditional write, the destination
4686 * register and source register two must be the same. */
4687 if (inst
->op
== TGSI_OPCODE_CMP
4688 && !(inst
->dst
[0].writemask
& prevWriteMask
)
4689 && inst
->src
[2].file
== inst
->dst
[0].file
4690 && inst
->src
[2].index
== inst
->dst
[0].index
4691 && inst
->dst
[0].writemask
== get_src_arg_mask(inst
->dst
[0], inst
->src
[2])) {
4693 inst
->op
= TGSI_OPCODE_MOV
;
4694 inst
->info
= tgsi_get_opcode_info(inst
->op
);
4695 inst
->src
[0] = inst
->src
[1];
4702 /* Replaces all references to a temporary register index with another index. */
4704 glsl_to_tgsi_visitor::rename_temp_registers(int num_renames
, struct rename_reg_pair
*renames
)
4706 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4709 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
4710 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
)
4711 for (k
= 0; k
< num_renames
; k
++)
4712 if (inst
->src
[j
].index
== renames
[k
].old_reg
)
4713 inst
->src
[j
].index
= renames
[k
].new_reg
;
4716 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
4717 if (inst
->tex_offsets
[j
].file
== PROGRAM_TEMPORARY
)
4718 for (k
= 0; k
< num_renames
; k
++)
4719 if (inst
->tex_offsets
[j
].index
== renames
[k
].old_reg
)
4720 inst
->tex_offsets
[j
].index
= renames
[k
].new_reg
;
4723 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4724 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
)
4725 for (k
= 0; k
< num_renames
; k
++)
4726 if (inst
->dst
[j
].index
== renames
[k
].old_reg
)
4727 inst
->dst
[j
].index
= renames
[k
].new_reg
;
4733 glsl_to_tgsi_visitor::get_first_temp_read(int *first_reads
)
4735 int depth
= 0; /* loop depth */
4736 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
4739 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4740 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
4741 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
) {
4742 if (first_reads
[inst
->src
[j
].index
] == -1)
4743 first_reads
[inst
->src
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4746 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
4747 if (inst
->tex_offsets
[j
].file
== PROGRAM_TEMPORARY
) {
4748 if (first_reads
[inst
->tex_offsets
[j
].index
] == -1)
4749 first_reads
[inst
->tex_offsets
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4752 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
4755 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
4765 glsl_to_tgsi_visitor::get_last_temp_read_first_temp_write(int *last_reads
, int *first_writes
)
4767 int depth
= 0; /* loop depth */
4768 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
4771 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4772 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
4773 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
)
4774 last_reads
[inst
->src
[j
].index
] = (depth
== 0) ? i
: -2;
4776 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4777 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
) {
4778 if (first_writes
[inst
->dst
[j
].index
] == -1)
4779 first_writes
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4780 last_reads
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: -2;
4783 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
4784 if (inst
->tex_offsets
[j
].file
== PROGRAM_TEMPORARY
)
4785 last_reads
[inst
->tex_offsets
[j
].index
] = (depth
== 0) ? i
: -2;
4787 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
4790 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
4793 for (k
= 0; k
< this->next_temp
; k
++) {
4794 if (last_reads
[k
] == -2) {
4806 glsl_to_tgsi_visitor::get_last_temp_write(int *last_writes
)
4808 int depth
= 0; /* loop depth */
4812 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4813 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4814 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
)
4815 last_writes
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: -2;
4818 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
4820 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
4822 for (k
= 0; k
< this->next_temp
; k
++) {
4823 if (last_writes
[k
] == -2) {
4834 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
4835 * channels for copy propagation and updates following instructions to
4836 * use the original versions.
4838 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
4839 * will occur. As an example, a TXP production before this pass:
4841 * 0: MOV TEMP[1], INPUT[4].xyyy;
4842 * 1: MOV TEMP[1].w, INPUT[4].wwww;
4843 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
4847 * 0: MOV TEMP[1], INPUT[4].xyyy;
4848 * 1: MOV TEMP[1].w, INPUT[4].wwww;
4849 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
4851 * which allows for dead code elimination on TEMP[1]'s writes.
4854 glsl_to_tgsi_visitor::copy_propagate(void)
4856 glsl_to_tgsi_instruction
**acp
= rzalloc_array(mem_ctx
,
4857 glsl_to_tgsi_instruction
*,
4858 this->next_temp
* 4);
4859 int *acp_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
4862 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4863 assert(inst
->dst
[0].file
!= PROGRAM_TEMPORARY
4864 || inst
->dst
[0].index
< this->next_temp
);
4866 /* First, do any copy propagation possible into the src regs. */
4867 for (int r
= 0; r
< 3; r
++) {
4868 glsl_to_tgsi_instruction
*first
= NULL
;
4870 int acp_base
= inst
->src
[r
].index
* 4;
4872 if (inst
->src
[r
].file
!= PROGRAM_TEMPORARY
||
4873 inst
->src
[r
].reladdr
||
4874 inst
->src
[r
].reladdr2
)
4877 /* See if we can find entries in the ACP consisting of MOVs
4878 * from the same src register for all the swizzled channels
4879 * of this src register reference.
4881 for (int i
= 0; i
< 4; i
++) {
4882 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
4883 glsl_to_tgsi_instruction
*copy_chan
= acp
[acp_base
+ src_chan
];
4890 assert(acp_level
[acp_base
+ src_chan
] <= level
);
4895 if (first
->src
[0].file
!= copy_chan
->src
[0].file
||
4896 first
->src
[0].index
!= copy_chan
->src
[0].index
||
4897 first
->src
[0].double_reg2
!= copy_chan
->src
[0].double_reg2
||
4898 first
->src
[0].index2D
!= copy_chan
->src
[0].index2D
) {
4906 /* We've now validated that we can copy-propagate to
4907 * replace this src register reference. Do it.
4909 inst
->src
[r
].file
= first
->src
[0].file
;
4910 inst
->src
[r
].index
= first
->src
[0].index
;
4911 inst
->src
[r
].index2D
= first
->src
[0].index2D
;
4912 inst
->src
[r
].has_index2
= first
->src
[0].has_index2
;
4913 inst
->src
[r
].double_reg2
= first
->src
[0].double_reg2
;
4914 inst
->src
[r
].array_id
= first
->src
[0].array_id
;
4917 for (int i
= 0; i
< 4; i
++) {
4918 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
4919 glsl_to_tgsi_instruction
*copy_inst
= acp
[acp_base
+ src_chan
];
4920 swizzle
|= (GET_SWZ(copy_inst
->src
[0].swizzle
, src_chan
) << (3 * i
));
4922 inst
->src
[r
].swizzle
= swizzle
;
4927 case TGSI_OPCODE_BGNLOOP
:
4928 case TGSI_OPCODE_ENDLOOP
:
4929 /* End of a basic block, clear the ACP entirely. */
4930 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
4933 case TGSI_OPCODE_IF
:
4934 case TGSI_OPCODE_UIF
:
4938 case TGSI_OPCODE_ENDIF
:
4939 case TGSI_OPCODE_ELSE
:
4940 /* Clear all channels written inside the block from the ACP, but
4941 * leaving those that were not touched.
4943 for (int r
= 0; r
< this->next_temp
; r
++) {
4944 for (int c
= 0; c
< 4; c
++) {
4945 if (!acp
[4 * r
+ c
])
4948 if (acp_level
[4 * r
+ c
] >= level
)
4949 acp
[4 * r
+ c
] = NULL
;
4952 if (inst
->op
== TGSI_OPCODE_ENDIF
)
4957 /* Continuing the block, clear any written channels from
4960 for (int d
= 0; d
< 2; d
++) {
4961 if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
&& inst
->dst
[d
].reladdr
) {
4962 /* Any temporary might be written, so no copy propagation
4963 * across this instruction.
4965 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
4966 } else if (inst
->dst
[d
].file
== PROGRAM_OUTPUT
&&
4967 inst
->dst
[d
].reladdr
) {
4968 /* Any output might be written, so no copy propagation
4969 * from outputs across this instruction.
4971 for (int r
= 0; r
< this->next_temp
; r
++) {
4972 for (int c
= 0; c
< 4; c
++) {
4973 if (!acp
[4 * r
+ c
])
4976 if (acp
[4 * r
+ c
]->src
[0].file
== PROGRAM_OUTPUT
)
4977 acp
[4 * r
+ c
] = NULL
;
4980 } else if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
||
4981 inst
->dst
[d
].file
== PROGRAM_OUTPUT
) {
4982 /* Clear where it's used as dst. */
4983 if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
) {
4984 for (int c
= 0; c
< 4; c
++) {
4985 if (inst
->dst
[d
].writemask
& (1 << c
))
4986 acp
[4 * inst
->dst
[d
].index
+ c
] = NULL
;
4990 /* Clear where it's used as src. */
4991 for (int r
= 0; r
< this->next_temp
; r
++) {
4992 for (int c
= 0; c
< 4; c
++) {
4993 if (!acp
[4 * r
+ c
])
4996 int src_chan
= GET_SWZ(acp
[4 * r
+ c
]->src
[0].swizzle
, c
);
4998 if (acp
[4 * r
+ c
]->src
[0].file
== inst
->dst
[d
].file
&&
4999 acp
[4 * r
+ c
]->src
[0].index
== inst
->dst
[d
].index
&&
5000 inst
->dst
[d
].writemask
& (1 << src_chan
)) {
5001 acp
[4 * r
+ c
] = NULL
;
5010 /* If this is a copy, add it to the ACP. */
5011 if (inst
->op
== TGSI_OPCODE_MOV
&&
5012 inst
->dst
[0].file
== PROGRAM_TEMPORARY
&&
5013 !(inst
->dst
[0].file
== inst
->src
[0].file
&&
5014 inst
->dst
[0].index
== inst
->src
[0].index
) &&
5015 !inst
->dst
[0].reladdr
&&
5016 !inst
->dst
[0].reladdr2
&&
5018 inst
->src
[0].file
!= PROGRAM_ARRAY
&&
5019 !inst
->src
[0].reladdr
&&
5020 !inst
->src
[0].reladdr2
&&
5021 !inst
->src
[0].negate
&&
5022 !inst
->src
[0].abs
) {
5023 for (int i
= 0; i
< 4; i
++) {
5024 if (inst
->dst
[0].writemask
& (1 << i
)) {
5025 acp
[4 * inst
->dst
[0].index
+ i
] = inst
;
5026 acp_level
[4 * inst
->dst
[0].index
+ i
] = level
;
5032 ralloc_free(acp_level
);
5037 * On a basic block basis, tracks available PROGRAM_TEMPORARY registers for dead
5040 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
5041 * will occur. As an example, a TXP production after copy propagation but
5044 * 0: MOV TEMP[1], INPUT[4].xyyy;
5045 * 1: MOV TEMP[1].w, INPUT[4].wwww;
5046 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5048 * and after this pass:
5050 * 0: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5053 glsl_to_tgsi_visitor::eliminate_dead_code(void)
5055 glsl_to_tgsi_instruction
**writes
= rzalloc_array(mem_ctx
,
5056 glsl_to_tgsi_instruction
*,
5057 this->next_temp
* 4);
5058 int *write_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
5062 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5063 assert(inst
->dst
[0].file
!= PROGRAM_TEMPORARY
5064 || inst
->dst
[0].index
< this->next_temp
);
5067 case TGSI_OPCODE_BGNLOOP
:
5068 case TGSI_OPCODE_ENDLOOP
:
5069 case TGSI_OPCODE_CONT
:
5070 case TGSI_OPCODE_BRK
:
5071 /* End of a basic block, clear the write array entirely.
5073 * This keeps us from killing dead code when the writes are
5074 * on either side of a loop, even when the register isn't touched
5075 * inside the loop. However, glsl_to_tgsi_visitor doesn't seem to emit
5076 * dead code of this type, so it shouldn't make a difference as long as
5077 * the dead code elimination pass in the GLSL compiler does its job.
5079 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5082 case TGSI_OPCODE_ENDIF
:
5083 case TGSI_OPCODE_ELSE
:
5084 /* Promote the recorded level of all channels written inside the
5085 * preceding if or else block to the level above the if/else block.
5087 for (int r
= 0; r
< this->next_temp
; r
++) {
5088 for (int c
= 0; c
< 4; c
++) {
5089 if (!writes
[4 * r
+ c
])
5092 if (write_level
[4 * r
+ c
] == level
)
5093 write_level
[4 * r
+ c
] = level
-1;
5096 if(inst
->op
== TGSI_OPCODE_ENDIF
)
5100 case TGSI_OPCODE_IF
:
5101 case TGSI_OPCODE_UIF
:
5103 /* fallthrough to default case to mark the condition as read */
5105 /* Continuing the block, clear any channels from the write array that
5106 * are read by this instruction.
5108 for (unsigned i
= 0; i
< ARRAY_SIZE(inst
->src
); i
++) {
5109 if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
&& inst
->src
[i
].reladdr
){
5110 /* Any temporary might be read, so no dead code elimination
5111 * across this instruction.
5113 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5114 } else if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
) {
5115 /* Clear where it's used as src. */
5116 int src_chans
= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 0);
5117 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 1);
5118 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 2);
5119 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 3);
5121 for (int c
= 0; c
< 4; c
++) {
5122 if (src_chans
& (1 << c
))
5123 writes
[4 * inst
->src
[i
].index
+ c
] = NULL
;
5127 for (unsigned i
= 0; i
< inst
->tex_offset_num_offset
; i
++) {
5128 if (inst
->tex_offsets
[i
].file
== PROGRAM_TEMPORARY
&& inst
->tex_offsets
[i
].reladdr
){
5129 /* Any temporary might be read, so no dead code elimination
5130 * across this instruction.
5132 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5133 } else if (inst
->tex_offsets
[i
].file
== PROGRAM_TEMPORARY
) {
5134 /* Clear where it's used as src. */
5135 int src_chans
= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 0);
5136 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 1);
5137 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 2);
5138 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 3);
5140 for (int c
= 0; c
< 4; c
++) {
5141 if (src_chans
& (1 << c
))
5142 writes
[4 * inst
->tex_offsets
[i
].index
+ c
] = NULL
;
5149 /* If this instruction writes to a temporary, add it to the write array.
5150 * If there is already an instruction in the write array for one or more
5151 * of the channels, flag that channel write as dead.
5153 for (unsigned i
= 0; i
< ARRAY_SIZE(inst
->dst
); i
++) {
5154 if (inst
->dst
[i
].file
== PROGRAM_TEMPORARY
&&
5155 !inst
->dst
[i
].reladdr
) {
5156 for (int c
= 0; c
< 4; c
++) {
5157 if (inst
->dst
[i
].writemask
& (1 << c
)) {
5158 if (writes
[4 * inst
->dst
[i
].index
+ c
]) {
5159 if (write_level
[4 * inst
->dst
[i
].index
+ c
] < level
)
5162 writes
[4 * inst
->dst
[i
].index
+ c
]->dead_mask
|= (1 << c
);
5164 writes
[4 * inst
->dst
[i
].index
+ c
] = inst
;
5165 write_level
[4 * inst
->dst
[i
].index
+ c
] = level
;
5172 /* Anything still in the write array at this point is dead code. */
5173 for (int r
= 0; r
< this->next_temp
; r
++) {
5174 for (int c
= 0; c
< 4; c
++) {
5175 glsl_to_tgsi_instruction
*inst
= writes
[4 * r
+ c
];
5177 inst
->dead_mask
|= (1 << c
);
5181 /* Now actually remove the instructions that are completely dead and update
5182 * the writemask of other instructions with dead channels.
5184 foreach_in_list_safe(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5185 if (!inst
->dead_mask
|| !inst
->dst
[0].writemask
)
5187 /* No amount of dead masks should remove memory stores */
5188 if (inst
->info
->is_store
)
5191 if ((inst
->dst
[0].writemask
& ~inst
->dead_mask
) == 0) {
5196 if (glsl_base_type_is_64bit(inst
->dst
[0].type
)) {
5197 if (inst
->dead_mask
== WRITEMASK_XY
||
5198 inst
->dead_mask
== WRITEMASK_ZW
)
5199 inst
->dst
[0].writemask
&= ~(inst
->dead_mask
);
5201 inst
->dst
[0].writemask
&= ~(inst
->dead_mask
);
5205 ralloc_free(write_level
);
5206 ralloc_free(writes
);
5211 /* merge DFRACEXP instructions into one. */
5213 glsl_to_tgsi_visitor::merge_two_dsts(void)
5215 foreach_in_list_safe(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5216 glsl_to_tgsi_instruction
*inst2
;
5218 if (num_inst_dst_regs(inst
) != 2)
5221 if (inst
->dst
[0].file
!= PROGRAM_UNDEFINED
&&
5222 inst
->dst
[1].file
!= PROGRAM_UNDEFINED
)
5225 inst2
= (glsl_to_tgsi_instruction
*) inst
->next
;
5228 if (inst
->src
[0].file
== inst2
->src
[0].file
&&
5229 inst
->src
[0].index
== inst2
->src
[0].index
&&
5230 inst
->src
[0].type
== inst2
->src
[0].type
&&
5231 inst
->src
[0].swizzle
== inst2
->src
[0].swizzle
)
5233 inst2
= (glsl_to_tgsi_instruction
*) inst2
->next
;
5239 if (inst
->dst
[0].file
== PROGRAM_UNDEFINED
) {
5241 inst
->dst
[0] = inst2
->dst
[0];
5242 } else if (inst
->dst
[1].file
== PROGRAM_UNDEFINED
) {
5243 inst
->dst
[1] = inst2
->dst
[1];
5254 /* Merges temporary registers together where possible to reduce the number of
5255 * registers needed to run a program.
5257 * Produces optimal code only after copy propagation and dead code elimination
5260 glsl_to_tgsi_visitor::merge_registers(void)
5262 int *last_reads
= rzalloc_array(mem_ctx
, int, this->next_temp
);
5263 int *first_writes
= rzalloc_array(mem_ctx
, int, this->next_temp
);
5264 struct rename_reg_pair
*renames
= rzalloc_array(mem_ctx
, struct rename_reg_pair
, this->next_temp
);
5266 int num_renames
= 0;
5268 /* Read the indices of the last read and first write to each temp register
5269 * into an array so that we don't have to traverse the instruction list as
5271 for (i
= 0; i
< this->next_temp
; i
++) {
5273 first_writes
[i
] = -1;
5275 get_last_temp_read_first_temp_write(last_reads
, first_writes
);
5277 /* Start looking for registers with non-overlapping usages that can be
5278 * merged together. */
5279 for (i
= 0; i
< this->next_temp
; i
++) {
5280 /* Don't touch unused registers. */
5281 if (last_reads
[i
] < 0 || first_writes
[i
] < 0) continue;
5283 for (j
= 0; j
< this->next_temp
; j
++) {
5284 /* Don't touch unused registers. */
5285 if (last_reads
[j
] < 0 || first_writes
[j
] < 0) continue;
5287 /* We can merge the two registers if the first write to j is after or
5288 * in the same instruction as the last read from i. Note that the
5289 * register at index i will always be used earlier or at the same time
5290 * as the register at index j. */
5291 if (first_writes
[i
] <= first_writes
[j
] &&
5292 last_reads
[i
] <= first_writes
[j
]) {
5293 renames
[num_renames
].old_reg
= j
;
5294 renames
[num_renames
].new_reg
= i
;
5297 /* Update the first_writes and last_reads arrays with the new
5298 * values for the merged register index, and mark the newly unused
5299 * register index as such. */
5300 assert(last_reads
[j
] >= last_reads
[i
]);
5301 last_reads
[i
] = last_reads
[j
];
5302 first_writes
[j
] = -1;
5308 rename_temp_registers(num_renames
, renames
);
5309 ralloc_free(renames
);
5310 ralloc_free(last_reads
);
5311 ralloc_free(first_writes
);
5314 /* Reassign indices to temporary registers by reusing unused indices created
5315 * by optimization passes. */
5317 glsl_to_tgsi_visitor::renumber_registers(void)
5321 int *first_reads
= rzalloc_array(mem_ctx
, int, this->next_temp
);
5322 struct rename_reg_pair
*renames
= rzalloc_array(mem_ctx
, struct rename_reg_pair
, this->next_temp
);
5323 int num_renames
= 0;
5324 for (i
= 0; i
< this->next_temp
; i
++) {
5325 first_reads
[i
] = -1;
5327 get_first_temp_read(first_reads
);
5329 for (i
= 0; i
< this->next_temp
; i
++) {
5330 if (first_reads
[i
] < 0) continue;
5331 if (i
!= new_index
) {
5332 renames
[num_renames
].old_reg
= i
;
5333 renames
[num_renames
].new_reg
= new_index
;
5339 rename_temp_registers(num_renames
, renames
);
5340 this->next_temp
= new_index
;
5341 ralloc_free(renames
);
5342 ralloc_free(first_reads
);
5345 /* ------------------------- TGSI conversion stuff -------------------------- */
5348 * Intermediate state used during shader translation.
5350 struct st_translate
{
5351 struct ureg_program
*ureg
;
5353 unsigned temps_size
;
5354 struct ureg_dst
*temps
;
5356 struct ureg_dst
*arrays
;
5357 unsigned num_temp_arrays
;
5358 struct ureg_src
*constants
;
5360 struct ureg_src
*immediates
;
5362 struct ureg_dst outputs
[PIPE_MAX_SHADER_OUTPUTS
];
5363 struct ureg_src inputs
[PIPE_MAX_SHADER_INPUTS
];
5364 struct ureg_dst address
[3];
5365 struct ureg_src samplers
[PIPE_MAX_SAMPLERS
];
5366 struct ureg_src buffers
[PIPE_MAX_SHADER_BUFFERS
];
5367 struct ureg_src images
[PIPE_MAX_SHADER_IMAGES
];
5368 struct ureg_src systemValues
[SYSTEM_VALUE_MAX
];
5369 struct ureg_src shared_memory
;
5370 unsigned *array_sizes
;
5371 struct inout_decl
*input_decls
;
5372 unsigned num_input_decls
;
5373 struct inout_decl
*output_decls
;
5374 unsigned num_output_decls
;
5376 const GLuint
*inputMapping
;
5377 const GLuint
*outputMapping
;
5379 unsigned procType
; /**< PIPE_SHADER_VERTEX/FRAGMENT */
5382 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
5384 _mesa_sysval_to_semantic(unsigned sysval
)
5388 case SYSTEM_VALUE_VERTEX_ID
:
5389 return TGSI_SEMANTIC_VERTEXID
;
5390 case SYSTEM_VALUE_INSTANCE_ID
:
5391 return TGSI_SEMANTIC_INSTANCEID
;
5392 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
5393 return TGSI_SEMANTIC_VERTEXID_NOBASE
;
5394 case SYSTEM_VALUE_BASE_VERTEX
:
5395 return TGSI_SEMANTIC_BASEVERTEX
;
5396 case SYSTEM_VALUE_BASE_INSTANCE
:
5397 return TGSI_SEMANTIC_BASEINSTANCE
;
5398 case SYSTEM_VALUE_DRAW_ID
:
5399 return TGSI_SEMANTIC_DRAWID
;
5401 /* Geometry shader */
5402 case SYSTEM_VALUE_INVOCATION_ID
:
5403 return TGSI_SEMANTIC_INVOCATIONID
;
5405 /* Fragment shader */
5406 case SYSTEM_VALUE_FRAG_COORD
:
5407 return TGSI_SEMANTIC_POSITION
;
5408 case SYSTEM_VALUE_FRONT_FACE
:
5409 return TGSI_SEMANTIC_FACE
;
5410 case SYSTEM_VALUE_SAMPLE_ID
:
5411 return TGSI_SEMANTIC_SAMPLEID
;
5412 case SYSTEM_VALUE_SAMPLE_POS
:
5413 return TGSI_SEMANTIC_SAMPLEPOS
;
5414 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
5415 return TGSI_SEMANTIC_SAMPLEMASK
;
5416 case SYSTEM_VALUE_HELPER_INVOCATION
:
5417 return TGSI_SEMANTIC_HELPER_INVOCATION
;
5419 /* Tessellation shader */
5420 case SYSTEM_VALUE_TESS_COORD
:
5421 return TGSI_SEMANTIC_TESSCOORD
;
5422 case SYSTEM_VALUE_VERTICES_IN
:
5423 return TGSI_SEMANTIC_VERTICESIN
;
5424 case SYSTEM_VALUE_PRIMITIVE_ID
:
5425 return TGSI_SEMANTIC_PRIMID
;
5426 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
5427 return TGSI_SEMANTIC_TESSOUTER
;
5428 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
5429 return TGSI_SEMANTIC_TESSINNER
;
5431 /* Compute shader */
5432 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
5433 return TGSI_SEMANTIC_THREAD_ID
;
5434 case SYSTEM_VALUE_WORK_GROUP_ID
:
5435 return TGSI_SEMANTIC_BLOCK_ID
;
5436 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
5437 return TGSI_SEMANTIC_GRID_SIZE
;
5438 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
5439 return TGSI_SEMANTIC_BLOCK_SIZE
;
5442 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
5443 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
5444 case SYSTEM_VALUE_VERTEX_CNT
:
5446 assert(!"Unexpected SYSTEM_VALUE_ enum");
5447 return TGSI_SEMANTIC_COUNT
;
5452 * Map a glsl_to_tgsi constant/immediate to a TGSI immediate.
5454 static struct ureg_src
5455 emit_immediate(struct st_translate
*t
,
5456 gl_constant_value values
[4],
5459 struct ureg_program
*ureg
= t
->ureg
;
5464 return ureg_DECL_immediate(ureg
, &values
[0].f
, size
);
5466 return ureg_DECL_immediate_f64(ureg
, (double *)&values
[0].f
, size
);
5468 return ureg_DECL_immediate_int64(ureg
, (int64_t *)&values
[0].f
, size
);
5469 case GL_UNSIGNED_INT64_ARB
:
5470 return ureg_DECL_immediate_uint64(ureg
, (uint64_t *)&values
[0].f
, size
);
5472 return ureg_DECL_immediate_int(ureg
, &values
[0].i
, size
);
5473 case GL_UNSIGNED_INT
:
5475 return ureg_DECL_immediate_uint(ureg
, &values
[0].u
, size
);
5477 assert(!"should not get here - type must be float, int, uint, or bool");
5478 return ureg_src_undef();
5483 * Map a glsl_to_tgsi dst register to a TGSI ureg_dst register.
5485 static struct ureg_dst
5486 dst_register(struct st_translate
*t
, gl_register_file file
, unsigned index
,
5492 case PROGRAM_UNDEFINED
:
5493 return ureg_dst_undef();
5495 case PROGRAM_TEMPORARY
:
5496 /* Allocate space for temporaries on demand. */
5497 if (index
>= t
->temps_size
) {
5498 const int inc
= align(index
- t
->temps_size
+ 1, 4096);
5500 t
->temps
= (struct ureg_dst
*)
5502 (t
->temps_size
+ inc
) * sizeof(struct ureg_dst
));
5504 return ureg_dst_undef();
5506 memset(t
->temps
+ t
->temps_size
, 0, inc
* sizeof(struct ureg_dst
));
5507 t
->temps_size
+= inc
;
5510 if (ureg_dst_is_undef(t
->temps
[index
]))
5511 t
->temps
[index
] = ureg_DECL_local_temporary(t
->ureg
);
5513 return t
->temps
[index
];
5516 assert(array_id
&& array_id
<= t
->num_temp_arrays
);
5517 array
= array_id
- 1;
5519 if (ureg_dst_is_undef(t
->arrays
[array
]))
5520 t
->arrays
[array
] = ureg_DECL_array_temporary(
5521 t
->ureg
, t
->array_sizes
[array
], TRUE
);
5523 return ureg_dst_array_offset(t
->arrays
[array
], index
);
5525 case PROGRAM_OUTPUT
:
5527 if (t
->procType
== PIPE_SHADER_FRAGMENT
)
5528 assert(index
< 2 * FRAG_RESULT_MAX
);
5529 else if (t
->procType
== PIPE_SHADER_TESS_CTRL
||
5530 t
->procType
== PIPE_SHADER_TESS_EVAL
)
5531 assert(index
< VARYING_SLOT_TESS_MAX
);
5533 assert(index
< VARYING_SLOT_MAX
);
5535 assert(t
->outputMapping
[index
] < ARRAY_SIZE(t
->outputs
));
5536 assert(t
->outputs
[t
->outputMapping
[index
]].File
!= TGSI_FILE_NULL
);
5537 return t
->outputs
[t
->outputMapping
[index
]];
5540 struct inout_decl
*decl
= find_inout_array(t
->output_decls
, t
->num_output_decls
, array_id
);
5541 unsigned mesa_index
= decl
->mesa_index
;
5542 int slot
= t
->outputMapping
[mesa_index
];
5544 assert(slot
!= -1 && t
->outputs
[slot
].File
== TGSI_FILE_OUTPUT
);
5546 struct ureg_dst dst
= t
->outputs
[slot
];
5547 dst
.ArrayID
= array_id
;
5548 return ureg_dst_array_offset(dst
, index
- mesa_index
);
5551 case PROGRAM_ADDRESS
:
5552 return t
->address
[index
];
5555 assert(!"unknown dst register file");
5556 return ureg_dst_undef();
5561 * Map a glsl_to_tgsi src register to a TGSI ureg_src register.
5563 static struct ureg_src
5564 src_register(struct st_translate
*t
, const st_src_reg
*reg
)
5566 int index
= reg
->index
;
5567 int double_reg2
= reg
->double_reg2
? 1 : 0;
5570 case PROGRAM_UNDEFINED
:
5571 return ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
5573 case PROGRAM_TEMPORARY
:
5575 return ureg_src(dst_register(t
, reg
->file
, reg
->index
, reg
->array_id
));
5577 case PROGRAM_OUTPUT
: {
5578 struct ureg_dst dst
= dst_register(t
, reg
->file
, reg
->index
, reg
->array_id
);
5579 assert(dst
.WriteMask
!= 0);
5580 unsigned shift
= ffs(dst
.WriteMask
) - 1;
5581 return ureg_swizzle(ureg_src(dst
),
5585 MIN2(shift
+ 3, 3));
5588 case PROGRAM_UNIFORM
:
5589 assert(reg
->index
>= 0);
5590 return reg
->index
< t
->num_constants
?
5591 t
->constants
[reg
->index
] : ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
5592 case PROGRAM_STATE_VAR
:
5593 case PROGRAM_CONSTANT
: /* ie, immediate */
5594 if (reg
->has_index2
)
5595 return ureg_src_register(TGSI_FILE_CONSTANT
, reg
->index
);
5597 return reg
->index
>= 0 && reg
->index
< t
->num_constants
?
5598 t
->constants
[reg
->index
] : ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
5600 case PROGRAM_IMMEDIATE
:
5601 assert(reg
->index
>= 0 && reg
->index
< t
->num_immediates
);
5602 return t
->immediates
[reg
->index
];
5605 /* GLSL inputs are 64-bit containers, so we have to
5606 * map back to the original index and add the offset after
5608 index
-= double_reg2
;
5609 if (!reg
->array_id
) {
5610 assert(t
->inputMapping
[index
] < ARRAY_SIZE(t
->inputs
));
5611 assert(t
->inputs
[t
->inputMapping
[index
]].File
!= TGSI_FILE_NULL
);
5612 return t
->inputs
[t
->inputMapping
[index
] + double_reg2
];
5615 struct inout_decl
*decl
= find_inout_array(t
->input_decls
, t
->num_input_decls
, reg
->array_id
);
5616 unsigned mesa_index
= decl
->mesa_index
;
5617 int slot
= t
->inputMapping
[mesa_index
];
5619 assert(slot
!= -1 && t
->inputs
[slot
].File
== TGSI_FILE_INPUT
);
5621 struct ureg_src src
= t
->inputs
[slot
];
5622 src
.ArrayID
= reg
->array_id
;
5623 return ureg_src_array_offset(src
, index
+ double_reg2
- mesa_index
);
5626 case PROGRAM_ADDRESS
:
5627 return ureg_src(t
->address
[reg
->index
]);
5629 case PROGRAM_SYSTEM_VALUE
:
5630 assert(reg
->index
< (int) ARRAY_SIZE(t
->systemValues
));
5631 return t
->systemValues
[reg
->index
];
5634 assert(!"unknown src register file");
5635 return ureg_src_undef();
5640 * Create a TGSI ureg_dst register from an st_dst_reg.
5642 static struct ureg_dst
5643 translate_dst(struct st_translate
*t
,
5644 const st_dst_reg
*dst_reg
,
5647 struct ureg_dst dst
= dst_register(t
, dst_reg
->file
, dst_reg
->index
,
5650 if (dst
.File
== TGSI_FILE_NULL
)
5653 dst
= ureg_writemask(dst
, dst_reg
->writemask
);
5656 dst
= ureg_saturate(dst
);
5658 if (dst_reg
->reladdr
!= NULL
) {
5659 assert(dst_reg
->file
!= PROGRAM_TEMPORARY
);
5660 dst
= ureg_dst_indirect(dst
, ureg_src(t
->address
[0]));
5663 if (dst_reg
->has_index2
) {
5664 if (dst_reg
->reladdr2
)
5665 dst
= ureg_dst_dimension_indirect(dst
, ureg_src(t
->address
[1]),
5668 dst
= ureg_dst_dimension(dst
, dst_reg
->index2D
);
5675 * Create a TGSI ureg_src register from an st_src_reg.
5677 static struct ureg_src
5678 translate_src(struct st_translate
*t
, const st_src_reg
*src_reg
)
5680 struct ureg_src src
= src_register(t
, src_reg
);
5682 if (src_reg
->has_index2
) {
5683 /* 2D indexes occur with geometry shader inputs (attrib, vertex)
5684 * and UBO constant buffers (buffer, position).
5686 if (src_reg
->reladdr2
)
5687 src
= ureg_src_dimension_indirect(src
, ureg_src(t
->address
[1]),
5690 src
= ureg_src_dimension(src
, src_reg
->index2D
);
5693 src
= ureg_swizzle(src
,
5694 GET_SWZ(src_reg
->swizzle
, 0) & 0x3,
5695 GET_SWZ(src_reg
->swizzle
, 1) & 0x3,
5696 GET_SWZ(src_reg
->swizzle
, 2) & 0x3,
5697 GET_SWZ(src_reg
->swizzle
, 3) & 0x3);
5700 src
= ureg_abs(src
);
5702 if ((src_reg
->negate
& 0xf) == NEGATE_XYZW
)
5703 src
= ureg_negate(src
);
5705 if (src_reg
->reladdr
!= NULL
) {
5706 assert(src_reg
->file
!= PROGRAM_TEMPORARY
);
5707 src
= ureg_src_indirect(src
, ureg_src(t
->address
[0]));
5713 static struct tgsi_texture_offset
5714 translate_tex_offset(struct st_translate
*t
,
5715 const st_src_reg
*in_offset
)
5717 struct tgsi_texture_offset offset
;
5718 struct ureg_src src
= translate_src(t
, in_offset
);
5720 offset
.File
= src
.File
;
5721 offset
.Index
= src
.Index
;
5722 offset
.SwizzleX
= src
.SwizzleX
;
5723 offset
.SwizzleY
= src
.SwizzleY
;
5724 offset
.SwizzleZ
= src
.SwizzleZ
;
5727 assert(!src
.Indirect
);
5728 assert(!src
.DimIndirect
);
5729 assert(!src
.Dimension
);
5730 assert(!src
.Absolute
); /* those shouldn't be used with integers anyway */
5731 assert(!src
.Negate
);
5737 compile_tgsi_instruction(struct st_translate
*t
,
5738 const glsl_to_tgsi_instruction
*inst
)
5740 struct ureg_program
*ureg
= t
->ureg
;
5742 struct ureg_dst dst
[2];
5743 struct ureg_src src
[4];
5744 struct tgsi_texture_offset texoffsets
[MAX_GLSL_TEXTURE_OFFSET
];
5748 unsigned tex_target
= 0;
5750 num_dst
= num_inst_dst_regs(inst
);
5751 num_src
= num_inst_src_regs(inst
);
5753 for (i
= 0; i
< num_dst
; i
++)
5754 dst
[i
] = translate_dst(t
,
5758 for (i
= 0; i
< num_src
; i
++)
5759 src
[i
] = translate_src(t
, &inst
->src
[i
]);
5762 case TGSI_OPCODE_BGNLOOP
:
5763 case TGSI_OPCODE_ELSE
:
5764 case TGSI_OPCODE_ENDLOOP
:
5765 case TGSI_OPCODE_IF
:
5766 case TGSI_OPCODE_UIF
:
5767 assert(num_dst
== 0);
5768 ureg_insn(ureg
, inst
->op
, NULL
, 0, src
, num_src
);
5771 case TGSI_OPCODE_TEX
:
5772 case TGSI_OPCODE_TEX_LZ
:
5773 case TGSI_OPCODE_TXB
:
5774 case TGSI_OPCODE_TXD
:
5775 case TGSI_OPCODE_TXL
:
5776 case TGSI_OPCODE_TXP
:
5777 case TGSI_OPCODE_TXQ
:
5778 case TGSI_OPCODE_TXQS
:
5779 case TGSI_OPCODE_TXF
:
5780 case TGSI_OPCODE_TXF_LZ
:
5781 case TGSI_OPCODE_TEX2
:
5782 case TGSI_OPCODE_TXB2
:
5783 case TGSI_OPCODE_TXL2
:
5784 case TGSI_OPCODE_TG4
:
5785 case TGSI_OPCODE_LODQ
:
5786 src
[num_src
] = t
->samplers
[inst
->resource
.index
];
5787 assert(src
[num_src
].File
!= TGSI_FILE_NULL
);
5788 if (inst
->resource
.reladdr
)
5790 ureg_src_indirect(src
[num_src
], ureg_src(t
->address
[2]));
5792 for (i
= 0; i
< (int)inst
->tex_offset_num_offset
; i
++) {
5793 texoffsets
[i
] = translate_tex_offset(t
, &inst
->tex_offsets
[i
]);
5795 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
5801 texoffsets
, inst
->tex_offset_num_offset
,
5805 case TGSI_OPCODE_RESQ
:
5806 case TGSI_OPCODE_LOAD
:
5807 case TGSI_OPCODE_ATOMUADD
:
5808 case TGSI_OPCODE_ATOMXCHG
:
5809 case TGSI_OPCODE_ATOMCAS
:
5810 case TGSI_OPCODE_ATOMAND
:
5811 case TGSI_OPCODE_ATOMOR
:
5812 case TGSI_OPCODE_ATOMXOR
:
5813 case TGSI_OPCODE_ATOMUMIN
:
5814 case TGSI_OPCODE_ATOMUMAX
:
5815 case TGSI_OPCODE_ATOMIMIN
:
5816 case TGSI_OPCODE_ATOMIMAX
:
5817 for (i
= num_src
- 1; i
>= 0; i
--)
5818 src
[i
+ 1] = src
[i
];
5820 if (inst
->resource
.file
== PROGRAM_MEMORY
) {
5821 src
[0] = t
->shared_memory
;
5822 } else if (inst
->resource
.file
== PROGRAM_BUFFER
) {
5823 src
[0] = t
->buffers
[inst
->resource
.index
];
5825 src
[0] = t
->images
[inst
->resource
.index
];
5826 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
5828 if (inst
->resource
.reladdr
)
5829 src
[0] = ureg_src_indirect(src
[0], ureg_src(t
->address
[2]));
5830 assert(src
[0].File
!= TGSI_FILE_NULL
);
5831 ureg_memory_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
,
5832 inst
->buffer_access
,
5833 tex_target
, inst
->image_format
);
5836 case TGSI_OPCODE_STORE
:
5837 if (inst
->resource
.file
== PROGRAM_MEMORY
) {
5838 dst
[0] = ureg_dst(t
->shared_memory
);
5839 } else if (inst
->resource
.file
== PROGRAM_BUFFER
) {
5840 dst
[0] = ureg_dst(t
->buffers
[inst
->resource
.index
]);
5842 dst
[0] = ureg_dst(t
->images
[inst
->resource
.index
]);
5843 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
5845 dst
[0] = ureg_writemask(dst
[0], inst
->dst
[0].writemask
);
5846 if (inst
->resource
.reladdr
)
5847 dst
[0] = ureg_dst_indirect(dst
[0], ureg_src(t
->address
[2]));
5848 assert(dst
[0].File
!= TGSI_FILE_NULL
);
5849 ureg_memory_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
,
5850 inst
->buffer_access
,
5851 tex_target
, inst
->image_format
);
5854 case TGSI_OPCODE_SCS
:
5855 dst
[0] = ureg_writemask(dst
[0], TGSI_WRITEMASK_XY
);
5856 ureg_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
);
5869 * Emit the TGSI instructions for inverting and adjusting WPOS.
5870 * This code is unavoidable because it also depends on whether
5871 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
5874 emit_wpos_adjustment(struct gl_context
*ctx
,
5875 struct st_translate
*t
,
5876 int wpos_transform_const
,
5878 GLfloat adjX
, GLfloat adjY
[2])
5880 struct ureg_program
*ureg
= t
->ureg
;
5882 assert(wpos_transform_const
>= 0);
5884 /* Fragment program uses fragment position input.
5885 * Need to replace instances of INPUT[WPOS] with temp T
5886 * where T = INPUT[WPOS] is inverted by Y.
5888 struct ureg_src wpostrans
= ureg_DECL_constant(ureg
, wpos_transform_const
);
5889 struct ureg_dst wpos_temp
= ureg_DECL_temporary( ureg
);
5890 struct ureg_src
*wpos
=
5891 ctx
->Const
.GLSLFragCoordIsSysVal
?
5892 &t
->systemValues
[SYSTEM_VALUE_FRAG_COORD
] :
5893 &t
->inputs
[t
->inputMapping
[VARYING_SLOT_POS
]];
5894 struct ureg_src wpos_input
= *wpos
;
5896 /* First, apply the coordinate shift: */
5897 if (adjX
|| adjY
[0] || adjY
[1]) {
5898 if (adjY
[0] != adjY
[1]) {
5899 /* Adjust the y coordinate by adjY[1] or adjY[0] respectively
5900 * depending on whether inversion is actually going to be applied
5901 * or not, which is determined by testing against the inversion
5902 * state variable used below, which will be either +1 or -1.
5904 struct ureg_dst adj_temp
= ureg_DECL_local_temporary(ureg
);
5906 ureg_CMP(ureg
, adj_temp
,
5907 ureg_scalar(wpostrans
, invert
? 2 : 0),
5908 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
),
5909 ureg_imm4f(ureg
, adjX
, adjY
[1], 0.0f
, 0.0f
));
5910 ureg_ADD(ureg
, wpos_temp
, wpos_input
, ureg_src(adj_temp
));
5912 ureg_ADD(ureg
, wpos_temp
, wpos_input
,
5913 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
));
5915 wpos_input
= ureg_src(wpos_temp
);
5917 /* MOV wpos_temp, input[wpos]
5919 ureg_MOV( ureg
, wpos_temp
, wpos_input
);
5922 /* Now the conditional y flip: STATE_FB_WPOS_Y_TRANSFORM.xy/zw will be
5923 * inversion/identity, or the other way around if we're drawing to an FBO.
5926 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
5929 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
5931 ureg_scalar(wpostrans
, 0),
5932 ureg_scalar(wpostrans
, 1));
5934 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
5937 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
5939 ureg_scalar(wpostrans
, 2),
5940 ureg_scalar(wpostrans
, 3));
5943 /* Use wpos_temp as position input from here on:
5945 *wpos
= ureg_src(wpos_temp
);
5950 * Emit fragment position/ooordinate code.
5953 emit_wpos(struct st_context
*st
,
5954 struct st_translate
*t
,
5955 const struct gl_program
*program
,
5956 struct ureg_program
*ureg
,
5957 int wpos_transform_const
)
5959 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
5960 GLfloat adjX
= 0.0f
;
5961 GLfloat adjY
[2] = { 0.0f
, 0.0f
};
5962 boolean invert
= FALSE
;
5964 /* Query the pixel center conventions supported by the pipe driver and set
5965 * adjX, adjY to help out if it cannot handle the requested one internally.
5967 * The bias of the y-coordinate depends on whether y-inversion takes place
5968 * (adjY[1]) or not (adjY[0]), which is in turn dependent on whether we are
5969 * drawing to an FBO (causes additional inversion), and whether the pipe
5970 * driver origin and the requested origin differ (the latter condition is
5971 * stored in the 'invert' variable).
5973 * For height = 100 (i = integer, h = half-integer, l = lower, u = upper):
5975 * center shift only:
5980 * l,i -> u,i: ( 0.0 + 1.0) * -1 + 100 = 99
5981 * l,h -> u,h: ( 0.5 + 0.0) * -1 + 100 = 99.5
5982 * u,i -> l,i: (99.0 + 1.0) * -1 + 100 = 0
5983 * u,h -> l,h: (99.5 + 0.0) * -1 + 100 = 0.5
5985 * inversion and center shift:
5986 * l,i -> u,h: ( 0.0 + 0.5) * -1 + 100 = 99.5
5987 * l,h -> u,i: ( 0.5 + 0.5) * -1 + 100 = 99
5988 * u,i -> l,h: (99.0 + 0.5) * -1 + 100 = 0.5
5989 * u,h -> l,i: (99.5 + 0.5) * -1 + 100 = 0
5991 if (program
->OriginUpperLeft
) {
5992 /* Fragment shader wants origin in upper-left */
5993 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
)) {
5994 /* the driver supports upper-left origin */
5996 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
)) {
5997 /* the driver supports lower-left origin, need to invert Y */
5998 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_ORIGIN
,
5999 TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
6006 /* Fragment shader wants origin in lower-left */
6007 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
))
6008 /* the driver supports lower-left origin */
6009 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_ORIGIN
,
6010 TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
6011 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
))
6012 /* the driver supports upper-left origin, need to invert Y */
6018 if (program
->PixelCenterInteger
) {
6019 /* Fragment shader wants pixel center integer */
6020 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
6021 /* the driver supports pixel center integer */
6023 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
,
6024 TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
6026 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
6027 /* the driver supports pixel center half integer, need to bias X,Y */
6036 /* Fragment shader wants pixel center half integer */
6037 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
6038 /* the driver supports pixel center half integer */
6040 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
6041 /* the driver supports pixel center integer, need to bias X,Y */
6042 adjX
= adjY
[0] = adjY
[1] = 0.5f
;
6043 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
,
6044 TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
6050 /* we invert after adjustment so that we avoid the MOV to temporary,
6051 * and reuse the adjustment ADD instead */
6052 emit_wpos_adjustment(st
->ctx
, t
, wpos_transform_const
, invert
, adjX
, adjY
);
6056 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
6057 * TGSI uses +1 for front, -1 for back.
6058 * This function converts the TGSI value to the GL value. Simply clamping/
6059 * saturating the value to [0,1] does the job.
6062 emit_face_var(struct gl_context
*ctx
, struct st_translate
*t
)
6064 struct ureg_program
*ureg
= t
->ureg
;
6065 struct ureg_dst face_temp
= ureg_DECL_temporary(ureg
);
6066 struct ureg_src face_input
= t
->inputs
[t
->inputMapping
[VARYING_SLOT_FACE
]];
6068 if (ctx
->Const
.NativeIntegers
) {
6069 ureg_FSGE(ureg
, face_temp
, face_input
, ureg_imm1f(ureg
, 0));
6072 /* MOV_SAT face_temp, input[face] */
6073 ureg_MOV(ureg
, ureg_saturate(face_temp
), face_input
);
6076 /* Use face_temp as face input from here on: */
6077 t
->inputs
[t
->inputMapping
[VARYING_SLOT_FACE
]] = ureg_src(face_temp
);
6081 emit_compute_block_size(const struct gl_program
*prog
,
6082 struct ureg_program
*ureg
) {
6083 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
,
6084 prog
->info
.cs
.local_size
[0]);
6085 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
,
6086 prog
->info
.cs
.local_size
[1]);
6087 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
,
6088 prog
->info
.cs
.local_size
[2]);
6091 struct sort_inout_decls
{
6092 bool operator()(const struct inout_decl
&a
, const struct inout_decl
&b
) const {
6093 return mapping
[a
.mesa_index
] < mapping
[b
.mesa_index
];
6096 const GLuint
*mapping
;
6099 /* Sort the given array of decls by the corresponding slot (TGSI file index).
6101 * This is for the benefit of older drivers which are broken when the
6102 * declarations aren't sorted in this way.
6105 sort_inout_decls_by_slot(struct inout_decl
*decls
,
6107 const GLuint mapping
[])
6109 sort_inout_decls sorter
;
6110 sorter
.mapping
= mapping
;
6111 std::sort(decls
, decls
+ count
, sorter
);
6115 st_translate_interp(enum glsl_interp_mode glsl_qual
, GLuint varying
)
6117 switch (glsl_qual
) {
6118 case INTERP_MODE_NONE
:
6119 if (varying
== VARYING_SLOT_COL0
|| varying
== VARYING_SLOT_COL1
)
6120 return TGSI_INTERPOLATE_COLOR
;
6121 return TGSI_INTERPOLATE_PERSPECTIVE
;
6122 case INTERP_MODE_SMOOTH
:
6123 return TGSI_INTERPOLATE_PERSPECTIVE
;
6124 case INTERP_MODE_FLAT
:
6125 return TGSI_INTERPOLATE_CONSTANT
;
6126 case INTERP_MODE_NOPERSPECTIVE
:
6127 return TGSI_INTERPOLATE_LINEAR
;
6129 assert(0 && "unexpected interp mode in st_translate_interp()");
6130 return TGSI_INTERPOLATE_PERSPECTIVE
;
6135 * Translate intermediate IR (glsl_to_tgsi_instruction) to TGSI format.
6136 * \param program the program to translate
6137 * \param numInputs number of input registers used
6138 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
6140 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
6141 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
6143 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
6144 * \param numOutputs number of output registers used
6145 * \param outputMapping maps Mesa fragment program outputs to TGSI
6147 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
6148 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
6151 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
6153 extern "C" enum pipe_error
6154 st_translate_program(
6155 struct gl_context
*ctx
,
6157 struct ureg_program
*ureg
,
6158 glsl_to_tgsi_visitor
*program
,
6159 const struct gl_program
*proginfo
,
6161 const GLuint inputMapping
[],
6162 const GLuint inputSlotToAttr
[],
6163 const ubyte inputSemanticName
[],
6164 const ubyte inputSemanticIndex
[],
6165 const GLuint interpMode
[],
6167 const GLuint outputMapping
[],
6168 const GLuint outputSlotToAttr
[],
6169 const ubyte outputSemanticName
[],
6170 const ubyte outputSemanticIndex
[])
6172 struct st_translate
*t
;
6174 struct gl_program_constants
*frag_const
=
6175 &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
6176 enum pipe_error ret
= PIPE_OK
;
6178 assert(numInputs
<= ARRAY_SIZE(t
->inputs
));
6179 assert(numOutputs
<= ARRAY_SIZE(t
->outputs
));
6181 t
= CALLOC_STRUCT(st_translate
);
6183 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
6187 t
->procType
= procType
;
6188 t
->inputMapping
= inputMapping
;
6189 t
->outputMapping
= outputMapping
;
6191 t
->num_temp_arrays
= program
->next_array
;
6192 if (t
->num_temp_arrays
)
6193 t
->arrays
= (struct ureg_dst
*)
6194 calloc(t
->num_temp_arrays
, sizeof(t
->arrays
[0]));
6197 * Declare input attributes.
6200 case PIPE_SHADER_FRAGMENT
:
6201 case PIPE_SHADER_GEOMETRY
:
6202 case PIPE_SHADER_TESS_EVAL
:
6203 case PIPE_SHADER_TESS_CTRL
:
6204 sort_inout_decls_by_slot(program
->inputs
, program
->num_inputs
, inputMapping
);
6206 for (i
= 0; i
< program
->num_inputs
; ++i
) {
6207 struct inout_decl
*decl
= &program
->inputs
[i
];
6208 unsigned slot
= inputMapping
[decl
->mesa_index
];
6209 struct ureg_src src
;
6210 ubyte tgsi_usage_mask
= decl
->usage_mask
;
6212 if (glsl_base_type_is_64bit(decl
->base_type
)) {
6213 if (tgsi_usage_mask
== 1)
6214 tgsi_usage_mask
= TGSI_WRITEMASK_XY
;
6215 else if (tgsi_usage_mask
== 2)
6216 tgsi_usage_mask
= TGSI_WRITEMASK_ZW
;
6218 tgsi_usage_mask
= TGSI_WRITEMASK_XYZW
;
6221 unsigned interp_mode
= 0;
6222 unsigned interp_location
= 0;
6223 if (procType
== PIPE_SHADER_FRAGMENT
) {
6225 interp_mode
= interpMode
[slot
] != TGSI_INTERPOLATE_COUNT
?
6227 st_translate_interp(decl
->interp
, inputSlotToAttr
[slot
]);
6229 interp_location
= decl
->interp_loc
;
6232 src
= ureg_DECL_fs_input_cyl_centroid_layout(ureg
,
6233 inputSemanticName
[slot
], inputSemanticIndex
[slot
],
6234 interp_mode
, 0, interp_location
, slot
, tgsi_usage_mask
,
6235 decl
->array_id
, decl
->size
);
6237 for (unsigned j
= 0; j
< decl
->size
; ++j
) {
6238 if (t
->inputs
[slot
+ j
].File
!= TGSI_FILE_INPUT
) {
6239 /* The ArrayID is set up in dst_register */
6240 t
->inputs
[slot
+ j
] = src
;
6241 t
->inputs
[slot
+ j
].ArrayID
= 0;
6242 t
->inputs
[slot
+ j
].Index
+= j
;
6247 case PIPE_SHADER_VERTEX
:
6248 for (i
= 0; i
< numInputs
; i
++) {
6249 t
->inputs
[i
] = ureg_DECL_vs_input(ureg
, i
);
6252 case PIPE_SHADER_COMPUTE
:
6259 * Declare output attributes.
6262 case PIPE_SHADER_FRAGMENT
:
6263 case PIPE_SHADER_COMPUTE
:
6265 case PIPE_SHADER_GEOMETRY
:
6266 case PIPE_SHADER_TESS_EVAL
:
6267 case PIPE_SHADER_TESS_CTRL
:
6268 case PIPE_SHADER_VERTEX
:
6269 sort_inout_decls_by_slot(program
->outputs
, program
->num_outputs
, outputMapping
);
6271 for (i
= 0; i
< program
->num_outputs
; ++i
) {
6272 struct inout_decl
*decl
= &program
->outputs
[i
];
6273 unsigned slot
= outputMapping
[decl
->mesa_index
];
6274 struct ureg_dst dst
;
6275 ubyte tgsi_usage_mask
= decl
->usage_mask
;
6277 if (glsl_base_type_is_64bit(decl
->base_type
)) {
6278 if (tgsi_usage_mask
== 1)
6279 tgsi_usage_mask
= TGSI_WRITEMASK_XY
;
6280 else if (tgsi_usage_mask
== 2)
6281 tgsi_usage_mask
= TGSI_WRITEMASK_ZW
;
6283 tgsi_usage_mask
= TGSI_WRITEMASK_XYZW
;
6286 dst
= ureg_DECL_output_layout(ureg
,
6287 outputSemanticName
[slot
], outputSemanticIndex
[slot
],
6288 decl
->gs_out_streams
,
6289 slot
, tgsi_usage_mask
, decl
->array_id
, decl
->size
);
6291 for (unsigned j
= 0; j
< decl
->size
; ++j
) {
6292 if (t
->outputs
[slot
+ j
].File
!= TGSI_FILE_OUTPUT
) {
6293 /* The ArrayID is set up in dst_register */
6294 t
->outputs
[slot
+ j
] = dst
;
6295 t
->outputs
[slot
+ j
].ArrayID
= 0;
6296 t
->outputs
[slot
+ j
].Index
+= j
;
6305 if (procType
== PIPE_SHADER_FRAGMENT
) {
6306 if (program
->shader
->Program
->info
.fs
.early_fragment_tests
)
6307 ureg_property(ureg
, TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL
, 1);
6309 if (proginfo
->info
.inputs_read
& VARYING_BIT_POS
) {
6310 /* Must do this after setting up t->inputs. */
6311 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
,
6312 program
->wpos_transform_const
);
6315 if (proginfo
->info
.inputs_read
& VARYING_BIT_FACE
)
6316 emit_face_var(ctx
, t
);
6318 for (i
= 0; i
< numOutputs
; i
++) {
6319 switch (outputSemanticName
[i
]) {
6320 case TGSI_SEMANTIC_POSITION
:
6321 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6322 TGSI_SEMANTIC_POSITION
, /* Z/Depth */
6323 outputSemanticIndex
[i
]);
6324 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Z
);
6326 case TGSI_SEMANTIC_STENCIL
:
6327 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6328 TGSI_SEMANTIC_STENCIL
, /* Stencil */
6329 outputSemanticIndex
[i
]);
6330 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Y
);
6332 case TGSI_SEMANTIC_COLOR
:
6333 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6334 TGSI_SEMANTIC_COLOR
,
6335 outputSemanticIndex
[i
]);
6337 case TGSI_SEMANTIC_SAMPLEMASK
:
6338 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6339 TGSI_SEMANTIC_SAMPLEMASK
,
6340 outputSemanticIndex
[i
]);
6341 /* TODO: If we ever support more than 32 samples, this will have
6342 * to become an array.
6344 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_X
);
6347 assert(!"fragment shader outputs must be POSITION/STENCIL/COLOR");
6348 ret
= PIPE_ERROR_BAD_INPUT
;
6353 else if (procType
== PIPE_SHADER_VERTEX
) {
6354 for (i
= 0; i
< numOutputs
; i
++) {
6355 if (outputSemanticName
[i
] == TGSI_SEMANTIC_FOG
) {
6356 /* force register to contain a fog coordinate in the form (F, 0, 0, 1). */
6358 ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_YZW
),
6359 ureg_imm4f(ureg
, 0.0f
, 0.0f
, 0.0f
, 1.0f
));
6360 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_X
);
6365 if (procType
== PIPE_SHADER_COMPUTE
) {
6366 emit_compute_block_size(proginfo
, ureg
);
6369 /* Declare address register.
6371 if (program
->num_address_regs
> 0) {
6372 assert(program
->num_address_regs
<= 3);
6373 for (int i
= 0; i
< program
->num_address_regs
; i
++)
6374 t
->address
[i
] = ureg_DECL_address(ureg
);
6377 /* Declare misc input registers
6380 GLbitfield sysInputs
= proginfo
->info
.system_values_read
;
6382 for (i
= 0; sysInputs
; i
++) {
6383 if (sysInputs
& (1 << i
)) {
6384 unsigned semName
= _mesa_sysval_to_semantic(i
);
6386 t
->systemValues
[i
] = ureg_DECL_system_value(ureg
, semName
, 0);
6388 if (semName
== TGSI_SEMANTIC_INSTANCEID
||
6389 semName
== TGSI_SEMANTIC_VERTEXID
) {
6390 /* From Gallium perspective, these system values are always
6391 * integer, and require native integer support. However, if
6392 * native integer is supported on the vertex stage but not the
6393 * pixel stage (e.g, i915g + draw), Mesa will generate IR that
6394 * assumes these system values are floats. To resolve the
6395 * inconsistency, we insert a U2F.
6397 struct st_context
*st
= st_context(ctx
);
6398 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
6399 assert(procType
== PIPE_SHADER_VERTEX
);
6400 assert(pscreen
->get_shader_param(pscreen
, PIPE_SHADER_VERTEX
, PIPE_SHADER_CAP_INTEGERS
));
6402 if (!ctx
->Const
.NativeIntegers
) {
6403 struct ureg_dst temp
= ureg_DECL_local_temporary(t
->ureg
);
6404 ureg_U2F( t
->ureg
, ureg_writemask(temp
, TGSI_WRITEMASK_X
), t
->systemValues
[i
]);
6405 t
->systemValues
[i
] = ureg_scalar(ureg_src(temp
), 0);
6409 if (procType
== PIPE_SHADER_FRAGMENT
&&
6410 semName
== TGSI_SEMANTIC_POSITION
)
6411 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
,
6412 program
->wpos_transform_const
);
6414 sysInputs
&= ~(1 << i
);
6419 t
->array_sizes
= program
->array_sizes
;
6420 t
->input_decls
= program
->inputs
;
6421 t
->num_input_decls
= program
->num_inputs
;
6422 t
->output_decls
= program
->outputs
;
6423 t
->num_output_decls
= program
->num_outputs
;
6425 /* Emit constants and uniforms. TGSI uses a single index space for these,
6426 * so we put all the translated regs in t->constants.
6428 if (proginfo
->Parameters
) {
6429 t
->constants
= (struct ureg_src
*)
6430 calloc(proginfo
->Parameters
->NumParameters
, sizeof(t
->constants
[0]));
6431 if (t
->constants
== NULL
) {
6432 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
6435 t
->num_constants
= proginfo
->Parameters
->NumParameters
;
6437 for (i
= 0; i
< proginfo
->Parameters
->NumParameters
; i
++) {
6438 switch (proginfo
->Parameters
->Parameters
[i
].Type
) {
6439 case PROGRAM_STATE_VAR
:
6440 case PROGRAM_UNIFORM
:
6441 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
6444 /* Emit immediates for PROGRAM_CONSTANT only when there's no indirect
6445 * addressing of the const buffer.
6446 * FIXME: Be smarter and recognize param arrays:
6447 * indirect addressing is only valid within the referenced
6450 case PROGRAM_CONSTANT
:
6451 if (program
->indirect_addr_consts
)
6452 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
6454 t
->constants
[i
] = emit_immediate(t
,
6455 proginfo
->Parameters
->ParameterValues
[i
],
6456 proginfo
->Parameters
->Parameters
[i
].DataType
,
6465 for (i
= 0; i
< proginfo
->info
.num_ubos
; i
++) {
6466 unsigned size
= proginfo
->sh
.UniformBlocks
[i
]->UniformBufferSize
;
6467 unsigned num_const_vecs
= (size
+ 15) / 16;
6468 unsigned first
, last
;
6469 assert(num_const_vecs
> 0);
6471 last
= num_const_vecs
> 0 ? num_const_vecs
- 1 : 0;
6472 ureg_DECL_constant2D(t
->ureg
, first
, last
, i
+ 1);
6475 /* Emit immediate values.
6477 t
->immediates
= (struct ureg_src
*)
6478 calloc(program
->num_immediates
, sizeof(struct ureg_src
));
6479 if (t
->immediates
== NULL
) {
6480 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
6483 t
->num_immediates
= program
->num_immediates
;
6486 foreach_in_list(immediate_storage
, imm
, &program
->immediates
) {
6487 assert(i
< program
->num_immediates
);
6488 t
->immediates
[i
++] = emit_immediate(t
, imm
->values
, imm
->type
, imm
->size32
);
6490 assert(i
== program
->num_immediates
);
6492 /* texture samplers */
6493 for (i
= 0; i
< frag_const
->MaxTextureImageUnits
; i
++) {
6494 if (program
->samplers_used
& (1u << i
)) {
6497 t
->samplers
[i
] = ureg_DECL_sampler(ureg
, i
);
6499 switch (program
->sampler_types
[i
]) {
6501 type
= TGSI_RETURN_TYPE_SINT
;
6503 case GLSL_TYPE_UINT
:
6504 type
= TGSI_RETURN_TYPE_UINT
;
6506 case GLSL_TYPE_FLOAT
:
6507 type
= TGSI_RETURN_TYPE_FLOAT
;
6510 unreachable("not reached");
6513 ureg_DECL_sampler_view( ureg
, i
, program
->sampler_targets
[i
],
6514 type
, type
, type
, type
);
6518 for (i
= 0; i
< frag_const
->MaxAtomicBuffers
; i
++) {
6519 if (program
->buffers_used
& (1 << i
)) {
6520 t
->buffers
[i
] = ureg_DECL_buffer(ureg
, i
, true);
6524 for (; i
< frag_const
->MaxAtomicBuffers
+ frag_const
->MaxShaderStorageBlocks
;
6526 if (program
->buffers_used
& (1 << i
)) {
6527 t
->buffers
[i
] = ureg_DECL_buffer(ureg
, i
, false);
6531 if (program
->use_shared_memory
)
6532 t
->shared_memory
= ureg_DECL_memory(ureg
, TGSI_MEMORY_TYPE_SHARED
);
6534 for (i
= 0; i
< program
->shader
->Program
->info
.num_images
; i
++) {
6535 if (program
->images_used
& (1 << i
)) {
6536 t
->images
[i
] = ureg_DECL_image(ureg
, i
,
6537 program
->image_targets
[i
],
6538 program
->image_formats
[i
],
6543 /* Emit each instruction in turn:
6545 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &program
->instructions
)
6546 compile_tgsi_instruction(t
, inst
);
6548 /* Set the next shader stage hint for VS and TES. */
6550 case PIPE_SHADER_VERTEX
:
6551 case PIPE_SHADER_TESS_EVAL
:
6552 if (program
->shader_program
->SeparateShader
)
6555 for (i
= program
->shader
->Stage
+1; i
<= MESA_SHADER_FRAGMENT
; i
++) {
6556 if (program
->shader_program
->_LinkedShaders
[i
]) {
6560 case MESA_SHADER_TESS_CTRL
:
6561 next
= PIPE_SHADER_TESS_CTRL
;
6563 case MESA_SHADER_TESS_EVAL
:
6564 next
= PIPE_SHADER_TESS_EVAL
;
6566 case MESA_SHADER_GEOMETRY
:
6567 next
= PIPE_SHADER_GEOMETRY
;
6569 case MESA_SHADER_FRAGMENT
:
6570 next
= PIPE_SHADER_FRAGMENT
;
6577 ureg_set_next_shader_processor(ureg
, next
);
6589 t
->num_constants
= 0;
6590 free(t
->immediates
);
6591 t
->num_immediates
= 0;
6597 /* ----------------------------- End TGSI code ------------------------------ */
6601 * Convert a shader's GLSL IR into a Mesa gl_program, although without
6602 * generating Mesa IR.
6604 static struct gl_program
*
6605 get_mesa_program_tgsi(struct gl_context
*ctx
,
6606 struct gl_shader_program
*shader_program
,
6607 struct gl_linked_shader
*shader
)
6609 glsl_to_tgsi_visitor
* v
;
6610 struct gl_program
*prog
;
6611 struct gl_shader_compiler_options
*options
=
6612 &ctx
->Const
.ShaderCompilerOptions
[shader
->Stage
];
6613 struct pipe_screen
*pscreen
= ctx
->st
->pipe
->screen
;
6614 enum pipe_shader_type ptarget
= st_shader_stage_to_ptarget(shader
->Stage
);
6616 validate_ir_tree(shader
->ir
);
6618 prog
= shader
->Program
;
6620 prog
->Parameters
= _mesa_new_parameter_list();
6621 v
= new glsl_to_tgsi_visitor();
6624 v
->shader_program
= shader_program
;
6626 v
->options
= options
;
6627 v
->glsl_version
= ctx
->Const
.GLSLVersion
;
6628 v
->native_integers
= ctx
->Const
.NativeIntegers
;
6630 v
->have_sqrt
= pscreen
->get_shader_param(pscreen
, ptarget
,
6631 PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED
);
6632 v
->have_fma
= pscreen
->get_shader_param(pscreen
, ptarget
,
6633 PIPE_SHADER_CAP_TGSI_FMA_SUPPORTED
);
6634 v
->has_tex_txf_lz
= pscreen
->get_param(pscreen
,
6635 PIPE_CAP_TGSI_TEX_TXF_LZ
);
6637 _mesa_generate_parameters_list_for_uniforms(shader_program
, shader
,
6640 /* Remove reads from output registers. */
6641 if (!pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_CAN_READ_OUTPUTS
))
6642 lower_output_reads(shader
->Stage
, shader
->ir
);
6644 /* Emit intermediate IR for main(). */
6645 visit_exec_list(shader
->ir
, v
);
6648 /* Print out some information (for debugging purposes) used by the
6649 * optimization passes. */
6652 int *first_writes
= rzalloc_array(v
->mem_ctx
, int, v
->next_temp
);
6653 int *first_reads
= rzalloc_array(v
->mem_ctx
, int, v
->next_temp
);
6654 int *last_writes
= rzalloc_array(v
->mem_ctx
, int, v
->next_temp
);
6655 int *last_reads
= rzalloc_array(v
->mem_ctx
, int, v
->next_temp
);
6657 for (i
= 0; i
< v
->next_temp
; i
++) {
6658 first_writes
[i
] = -1;
6659 first_reads
[i
] = -1;
6660 last_writes
[i
] = -1;
6663 v
->get_first_temp_read(first_reads
);
6664 v
->get_last_temp_read_first_temp_write(last_reads
, first_writes
);
6665 v
->get_last_temp_write(last_writes
);
6666 for (i
= 0; i
< v
->next_temp
; i
++)
6667 printf("Temp %d: FR=%3d FW=%3d LR=%3d LW=%3d\n", i
, first_reads
[i
],
6671 ralloc_free(first_writes
);
6672 ralloc_free(first_reads
);
6673 ralloc_free(last_writes
);
6674 ralloc_free(last_reads
);
6678 /* Perform optimizations on the instructions in the glsl_to_tgsi_visitor. */
6681 if (shader
->Stage
!= MESA_SHADER_TESS_CTRL
&&
6682 shader
->Stage
!= MESA_SHADER_TESS_EVAL
)
6683 v
->copy_propagate();
6685 while (v
->eliminate_dead_code());
6687 v
->merge_two_dsts();
6688 v
->merge_registers();
6689 v
->renumber_registers();
6691 /* Write the END instruction. */
6692 v
->emit_asm(NULL
, TGSI_OPCODE_END
);
6694 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
6696 _mesa_log("GLSL IR for linked %s program %d:\n",
6697 _mesa_shader_stage_to_string(shader
->Stage
),
6698 shader_program
->Name
);
6699 _mesa_print_ir(_mesa_get_log_file(), shader
->ir
, NULL
);
6703 do_set_program_inouts(shader
->ir
, prog
, shader
->Stage
);
6704 _mesa_copy_linked_program_data(shader_program
, shader
);
6705 shrink_array_declarations(v
->inputs
, v
->num_inputs
,
6706 &prog
->info
.inputs_read
,
6707 prog
->info
.double_inputs_read
,
6708 &prog
->info
.patch_inputs_read
);
6709 shrink_array_declarations(v
->outputs
, v
->num_outputs
,
6710 &prog
->info
.outputs_written
, 0ULL,
6711 &prog
->info
.patch_outputs_written
);
6712 count_resources(v
, prog
);
6714 /* The GLSL IR won't be needed anymore. */
6715 ralloc_free(shader
->ir
);
6718 /* This must be done before the uniform storage is associated. */
6719 if (shader
->Stage
== MESA_SHADER_FRAGMENT
&&
6720 (prog
->info
.inputs_read
& VARYING_BIT_POS
||
6721 prog
->info
.system_values_read
& (1 << SYSTEM_VALUE_FRAG_COORD
))) {
6722 static const gl_state_index wposTransformState
[STATE_LENGTH
] = {
6723 STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
6726 v
->wpos_transform_const
= _mesa_add_state_reference(prog
->Parameters
,
6727 wposTransformState
);
6730 /* Avoid reallocation of the program parameter list, because the uniform
6731 * storage is only associated with the original parameter list.
6732 * This should be enough for Bitmap and DrawPixels constants.
6734 _mesa_reserve_parameter_storage(prog
->Parameters
, 8);
6736 /* This has to be done last. Any operation the can cause
6737 * prog->ParameterValues to get reallocated (e.g., anything that adds a
6738 * program constant) has to happen before creating this linkage.
6740 _mesa_associate_uniform_storage(ctx
, shader_program
, prog
->Parameters
,
6742 if (!shader_program
->data
->LinkStatus
) {
6743 free_glsl_to_tgsi_visitor(v
);
6744 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
6748 struct st_vertex_program
*stvp
;
6749 struct st_fragment_program
*stfp
;
6750 struct st_geometry_program
*stgp
;
6751 struct st_tessctrl_program
*sttcp
;
6752 struct st_tesseval_program
*sttep
;
6753 struct st_compute_program
*stcp
;
6755 switch (shader
->Stage
) {
6756 case MESA_SHADER_VERTEX
:
6757 stvp
= (struct st_vertex_program
*)prog
;
6758 stvp
->glsl_to_tgsi
= v
;
6760 case MESA_SHADER_FRAGMENT
:
6761 stfp
= (struct st_fragment_program
*)prog
;
6762 stfp
->glsl_to_tgsi
= v
;
6764 case MESA_SHADER_GEOMETRY
:
6765 stgp
= (struct st_geometry_program
*)prog
;
6766 stgp
->glsl_to_tgsi
= v
;
6768 case MESA_SHADER_TESS_CTRL
:
6769 sttcp
= (struct st_tessctrl_program
*)prog
;
6770 sttcp
->glsl_to_tgsi
= v
;
6772 case MESA_SHADER_TESS_EVAL
:
6773 sttep
= (struct st_tesseval_program
*)prog
;
6774 sttep
->glsl_to_tgsi
= v
;
6776 case MESA_SHADER_COMPUTE
:
6777 stcp
= (struct st_compute_program
*)prog
;
6778 stcp
->glsl_to_tgsi
= v
;
6781 assert(!"should not be reached");
6788 /* See if there are unsupported control flow statements. */
6789 class ir_control_flow_info_visitor
: public ir_hierarchical_visitor
{
6791 const struct gl_shader_compiler_options
*options
;
6793 ir_control_flow_info_visitor(const struct gl_shader_compiler_options
*options
)
6799 virtual ir_visitor_status
visit_enter(ir_function
*ir
)
6801 /* Other functions are skipped (same as glsl_to_tgsi). */
6802 if (strcmp(ir
->name
, "main") == 0)
6803 return visit_continue
;
6805 return visit_continue_with_parent
;
6808 virtual ir_visitor_status
visit_enter(ir_call
*ir
)
6810 if (!ir
->callee
->is_intrinsic()) {
6811 unsupported
= true; /* it's a function call */
6814 return visit_continue
;
6817 virtual ir_visitor_status
visit_enter(ir_return
*ir
)
6819 if (options
->EmitNoMainReturn
) {
6823 return visit_continue
;
6830 has_unsupported_control_flow(exec_list
*ir
,
6831 const struct gl_shader_compiler_options
*options
)
6833 ir_control_flow_info_visitor
visitor(options
);
6834 visit_list_elements(&visitor
, ir
);
6835 return visitor
.unsupported
;
6842 * Called via ctx->Driver.LinkShader()
6843 * This actually involves converting GLSL IR into an intermediate TGSI-like IR
6844 * with code lowering and other optimizations.
6847 st_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
6849 /* Return early if we are loading the shader from on-disk cache */
6850 if (st_load_tgsi_from_disk_cache(ctx
, prog
)) {
6854 struct pipe_screen
*pscreen
= ctx
->st
->pipe
->screen
;
6855 assert(prog
->data
->LinkStatus
);
6857 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
6858 if (prog
->_LinkedShaders
[i
] == NULL
)
6861 struct gl_linked_shader
*shader
= prog
->_LinkedShaders
[i
];
6862 exec_list
*ir
= shader
->ir
;
6863 gl_shader_stage stage
= shader
->Stage
;
6864 const struct gl_shader_compiler_options
*options
=
6865 &ctx
->Const
.ShaderCompilerOptions
[stage
];
6866 enum pipe_shader_type ptarget
= st_shader_stage_to_ptarget(stage
);
6867 bool have_dround
= pscreen
->get_shader_param(pscreen
, ptarget
,
6868 PIPE_SHADER_CAP_TGSI_DROUND_SUPPORTED
);
6869 bool have_dfrexp
= pscreen
->get_shader_param(pscreen
, ptarget
,
6870 PIPE_SHADER_CAP_TGSI_DFRACEXP_DLDEXP_SUPPORTED
);
6871 unsigned if_threshold
= pscreen
->get_shader_param(pscreen
, ptarget
,
6872 PIPE_SHADER_CAP_LOWER_IF_THRESHOLD
);
6874 /* If there are forms of indirect addressing that the driver
6875 * cannot handle, perform the lowering pass.
6877 if (options
->EmitNoIndirectInput
|| options
->EmitNoIndirectOutput
||
6878 options
->EmitNoIndirectTemp
|| options
->EmitNoIndirectUniform
) {
6879 lower_variable_index_to_cond_assign(stage
, ir
,
6880 options
->EmitNoIndirectInput
,
6881 options
->EmitNoIndirectOutput
,
6882 options
->EmitNoIndirectTemp
,
6883 options
->EmitNoIndirectUniform
);
6886 if (!pscreen
->get_param(pscreen
, PIPE_CAP_INT64_DIVMOD
))
6887 lower_64bit_integer_instructions(ir
, DIV64
| MOD64
);
6889 if (ctx
->Extensions
.ARB_shading_language_packing
) {
6890 unsigned lower_inst
= LOWER_PACK_SNORM_2x16
|
6891 LOWER_UNPACK_SNORM_2x16
|
6892 LOWER_PACK_UNORM_2x16
|
6893 LOWER_UNPACK_UNORM_2x16
|
6894 LOWER_PACK_SNORM_4x8
|
6895 LOWER_UNPACK_SNORM_4x8
|
6896 LOWER_UNPACK_UNORM_4x8
|
6897 LOWER_PACK_UNORM_4x8
;
6899 if (ctx
->Extensions
.ARB_gpu_shader5
)
6900 lower_inst
|= LOWER_PACK_USE_BFI
|
6902 if (!ctx
->st
->has_half_float_packing
)
6903 lower_inst
|= LOWER_PACK_HALF_2x16
|
6904 LOWER_UNPACK_HALF_2x16
;
6906 lower_packing_builtins(ir
, lower_inst
);
6909 if (!pscreen
->get_param(pscreen
, PIPE_CAP_TEXTURE_GATHER_OFFSETS
))
6910 lower_offset_arrays(ir
);
6911 do_mat_op_to_vec(ir
);
6913 if (stage
== MESA_SHADER_FRAGMENT
)
6914 lower_blend_equation_advanced(shader
);
6916 lower_instructions(ir
,
6922 (have_dfrexp
? 0 : DFREXP_DLDEXP_TO_ARITH
) |
6925 (have_dround
? 0 : DOPS_TO_DFRAC
) |
6926 (options
->EmitNoPow
? POW_TO_EXP2
: 0) |
6927 (!ctx
->Const
.NativeIntegers
? INT_DIV_TO_MUL_RCP
: 0) |
6928 (options
->EmitNoSat
? SAT_TO_CLAMP
: 0) |
6929 (ctx
->Const
.ForceGLSLAbsSqrt
? SQRT_TO_ABS_SQRT
: 0) |
6930 /* Assume that if ARB_gpu_shader5 is not supported
6931 * then all of the extended integer functions need
6932 * lowering. It may be necessary to add some caps
6933 * for individual instructions.
6935 (!ctx
->Extensions
.ARB_gpu_shader5
6936 ? BIT_COUNT_TO_MATH
|
6940 FIND_LSB_TO_FLOAT_CAST
|
6941 FIND_MSB_TO_FLOAT_CAST
|
6945 do_vec_index_to_cond_assign(ir
);
6946 lower_vector_insert(ir
, true);
6947 lower_quadop_vector(ir
, false);
6949 if (options
->MaxIfDepth
== 0) {
6953 if (ctx
->Const
.GLSLOptimizeConservatively
) {
6954 /* Do it once and repeat only if there's unsupported control flow. */
6956 do_common_optimization(ir
, true, true, options
,
6957 ctx
->Const
.NativeIntegers
);
6958 lower_if_to_cond_assign((gl_shader_stage
)i
, ir
,
6959 options
->MaxIfDepth
, if_threshold
);
6960 } while (has_unsupported_control_flow(ir
, options
));
6962 /* Repeat it until it stops making changes. */
6965 progress
= do_common_optimization(ir
, true, true, options
,
6966 ctx
->Const
.NativeIntegers
);
6967 progress
|= lower_if_to_cond_assign((gl_shader_stage
)i
, ir
,
6968 options
->MaxIfDepth
, if_threshold
);
6972 validate_ir_tree(ir
);
6975 build_program_resource_list(ctx
, prog
);
6977 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
6978 struct gl_linked_shader
*shader
= prog
->_LinkedShaders
[i
];
6982 enum pipe_shader_type ptarget
=
6983 st_shader_stage_to_ptarget(shader
->Stage
);
6984 enum pipe_shader_ir preferred_ir
= (enum pipe_shader_ir
)
6985 pscreen
->get_shader_param(pscreen
, ptarget
,
6986 PIPE_SHADER_CAP_PREFERRED_IR
);
6988 struct gl_program
*linked_prog
= NULL
;
6989 if (preferred_ir
== PIPE_SHADER_IR_NIR
) {
6990 /* TODO only for GLSL VS/FS for now: */
6991 switch (shader
->Stage
) {
6992 case MESA_SHADER_VERTEX
:
6993 case MESA_SHADER_FRAGMENT
:
6994 linked_prog
= st_nir_get_mesa_program(ctx
, prog
, shader
);
6999 linked_prog
= get_mesa_program_tgsi(ctx
, prog
, shader
);
7003 st_set_prog_affected_state_flags(linked_prog
);
7004 if (!ctx
->Driver
.ProgramStringNotify(ctx
,
7005 _mesa_shader_stage_to_program(i
),
7007 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
7017 st_translate_stream_output_info(glsl_to_tgsi_visitor
*glsl_to_tgsi
,
7018 const GLuint outputMapping
[],
7019 struct pipe_stream_output_info
*so
)
7021 if (!glsl_to_tgsi
->shader_program
->last_vert_prog
)
7024 struct gl_transform_feedback_info
*info
=
7025 glsl_to_tgsi
->shader_program
->last_vert_prog
->sh
.LinkedTransformFeedback
;
7026 st_translate_stream_output_info2(info
, outputMapping
, so
);
7030 st_translate_stream_output_info2(struct gl_transform_feedback_info
*info
,
7031 const GLuint outputMapping
[],
7032 struct pipe_stream_output_info
*so
)
7036 for (i
= 0; i
< info
->NumOutputs
; i
++) {
7037 so
->output
[i
].register_index
=
7038 outputMapping
[info
->Outputs
[i
].OutputRegister
];
7039 so
->output
[i
].start_component
= info
->Outputs
[i
].ComponentOffset
;
7040 so
->output
[i
].num_components
= info
->Outputs
[i
].NumComponents
;
7041 so
->output
[i
].output_buffer
= info
->Outputs
[i
].OutputBuffer
;
7042 so
->output
[i
].dst_offset
= info
->Outputs
[i
].DstOffset
;
7043 so
->output
[i
].stream
= info
->Outputs
[i
].StreamId
;
7046 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
7047 so
->stride
[i
] = info
->Buffers
[i
].Stride
;
7049 so
->num_outputs
= info
->NumOutputs
;