2 * Copyright 2017 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Authors: Karol Herbst <kherbst@redhat.com>
25 #include "compiler/nir/nir.h"
27 #include "util/u_debug.h"
29 #include "codegen/nv50_ir.h"
30 #include "codegen/nv50_ir_from_common.h"
31 #include "codegen/nv50_ir_lowering_helper.h"
32 #include "codegen/nv50_ir_util.h"
34 #if __cplusplus >= 201103L
35 #include <unordered_map>
37 #include <tr1/unordered_map>
45 #if __cplusplus >= 201103L
47 using std::unordered_map
;
50 using std::tr1::unordered_map
;
53 using namespace nv50_ir
;
56 type_size(const struct glsl_type
*type
, bool bindless
)
58 return glsl_count_attribute_slots(type
, false);
61 class Converter
: public ConverterCommon
64 Converter(Program
*, nir_shader
*, nv50_ir_prog_info
*);
68 typedef std::vector
<LValue
*> LValues
;
69 typedef unordered_map
<unsigned, LValues
> NirDefMap
;
70 typedef unordered_map
<unsigned, nir_load_const_instr
*> ImmediateMap
;
71 typedef unordered_map
<unsigned, uint32_t> NirArrayLMemOffsets
;
72 typedef unordered_map
<unsigned, BasicBlock
*> NirBlockMap
;
74 CacheMode
convert(enum gl_access_qualifier
);
75 TexTarget
convert(glsl_sampler_dim
, bool isArray
, bool isShadow
);
76 LValues
& convert(nir_alu_dest
*);
77 BasicBlock
* convert(nir_block
*);
78 LValues
& convert(nir_dest
*);
79 SVSemantic
convert(nir_intrinsic_op
);
80 Value
* convert(nir_load_const_instr
*, uint8_t);
81 LValues
& convert(nir_register
*);
82 LValues
& convert(nir_ssa_def
*);
84 ImgFormat
convertGLImgFormat(GLuint
);
86 Value
* getSrc(nir_alu_src
*, uint8_t component
= 0);
87 Value
* getSrc(nir_register
*, uint8_t);
88 Value
* getSrc(nir_src
*, uint8_t, bool indirect
= false);
89 Value
* getSrc(nir_ssa_def
*, uint8_t);
91 // returned value is the constant part of the given source (either the
92 // nir_src or the selected source component of an intrinsic). Even though
93 // this is mostly an optimization to be able to skip indirects in a few
94 // cases, sometimes we require immediate values or set some fileds on
95 // instructions (e.g. tex) in order for codegen to consume those.
96 // If the found value has not a constant part, the Value gets returned
97 // through the Value parameter.
98 uint32_t getIndirect(nir_src
*, uint8_t, Value
*&);
99 uint32_t getIndirect(nir_intrinsic_instr
*, uint8_t s
, uint8_t c
, Value
*&);
101 uint32_t getSlotAddress(nir_intrinsic_instr
*, uint8_t idx
, uint8_t slot
);
103 void setInterpolate(nv50_ir_varying
*,
108 Instruction
*loadFrom(DataFile
, uint8_t, DataType
, Value
*def
, uint32_t base
,
109 uint8_t c
, Value
*indirect0
= NULL
,
110 Value
*indirect1
= NULL
, bool patch
= false);
111 void storeTo(nir_intrinsic_instr
*, DataFile
, operation
, DataType
,
112 Value
*src
, uint8_t idx
, uint8_t c
, Value
*indirect0
= NULL
,
113 Value
*indirect1
= NULL
);
115 bool isFloatType(nir_alu_type
);
116 bool isSignedType(nir_alu_type
);
117 bool isResultFloat(nir_op
);
118 bool isResultSigned(nir_op
);
120 DataType
getDType(nir_alu_instr
*);
121 DataType
getDType(nir_intrinsic_instr
*);
122 DataType
getDType(nir_intrinsic_instr
*, bool isSigned
);
123 DataType
getDType(nir_op
, uint8_t);
125 std::vector
<DataType
> getSTypes(nir_alu_instr
*);
126 DataType
getSType(nir_src
&, bool isFloat
, bool isSigned
);
128 operation
getOperation(nir_intrinsic_op
);
129 operation
getOperation(nir_op
);
130 operation
getOperation(nir_texop
);
131 operation
preOperationNeeded(nir_op
);
133 int getSubOp(nir_intrinsic_op
);
134 int getSubOp(nir_op
);
136 CondCode
getCondCode(nir_op
);
141 bool visit(nir_alu_instr
*);
142 bool visit(nir_block
*);
143 bool visit(nir_cf_node
*);
144 bool visit(nir_deref_instr
*);
145 bool visit(nir_function
*);
146 bool visit(nir_if
*);
147 bool visit(nir_instr
*);
148 bool visit(nir_intrinsic_instr
*);
149 bool visit(nir_jump_instr
*);
150 bool visit(nir_load_const_instr
*);
151 bool visit(nir_loop
*);
152 bool visit(nir_ssa_undef_instr
*);
153 bool visit(nir_tex_instr
*);
156 Value
* applyProjection(Value
*src
, Value
*proj
);
157 unsigned int getNIRArgCount(TexInstruction::Target
&);
160 uint16_t handleDeref(nir_deref_instr
*, Value
* & indirect
, const nir_variable
* &);
161 CacheMode
getCacheModeFromVar(const nir_variable
*);
167 ImmediateMap immediates
;
168 NirArrayLMemOffsets regToLmemOffset
;
170 unsigned int curLoopDepth
;
174 Instruction
*immInsertPos
;
176 int clipVertexOutput
;
185 Converter::Converter(Program
*prog
, nir_shader
*nir
, nv50_ir_prog_info
*info
)
186 : ConverterCommon(prog
, info
),
191 zero
= mkImm((uint32_t)0);
195 Converter::convert(nir_block
*block
)
197 NirBlockMap::iterator it
= blocks
.find(block
->index
);
198 if (it
!= blocks
.end())
201 BasicBlock
*bb
= new BasicBlock(func
);
202 blocks
[block
->index
] = bb
;
207 Converter::isFloatType(nir_alu_type type
)
209 return nir_alu_type_get_base_type(type
) == nir_type_float
;
213 Converter::isSignedType(nir_alu_type type
)
215 return nir_alu_type_get_base_type(type
) == nir_type_int
;
219 Converter::isResultFloat(nir_op op
)
221 const nir_op_info
&info
= nir_op_infos
[op
];
222 if (info
.output_type
!= nir_type_invalid
)
223 return isFloatType(info
.output_type
);
225 ERROR("isResultFloat not implemented for %s\n", nir_op_infos
[op
].name
);
231 Converter::isResultSigned(nir_op op
)
234 // there is no umul and we get wrong results if we treat all muls as signed
239 const nir_op_info
&info
= nir_op_infos
[op
];
240 if (info
.output_type
!= nir_type_invalid
)
241 return isSignedType(info
.output_type
);
242 ERROR("isResultSigned not implemented for %s\n", nir_op_infos
[op
].name
);
249 Converter::getDType(nir_alu_instr
*insn
)
251 if (insn
->dest
.dest
.is_ssa
)
252 return getDType(insn
->op
, insn
->dest
.dest
.ssa
.bit_size
);
254 return getDType(insn
->op
, insn
->dest
.dest
.reg
.reg
->bit_size
);
258 Converter::getDType(nir_intrinsic_instr
*insn
)
261 switch (insn
->intrinsic
) {
262 case nir_intrinsic_shared_atomic_imax
:
263 case nir_intrinsic_shared_atomic_imin
:
264 case nir_intrinsic_ssbo_atomic_imax
:
265 case nir_intrinsic_ssbo_atomic_imin
:
273 return getDType(insn
, isSigned
);
277 Converter::getDType(nir_intrinsic_instr
*insn
, bool isSigned
)
279 if (insn
->dest
.is_ssa
)
280 return typeOfSize(insn
->dest
.ssa
.bit_size
/ 8, false, isSigned
);
282 return typeOfSize(insn
->dest
.reg
.reg
->bit_size
/ 8, false, isSigned
);
286 Converter::getDType(nir_op op
, uint8_t bitSize
)
288 DataType ty
= typeOfSize(bitSize
/ 8, isResultFloat(op
), isResultSigned(op
));
289 if (ty
== TYPE_NONE
) {
290 ERROR("couldn't get Type for op %s with bitSize %u\n", nir_op_infos
[op
].name
, bitSize
);
296 std::vector
<DataType
>
297 Converter::getSTypes(nir_alu_instr
*insn
)
299 const nir_op_info
&info
= nir_op_infos
[insn
->op
];
300 std::vector
<DataType
> res(info
.num_inputs
);
302 for (uint8_t i
= 0; i
< info
.num_inputs
; ++i
) {
303 if (info
.input_types
[i
] != nir_type_invalid
) {
304 res
[i
] = getSType(insn
->src
[i
].src
, isFloatType(info
.input_types
[i
]), isSignedType(info
.input_types
[i
]));
306 ERROR("getSType not implemented for %s idx %u\n", info
.name
, i
);
317 Converter::getSType(nir_src
&src
, bool isFloat
, bool isSigned
)
321 bitSize
= src
.ssa
->bit_size
;
323 bitSize
= src
.reg
.reg
->bit_size
;
325 DataType ty
= typeOfSize(bitSize
/ 8, isFloat
, isSigned
);
326 if (ty
== TYPE_NONE
) {
334 ERROR("couldn't get Type for %s with bitSize %u\n", str
, bitSize
);
341 Converter::getOperation(nir_op op
)
344 // basic ops with float and int variants
353 case nir_op_ifind_msb
:
354 case nir_op_ufind_msb
:
376 case nir_op_fddx_coarse
:
377 case nir_op_fddx_fine
:
380 case nir_op_fddy_coarse
:
381 case nir_op_fddy_fine
:
399 case nir_op_pack_64_2x32_split
:
413 case nir_op_imul_high
:
414 case nir_op_umul_high
:
459 ERROR("couldn't get operation for op %s\n", nir_op_infos
[op
].name
);
466 Converter::getOperation(nir_texop op
)
478 case nir_texop_txf_ms
:
484 case nir_texop_query_levels
:
485 case nir_texop_texture_samples
:
489 ERROR("couldn't get operation for nir_texop %u\n", op
);
496 Converter::getOperation(nir_intrinsic_op op
)
499 case nir_intrinsic_emit_vertex
:
501 case nir_intrinsic_end_primitive
:
503 case nir_intrinsic_bindless_image_atomic_add
:
504 case nir_intrinsic_image_atomic_add
:
505 case nir_intrinsic_image_deref_atomic_add
:
506 case nir_intrinsic_bindless_image_atomic_and
:
507 case nir_intrinsic_image_atomic_and
:
508 case nir_intrinsic_image_deref_atomic_and
:
509 case nir_intrinsic_bindless_image_atomic_comp_swap
:
510 case nir_intrinsic_image_atomic_comp_swap
:
511 case nir_intrinsic_image_deref_atomic_comp_swap
:
512 case nir_intrinsic_bindless_image_atomic_exchange
:
513 case nir_intrinsic_image_atomic_exchange
:
514 case nir_intrinsic_image_deref_atomic_exchange
:
515 case nir_intrinsic_bindless_image_atomic_max
:
516 case nir_intrinsic_image_atomic_max
:
517 case nir_intrinsic_image_deref_atomic_max
:
518 case nir_intrinsic_bindless_image_atomic_min
:
519 case nir_intrinsic_image_atomic_min
:
520 case nir_intrinsic_image_deref_atomic_min
:
521 case nir_intrinsic_bindless_image_atomic_or
:
522 case nir_intrinsic_image_atomic_or
:
523 case nir_intrinsic_image_deref_atomic_or
:
524 case nir_intrinsic_bindless_image_atomic_xor
:
525 case nir_intrinsic_image_atomic_xor
:
526 case nir_intrinsic_image_deref_atomic_xor
:
528 case nir_intrinsic_bindless_image_load
:
529 case nir_intrinsic_image_load
:
530 case nir_intrinsic_image_deref_load
:
532 case nir_intrinsic_bindless_image_samples
:
533 case nir_intrinsic_image_samples
:
534 case nir_intrinsic_image_deref_samples
:
535 case nir_intrinsic_bindless_image_size
:
536 case nir_intrinsic_image_size
:
537 case nir_intrinsic_image_deref_size
:
539 case nir_intrinsic_bindless_image_store
:
540 case nir_intrinsic_image_store
:
541 case nir_intrinsic_image_deref_store
:
544 ERROR("couldn't get operation for nir_intrinsic_op %u\n", op
);
551 Converter::preOperationNeeded(nir_op op
)
563 Converter::getSubOp(nir_op op
)
566 case nir_op_imul_high
:
567 case nir_op_umul_high
:
568 return NV50_IR_SUBOP_MUL_HIGH
;
575 Converter::getSubOp(nir_intrinsic_op op
)
578 case nir_intrinsic_bindless_image_atomic_add
:
579 case nir_intrinsic_image_atomic_add
:
580 case nir_intrinsic_image_deref_atomic_add
:
581 case nir_intrinsic_shared_atomic_add
:
582 case nir_intrinsic_ssbo_atomic_add
:
583 return NV50_IR_SUBOP_ATOM_ADD
;
584 case nir_intrinsic_bindless_image_atomic_and
:
585 case nir_intrinsic_image_atomic_and
:
586 case nir_intrinsic_image_deref_atomic_and
:
587 case nir_intrinsic_shared_atomic_and
:
588 case nir_intrinsic_ssbo_atomic_and
:
589 return NV50_IR_SUBOP_ATOM_AND
;
590 case nir_intrinsic_bindless_image_atomic_comp_swap
:
591 case nir_intrinsic_image_atomic_comp_swap
:
592 case nir_intrinsic_image_deref_atomic_comp_swap
:
593 case nir_intrinsic_shared_atomic_comp_swap
:
594 case nir_intrinsic_ssbo_atomic_comp_swap
:
595 return NV50_IR_SUBOP_ATOM_CAS
;
596 case nir_intrinsic_bindless_image_atomic_exchange
:
597 case nir_intrinsic_image_atomic_exchange
:
598 case nir_intrinsic_image_deref_atomic_exchange
:
599 case nir_intrinsic_shared_atomic_exchange
:
600 case nir_intrinsic_ssbo_atomic_exchange
:
601 return NV50_IR_SUBOP_ATOM_EXCH
;
602 case nir_intrinsic_bindless_image_atomic_or
:
603 case nir_intrinsic_image_atomic_or
:
604 case nir_intrinsic_image_deref_atomic_or
:
605 case nir_intrinsic_shared_atomic_or
:
606 case nir_intrinsic_ssbo_atomic_or
:
607 return NV50_IR_SUBOP_ATOM_OR
;
608 case nir_intrinsic_bindless_image_atomic_max
:
609 case nir_intrinsic_image_atomic_max
:
610 case nir_intrinsic_image_deref_atomic_max
:
611 case nir_intrinsic_shared_atomic_imax
:
612 case nir_intrinsic_shared_atomic_umax
:
613 case nir_intrinsic_ssbo_atomic_imax
:
614 case nir_intrinsic_ssbo_atomic_umax
:
615 return NV50_IR_SUBOP_ATOM_MAX
;
616 case nir_intrinsic_bindless_image_atomic_min
:
617 case nir_intrinsic_image_atomic_min
:
618 case nir_intrinsic_image_deref_atomic_min
:
619 case nir_intrinsic_shared_atomic_imin
:
620 case nir_intrinsic_shared_atomic_umin
:
621 case nir_intrinsic_ssbo_atomic_imin
:
622 case nir_intrinsic_ssbo_atomic_umin
:
623 return NV50_IR_SUBOP_ATOM_MIN
;
624 case nir_intrinsic_bindless_image_atomic_xor
:
625 case nir_intrinsic_image_atomic_xor
:
626 case nir_intrinsic_image_deref_atomic_xor
:
627 case nir_intrinsic_shared_atomic_xor
:
628 case nir_intrinsic_ssbo_atomic_xor
:
629 return NV50_IR_SUBOP_ATOM_XOR
;
631 case nir_intrinsic_group_memory_barrier
:
632 case nir_intrinsic_memory_barrier
:
633 case nir_intrinsic_memory_barrier_atomic_counter
:
634 case nir_intrinsic_memory_barrier_buffer
:
635 case nir_intrinsic_memory_barrier_image
:
636 return NV50_IR_SUBOP_MEMBAR(M
, GL
);
637 case nir_intrinsic_memory_barrier_shared
:
638 return NV50_IR_SUBOP_MEMBAR(M
, CTA
);
640 case nir_intrinsic_vote_all
:
641 return NV50_IR_SUBOP_VOTE_ALL
;
642 case nir_intrinsic_vote_any
:
643 return NV50_IR_SUBOP_VOTE_ANY
;
644 case nir_intrinsic_vote_ieq
:
645 return NV50_IR_SUBOP_VOTE_UNI
;
652 Converter::getCondCode(nir_op op
)
671 ERROR("couldn't get CondCode for op %s\n", nir_op_infos
[op
].name
);
678 Converter::convert(nir_alu_dest
*dest
)
680 return convert(&dest
->dest
);
684 Converter::convert(nir_dest
*dest
)
687 return convert(&dest
->ssa
);
688 if (dest
->reg
.indirect
) {
689 ERROR("no support for indirects.");
692 return convert(dest
->reg
.reg
);
696 Converter::convert(nir_register
*reg
)
698 NirDefMap::iterator it
= regDefs
.find(reg
->index
);
699 if (it
!= regDefs
.end())
702 LValues
newDef(reg
->num_components
);
703 for (uint8_t i
= 0; i
< reg
->num_components
; i
++)
704 newDef
[i
] = getScratch(std::max(4, reg
->bit_size
/ 8));
705 return regDefs
[reg
->index
] = newDef
;
709 Converter::convert(nir_ssa_def
*def
)
711 NirDefMap::iterator it
= ssaDefs
.find(def
->index
);
712 if (it
!= ssaDefs
.end())
715 LValues
newDef(def
->num_components
);
716 for (uint8_t i
= 0; i
< def
->num_components
; i
++)
717 newDef
[i
] = getSSA(std::max(4, def
->bit_size
/ 8));
718 return ssaDefs
[def
->index
] = newDef
;
722 Converter::getSrc(nir_alu_src
*src
, uint8_t component
)
724 if (src
->abs
|| src
->negate
) {
725 ERROR("modifiers currently not supported on nir_alu_src\n");
728 return getSrc(&src
->src
, src
->swizzle
[component
]);
732 Converter::getSrc(nir_register
*reg
, uint8_t idx
)
734 NirDefMap::iterator it
= regDefs
.find(reg
->index
);
735 if (it
== regDefs
.end())
736 return convert(reg
)[idx
];
737 return it
->second
[idx
];
741 Converter::getSrc(nir_src
*src
, uint8_t idx
, bool indirect
)
744 return getSrc(src
->ssa
, idx
);
746 if (src
->reg
.indirect
) {
748 return getSrc(src
->reg
.indirect
, idx
);
749 ERROR("no support for indirects.");
754 return getSrc(src
->reg
.reg
, idx
);
758 Converter::getSrc(nir_ssa_def
*src
, uint8_t idx
)
760 ImmediateMap::iterator iit
= immediates
.find(src
->index
);
761 if (iit
!= immediates
.end())
762 return convert((*iit
).second
, idx
);
764 NirDefMap::iterator it
= ssaDefs
.find(src
->index
);
765 if (it
== ssaDefs
.end()) {
766 ERROR("SSA value %u not found\n", src
->index
);
770 return it
->second
[idx
];
774 Converter::getIndirect(nir_src
*src
, uint8_t idx
, Value
*&indirect
)
776 nir_const_value
*offset
= nir_src_as_const_value(*src
);
780 return offset
[0].u32
;
783 indirect
= getSrc(src
, idx
, true);
788 Converter::getIndirect(nir_intrinsic_instr
*insn
, uint8_t s
, uint8_t c
, Value
*&indirect
)
790 int32_t idx
= nir_intrinsic_base(insn
) + getIndirect(&insn
->src
[s
], c
, indirect
);
792 indirect
= mkOp2v(OP_SHL
, TYPE_U32
, getSSA(4, FILE_ADDRESS
), indirect
, loadImm(NULL
, 4));
797 vert_attrib_to_tgsi_semantic(gl_vert_attrib slot
, unsigned *name
, unsigned *index
)
799 assert(name
&& index
);
801 if (slot
>= VERT_ATTRIB_MAX
) {
802 ERROR("invalid varying slot %u\n", slot
);
807 if (slot
>= VERT_ATTRIB_GENERIC0
&&
808 slot
< VERT_ATTRIB_GENERIC0
+ VERT_ATTRIB_GENERIC_MAX
) {
809 *name
= TGSI_SEMANTIC_GENERIC
;
810 *index
= slot
- VERT_ATTRIB_GENERIC0
;
814 if (slot
>= VERT_ATTRIB_TEX0
&&
815 slot
< VERT_ATTRIB_TEX0
+ VERT_ATTRIB_TEX_MAX
) {
816 *name
= TGSI_SEMANTIC_TEXCOORD
;
817 *index
= slot
- VERT_ATTRIB_TEX0
;
822 case VERT_ATTRIB_COLOR0
:
823 *name
= TGSI_SEMANTIC_COLOR
;
826 case VERT_ATTRIB_COLOR1
:
827 *name
= TGSI_SEMANTIC_COLOR
;
830 case VERT_ATTRIB_EDGEFLAG
:
831 *name
= TGSI_SEMANTIC_EDGEFLAG
;
834 case VERT_ATTRIB_FOG
:
835 *name
= TGSI_SEMANTIC_FOG
;
838 case VERT_ATTRIB_NORMAL
:
839 *name
= TGSI_SEMANTIC_NORMAL
;
842 case VERT_ATTRIB_POS
:
843 *name
= TGSI_SEMANTIC_POSITION
;
846 case VERT_ATTRIB_POINT_SIZE
:
847 *name
= TGSI_SEMANTIC_PSIZE
;
851 ERROR("unknown vert attrib slot %u\n", slot
);
858 varying_slot_to_tgsi_semantic(gl_varying_slot slot
, unsigned *name
, unsigned *index
)
860 assert(name
&& index
);
862 if (slot
>= VARYING_SLOT_TESS_MAX
) {
863 ERROR("invalid varying slot %u\n", slot
);
868 if (slot
>= VARYING_SLOT_PATCH0
) {
869 *name
= TGSI_SEMANTIC_PATCH
;
870 *index
= slot
- VARYING_SLOT_PATCH0
;
874 if (slot
>= VARYING_SLOT_VAR0
) {
875 *name
= TGSI_SEMANTIC_GENERIC
;
876 *index
= slot
- VARYING_SLOT_VAR0
;
880 if (slot
>= VARYING_SLOT_TEX0
&& slot
<= VARYING_SLOT_TEX7
) {
881 *name
= TGSI_SEMANTIC_TEXCOORD
;
882 *index
= slot
- VARYING_SLOT_TEX0
;
887 case VARYING_SLOT_BFC0
:
888 *name
= TGSI_SEMANTIC_BCOLOR
;
891 case VARYING_SLOT_BFC1
:
892 *name
= TGSI_SEMANTIC_BCOLOR
;
895 case VARYING_SLOT_CLIP_DIST0
:
896 *name
= TGSI_SEMANTIC_CLIPDIST
;
899 case VARYING_SLOT_CLIP_DIST1
:
900 *name
= TGSI_SEMANTIC_CLIPDIST
;
903 case VARYING_SLOT_CLIP_VERTEX
:
904 *name
= TGSI_SEMANTIC_CLIPVERTEX
;
907 case VARYING_SLOT_COL0
:
908 *name
= TGSI_SEMANTIC_COLOR
;
911 case VARYING_SLOT_COL1
:
912 *name
= TGSI_SEMANTIC_COLOR
;
915 case VARYING_SLOT_EDGE
:
916 *name
= TGSI_SEMANTIC_EDGEFLAG
;
919 case VARYING_SLOT_FACE
:
920 *name
= TGSI_SEMANTIC_FACE
;
923 case VARYING_SLOT_FOGC
:
924 *name
= TGSI_SEMANTIC_FOG
;
927 case VARYING_SLOT_LAYER
:
928 *name
= TGSI_SEMANTIC_LAYER
;
931 case VARYING_SLOT_PNTC
:
932 *name
= TGSI_SEMANTIC_PCOORD
;
935 case VARYING_SLOT_POS
:
936 *name
= TGSI_SEMANTIC_POSITION
;
939 case VARYING_SLOT_PRIMITIVE_ID
:
940 *name
= TGSI_SEMANTIC_PRIMID
;
943 case VARYING_SLOT_PSIZ
:
944 *name
= TGSI_SEMANTIC_PSIZE
;
947 case VARYING_SLOT_TESS_LEVEL_INNER
:
948 *name
= TGSI_SEMANTIC_TESSINNER
;
951 case VARYING_SLOT_TESS_LEVEL_OUTER
:
952 *name
= TGSI_SEMANTIC_TESSOUTER
;
955 case VARYING_SLOT_VIEWPORT
:
956 *name
= TGSI_SEMANTIC_VIEWPORT_INDEX
;
960 ERROR("unknown varying slot %u\n", slot
);
967 frag_result_to_tgsi_semantic(unsigned slot
, unsigned *name
, unsigned *index
)
969 if (slot
>= FRAG_RESULT_DATA0
) {
970 *name
= TGSI_SEMANTIC_COLOR
;
971 *index
= slot
- FRAG_RESULT_COLOR
- 2; // intentional
976 case FRAG_RESULT_COLOR
:
977 *name
= TGSI_SEMANTIC_COLOR
;
980 case FRAG_RESULT_DEPTH
:
981 *name
= TGSI_SEMANTIC_POSITION
;
984 case FRAG_RESULT_SAMPLE_MASK
:
985 *name
= TGSI_SEMANTIC_SAMPLEMASK
;
989 ERROR("unknown frag result slot %u\n", slot
);
995 // copy of _mesa_sysval_to_semantic
997 system_val_to_tgsi_semantic(unsigned val
, unsigned *name
, unsigned *index
)
1002 case SYSTEM_VALUE_VERTEX_ID
:
1003 *name
= TGSI_SEMANTIC_VERTEXID
;
1005 case SYSTEM_VALUE_INSTANCE_ID
:
1006 *name
= TGSI_SEMANTIC_INSTANCEID
;
1008 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
1009 *name
= TGSI_SEMANTIC_VERTEXID_NOBASE
;
1011 case SYSTEM_VALUE_BASE_VERTEX
:
1012 *name
= TGSI_SEMANTIC_BASEVERTEX
;
1014 case SYSTEM_VALUE_BASE_INSTANCE
:
1015 *name
= TGSI_SEMANTIC_BASEINSTANCE
;
1017 case SYSTEM_VALUE_DRAW_ID
:
1018 *name
= TGSI_SEMANTIC_DRAWID
;
1022 case SYSTEM_VALUE_INVOCATION_ID
:
1023 *name
= TGSI_SEMANTIC_INVOCATIONID
;
1027 case SYSTEM_VALUE_FRAG_COORD
:
1028 *name
= TGSI_SEMANTIC_POSITION
;
1030 case SYSTEM_VALUE_FRONT_FACE
:
1031 *name
= TGSI_SEMANTIC_FACE
;
1033 case SYSTEM_VALUE_SAMPLE_ID
:
1034 *name
= TGSI_SEMANTIC_SAMPLEID
;
1036 case SYSTEM_VALUE_SAMPLE_POS
:
1037 *name
= TGSI_SEMANTIC_SAMPLEPOS
;
1039 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
1040 *name
= TGSI_SEMANTIC_SAMPLEMASK
;
1042 case SYSTEM_VALUE_HELPER_INVOCATION
:
1043 *name
= TGSI_SEMANTIC_HELPER_INVOCATION
;
1046 // Tessellation shader
1047 case SYSTEM_VALUE_TESS_COORD
:
1048 *name
= TGSI_SEMANTIC_TESSCOORD
;
1050 case SYSTEM_VALUE_VERTICES_IN
:
1051 *name
= TGSI_SEMANTIC_VERTICESIN
;
1053 case SYSTEM_VALUE_PRIMITIVE_ID
:
1054 *name
= TGSI_SEMANTIC_PRIMID
;
1056 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
1057 *name
= TGSI_SEMANTIC_TESSOUTER
;
1059 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
1060 *name
= TGSI_SEMANTIC_TESSINNER
;
1064 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
1065 *name
= TGSI_SEMANTIC_THREAD_ID
;
1067 case SYSTEM_VALUE_WORK_GROUP_ID
:
1068 *name
= TGSI_SEMANTIC_BLOCK_ID
;
1070 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
1071 *name
= TGSI_SEMANTIC_GRID_SIZE
;
1073 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
1074 *name
= TGSI_SEMANTIC_BLOCK_SIZE
;
1077 // ARB_shader_ballot
1078 case SYSTEM_VALUE_SUBGROUP_SIZE
:
1079 *name
= TGSI_SEMANTIC_SUBGROUP_SIZE
;
1081 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
1082 *name
= TGSI_SEMANTIC_SUBGROUP_INVOCATION
;
1084 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
1085 *name
= TGSI_SEMANTIC_SUBGROUP_EQ_MASK
;
1087 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
1088 *name
= TGSI_SEMANTIC_SUBGROUP_GE_MASK
;
1090 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
1091 *name
= TGSI_SEMANTIC_SUBGROUP_GT_MASK
;
1093 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
1094 *name
= TGSI_SEMANTIC_SUBGROUP_LE_MASK
;
1096 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
1097 *name
= TGSI_SEMANTIC_SUBGROUP_LT_MASK
;
1101 ERROR("unknown system value %u\n", val
);
1108 Converter::setInterpolate(nv50_ir_varying
*var
,
1114 case INTERP_MODE_FLAT
:
1117 case INTERP_MODE_NONE
:
1118 if (semantic
== TGSI_SEMANTIC_COLOR
)
1120 else if (semantic
== TGSI_SEMANTIC_POSITION
)
1123 case INTERP_MODE_NOPERSPECTIVE
:
1126 case INTERP_MODE_SMOOTH
:
1129 var
->centroid
= centroid
;
1133 calcSlots(const glsl_type
*type
, Program::Type stage
, const shader_info
&info
,
1134 bool input
, const nir_variable
*var
)
1136 if (!type
->is_array())
1137 return type
->count_attribute_slots(false);
1141 case Program::TYPE_GEOMETRY
:
1142 slots
= type
->uniform_locations();
1144 slots
/= info
.gs
.vertices_in
;
1146 case Program::TYPE_TESSELLATION_CONTROL
:
1147 case Program::TYPE_TESSELLATION_EVAL
:
1148 // remove first dimension
1149 if (var
->data
.patch
|| (!input
&& stage
== Program::TYPE_TESSELLATION_EVAL
))
1150 slots
= type
->uniform_locations();
1152 slots
= type
->fields
.array
->uniform_locations();
1155 slots
= type
->count_attribute_slots(false);
1162 bool Converter::assignSlots() {
1166 info
->io
.viewportId
= -1;
1167 info
->numInputs
= 0;
1168 info
->numOutputs
= 0;
1170 // we have to fixup the uniform locations for arrays
1171 unsigned numImages
= 0;
1172 nir_foreach_variable(var
, &nir
->uniforms
) {
1173 const glsl_type
*type
= var
->type
;
1174 if (!type
->without_array()->is_image())
1176 var
->data
.driver_location
= numImages
;
1177 numImages
+= type
->is_array() ? type
->arrays_of_arrays_size() : 1;
1180 info
->numSysVals
= 0;
1181 for (uint8_t i
= 0; i
< SYSTEM_VALUE_MAX
; ++i
) {
1182 if (!(nir
->info
.system_values_read
& 1ull << i
))
1185 system_val_to_tgsi_semantic(i
, &name
, &index
);
1186 info
->sv
[info
->numSysVals
].sn
= name
;
1187 info
->sv
[info
->numSysVals
].si
= index
;
1188 info
->sv
[info
->numSysVals
].input
= 0; // TODO inferSysValDirection(sn);
1191 case SYSTEM_VALUE_INSTANCE_ID
:
1192 info
->io
.instanceId
= info
->numSysVals
;
1194 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
1195 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
1196 info
->sv
[info
->numSysVals
].patch
= 1;
1198 case SYSTEM_VALUE_VERTEX_ID
:
1199 info
->io
.vertexId
= info
->numSysVals
;
1205 info
->numSysVals
+= 1;
1208 if (prog
->getType() == Program::TYPE_COMPUTE
)
1211 nir_foreach_variable(var
, &nir
->inputs
) {
1212 const glsl_type
*type
= var
->type
;
1213 int slot
= var
->data
.location
;
1214 uint16_t slots
= calcSlots(type
, prog
->getType(), nir
->info
, true, var
);
1215 uint32_t comp
= type
->is_array() ? type
->without_array()->component_slots()
1216 : type
->component_slots();
1217 uint32_t frac
= var
->data
.location_frac
;
1218 uint32_t vary
= var
->data
.driver_location
;
1220 if (glsl_base_type_is_64bit(type
->without_array()->base_type
)) {
1225 assert(vary
+ slots
<= PIPE_MAX_SHADER_INPUTS
);
1227 switch(prog
->getType()) {
1228 case Program::TYPE_FRAGMENT
:
1229 varying_slot_to_tgsi_semantic((gl_varying_slot
)slot
, &name
, &index
);
1230 for (uint16_t i
= 0; i
< slots
; ++i
) {
1231 setInterpolate(&info
->in
[vary
+ i
], var
->data
.interpolation
,
1232 var
->data
.centroid
| var
->data
.sample
, name
);
1235 case Program::TYPE_GEOMETRY
:
1236 varying_slot_to_tgsi_semantic((gl_varying_slot
)slot
, &name
, &index
);
1238 case Program::TYPE_TESSELLATION_CONTROL
:
1239 case Program::TYPE_TESSELLATION_EVAL
:
1240 varying_slot_to_tgsi_semantic((gl_varying_slot
)slot
, &name
, &index
);
1241 if (var
->data
.patch
&& name
== TGSI_SEMANTIC_PATCH
)
1242 info
->numPatchConstants
= MAX2(info
->numPatchConstants
, index
+ slots
);
1244 case Program::TYPE_VERTEX
:
1245 vert_attrib_to_tgsi_semantic((gl_vert_attrib
)slot
, &name
, &index
);
1247 case TGSI_SEMANTIC_EDGEFLAG
:
1248 info
->io
.edgeFlagIn
= vary
;
1255 ERROR("unknown shader type %u in assignSlots\n", prog
->getType());
1259 for (uint16_t i
= 0u; i
< slots
; ++i
, ++vary
) {
1260 info
->in
[vary
].id
= vary
;
1261 info
->in
[vary
].patch
= var
->data
.patch
;
1262 info
->in
[vary
].sn
= name
;
1263 info
->in
[vary
].si
= index
+ i
;
1264 if (glsl_base_type_is_64bit(type
->without_array()->base_type
))
1266 info
->in
[vary
].mask
|= (((1 << (comp
* 2)) - 1) << (frac
* 2) >> 0x4);
1268 info
->in
[vary
].mask
|= (((1 << (comp
* 2)) - 1) << (frac
* 2) & 0xf);
1270 info
->in
[vary
].mask
|= ((1 << comp
) - 1) << frac
;
1272 info
->numInputs
= std::max
<uint8_t>(info
->numInputs
, vary
);
1275 nir_foreach_variable(var
, &nir
->outputs
) {
1276 const glsl_type
*type
= var
->type
;
1277 int slot
= var
->data
.location
;
1278 uint16_t slots
= calcSlots(type
, prog
->getType(), nir
->info
, false, var
);
1279 uint32_t comp
= type
->is_array() ? type
->without_array()->component_slots()
1280 : type
->component_slots();
1281 uint32_t frac
= var
->data
.location_frac
;
1282 uint32_t vary
= var
->data
.driver_location
;
1284 if (glsl_base_type_is_64bit(type
->without_array()->base_type
)) {
1289 assert(vary
< PIPE_MAX_SHADER_OUTPUTS
);
1291 switch(prog
->getType()) {
1292 case Program::TYPE_FRAGMENT
:
1293 frag_result_to_tgsi_semantic((gl_frag_result
)slot
, &name
, &index
);
1295 case TGSI_SEMANTIC_COLOR
:
1296 if (!var
->data
.fb_fetch_output
)
1297 info
->prop
.fp
.numColourResults
++;
1298 info
->prop
.fp
.separateFragData
= true;
1299 // sometimes we get FRAG_RESULT_DATAX with data.index 0
1300 // sometimes we get FRAG_RESULT_DATA0 with data.index X
1301 index
= index
== 0 ? var
->data
.index
: index
;
1303 case TGSI_SEMANTIC_POSITION
:
1304 info
->io
.fragDepth
= vary
;
1305 info
->prop
.fp
.writesDepth
= true;
1307 case TGSI_SEMANTIC_SAMPLEMASK
:
1308 info
->io
.sampleMask
= vary
;
1314 case Program::TYPE_GEOMETRY
:
1315 case Program::TYPE_TESSELLATION_CONTROL
:
1316 case Program::TYPE_TESSELLATION_EVAL
:
1317 case Program::TYPE_VERTEX
:
1318 varying_slot_to_tgsi_semantic((gl_varying_slot
)slot
, &name
, &index
);
1320 if (var
->data
.patch
&& name
!= TGSI_SEMANTIC_TESSINNER
&&
1321 name
!= TGSI_SEMANTIC_TESSOUTER
)
1322 info
->numPatchConstants
= MAX2(info
->numPatchConstants
, index
+ slots
);
1325 case TGSI_SEMANTIC_CLIPDIST
:
1326 info
->io
.genUserClip
= -1;
1328 case TGSI_SEMANTIC_CLIPVERTEX
:
1329 clipVertexOutput
= vary
;
1331 case TGSI_SEMANTIC_EDGEFLAG
:
1332 info
->io
.edgeFlagOut
= vary
;
1334 case TGSI_SEMANTIC_POSITION
:
1335 if (clipVertexOutput
< 0)
1336 clipVertexOutput
= vary
;
1343 ERROR("unknown shader type %u in assignSlots\n", prog
->getType());
1347 for (uint16_t i
= 0u; i
< slots
; ++i
, ++vary
) {
1348 info
->out
[vary
].id
= vary
;
1349 info
->out
[vary
].patch
= var
->data
.patch
;
1350 info
->out
[vary
].sn
= name
;
1351 info
->out
[vary
].si
= index
+ i
;
1352 if (glsl_base_type_is_64bit(type
->without_array()->base_type
))
1354 info
->out
[vary
].mask
|= (((1 << (comp
* 2)) - 1) << (frac
* 2) >> 0x4);
1356 info
->out
[vary
].mask
|= (((1 << (comp
* 2)) - 1) << (frac
* 2) & 0xf);
1358 info
->out
[vary
].mask
|= ((1 << comp
) - 1) << frac
;
1360 if (nir
->info
.outputs_read
& 1ull << slot
)
1361 info
->out
[vary
].oread
= 1;
1363 info
->numOutputs
= std::max
<uint8_t>(info
->numOutputs
, vary
);
1366 if (info
->io
.genUserClip
> 0) {
1367 info
->io
.clipDistances
= info
->io
.genUserClip
;
1369 const unsigned int nOut
= (info
->io
.genUserClip
+ 3) / 4;
1371 for (unsigned int n
= 0; n
< nOut
; ++n
) {
1372 unsigned int i
= info
->numOutputs
++;
1373 info
->out
[i
].id
= i
;
1374 info
->out
[i
].sn
= TGSI_SEMANTIC_CLIPDIST
;
1375 info
->out
[i
].si
= n
;
1376 info
->out
[i
].mask
= ((1 << info
->io
.clipDistances
) - 1) >> (n
* 4);
1380 return info
->assignSlots(info
) == 0;
1384 Converter::getSlotAddress(nir_intrinsic_instr
*insn
, uint8_t idx
, uint8_t slot
)
1387 int offset
= nir_intrinsic_component(insn
);
1390 if (nir_intrinsic_infos
[insn
->intrinsic
].has_dest
)
1391 ty
= getDType(insn
);
1393 ty
= getSType(insn
->src
[0], false, false);
1395 switch (insn
->intrinsic
) {
1396 case nir_intrinsic_load_input
:
1397 case nir_intrinsic_load_interpolated_input
:
1398 case nir_intrinsic_load_per_vertex_input
:
1401 case nir_intrinsic_load_output
:
1402 case nir_intrinsic_load_per_vertex_output
:
1403 case nir_intrinsic_store_output
:
1404 case nir_intrinsic_store_per_vertex_output
:
1408 ERROR("unknown intrinsic in getSlotAddress %s",
1409 nir_intrinsic_infos
[insn
->intrinsic
].name
);
1415 if (typeSizeof(ty
) == 8) {
1427 assert(!input
|| idx
< PIPE_MAX_SHADER_INPUTS
);
1428 assert(input
|| idx
< PIPE_MAX_SHADER_OUTPUTS
);
1430 const nv50_ir_varying
*vary
= input
? info
->in
: info
->out
;
1431 return vary
[idx
].slot
[slot
] * 4;
1435 Converter::loadFrom(DataFile file
, uint8_t i
, DataType ty
, Value
*def
,
1436 uint32_t base
, uint8_t c
, Value
*indirect0
,
1437 Value
*indirect1
, bool patch
)
1439 unsigned int tySize
= typeSizeof(ty
);
1442 (file
== FILE_MEMORY_CONST
|| file
== FILE_MEMORY_BUFFER
|| indirect0
)) {
1443 Value
*lo
= getSSA();
1444 Value
*hi
= getSSA();
1447 mkLoad(TYPE_U32
, lo
,
1448 mkSymbol(file
, i
, TYPE_U32
, base
+ c
* tySize
),
1450 loi
->setIndirect(0, 1, indirect1
);
1451 loi
->perPatch
= patch
;
1454 mkLoad(TYPE_U32
, hi
,
1455 mkSymbol(file
, i
, TYPE_U32
, base
+ c
* tySize
+ 4),
1457 hii
->setIndirect(0, 1, indirect1
);
1458 hii
->perPatch
= patch
;
1460 return mkOp2(OP_MERGE
, ty
, def
, lo
, hi
);
1463 mkLoad(ty
, def
, mkSymbol(file
, i
, ty
, base
+ c
* tySize
), indirect0
);
1464 ld
->setIndirect(0, 1, indirect1
);
1465 ld
->perPatch
= patch
;
1471 Converter::storeTo(nir_intrinsic_instr
*insn
, DataFile file
, operation op
,
1472 DataType ty
, Value
*src
, uint8_t idx
, uint8_t c
,
1473 Value
*indirect0
, Value
*indirect1
)
1475 uint8_t size
= typeSizeof(ty
);
1476 uint32_t address
= getSlotAddress(insn
, idx
, c
);
1478 if (size
== 8 && indirect0
) {
1480 mkSplit(split
, 4, src
);
1482 if (op
== OP_EXPORT
) {
1483 split
[0] = mkMov(getSSA(), split
[0], ty
)->getDef(0);
1484 split
[1] = mkMov(getSSA(), split
[1], ty
)->getDef(0);
1487 mkStore(op
, TYPE_U32
, mkSymbol(file
, 0, TYPE_U32
, address
), indirect0
,
1488 split
[0])->perPatch
= info
->out
[idx
].patch
;
1489 mkStore(op
, TYPE_U32
, mkSymbol(file
, 0, TYPE_U32
, address
+ 4), indirect0
,
1490 split
[1])->perPatch
= info
->out
[idx
].patch
;
1492 if (op
== OP_EXPORT
)
1493 src
= mkMov(getSSA(size
), src
, ty
)->getDef(0);
1494 mkStore(op
, ty
, mkSymbol(file
, 0, ty
, address
), indirect0
,
1495 src
)->perPatch
= info
->out
[idx
].patch
;
1500 Converter::parseNIR()
1502 info
->bin
.tlsSpace
= 0;
1503 info
->io
.clipDistances
= nir
->info
.clip_distance_array_size
;
1504 info
->io
.cullDistances
= nir
->info
.cull_distance_array_size
;
1506 switch(prog
->getType()) {
1507 case Program::TYPE_COMPUTE
:
1508 info
->prop
.cp
.numThreads
[0] = nir
->info
.cs
.local_size
[0];
1509 info
->prop
.cp
.numThreads
[1] = nir
->info
.cs
.local_size
[1];
1510 info
->prop
.cp
.numThreads
[2] = nir
->info
.cs
.local_size
[2];
1511 info
->bin
.smemSize
= nir
->info
.cs
.shared_size
;
1513 case Program::TYPE_FRAGMENT
:
1514 info
->prop
.fp
.earlyFragTests
= nir
->info
.fs
.early_fragment_tests
;
1515 info
->prop
.fp
.persampleInvocation
=
1516 (nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_ID
) ||
1517 (nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_POS
);
1518 info
->prop
.fp
.postDepthCoverage
= nir
->info
.fs
.post_depth_coverage
;
1519 info
->prop
.fp
.readsSampleLocations
=
1520 (nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_POS
);
1521 info
->prop
.fp
.usesDiscard
= nir
->info
.fs
.uses_discard
;
1522 info
->prop
.fp
.usesSampleMaskIn
=
1523 !!(nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_MASK_IN
);
1525 case Program::TYPE_GEOMETRY
:
1526 info
->prop
.gp
.inputPrim
= nir
->info
.gs
.input_primitive
;
1527 info
->prop
.gp
.instanceCount
= nir
->info
.gs
.invocations
;
1528 info
->prop
.gp
.maxVertices
= nir
->info
.gs
.vertices_out
;
1529 info
->prop
.gp
.outputPrim
= nir
->info
.gs
.output_primitive
;
1531 case Program::TYPE_TESSELLATION_CONTROL
:
1532 case Program::TYPE_TESSELLATION_EVAL
:
1533 if (nir
->info
.tess
.primitive_mode
== GL_ISOLINES
)
1534 info
->prop
.tp
.domain
= GL_LINES
;
1536 info
->prop
.tp
.domain
= nir
->info
.tess
.primitive_mode
;
1537 info
->prop
.tp
.outputPatchSize
= nir
->info
.tess
.tcs_vertices_out
;
1538 info
->prop
.tp
.outputPrim
=
1539 nir
->info
.tess
.point_mode
? PIPE_PRIM_POINTS
: PIPE_PRIM_TRIANGLES
;
1540 info
->prop
.tp
.partitioning
= (nir
->info
.tess
.spacing
+ 1) % 3;
1541 info
->prop
.tp
.winding
= !nir
->info
.tess
.ccw
;
1543 case Program::TYPE_VERTEX
:
1544 info
->prop
.vp
.usesDrawParameters
=
1545 (nir
->info
.system_values_read
& BITFIELD64_BIT(SYSTEM_VALUE_BASE_VERTEX
)) ||
1546 (nir
->info
.system_values_read
& BITFIELD64_BIT(SYSTEM_VALUE_BASE_INSTANCE
)) ||
1547 (nir
->info
.system_values_read
& BITFIELD64_BIT(SYSTEM_VALUE_DRAW_ID
));
1557 Converter::visit(nir_function
*function
)
1559 // we only support emiting the main function for now
1560 assert(!strcmp(function
->name
, "main"));
1561 assert(function
->impl
);
1563 // usually the blocks will set everything up, but main is special
1564 BasicBlock
*entry
= new BasicBlock(prog
->main
);
1565 exit
= new BasicBlock(prog
->main
);
1566 blocks
[nir_start_block(function
->impl
)->index
] = entry
;
1567 prog
->main
->setEntry(entry
);
1568 prog
->main
->setExit(exit
);
1570 setPosition(entry
, true);
1572 if (info
->io
.genUserClip
> 0) {
1573 for (int c
= 0; c
< 4; ++c
)
1574 clipVtx
[c
] = getScratch();
1577 switch (prog
->getType()) {
1578 case Program::TYPE_TESSELLATION_CONTROL
:
1580 OP_SUB
, TYPE_U32
, getSSA(),
1581 mkOp1v(OP_RDSV
, TYPE_U32
, getSSA(), mkSysVal(SV_LANEID
, 0)),
1582 mkOp1v(OP_RDSV
, TYPE_U32
, getSSA(), mkSysVal(SV_INVOCATION_ID
, 0)));
1584 case Program::TYPE_FRAGMENT
: {
1585 Symbol
*sv
= mkSysVal(SV_POSITION
, 3);
1586 fragCoord
[3] = mkOp1v(OP_RDSV
, TYPE_F32
, getSSA(), sv
);
1587 fp
.position
= mkOp1v(OP_RCP
, TYPE_F32
, fragCoord
[3], fragCoord
[3]);
1594 nir_foreach_register(reg
, &function
->impl
->registers
) {
1595 if (reg
->num_array_elems
) {
1596 // TODO: packed variables would be nice, but MemoryOpt fails
1597 // replace 4 with reg->num_components
1598 uint32_t size
= 4 * reg
->num_array_elems
* (reg
->bit_size
/ 8);
1599 regToLmemOffset
[reg
->index
] = info
->bin
.tlsSpace
;
1600 info
->bin
.tlsSpace
+= size
;
1604 nir_index_ssa_defs(function
->impl
);
1605 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
1610 bb
->cfg
.attach(&exit
->cfg
, Graph::Edge::TREE
);
1611 setPosition(exit
, true);
1613 if ((prog
->getType() == Program::TYPE_VERTEX
||
1614 prog
->getType() == Program::TYPE_TESSELLATION_EVAL
)
1615 && info
->io
.genUserClip
> 0)
1616 handleUserClipPlanes();
1618 // TODO: for non main function this needs to be a OP_RETURN
1619 mkOp(OP_EXIT
, TYPE_NONE
, NULL
)->terminator
= 1;
1624 Converter::visit(nir_cf_node
*node
)
1626 switch (node
->type
) {
1627 case nir_cf_node_block
:
1628 return visit(nir_cf_node_as_block(node
));
1629 case nir_cf_node_if
:
1630 return visit(nir_cf_node_as_if(node
));
1631 case nir_cf_node_loop
:
1632 return visit(nir_cf_node_as_loop(node
));
1634 ERROR("unknown nir_cf_node type %u\n", node
->type
);
1640 Converter::visit(nir_block
*block
)
1642 if (!block
->predecessors
->entries
&& block
->instr_list
.is_empty())
1645 BasicBlock
*bb
= convert(block
);
1647 setPosition(bb
, true);
1648 nir_foreach_instr(insn
, block
) {
1656 Converter::visit(nir_if
*nif
)
1658 DataType sType
= getSType(nif
->condition
, false, false);
1659 Value
*src
= getSrc(&nif
->condition
, 0);
1661 nir_block
*lastThen
= nir_if_last_then_block(nif
);
1662 nir_block
*lastElse
= nir_if_last_else_block(nif
);
1664 assert(!lastThen
->successors
[1]);
1665 assert(!lastElse
->successors
[1]);
1667 BasicBlock
*ifBB
= convert(nir_if_first_then_block(nif
));
1668 BasicBlock
*elseBB
= convert(nir_if_first_else_block(nif
));
1670 bb
->cfg
.attach(&ifBB
->cfg
, Graph::Edge::TREE
);
1671 bb
->cfg
.attach(&elseBB
->cfg
, Graph::Edge::TREE
);
1673 // we only insert joinats, if both nodes end up at the end of the if again.
1674 // the reason for this to not happens are breaks/continues/ret/... which
1675 // have their own handling
1676 if (lastThen
->successors
[0] == lastElse
->successors
[0])
1677 bb
->joinAt
= mkFlow(OP_JOINAT
, convert(lastThen
->successors
[0]),
1680 mkFlow(OP_BRA
, elseBB
, CC_EQ
, src
)->setType(sType
);
1682 foreach_list_typed(nir_cf_node
, node
, node
, &nif
->then_list
) {
1686 setPosition(convert(lastThen
), true);
1687 if (!bb
->getExit() ||
1688 !bb
->getExit()->asFlow() ||
1689 bb
->getExit()->asFlow()->op
== OP_JOIN
) {
1690 BasicBlock
*tailBB
= convert(lastThen
->successors
[0]);
1691 mkFlow(OP_BRA
, tailBB
, CC_ALWAYS
, NULL
);
1692 bb
->cfg
.attach(&tailBB
->cfg
, Graph::Edge::FORWARD
);
1695 foreach_list_typed(nir_cf_node
, node
, node
, &nif
->else_list
) {
1699 setPosition(convert(lastElse
), true);
1700 if (!bb
->getExit() ||
1701 !bb
->getExit()->asFlow() ||
1702 bb
->getExit()->asFlow()->op
== OP_JOIN
) {
1703 BasicBlock
*tailBB
= convert(lastElse
->successors
[0]);
1704 mkFlow(OP_BRA
, tailBB
, CC_ALWAYS
, NULL
);
1705 bb
->cfg
.attach(&tailBB
->cfg
, Graph::Edge::FORWARD
);
1708 if (lastThen
->successors
[0] == lastElse
->successors
[0]) {
1709 setPosition(convert(lastThen
->successors
[0]), true);
1710 mkFlow(OP_JOIN
, NULL
, CC_ALWAYS
, NULL
)->fixed
= 1;
1717 Converter::visit(nir_loop
*loop
)
1720 func
->loopNestingBound
= std::max(func
->loopNestingBound
, curLoopDepth
);
1722 BasicBlock
*loopBB
= convert(nir_loop_first_block(loop
));
1723 BasicBlock
*tailBB
=
1724 convert(nir_cf_node_as_block(nir_cf_node_next(&loop
->cf_node
)));
1725 bb
->cfg
.attach(&loopBB
->cfg
, Graph::Edge::TREE
);
1727 mkFlow(OP_PREBREAK
, tailBB
, CC_ALWAYS
, NULL
);
1728 setPosition(loopBB
, false);
1729 mkFlow(OP_PRECONT
, loopBB
, CC_ALWAYS
, NULL
);
1731 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
1735 Instruction
*insn
= bb
->getExit();
1736 if (bb
->cfg
.incidentCount() != 0) {
1737 if (!insn
|| !insn
->asFlow()) {
1738 mkFlow(OP_CONT
, loopBB
, CC_ALWAYS
, NULL
);
1739 bb
->cfg
.attach(&loopBB
->cfg
, Graph::Edge::BACK
);
1740 } else if (insn
&& insn
->op
== OP_BRA
&& !insn
->getPredicate() &&
1741 tailBB
->cfg
.incidentCount() == 0) {
1742 // RA doesn't like having blocks around with no incident edge,
1743 // so we create a fake one to make it happy
1744 bb
->cfg
.attach(&tailBB
->cfg
, Graph::Edge::TREE
);
1754 Converter::visit(nir_instr
*insn
)
1756 // we need an insertion point for on the fly generated immediate loads
1757 immInsertPos
= bb
->getExit();
1758 switch (insn
->type
) {
1759 case nir_instr_type_alu
:
1760 return visit(nir_instr_as_alu(insn
));
1761 case nir_instr_type_deref
:
1762 return visit(nir_instr_as_deref(insn
));
1763 case nir_instr_type_intrinsic
:
1764 return visit(nir_instr_as_intrinsic(insn
));
1765 case nir_instr_type_jump
:
1766 return visit(nir_instr_as_jump(insn
));
1767 case nir_instr_type_load_const
:
1768 return visit(nir_instr_as_load_const(insn
));
1769 case nir_instr_type_ssa_undef
:
1770 return visit(nir_instr_as_ssa_undef(insn
));
1771 case nir_instr_type_tex
:
1772 return visit(nir_instr_as_tex(insn
));
1774 ERROR("unknown nir_instr type %u\n", insn
->type
);
1781 Converter::convert(nir_intrinsic_op intr
)
1784 case nir_intrinsic_load_base_vertex
:
1785 return SV_BASEVERTEX
;
1786 case nir_intrinsic_load_base_instance
:
1787 return SV_BASEINSTANCE
;
1788 case nir_intrinsic_load_draw_id
:
1790 case nir_intrinsic_load_front_face
:
1792 case nir_intrinsic_load_helper_invocation
:
1793 return SV_THREAD_KILL
;
1794 case nir_intrinsic_load_instance_id
:
1795 return SV_INSTANCE_ID
;
1796 case nir_intrinsic_load_invocation_id
:
1797 return SV_INVOCATION_ID
;
1798 case nir_intrinsic_load_local_group_size
:
1800 case nir_intrinsic_load_local_invocation_id
:
1802 case nir_intrinsic_load_num_work_groups
:
1804 case nir_intrinsic_load_patch_vertices_in
:
1805 return SV_VERTEX_COUNT
;
1806 case nir_intrinsic_load_primitive_id
:
1807 return SV_PRIMITIVE_ID
;
1808 case nir_intrinsic_load_sample_id
:
1809 return SV_SAMPLE_INDEX
;
1810 case nir_intrinsic_load_sample_mask_in
:
1811 return SV_SAMPLE_MASK
;
1812 case nir_intrinsic_load_sample_pos
:
1813 return SV_SAMPLE_POS
;
1814 case nir_intrinsic_load_subgroup_eq_mask
:
1815 return SV_LANEMASK_EQ
;
1816 case nir_intrinsic_load_subgroup_ge_mask
:
1817 return SV_LANEMASK_GE
;
1818 case nir_intrinsic_load_subgroup_gt_mask
:
1819 return SV_LANEMASK_GT
;
1820 case nir_intrinsic_load_subgroup_le_mask
:
1821 return SV_LANEMASK_LE
;
1822 case nir_intrinsic_load_subgroup_lt_mask
:
1823 return SV_LANEMASK_LT
;
1824 case nir_intrinsic_load_subgroup_invocation
:
1826 case nir_intrinsic_load_tess_coord
:
1827 return SV_TESS_COORD
;
1828 case nir_intrinsic_load_tess_level_inner
:
1829 return SV_TESS_INNER
;
1830 case nir_intrinsic_load_tess_level_outer
:
1831 return SV_TESS_OUTER
;
1832 case nir_intrinsic_load_vertex_id
:
1833 return SV_VERTEX_ID
;
1834 case nir_intrinsic_load_work_group_id
:
1837 ERROR("unknown SVSemantic for nir_intrinsic_op %s\n",
1838 nir_intrinsic_infos
[intr
].name
);
1845 Converter::convertGLImgFormat(GLuint format
)
1847 #define FMT_CASE(a, b) \
1848 case GL_ ## a: return nv50_ir::FMT_ ## b
1851 FMT_CASE(NONE
, NONE
);
1853 FMT_CASE(RGBA32F
, RGBA32F
);
1854 FMT_CASE(RGBA16F
, RGBA16F
);
1855 FMT_CASE(RG32F
, RG32F
);
1856 FMT_CASE(RG16F
, RG16F
);
1857 FMT_CASE(R11F_G11F_B10F
, R11G11B10F
);
1858 FMT_CASE(R32F
, R32F
);
1859 FMT_CASE(R16F
, R16F
);
1861 FMT_CASE(RGBA32UI
, RGBA32UI
);
1862 FMT_CASE(RGBA16UI
, RGBA16UI
);
1863 FMT_CASE(RGB10_A2UI
, RGB10A2UI
);
1864 FMT_CASE(RGBA8UI
, RGBA8UI
);
1865 FMT_CASE(RG32UI
, RG32UI
);
1866 FMT_CASE(RG16UI
, RG16UI
);
1867 FMT_CASE(RG8UI
, RG8UI
);
1868 FMT_CASE(R32UI
, R32UI
);
1869 FMT_CASE(R16UI
, R16UI
);
1870 FMT_CASE(R8UI
, R8UI
);
1872 FMT_CASE(RGBA32I
, RGBA32I
);
1873 FMT_CASE(RGBA16I
, RGBA16I
);
1874 FMT_CASE(RGBA8I
, RGBA8I
);
1875 FMT_CASE(RG32I
, RG32I
);
1876 FMT_CASE(RG16I
, RG16I
);
1877 FMT_CASE(RG8I
, RG8I
);
1878 FMT_CASE(R32I
, R32I
);
1879 FMT_CASE(R16I
, R16I
);
1882 FMT_CASE(RGBA16
, RGBA16
);
1883 FMT_CASE(RGB10_A2
, RGB10A2
);
1884 FMT_CASE(RGBA8
, RGBA8
);
1885 FMT_CASE(RG16
, RG16
);
1890 FMT_CASE(RGBA16_SNORM
, RGBA16_SNORM
);
1891 FMT_CASE(RGBA8_SNORM
, RGBA8_SNORM
);
1892 FMT_CASE(RG16_SNORM
, RG16_SNORM
);
1893 FMT_CASE(RG8_SNORM
, RG8_SNORM
);
1894 FMT_CASE(R16_SNORM
, R16_SNORM
);
1895 FMT_CASE(R8_SNORM
, R8_SNORM
);
1897 FMT_CASE(BGRA_INTEGER
, BGRA8
);
1899 ERROR("unknown format %x\n", format
);
1901 return nv50_ir::FMT_NONE
;
1907 Converter::visit(nir_intrinsic_instr
*insn
)
1909 nir_intrinsic_op op
= insn
->intrinsic
;
1910 const nir_intrinsic_info
&opInfo
= nir_intrinsic_infos
[op
];
1913 case nir_intrinsic_load_uniform
: {
1914 LValues
&newDefs
= convert(&insn
->dest
);
1915 const DataType dType
= getDType(insn
);
1917 uint32_t coffset
= getIndirect(insn
, 0, 0, indirect
);
1918 for (uint8_t i
= 0; i
< insn
->num_components
; ++i
) {
1919 loadFrom(FILE_MEMORY_CONST
, 0, dType
, newDefs
[i
], 16 * coffset
, i
, indirect
);
1923 case nir_intrinsic_store_output
:
1924 case nir_intrinsic_store_per_vertex_output
: {
1926 DataType dType
= getSType(insn
->src
[0], false, false);
1927 uint32_t idx
= getIndirect(insn
, op
== nir_intrinsic_store_output
? 1 : 2, 0, indirect
);
1929 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
1930 if (!((1u << i
) & nir_intrinsic_write_mask(insn
)))
1934 Value
*src
= getSrc(&insn
->src
[0], i
);
1935 switch (prog
->getType()) {
1936 case Program::TYPE_FRAGMENT
: {
1937 if (info
->out
[idx
].sn
== TGSI_SEMANTIC_POSITION
) {
1938 // TGSI uses a different interface than NIR, TGSI stores that
1939 // value in the z component, NIR in X
1941 src
= mkOp1v(OP_SAT
, TYPE_F32
, getScratch(), src
);
1945 case Program::TYPE_GEOMETRY
:
1946 case Program::TYPE_VERTEX
: {
1947 if (info
->io
.genUserClip
> 0 && idx
== clipVertexOutput
) {
1948 mkMov(clipVtx
[i
], src
);
1957 storeTo(insn
, FILE_SHADER_OUTPUT
, OP_EXPORT
, dType
, src
, idx
, i
+ offset
, indirect
);
1961 case nir_intrinsic_load_input
:
1962 case nir_intrinsic_load_interpolated_input
:
1963 case nir_intrinsic_load_output
: {
1964 LValues
&newDefs
= convert(&insn
->dest
);
1967 if (prog
->getType() == Program::TYPE_FRAGMENT
&&
1968 op
== nir_intrinsic_load_output
) {
1969 std::vector
<Value
*> defs
, srcs
;
1972 srcs
.push_back(getSSA());
1973 srcs
.push_back(getSSA());
1974 Value
*x
= mkOp1v(OP_RDSV
, TYPE_F32
, getSSA(), mkSysVal(SV_POSITION
, 0));
1975 Value
*y
= mkOp1v(OP_RDSV
, TYPE_F32
, getSSA(), mkSysVal(SV_POSITION
, 1));
1976 mkCvt(OP_CVT
, TYPE_U32
, srcs
[0], TYPE_F32
, x
)->rnd
= ROUND_Z
;
1977 mkCvt(OP_CVT
, TYPE_U32
, srcs
[1], TYPE_F32
, y
)->rnd
= ROUND_Z
;
1979 srcs
.push_back(mkOp1v(OP_RDSV
, TYPE_U32
, getSSA(), mkSysVal(SV_LAYER
, 0)));
1980 srcs
.push_back(mkOp1v(OP_RDSV
, TYPE_U32
, getSSA(), mkSysVal(SV_SAMPLE_INDEX
, 0)));
1982 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
1983 defs
.push_back(newDefs
[i
]);
1987 TexInstruction
*texi
= mkTex(OP_TXF
, TEX_TARGET_2D_MS_ARRAY
, 0, 0, defs
, srcs
);
1988 texi
->tex
.levelZero
= 1;
1989 texi
->tex
.mask
= mask
;
1990 texi
->tex
.useOffsets
= 0;
1991 texi
->tex
.r
= 0xffff;
1992 texi
->tex
.s
= 0xffff;
1994 info
->prop
.fp
.readsFramebuffer
= true;
1998 const DataType dType
= getDType(insn
);
2000 bool input
= op
!= nir_intrinsic_load_output
;
2004 uint32_t idx
= getIndirect(insn
, op
== nir_intrinsic_load_interpolated_input
? 1 : 0, 0, indirect
);
2005 nv50_ir_varying
& vary
= input
? info
->in
[idx
] : info
->out
[idx
];
2007 // see load_barycentric_* handling
2008 if (prog
->getType() == Program::TYPE_FRAGMENT
) {
2009 mode
= translateInterpMode(&vary
, nvirOp
);
2010 if (op
== nir_intrinsic_load_interpolated_input
) {
2011 ImmediateValue immMode
;
2012 if (getSrc(&insn
->src
[0], 1)->getUniqueInsn()->src(0).getImmediate(immMode
))
2013 mode
|= immMode
.reg
.data
.u32
;
2017 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
2018 uint32_t address
= getSlotAddress(insn
, idx
, i
);
2019 Symbol
*sym
= mkSymbol(input
? FILE_SHADER_INPUT
: FILE_SHADER_OUTPUT
, 0, dType
, address
);
2020 if (prog
->getType() == Program::TYPE_FRAGMENT
) {
2022 if (typeSizeof(dType
) == 8) {
2023 Value
*lo
= getSSA();
2024 Value
*hi
= getSSA();
2025 Instruction
*interp
;
2027 interp
= mkOp1(nvirOp
, TYPE_U32
, lo
, sym
);
2028 if (nvirOp
== OP_PINTERP
)
2029 interp
->setSrc(s
++, fp
.position
);
2030 if (mode
& NV50_IR_INTERP_OFFSET
)
2031 interp
->setSrc(s
++, getSrc(&insn
->src
[0], 0));
2032 interp
->setInterpolate(mode
);
2033 interp
->setIndirect(0, 0, indirect
);
2035 Symbol
*sym1
= mkSymbol(input
? FILE_SHADER_INPUT
: FILE_SHADER_OUTPUT
, 0, dType
, address
+ 4);
2036 interp
= mkOp1(nvirOp
, TYPE_U32
, hi
, sym1
);
2037 if (nvirOp
== OP_PINTERP
)
2038 interp
->setSrc(s
++, fp
.position
);
2039 if (mode
& NV50_IR_INTERP_OFFSET
)
2040 interp
->setSrc(s
++, getSrc(&insn
->src
[0], 0));
2041 interp
->setInterpolate(mode
);
2042 interp
->setIndirect(0, 0, indirect
);
2044 mkOp2(OP_MERGE
, dType
, newDefs
[i
], lo
, hi
);
2046 Instruction
*interp
= mkOp1(nvirOp
, dType
, newDefs
[i
], sym
);
2047 if (nvirOp
== OP_PINTERP
)
2048 interp
->setSrc(s
++, fp
.position
);
2049 if (mode
& NV50_IR_INTERP_OFFSET
)
2050 interp
->setSrc(s
++, getSrc(&insn
->src
[0], 0));
2051 interp
->setInterpolate(mode
);
2052 interp
->setIndirect(0, 0, indirect
);
2055 mkLoad(dType
, newDefs
[i
], sym
, indirect
)->perPatch
= vary
.patch
;
2060 case nir_intrinsic_load_barycentric_at_offset
:
2061 case nir_intrinsic_load_barycentric_at_sample
:
2062 case nir_intrinsic_load_barycentric_centroid
:
2063 case nir_intrinsic_load_barycentric_pixel
:
2064 case nir_intrinsic_load_barycentric_sample
: {
2065 LValues
&newDefs
= convert(&insn
->dest
);
2068 if (op
== nir_intrinsic_load_barycentric_centroid
||
2069 op
== nir_intrinsic_load_barycentric_sample
) {
2070 mode
= NV50_IR_INTERP_CENTROID
;
2071 } else if (op
== nir_intrinsic_load_barycentric_at_offset
) {
2073 for (uint8_t c
= 0; c
< 2; c
++) {
2074 offs
[c
] = getScratch();
2075 mkOp2(OP_MIN
, TYPE_F32
, offs
[c
], getSrc(&insn
->src
[0], c
), loadImm(NULL
, 0.4375f
));
2076 mkOp2(OP_MAX
, TYPE_F32
, offs
[c
], offs
[c
], loadImm(NULL
, -0.5f
));
2077 mkOp2(OP_MUL
, TYPE_F32
, offs
[c
], offs
[c
], loadImm(NULL
, 4096.0f
));
2078 mkCvt(OP_CVT
, TYPE_S32
, offs
[c
], TYPE_F32
, offs
[c
]);
2080 mkOp3v(OP_INSBF
, TYPE_U32
, newDefs
[0], offs
[1], mkImm(0x1010), offs
[0]);
2082 mode
= NV50_IR_INTERP_OFFSET
;
2083 } else if (op
== nir_intrinsic_load_barycentric_pixel
) {
2084 mode
= NV50_IR_INTERP_DEFAULT
;
2085 } else if (op
== nir_intrinsic_load_barycentric_at_sample
) {
2086 info
->prop
.fp
.readsSampleLocations
= true;
2087 mkOp1(OP_PIXLD
, TYPE_U32
, newDefs
[0], getSrc(&insn
->src
[0], 0))->subOp
= NV50_IR_SUBOP_PIXLD_OFFSET
;
2088 mode
= NV50_IR_INTERP_OFFSET
;
2090 unreachable("all intrinsics already handled above");
2093 loadImm(newDefs
[1], mode
);
2096 case nir_intrinsic_discard
:
2097 mkOp(OP_DISCARD
, TYPE_NONE
, NULL
);
2099 case nir_intrinsic_discard_if
: {
2100 Value
*pred
= getSSA(1, FILE_PREDICATE
);
2101 if (insn
->num_components
> 1) {
2102 ERROR("nir_intrinsic_discard_if only with 1 component supported!\n");
2106 mkCmp(OP_SET
, CC_NE
, TYPE_U8
, pred
, TYPE_U32
, getSrc(&insn
->src
[0], 0), zero
);
2107 mkOp(OP_DISCARD
, TYPE_NONE
, NULL
)->setPredicate(CC_P
, pred
);
2110 case nir_intrinsic_load_base_vertex
:
2111 case nir_intrinsic_load_base_instance
:
2112 case nir_intrinsic_load_draw_id
:
2113 case nir_intrinsic_load_front_face
:
2114 case nir_intrinsic_load_helper_invocation
:
2115 case nir_intrinsic_load_instance_id
:
2116 case nir_intrinsic_load_invocation_id
:
2117 case nir_intrinsic_load_local_group_size
:
2118 case nir_intrinsic_load_local_invocation_id
:
2119 case nir_intrinsic_load_num_work_groups
:
2120 case nir_intrinsic_load_patch_vertices_in
:
2121 case nir_intrinsic_load_primitive_id
:
2122 case nir_intrinsic_load_sample_id
:
2123 case nir_intrinsic_load_sample_mask_in
:
2124 case nir_intrinsic_load_sample_pos
:
2125 case nir_intrinsic_load_subgroup_eq_mask
:
2126 case nir_intrinsic_load_subgroup_ge_mask
:
2127 case nir_intrinsic_load_subgroup_gt_mask
:
2128 case nir_intrinsic_load_subgroup_le_mask
:
2129 case nir_intrinsic_load_subgroup_lt_mask
:
2130 case nir_intrinsic_load_subgroup_invocation
:
2131 case nir_intrinsic_load_tess_coord
:
2132 case nir_intrinsic_load_tess_level_inner
:
2133 case nir_intrinsic_load_tess_level_outer
:
2134 case nir_intrinsic_load_vertex_id
:
2135 case nir_intrinsic_load_work_group_id
: {
2136 const DataType dType
= getDType(insn
);
2137 SVSemantic sv
= convert(op
);
2138 LValues
&newDefs
= convert(&insn
->dest
);
2140 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
2142 if (typeSizeof(dType
) == 8)
2147 if (sv
== SV_TID
&& info
->prop
.cp
.numThreads
[i
] == 1) {
2150 Symbol
*sym
= mkSysVal(sv
, i
);
2151 Instruction
*rdsv
= mkOp1(OP_RDSV
, TYPE_U32
, def
, sym
);
2152 if (sv
== SV_TESS_OUTER
|| sv
== SV_TESS_INNER
)
2156 if (typeSizeof(dType
) == 8)
2157 mkOp2(OP_MERGE
, dType
, newDefs
[i
], def
, loadImm(getSSA(), 0u));
2162 case nir_intrinsic_load_subgroup_size
: {
2163 LValues
&newDefs
= convert(&insn
->dest
);
2164 loadImm(newDefs
[0], 32u);
2167 case nir_intrinsic_vote_all
:
2168 case nir_intrinsic_vote_any
:
2169 case nir_intrinsic_vote_ieq
: {
2170 LValues
&newDefs
= convert(&insn
->dest
);
2171 Value
*pred
= getScratch(1, FILE_PREDICATE
);
2172 mkCmp(OP_SET
, CC_NE
, TYPE_U32
, pred
, TYPE_U32
, getSrc(&insn
->src
[0], 0), zero
);
2173 mkOp1(OP_VOTE
, TYPE_U32
, pred
, pred
)->subOp
= getSubOp(op
);
2174 mkCvt(OP_CVT
, TYPE_U32
, newDefs
[0], TYPE_U8
, pred
);
2177 case nir_intrinsic_ballot
: {
2178 LValues
&newDefs
= convert(&insn
->dest
);
2179 Value
*pred
= getSSA(1, FILE_PREDICATE
);
2180 mkCmp(OP_SET
, CC_NE
, TYPE_U32
, pred
, TYPE_U32
, getSrc(&insn
->src
[0], 0), zero
);
2181 mkOp1(OP_VOTE
, TYPE_U32
, newDefs
[0], pred
)->subOp
= NV50_IR_SUBOP_VOTE_ANY
;
2184 case nir_intrinsic_read_first_invocation
:
2185 case nir_intrinsic_read_invocation
: {
2186 LValues
&newDefs
= convert(&insn
->dest
);
2187 const DataType dType
= getDType(insn
);
2188 Value
*tmp
= getScratch();
2190 if (op
== nir_intrinsic_read_first_invocation
) {
2191 mkOp1(OP_VOTE
, TYPE_U32
, tmp
, mkImm(1))->subOp
= NV50_IR_SUBOP_VOTE_ANY
;
2192 mkOp2(OP_EXTBF
, TYPE_U32
, tmp
, tmp
, mkImm(0x2000))->subOp
= NV50_IR_SUBOP_EXTBF_REV
;
2193 mkOp1(OP_BFIND
, TYPE_U32
, tmp
, tmp
)->subOp
= NV50_IR_SUBOP_BFIND_SAMT
;
2195 tmp
= getSrc(&insn
->src
[1], 0);
2197 for (uint8_t i
= 0; i
< insn
->num_components
; ++i
) {
2198 mkOp3(OP_SHFL
, dType
, newDefs
[i
], getSrc(&insn
->src
[0], i
), tmp
, mkImm(0x1f))
2199 ->subOp
= NV50_IR_SUBOP_SHFL_IDX
;
2203 case nir_intrinsic_load_per_vertex_input
: {
2204 const DataType dType
= getDType(insn
);
2205 LValues
&newDefs
= convert(&insn
->dest
);
2206 Value
*indirectVertex
;
2207 Value
*indirectOffset
;
2208 uint32_t baseVertex
= getIndirect(&insn
->src
[0], 0, indirectVertex
);
2209 uint32_t idx
= getIndirect(insn
, 1, 0, indirectOffset
);
2211 Value
*vtxBase
= mkOp2v(OP_PFETCH
, TYPE_U32
, getSSA(4, FILE_ADDRESS
),
2212 mkImm(baseVertex
), indirectVertex
);
2213 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
2214 uint32_t address
= getSlotAddress(insn
, idx
, i
);
2215 loadFrom(FILE_SHADER_INPUT
, 0, dType
, newDefs
[i
], address
, 0,
2216 indirectOffset
, vtxBase
, info
->in
[idx
].patch
);
2220 case nir_intrinsic_load_per_vertex_output
: {
2221 const DataType dType
= getDType(insn
);
2222 LValues
&newDefs
= convert(&insn
->dest
);
2223 Value
*indirectVertex
;
2224 Value
*indirectOffset
;
2225 uint32_t baseVertex
= getIndirect(&insn
->src
[0], 0, indirectVertex
);
2226 uint32_t idx
= getIndirect(insn
, 1, 0, indirectOffset
);
2227 Value
*vtxBase
= NULL
;
2230 vtxBase
= indirectVertex
;
2232 vtxBase
= loadImm(NULL
, baseVertex
);
2234 vtxBase
= mkOp2v(OP_ADD
, TYPE_U32
, getSSA(4, FILE_ADDRESS
), outBase
, vtxBase
);
2236 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
2237 uint32_t address
= getSlotAddress(insn
, idx
, i
);
2238 loadFrom(FILE_SHADER_OUTPUT
, 0, dType
, newDefs
[i
], address
, 0,
2239 indirectOffset
, vtxBase
, info
->in
[idx
].patch
);
2243 case nir_intrinsic_emit_vertex
:
2244 if (info
->io
.genUserClip
> 0)
2245 handleUserClipPlanes();
2247 case nir_intrinsic_end_primitive
: {
2248 uint32_t idx
= nir_intrinsic_stream_id(insn
);
2249 mkOp1(getOperation(op
), TYPE_U32
, NULL
, mkImm(idx
))->fixed
= 1;
2252 case nir_intrinsic_load_ubo
: {
2253 const DataType dType
= getDType(insn
);
2254 LValues
&newDefs
= convert(&insn
->dest
);
2255 Value
*indirectIndex
;
2256 Value
*indirectOffset
;
2257 uint32_t index
= getIndirect(&insn
->src
[0], 0, indirectIndex
) + 1;
2258 uint32_t offset
= getIndirect(&insn
->src
[1], 0, indirectOffset
);
2260 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
2261 loadFrom(FILE_MEMORY_CONST
, index
, dType
, newDefs
[i
], offset
, i
,
2262 indirectOffset
, indirectIndex
);
2266 case nir_intrinsic_get_buffer_size
: {
2267 LValues
&newDefs
= convert(&insn
->dest
);
2268 const DataType dType
= getDType(insn
);
2269 Value
*indirectBuffer
;
2270 uint32_t buffer
= getIndirect(&insn
->src
[0], 0, indirectBuffer
);
2272 Symbol
*sym
= mkSymbol(FILE_MEMORY_BUFFER
, buffer
, dType
, 0);
2273 mkOp1(OP_BUFQ
, dType
, newDefs
[0], sym
)->setIndirect(0, 0, indirectBuffer
);
2276 case nir_intrinsic_store_ssbo
: {
2277 DataType sType
= getSType(insn
->src
[0], false, false);
2278 Value
*indirectBuffer
;
2279 Value
*indirectOffset
;
2280 uint32_t buffer
= getIndirect(&insn
->src
[1], 0, indirectBuffer
);
2281 uint32_t offset
= getIndirect(&insn
->src
[2], 0, indirectOffset
);
2283 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
2284 if (!((1u << i
) & nir_intrinsic_write_mask(insn
)))
2286 Symbol
*sym
= mkSymbol(FILE_MEMORY_BUFFER
, buffer
, sType
,
2287 offset
+ i
* typeSizeof(sType
));
2288 mkStore(OP_STORE
, sType
, sym
, indirectOffset
, getSrc(&insn
->src
[0], i
))
2289 ->setIndirect(0, 1, indirectBuffer
);
2291 info
->io
.globalAccess
|= 0x2;
2294 case nir_intrinsic_load_ssbo
: {
2295 const DataType dType
= getDType(insn
);
2296 LValues
&newDefs
= convert(&insn
->dest
);
2297 Value
*indirectBuffer
;
2298 Value
*indirectOffset
;
2299 uint32_t buffer
= getIndirect(&insn
->src
[0], 0, indirectBuffer
);
2300 uint32_t offset
= getIndirect(&insn
->src
[1], 0, indirectOffset
);
2302 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
)
2303 loadFrom(FILE_MEMORY_BUFFER
, buffer
, dType
, newDefs
[i
], offset
, i
,
2304 indirectOffset
, indirectBuffer
);
2306 info
->io
.globalAccess
|= 0x1;
2309 case nir_intrinsic_shared_atomic_add
:
2310 case nir_intrinsic_shared_atomic_and
:
2311 case nir_intrinsic_shared_atomic_comp_swap
:
2312 case nir_intrinsic_shared_atomic_exchange
:
2313 case nir_intrinsic_shared_atomic_or
:
2314 case nir_intrinsic_shared_atomic_imax
:
2315 case nir_intrinsic_shared_atomic_imin
:
2316 case nir_intrinsic_shared_atomic_umax
:
2317 case nir_intrinsic_shared_atomic_umin
:
2318 case nir_intrinsic_shared_atomic_xor
: {
2319 const DataType dType
= getDType(insn
);
2320 LValues
&newDefs
= convert(&insn
->dest
);
2321 Value
*indirectOffset
;
2322 uint32_t offset
= getIndirect(&insn
->src
[0], 0, indirectOffset
);
2323 Symbol
*sym
= mkSymbol(FILE_MEMORY_SHARED
, 0, dType
, offset
);
2324 Instruction
*atom
= mkOp2(OP_ATOM
, dType
, newDefs
[0], sym
, getSrc(&insn
->src
[1], 0));
2325 if (op
== nir_intrinsic_shared_atomic_comp_swap
)
2326 atom
->setSrc(2, getSrc(&insn
->src
[2], 0));
2327 atom
->setIndirect(0, 0, indirectOffset
);
2328 atom
->subOp
= getSubOp(op
);
2331 case nir_intrinsic_ssbo_atomic_add
:
2332 case nir_intrinsic_ssbo_atomic_and
:
2333 case nir_intrinsic_ssbo_atomic_comp_swap
:
2334 case nir_intrinsic_ssbo_atomic_exchange
:
2335 case nir_intrinsic_ssbo_atomic_or
:
2336 case nir_intrinsic_ssbo_atomic_imax
:
2337 case nir_intrinsic_ssbo_atomic_imin
:
2338 case nir_intrinsic_ssbo_atomic_umax
:
2339 case nir_intrinsic_ssbo_atomic_umin
:
2340 case nir_intrinsic_ssbo_atomic_xor
: {
2341 const DataType dType
= getDType(insn
);
2342 LValues
&newDefs
= convert(&insn
->dest
);
2343 Value
*indirectBuffer
;
2344 Value
*indirectOffset
;
2345 uint32_t buffer
= getIndirect(&insn
->src
[0], 0, indirectBuffer
);
2346 uint32_t offset
= getIndirect(&insn
->src
[1], 0, indirectOffset
);
2348 Symbol
*sym
= mkSymbol(FILE_MEMORY_BUFFER
, buffer
, dType
, offset
);
2349 Instruction
*atom
= mkOp2(OP_ATOM
, dType
, newDefs
[0], sym
,
2350 getSrc(&insn
->src
[2], 0));
2351 if (op
== nir_intrinsic_ssbo_atomic_comp_swap
)
2352 atom
->setSrc(2, getSrc(&insn
->src
[3], 0));
2353 atom
->setIndirect(0, 0, indirectOffset
);
2354 atom
->setIndirect(0, 1, indirectBuffer
);
2355 atom
->subOp
= getSubOp(op
);
2357 info
->io
.globalAccess
|= 0x2;
2360 case nir_intrinsic_bindless_image_atomic_add
:
2361 case nir_intrinsic_bindless_image_atomic_and
:
2362 case nir_intrinsic_bindless_image_atomic_comp_swap
:
2363 case nir_intrinsic_bindless_image_atomic_exchange
:
2364 case nir_intrinsic_bindless_image_atomic_max
:
2365 case nir_intrinsic_bindless_image_atomic_min
:
2366 case nir_intrinsic_bindless_image_atomic_or
:
2367 case nir_intrinsic_bindless_image_atomic_xor
:
2368 case nir_intrinsic_bindless_image_load
:
2369 case nir_intrinsic_bindless_image_samples
:
2370 case nir_intrinsic_bindless_image_size
:
2371 case nir_intrinsic_bindless_image_store
: {
2372 std::vector
<Value
*> srcs
, defs
;
2373 Value
*indirect
= getSrc(&insn
->src
[0], 0);
2377 TexInstruction::Target target
=
2378 convert(nir_intrinsic_image_dim(insn
), !!nir_intrinsic_image_array(insn
), false);
2379 unsigned int argCount
= getNIRArgCount(target
);
2380 uint16_t location
= 0;
2382 if (opInfo
.has_dest
) {
2383 LValues
&newDefs
= convert(&insn
->dest
);
2384 for (uint8_t i
= 0u; i
< newDefs
.size(); ++i
) {
2385 defs
.push_back(newDefs
[i
]);
2391 case nir_intrinsic_bindless_image_atomic_add
:
2392 case nir_intrinsic_bindless_image_atomic_and
:
2393 case nir_intrinsic_bindless_image_atomic_comp_swap
:
2394 case nir_intrinsic_bindless_image_atomic_exchange
:
2395 case nir_intrinsic_bindless_image_atomic_max
:
2396 case nir_intrinsic_bindless_image_atomic_min
:
2397 case nir_intrinsic_bindless_image_atomic_or
:
2398 case nir_intrinsic_bindless_image_atomic_xor
:
2399 ty
= getDType(insn
);
2401 info
->io
.globalAccess
|= 0x2;
2403 case nir_intrinsic_bindless_image_load
:
2405 info
->io
.globalAccess
|= 0x1;
2407 case nir_intrinsic_bindless_image_store
:
2410 info
->io
.globalAccess
|= 0x2;
2412 case nir_intrinsic_bindless_image_samples
:
2416 case nir_intrinsic_bindless_image_size
:
2420 unreachable("unhandled image opcode");
2425 if (opInfo
.num_srcs
>= 2)
2426 for (unsigned int i
= 0u; i
< argCount
; ++i
)
2427 srcs
.push_back(getSrc(&insn
->src
[1], i
));
2429 // the sampler is just another src added after coords
2430 if (opInfo
.num_srcs
>= 3 && target
.isMS())
2431 srcs
.push_back(getSrc(&insn
->src
[2], 0));
2433 if (opInfo
.num_srcs
>= 4) {
2434 unsigned components
= opInfo
.src_components
[3] ? opInfo
.src_components
[3] : insn
->num_components
;
2435 for (uint8_t i
= 0u; i
< components
; ++i
)
2436 srcs
.push_back(getSrc(&insn
->src
[3], i
));
2439 if (opInfo
.num_srcs
>= 5)
2440 // 1 for aotmic swap
2441 for (uint8_t i
= 0u; i
< opInfo
.src_components
[4]; ++i
)
2442 srcs
.push_back(getSrc(&insn
->src
[4], i
));
2444 TexInstruction
*texi
= mkTex(getOperation(op
), target
.getEnum(), location
, 0, defs
, srcs
);
2445 texi
->tex
.bindless
= false;
2446 texi
->tex
.format
= &nv50_ir::TexInstruction::formatTable
[convertGLImgFormat(nir_intrinsic_format(insn
))];
2447 texi
->tex
.mask
= mask
;
2448 texi
->tex
.bindless
= true;
2449 texi
->cache
= convert(nir_intrinsic_access(insn
));
2451 texi
->subOp
= getSubOp(op
);
2454 texi
->setIndirectR(indirect
);
2458 case nir_intrinsic_image_deref_atomic_add
:
2459 case nir_intrinsic_image_deref_atomic_and
:
2460 case nir_intrinsic_image_deref_atomic_comp_swap
:
2461 case nir_intrinsic_image_deref_atomic_exchange
:
2462 case nir_intrinsic_image_deref_atomic_max
:
2463 case nir_intrinsic_image_deref_atomic_min
:
2464 case nir_intrinsic_image_deref_atomic_or
:
2465 case nir_intrinsic_image_deref_atomic_xor
:
2466 case nir_intrinsic_image_deref_load
:
2467 case nir_intrinsic_image_deref_samples
:
2468 case nir_intrinsic_image_deref_size
:
2469 case nir_intrinsic_image_deref_store
: {
2470 const nir_variable
*tex
;
2471 std::vector
<Value
*> srcs
, defs
;
2476 nir_deref_instr
*deref
= nir_src_as_deref(insn
->src
[0]);
2477 const glsl_type
*type
= deref
->type
;
2478 TexInstruction::Target target
=
2479 convert((glsl_sampler_dim
)type
->sampler_dimensionality
,
2480 type
->sampler_array
, type
->sampler_shadow
);
2481 unsigned int argCount
= getNIRArgCount(target
);
2482 uint16_t location
= handleDeref(deref
, indirect
, tex
);
2484 if (opInfo
.has_dest
) {
2485 LValues
&newDefs
= convert(&insn
->dest
);
2486 for (uint8_t i
= 0u; i
< newDefs
.size(); ++i
) {
2487 defs
.push_back(newDefs
[i
]);
2493 case nir_intrinsic_image_deref_atomic_add
:
2494 case nir_intrinsic_image_deref_atomic_and
:
2495 case nir_intrinsic_image_deref_atomic_comp_swap
:
2496 case nir_intrinsic_image_deref_atomic_exchange
:
2497 case nir_intrinsic_image_deref_atomic_max
:
2498 case nir_intrinsic_image_deref_atomic_min
:
2499 case nir_intrinsic_image_deref_atomic_or
:
2500 case nir_intrinsic_image_deref_atomic_xor
:
2501 ty
= getDType(insn
);
2503 info
->io
.globalAccess
|= 0x2;
2505 case nir_intrinsic_image_deref_load
:
2507 info
->io
.globalAccess
|= 0x1;
2509 case nir_intrinsic_image_deref_store
:
2512 info
->io
.globalAccess
|= 0x2;
2514 case nir_intrinsic_image_deref_samples
:
2518 case nir_intrinsic_image_deref_size
:
2522 unreachable("unhandled image opcode");
2527 if (opInfo
.num_srcs
>= 2)
2528 for (unsigned int i
= 0u; i
< argCount
; ++i
)
2529 srcs
.push_back(getSrc(&insn
->src
[1], i
));
2531 // the sampler is just another src added after coords
2532 if (opInfo
.num_srcs
>= 3 && target
.isMS())
2533 srcs
.push_back(getSrc(&insn
->src
[2], 0));
2535 if (opInfo
.num_srcs
>= 4) {
2536 unsigned components
= opInfo
.src_components
[3] ? opInfo
.src_components
[3] : insn
->num_components
;
2537 for (uint8_t i
= 0u; i
< components
; ++i
)
2538 srcs
.push_back(getSrc(&insn
->src
[3], i
));
2541 if (opInfo
.num_srcs
>= 5)
2542 // 1 for aotmic swap
2543 for (uint8_t i
= 0u; i
< opInfo
.src_components
[4]; ++i
)
2544 srcs
.push_back(getSrc(&insn
->src
[4], i
));
2546 TexInstruction
*texi
= mkTex(getOperation(op
), target
.getEnum(), location
, 0, defs
, srcs
);
2547 texi
->tex
.bindless
= false;
2548 texi
->tex
.format
= &nv50_ir::TexInstruction::formatTable
[convertGLImgFormat(tex
->data
.image
.format
)];
2549 texi
->tex
.mask
= mask
;
2550 texi
->cache
= getCacheModeFromVar(tex
);
2552 texi
->subOp
= getSubOp(op
);
2555 texi
->setIndirectR(indirect
);
2559 case nir_intrinsic_store_shared
: {
2560 DataType sType
= getSType(insn
->src
[0], false, false);
2561 Value
*indirectOffset
;
2562 uint32_t offset
= getIndirect(&insn
->src
[1], 0, indirectOffset
);
2564 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
2565 if (!((1u << i
) & nir_intrinsic_write_mask(insn
)))
2567 Symbol
*sym
= mkSymbol(FILE_MEMORY_SHARED
, 0, sType
, offset
+ i
* typeSizeof(sType
));
2568 mkStore(OP_STORE
, sType
, sym
, indirectOffset
, getSrc(&insn
->src
[0], i
));
2572 case nir_intrinsic_load_shared
: {
2573 const DataType dType
= getDType(insn
);
2574 LValues
&newDefs
= convert(&insn
->dest
);
2575 Value
*indirectOffset
;
2576 uint32_t offset
= getIndirect(&insn
->src
[0], 0, indirectOffset
);
2578 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
)
2579 loadFrom(FILE_MEMORY_SHARED
, 0, dType
, newDefs
[i
], offset
, i
, indirectOffset
);
2583 case nir_intrinsic_barrier
: {
2584 // TODO: add flag to shader_info
2585 info
->numBarriers
= 1;
2586 Instruction
*bar
= mkOp2(OP_BAR
, TYPE_U32
, NULL
, mkImm(0), mkImm(0));
2588 bar
->subOp
= NV50_IR_SUBOP_BAR_SYNC
;
2591 case nir_intrinsic_group_memory_barrier
:
2592 case nir_intrinsic_memory_barrier
:
2593 case nir_intrinsic_memory_barrier_atomic_counter
:
2594 case nir_intrinsic_memory_barrier_buffer
:
2595 case nir_intrinsic_memory_barrier_image
:
2596 case nir_intrinsic_memory_barrier_shared
: {
2597 Instruction
*bar
= mkOp(OP_MEMBAR
, TYPE_NONE
, NULL
);
2599 bar
->subOp
= getSubOp(op
);
2602 case nir_intrinsic_shader_clock
: {
2603 const DataType dType
= getDType(insn
);
2604 LValues
&newDefs
= convert(&insn
->dest
);
2606 loadImm(newDefs
[0], 0u);
2607 mkOp1(OP_RDSV
, dType
, newDefs
[1], mkSysVal(SV_CLOCK
, 0))->fixed
= 1;
2611 ERROR("unknown nir_intrinsic_op %s\n", nir_intrinsic_infos
[op
].name
);
2619 Converter::visit(nir_jump_instr
*insn
)
2621 switch (insn
->type
) {
2622 case nir_jump_return
:
2623 // TODO: this only works in the main function
2624 mkFlow(OP_BRA
, exit
, CC_ALWAYS
, NULL
);
2625 bb
->cfg
.attach(&exit
->cfg
, Graph::Edge::CROSS
);
2627 case nir_jump_break
:
2628 case nir_jump_continue
: {
2629 bool isBreak
= insn
->type
== nir_jump_break
;
2630 nir_block
*block
= insn
->instr
.block
;
2631 assert(!block
->successors
[1]);
2632 BasicBlock
*target
= convert(block
->successors
[0]);
2633 mkFlow(isBreak
? OP_BREAK
: OP_CONT
, target
, CC_ALWAYS
, NULL
);
2634 bb
->cfg
.attach(&target
->cfg
, isBreak
? Graph::Edge::CROSS
: Graph::Edge::BACK
);
2638 ERROR("unknown nir_jump_type %u\n", insn
->type
);
2646 Converter::convert(nir_load_const_instr
*insn
, uint8_t idx
)
2651 setPosition(immInsertPos
, true);
2653 setPosition(bb
, false);
2655 switch (insn
->def
.bit_size
) {
2657 val
= loadImm(getSSA(8), insn
->value
[idx
].u64
);
2660 val
= loadImm(getSSA(4), insn
->value
[idx
].u32
);
2663 val
= loadImm(getSSA(2), insn
->value
[idx
].u16
);
2666 val
= loadImm(getSSA(1), insn
->value
[idx
].u8
);
2669 unreachable("unhandled bit size!\n");
2671 setPosition(bb
, true);
2676 Converter::visit(nir_load_const_instr
*insn
)
2678 assert(insn
->def
.bit_size
<= 64);
2679 immediates
[insn
->def
.index
] = insn
;
2683 #define DEFAULT_CHECKS \
2684 if (insn->dest.dest.ssa.num_components > 1) { \
2685 ERROR("nir_alu_instr only supported with 1 component!\n"); \
2688 if (insn->dest.write_mask != 1) { \
2689 ERROR("nir_alu_instr only with write_mask of 1 supported!\n"); \
2693 Converter::visit(nir_alu_instr
*insn
)
2695 const nir_op op
= insn
->op
;
2696 const nir_op_info
&info
= nir_op_infos
[op
];
2697 DataType dType
= getDType(insn
);
2698 const std::vector
<DataType
> sTypes
= getSTypes(insn
);
2700 Instruction
*oldPos
= this->bb
->getExit();
2711 case nir_op_fddx_coarse
:
2712 case nir_op_fddx_fine
:
2714 case nir_op_fddy_coarse
:
2715 case nir_op_fddy_fine
:
2734 case nir_op_imul_high
:
2735 case nir_op_umul_high
:
2740 case nir_op_pack_64_2x32_split
:
2757 LValues
&newDefs
= convert(&insn
->dest
);
2758 operation preOp
= preOperationNeeded(op
);
2759 if (preOp
!= OP_NOP
) {
2760 assert(info
.num_inputs
< 2);
2761 Value
*tmp
= getSSA(typeSizeof(dType
));
2762 Instruction
*i0
= mkOp(preOp
, dType
, tmp
);
2763 Instruction
*i1
= mkOp(getOperation(op
), dType
, newDefs
[0]);
2764 if (info
.num_inputs
) {
2765 i0
->setSrc(0, getSrc(&insn
->src
[0]));
2768 i1
->subOp
= getSubOp(op
);
2770 Instruction
*i
= mkOp(getOperation(op
), dType
, newDefs
[0]);
2771 for (unsigned s
= 0u; s
< info
.num_inputs
; ++s
) {
2772 i
->setSrc(s
, getSrc(&insn
->src
[s
]));
2774 i
->subOp
= getSubOp(op
);
2778 case nir_op_ifind_msb
:
2779 case nir_op_ufind_msb
: {
2781 LValues
&newDefs
= convert(&insn
->dest
);
2783 mkOp1(getOperation(op
), dType
, newDefs
[0], getSrc(&insn
->src
[0]));
2786 case nir_op_fround_even
: {
2788 LValues
&newDefs
= convert(&insn
->dest
);
2789 mkCvt(OP_CVT
, dType
, newDefs
[0], dType
, getSrc(&insn
->src
[0]))->rnd
= ROUND_NI
;
2792 // convert instructions
2806 case nir_op_u2u64
: {
2808 LValues
&newDefs
= convert(&insn
->dest
);
2809 Instruction
*i
= mkOp1(getOperation(op
), dType
, newDefs
[0], getSrc(&insn
->src
[0]));
2810 if (op
== nir_op_f2i32
|| op
== nir_op_f2i64
|| op
== nir_op_f2u32
|| op
== nir_op_f2u64
)
2812 i
->sType
= sTypes
[0];
2815 // compare instructions
2825 case nir_op_ine32
: {
2827 LValues
&newDefs
= convert(&insn
->dest
);
2828 Instruction
*i
= mkCmp(getOperation(op
),
2833 getSrc(&insn
->src
[0]),
2834 getSrc(&insn
->src
[1]));
2835 if (info
.num_inputs
== 3)
2836 i
->setSrc(2, getSrc(&insn
->src
[2]));
2837 i
->sType
= sTypes
[0];
2840 // those are weird ALU ops and need special handling, because
2841 // 1. they are always componend based
2842 // 2. they basically just merge multiple values into one data type
2844 if (!insn
->dest
.dest
.is_ssa
&& insn
->dest
.dest
.reg
.reg
->num_array_elems
) {
2845 nir_reg_dest
& reg
= insn
->dest
.dest
.reg
;
2846 uint32_t goffset
= regToLmemOffset
[reg
.reg
->index
];
2847 uint8_t comps
= reg
.reg
->num_components
;
2848 uint8_t size
= reg
.reg
->bit_size
/ 8;
2849 uint8_t csize
= 4 * size
; // TODO after fixing MemoryOpts: comps * size;
2850 uint32_t aoffset
= csize
* reg
.base_offset
;
2851 Value
*indirect
= NULL
;
2854 indirect
= mkOp2v(OP_MUL
, TYPE_U32
, getSSA(4, FILE_ADDRESS
),
2855 getSrc(reg
.indirect
, 0), mkImm(csize
));
2857 for (uint8_t i
= 0u; i
< comps
; ++i
) {
2858 if (!((1u << i
) & insn
->dest
.write_mask
))
2861 Symbol
*sym
= mkSymbol(FILE_MEMORY_LOCAL
, 0, dType
, goffset
+ aoffset
+ i
* size
);
2862 mkStore(OP_STORE
, dType
, sym
, indirect
, getSrc(&insn
->src
[0], i
));
2865 } else if (!insn
->src
[0].src
.is_ssa
&& insn
->src
[0].src
.reg
.reg
->num_array_elems
) {
2866 LValues
&newDefs
= convert(&insn
->dest
);
2867 nir_reg_src
& reg
= insn
->src
[0].src
.reg
;
2868 uint32_t goffset
= regToLmemOffset
[reg
.reg
->index
];
2869 // uint8_t comps = reg.reg->num_components;
2870 uint8_t size
= reg
.reg
->bit_size
/ 8;
2871 uint8_t csize
= 4 * size
; // TODO after fixing MemoryOpts: comps * size;
2872 uint32_t aoffset
= csize
* reg
.base_offset
;
2873 Value
*indirect
= NULL
;
2876 indirect
= mkOp2v(OP_MUL
, TYPE_U32
, getSSA(4, FILE_ADDRESS
), getSrc(reg
.indirect
, 0), mkImm(csize
));
2878 for (uint8_t i
= 0u; i
< newDefs
.size(); ++i
)
2879 loadFrom(FILE_MEMORY_LOCAL
, 0, dType
, newDefs
[i
], goffset
+ aoffset
, i
, indirect
);
2883 LValues
&newDefs
= convert(&insn
->dest
);
2884 for (LValues::size_type c
= 0u; c
< newDefs
.size(); ++c
) {
2885 mkMov(newDefs
[c
], getSrc(&insn
->src
[0], c
), dType
);
2892 LValues
&newDefs
= convert(&insn
->dest
);
2893 for (LValues::size_type c
= 0u; c
< newDefs
.size(); ++c
) {
2894 mkMov(newDefs
[c
], getSrc(&insn
->src
[c
]), dType
);
2899 case nir_op_pack_64_2x32
: {
2900 LValues
&newDefs
= convert(&insn
->dest
);
2901 Instruction
*merge
= mkOp(OP_MERGE
, dType
, newDefs
[0]);
2902 merge
->setSrc(0, getSrc(&insn
->src
[0], 0));
2903 merge
->setSrc(1, getSrc(&insn
->src
[0], 1));
2906 case nir_op_pack_half_2x16_split
: {
2907 LValues
&newDefs
= convert(&insn
->dest
);
2908 Value
*tmpH
= getSSA();
2909 Value
*tmpL
= getSSA();
2911 mkCvt(OP_CVT
, TYPE_F16
, tmpL
, TYPE_F32
, getSrc(&insn
->src
[0]));
2912 mkCvt(OP_CVT
, TYPE_F16
, tmpH
, TYPE_F32
, getSrc(&insn
->src
[1]));
2913 mkOp3(OP_INSBF
, TYPE_U32
, newDefs
[0], tmpH
, mkImm(0x1010), tmpL
);
2916 case nir_op_unpack_half_2x16_split_x
:
2917 case nir_op_unpack_half_2x16_split_y
: {
2918 LValues
&newDefs
= convert(&insn
->dest
);
2919 Instruction
*cvt
= mkCvt(OP_CVT
, TYPE_F32
, newDefs
[0], TYPE_F16
, getSrc(&insn
->src
[0]));
2920 if (op
== nir_op_unpack_half_2x16_split_y
)
2924 case nir_op_unpack_64_2x32
: {
2925 LValues
&newDefs
= convert(&insn
->dest
);
2926 mkOp1(OP_SPLIT
, dType
, newDefs
[0], getSrc(&insn
->src
[0]))->setDef(1, newDefs
[1]);
2929 case nir_op_unpack_64_2x32_split_x
: {
2930 LValues
&newDefs
= convert(&insn
->dest
);
2931 mkOp1(OP_SPLIT
, dType
, newDefs
[0], getSrc(&insn
->src
[0]))->setDef(1, getSSA());
2934 case nir_op_unpack_64_2x32_split_y
: {
2935 LValues
&newDefs
= convert(&insn
->dest
);
2936 mkOp1(OP_SPLIT
, dType
, getSSA(), getSrc(&insn
->src
[0]))->setDef(1, newDefs
[0]);
2939 // special instructions
2941 case nir_op_isign
: {
2944 if (::isFloatType(dType
))
2949 LValues
&newDefs
= convert(&insn
->dest
);
2950 LValue
*val0
= getScratch();
2951 LValue
*val1
= getScratch();
2952 mkCmp(OP_SET
, CC_GT
, iType
, val0
, dType
, getSrc(&insn
->src
[0]), zero
);
2953 mkCmp(OP_SET
, CC_LT
, iType
, val1
, dType
, getSrc(&insn
->src
[0]), zero
);
2955 if (dType
== TYPE_F64
) {
2956 mkOp2(OP_SUB
, iType
, val0
, val0
, val1
);
2957 mkCvt(OP_CVT
, TYPE_F64
, newDefs
[0], iType
, val0
);
2958 } else if (dType
== TYPE_S64
|| dType
== TYPE_U64
) {
2959 mkOp2(OP_SUB
, iType
, val0
, val1
, val0
);
2960 mkOp2(OP_SHR
, iType
, val1
, val0
, loadImm(NULL
, 31));
2961 mkOp2(OP_MERGE
, dType
, newDefs
[0], val0
, val1
);
2962 } else if (::isFloatType(dType
))
2963 mkOp2(OP_SUB
, iType
, newDefs
[0], val0
, val1
);
2965 mkOp2(OP_SUB
, iType
, newDefs
[0], val1
, val0
);
2969 case nir_op_b32csel
: {
2971 LValues
&newDefs
= convert(&insn
->dest
);
2972 mkCmp(OP_SLCT
, CC_NE
, dType
, newDefs
[0], sTypes
[0], getSrc(&insn
->src
[1]), getSrc(&insn
->src
[2]), getSrc(&insn
->src
[0]));
2975 case nir_op_ibitfield_extract
:
2976 case nir_op_ubitfield_extract
: {
2978 Value
*tmp
= getSSA();
2979 LValues
&newDefs
= convert(&insn
->dest
);
2980 mkOp3(OP_INSBF
, dType
, tmp
, getSrc(&insn
->src
[2]), loadImm(NULL
, 0x808), getSrc(&insn
->src
[1]));
2981 mkOp2(OP_EXTBF
, dType
, newDefs
[0], getSrc(&insn
->src
[0]), tmp
);
2986 LValues
&newDefs
= convert(&insn
->dest
);
2987 mkOp3(OP_INSBF
, dType
, newDefs
[0], getSrc(&insn
->src
[0]), loadImm(NULL
, 0x808), getSrc(&insn
->src
[1]));
2990 case nir_op_bitfield_insert
: {
2992 LValues
&newDefs
= convert(&insn
->dest
);
2993 LValue
*temp
= getSSA();
2994 mkOp3(OP_INSBF
, TYPE_U32
, temp
, getSrc(&insn
->src
[3]), mkImm(0x808), getSrc(&insn
->src
[2]));
2995 mkOp3(OP_INSBF
, dType
, newDefs
[0], getSrc(&insn
->src
[1]), temp
, getSrc(&insn
->src
[0]));
2998 case nir_op_bit_count
: {
3000 LValues
&newDefs
= convert(&insn
->dest
);
3001 mkOp2(OP_POPCNT
, dType
, newDefs
[0], getSrc(&insn
->src
[0]), getSrc(&insn
->src
[0]));
3004 case nir_op_bitfield_reverse
: {
3006 LValues
&newDefs
= convert(&insn
->dest
);
3007 mkOp2(OP_EXTBF
, TYPE_U32
, newDefs
[0], getSrc(&insn
->src
[0]), mkImm(0x2000))->subOp
= NV50_IR_SUBOP_EXTBF_REV
;
3010 case nir_op_find_lsb
: {
3012 LValues
&newDefs
= convert(&insn
->dest
);
3013 Value
*tmp
= getSSA();
3014 mkOp2(OP_EXTBF
, TYPE_U32
, tmp
, getSrc(&insn
->src
[0]), mkImm(0x2000))->subOp
= NV50_IR_SUBOP_EXTBF_REV
;
3015 mkOp1(OP_BFIND
, TYPE_U32
, newDefs
[0], tmp
)->subOp
= NV50_IR_SUBOP_BFIND_SAMT
;
3018 // boolean conversions
3019 case nir_op_b2f32
: {
3021 LValues
&newDefs
= convert(&insn
->dest
);
3022 mkOp2(OP_AND
, TYPE_U32
, newDefs
[0], getSrc(&insn
->src
[0]), loadImm(NULL
, 1.0f
));
3025 case nir_op_b2f64
: {
3027 LValues
&newDefs
= convert(&insn
->dest
);
3028 Value
*tmp
= getSSA(4);
3029 mkOp2(OP_AND
, TYPE_U32
, tmp
, getSrc(&insn
->src
[0]), loadImm(NULL
, 0x3ff00000));
3030 mkOp2(OP_MERGE
, TYPE_U64
, newDefs
[0], loadImm(NULL
, 0), tmp
);
3034 case nir_op_i2b32
: {
3036 LValues
&newDefs
= convert(&insn
->dest
);
3038 if (typeSizeof(sTypes
[0]) == 8) {
3039 src1
= loadImm(getSSA(8), 0.0);
3043 CondCode cc
= op
== nir_op_f2b32
? CC_NEU
: CC_NE
;
3044 mkCmp(OP_SET
, cc
, TYPE_U32
, newDefs
[0], sTypes
[0], getSrc(&insn
->src
[0]), src1
);
3047 case nir_op_b2i32
: {
3049 LValues
&newDefs
= convert(&insn
->dest
);
3050 mkOp2(OP_AND
, TYPE_U32
, newDefs
[0], getSrc(&insn
->src
[0]), loadImm(NULL
, 1));
3053 case nir_op_b2i64
: {
3055 LValues
&newDefs
= convert(&insn
->dest
);
3056 LValue
*def
= getScratch();
3057 mkOp2(OP_AND
, TYPE_U32
, def
, getSrc(&insn
->src
[0]), loadImm(NULL
, 1));
3058 mkOp2(OP_MERGE
, TYPE_S64
, newDefs
[0], def
, loadImm(NULL
, 0));
3062 ERROR("unknown nir_op %s\n", info
.name
);
3067 oldPos
= this->bb
->getEntry();
3068 oldPos
->precise
= insn
->exact
;
3071 if (unlikely(!oldPos
))
3074 while (oldPos
->next
) {
3075 oldPos
= oldPos
->next
;
3076 oldPos
->precise
= insn
->exact
;
3078 oldPos
->saturate
= insn
->dest
.saturate
;
3082 #undef DEFAULT_CHECKS
3085 Converter::visit(nir_ssa_undef_instr
*insn
)
3087 LValues
&newDefs
= convert(&insn
->def
);
3088 for (uint8_t i
= 0u; i
< insn
->def
.num_components
; ++i
) {
3089 mkOp(OP_NOP
, TYPE_NONE
, newDefs
[i
]);
3094 #define CASE_SAMPLER(ty) \
3095 case GLSL_SAMPLER_DIM_ ## ty : \
3096 if (isArray && !isShadow) \
3097 return TEX_TARGET_ ## ty ## _ARRAY; \
3098 else if (!isArray && isShadow) \
3099 return TEX_TARGET_## ty ## _SHADOW; \
3100 else if (isArray && isShadow) \
3101 return TEX_TARGET_## ty ## _ARRAY_SHADOW; \
3103 return TEX_TARGET_ ## ty
3106 Converter::convert(glsl_sampler_dim dim
, bool isArray
, bool isShadow
)
3112 case GLSL_SAMPLER_DIM_3D
:
3113 return TEX_TARGET_3D
;
3114 case GLSL_SAMPLER_DIM_MS
:
3116 return TEX_TARGET_2D_MS_ARRAY
;
3117 return TEX_TARGET_2D_MS
;
3118 case GLSL_SAMPLER_DIM_RECT
:
3120 return TEX_TARGET_RECT_SHADOW
;
3121 return TEX_TARGET_RECT
;
3122 case GLSL_SAMPLER_DIM_BUF
:
3123 return TEX_TARGET_BUFFER
;
3124 case GLSL_SAMPLER_DIM_EXTERNAL
:
3125 return TEX_TARGET_2D
;
3127 ERROR("unknown glsl_sampler_dim %u\n", dim
);
3129 return TEX_TARGET_COUNT
;
3135 Converter::applyProjection(Value
*src
, Value
*proj
)
3139 return mkOp2v(OP_MUL
, TYPE_F32
, getScratch(), src
, proj
);
3143 Converter::getNIRArgCount(TexInstruction::Target
& target
)
3145 unsigned int result
= target
.getArgCount();
3146 if (target
.isCube() && target
.isArray())
3154 Converter::handleDeref(nir_deref_instr
*deref
, Value
* &indirect
, const nir_variable
* &tex
)
3156 typedef std::pair
<uint32_t,Value
*> DerefPair
;
3157 std::list
<DerefPair
> derefs
;
3159 uint16_t result
= 0;
3160 while (deref
->deref_type
!= nir_deref_type_var
) {
3161 switch (deref
->deref_type
) {
3162 case nir_deref_type_array
: {
3164 uint8_t size
= type_size(deref
->type
, true);
3165 result
+= size
* getIndirect(&deref
->arr
.index
, 0, indirect
);
3168 derefs
.push_front(std::make_pair(size
, indirect
));
3173 case nir_deref_type_struct
: {
3174 result
+= nir_deref_instr_parent(deref
)->type
->struct_location_offset(deref
->strct
.index
);
3177 case nir_deref_type_var
:
3179 unreachable("nir_deref_type_var reached in handleDeref!");
3182 deref
= nir_deref_instr_parent(deref
);
3186 for (std::list
<DerefPair
>::const_iterator it
= derefs
.begin(); it
!= derefs
.end(); ++it
) {
3187 Value
*offset
= mkOp2v(OP_MUL
, TYPE_U32
, getSSA(), loadImm(getSSA(), it
->first
), it
->second
);
3189 indirect
= mkOp2v(OP_ADD
, TYPE_U32
, getSSA(), indirect
, offset
);
3194 tex
= nir_deref_instr_get_variable(deref
);
3197 return result
+ tex
->data
.driver_location
;
3201 Converter::convert(enum gl_access_qualifier access
)
3204 case ACCESS_VOLATILE
:
3206 case ACCESS_COHERENT
:
3214 Converter::getCacheModeFromVar(const nir_variable
*var
)
3216 return convert(var
->data
.image
.access
);
3220 Converter::visit(nir_tex_instr
*insn
)
3224 case nir_texop_query_levels
:
3226 case nir_texop_texture_samples
:
3231 case nir_texop_txf_ms
:
3233 case nir_texop_txs
: {
3234 LValues
&newDefs
= convert(&insn
->dest
);
3235 std::vector
<Value
*> srcs
;
3236 std::vector
<Value
*> defs
;
3237 std::vector
<nir_src
*> offsets
;
3241 TexInstruction::Target target
= convert(insn
->sampler_dim
, insn
->is_array
, insn
->is_shadow
);
3242 operation op
= getOperation(insn
->op
);
3245 int biasIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_bias
);
3246 int compIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_comparator
);
3247 int coordsIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_coord
);
3248 int ddxIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_ddx
);
3249 int ddyIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_ddy
);
3250 int msIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_ms_index
);
3251 int lodIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_lod
);
3252 int offsetIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_offset
);
3253 int projIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_projector
);
3254 int sampOffIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_sampler_offset
);
3255 int texOffIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_texture_offset
);
3256 int sampHandleIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_sampler_handle
);
3257 int texHandleIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_texture_handle
);
3259 bool bindless
= sampHandleIdx
!= -1 || texHandleIdx
!= -1;
3260 assert((sampHandleIdx
!= -1) == (texHandleIdx
!= -1));
3263 proj
= mkOp1v(OP_RCP
, TYPE_F32
, getScratch(), getSrc(&insn
->src
[projIdx
].src
, 0));
3265 srcs
.resize(insn
->coord_components
);
3266 for (uint8_t i
= 0u; i
< insn
->coord_components
; ++i
)
3267 srcs
[i
] = applyProjection(getSrc(&insn
->src
[coordsIdx
].src
, i
), proj
);
3269 // sometimes we get less args than target.getArgCount, but codegen expects the latter
3270 if (insn
->coord_components
) {
3271 uint32_t argCount
= target
.getArgCount();
3276 for (uint32_t i
= 0u; i
< (argCount
- insn
->coord_components
); ++i
)
3277 srcs
.push_back(getSSA());
3280 if (insn
->op
== nir_texop_texture_samples
)
3281 srcs
.push_back(zero
);
3282 else if (!insn
->num_srcs
)
3283 srcs
.push_back(loadImm(NULL
, 0));
3285 srcs
.push_back(getSrc(&insn
->src
[biasIdx
].src
, 0));
3287 srcs
.push_back(getSrc(&insn
->src
[lodIdx
].src
, 0));
3288 else if (op
== OP_TXF
)
3291 srcs
.push_back(getSrc(&insn
->src
[msIdx
].src
, 0));
3292 if (offsetIdx
!= -1)
3293 offsets
.push_back(&insn
->src
[offsetIdx
].src
);
3295 srcs
.push_back(applyProjection(getSrc(&insn
->src
[compIdx
].src
, 0), proj
));
3296 if (texOffIdx
!= -1) {
3297 srcs
.push_back(getSrc(&insn
->src
[texOffIdx
].src
, 0));
3298 texOffIdx
= srcs
.size() - 1;
3300 if (sampOffIdx
!= -1) {
3301 srcs
.push_back(getSrc(&insn
->src
[sampOffIdx
].src
, 0));
3302 sampOffIdx
= srcs
.size() - 1;
3305 // currently we use the lower bits
3307 Value
*handle
= getSrc(&insn
->src
[sampHandleIdx
].src
, 0);
3309 mkSplit(split
, 4, handle
);
3311 srcs
.push_back(split
[0]);
3312 texOffIdx
= srcs
.size() - 1;
3315 r
= bindless
? 0xff : insn
->texture_index
;
3316 s
= bindless
? 0x1f : insn
->sampler_index
;
3318 defs
.resize(newDefs
.size());
3319 for (uint8_t d
= 0u; d
< newDefs
.size(); ++d
) {
3320 defs
[d
] = newDefs
[d
];
3323 if (target
.isMS() || (op
== OP_TEX
&& prog
->getType() != Program::TYPE_FRAGMENT
))
3326 TexInstruction
*texi
= mkTex(op
, target
.getEnum(), r
, s
, defs
, srcs
);
3327 texi
->tex
.levelZero
= lz
;
3328 texi
->tex
.mask
= mask
;
3329 texi
->tex
.bindless
= bindless
;
3331 if (texOffIdx
!= -1)
3332 texi
->tex
.rIndirectSrc
= texOffIdx
;
3333 if (sampOffIdx
!= -1)
3334 texi
->tex
.sIndirectSrc
= sampOffIdx
;
3338 if (!target
.isShadow())
3339 texi
->tex
.gatherComp
= insn
->component
;
3342 texi
->tex
.query
= TXQ_DIMS
;
3344 case nir_texop_texture_samples
:
3345 texi
->tex
.mask
= 0x4;
3346 texi
->tex
.query
= TXQ_TYPE
;
3348 case nir_texop_query_levels
:
3349 texi
->tex
.mask
= 0x8;
3350 texi
->tex
.query
= TXQ_DIMS
;
3356 texi
->tex
.useOffsets
= offsets
.size();
3357 if (texi
->tex
.useOffsets
) {
3358 for (uint8_t s
= 0; s
< texi
->tex
.useOffsets
; ++s
) {
3359 for (uint32_t c
= 0u; c
< 3; ++c
) {
3360 uint8_t s2
= std::min(c
, target
.getDim() - 1);
3361 texi
->offset
[s
][c
].set(getSrc(offsets
[s
], s2
));
3362 texi
->offset
[s
][c
].setInsn(texi
);
3367 if (op
== OP_TXG
&& offsetIdx
== -1) {
3368 if (nir_tex_instr_has_explicit_tg4_offsets(insn
)) {
3369 texi
->tex
.useOffsets
= 4;
3370 setPosition(texi
, false);
3371 for (uint8_t i
= 0; i
< 4; ++i
) {
3372 for (uint8_t j
= 0; j
< 2; ++j
) {
3373 texi
->offset
[i
][j
].set(loadImm(NULL
, insn
->tg4_offsets
[i
][j
]));
3374 texi
->offset
[i
][j
].setInsn(texi
);
3377 setPosition(texi
, true);
3381 if (ddxIdx
!= -1 && ddyIdx
!= -1) {
3382 for (uint8_t c
= 0u; c
< target
.getDim() + target
.isCube(); ++c
) {
3383 texi
->dPdx
[c
].set(getSrc(&insn
->src
[ddxIdx
].src
, c
));
3384 texi
->dPdy
[c
].set(getSrc(&insn
->src
[ddyIdx
].src
, c
));
3391 ERROR("unknown nir_texop %u\n", insn
->op
);
3398 Converter::visit(nir_deref_instr
*deref
)
3400 // we just ignore those, because images intrinsics are the only place where
3401 // we should end up with deref sources and those have to backtrack anyway
3402 // to get the nir_variable. This code just exists to handle some special
3404 switch (deref
->deref_type
) {
3405 case nir_deref_type_array
:
3406 case nir_deref_type_struct
:
3407 case nir_deref_type_var
:
3410 ERROR("unknown nir_deref_instr %u\n", deref
->deref_type
);
3421 if (prog
->dbgFlags
& NV50_IR_DEBUG_VERBOSE
)
3422 nir_print_shader(nir
, stderr
);
3424 struct nir_lower_subgroups_options subgroup_options
= {
3425 .subgroup_size
= 32,
3426 .ballot_bit_size
= 32,
3429 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, type_size
, (nir_lower_io_options
)0);
3430 NIR_PASS_V(nir
, nir_lower_subgroups
, &subgroup_options
);
3431 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
);
3432 NIR_PASS_V(nir
, nir_lower_load_const_to_scalar
);
3433 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
3434 NIR_PASS_V(nir
, nir_lower_alu_to_scalar
, NULL
);
3435 NIR_PASS_V(nir
, nir_lower_phis_to_scalar
);
3439 NIR_PASS(progress
, nir
, nir_copy_prop
);
3440 NIR_PASS(progress
, nir
, nir_opt_remove_phis
);
3441 NIR_PASS(progress
, nir
, nir_opt_trivial_continues
);
3442 NIR_PASS(progress
, nir
, nir_opt_cse
);
3443 NIR_PASS(progress
, nir
, nir_opt_algebraic
);
3444 NIR_PASS(progress
, nir
, nir_opt_constant_folding
);
3445 NIR_PASS(progress
, nir
, nir_copy_prop
);
3446 NIR_PASS(progress
, nir
, nir_opt_dce
);
3447 NIR_PASS(progress
, nir
, nir_opt_dead_cf
);
3450 NIR_PASS_V(nir
, nir_lower_bool_to_int32
);
3451 NIR_PASS_V(nir
, nir_lower_locals_to_regs
);
3452 NIR_PASS_V(nir
, nir_remove_dead_variables
, nir_var_function_temp
);
3453 NIR_PASS_V(nir
, nir_convert_from_ssa
, true);
3455 // Garbage collect dead instructions
3459 ERROR("Couldn't prase NIR!\n");
3463 if (!assignSlots()) {
3464 ERROR("Couldn't assign slots!\n");
3468 if (prog
->dbgFlags
& NV50_IR_DEBUG_BASIC
)
3469 nir_print_shader(nir
, stderr
);
3471 nir_foreach_function(function
, nir
) {
3472 if (!visit(function
))
3479 } // unnamed namespace
3484 Program::makeFromNIR(struct nv50_ir_prog_info
*info
)
3486 nir_shader
*nir
= (nir_shader
*)info
->bin
.source
;
3487 Converter
converter(this, nir
, info
);
3488 bool result
= converter
.run();
3491 LoweringHelper lowering
;
3493 tlsSize
= info
->bin
.tlsSpace
;
3497 } // namespace nv50_ir