2 * Copyright 2011 Christoph Bumiller
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.
24 #include "tgsi/tgsi_dump.h"
25 #include "tgsi/tgsi_scan.h"
26 #include "tgsi/tgsi_util.h"
32 #include "nv50_ir_util.h"
33 #include "nv50_ir_build_util.h"
39 static nv50_ir::operation
translateOpcode(uint opcode
);
40 static nv50_ir::DataFile
translateFile(uint file
);
41 static nv50_ir::TexTarget
translateTexture(uint texTarg
);
42 static nv50_ir::SVSemantic
translateSysVal(uint sysval
);
47 Instruction(const struct tgsi_full_instruction
*inst
) : insn(inst
) { }
52 SrcRegister(const struct tgsi_full_src_register
*src
)
57 SrcRegister(const struct tgsi_src_register
& src
) : reg(src
), fsr(NULL
) { }
59 SrcRegister(const struct tgsi_ind_register
& ind
)
60 : reg(tgsi_util_get_src_from_ind(&ind
)),
64 struct tgsi_src_register
offsetToSrc(struct tgsi_texture_offset off
)
66 struct tgsi_src_register reg
;
67 memset(®
, 0, sizeof(reg
));
68 reg
.Index
= off
.Index
;
70 reg
.SwizzleX
= off
.SwizzleX
;
71 reg
.SwizzleY
= off
.SwizzleY
;
72 reg
.SwizzleZ
= off
.SwizzleZ
;
76 SrcRegister(const struct tgsi_texture_offset
& off
) :
77 reg(offsetToSrc(off
)),
81 uint
getFile() const { return reg
.File
; }
83 bool is2D() const { return reg
.Dimension
; }
85 bool isIndirect(int dim
) const
87 return (dim
&& fsr
) ? fsr
->Dimension
.Indirect
: reg
.Indirect
;
90 int getIndex(int dim
) const
92 return (dim
&& fsr
) ? fsr
->Dimension
.Index
: reg
.Index
;
95 int getSwizzle(int chan
) const
97 return tgsi_util_get_src_register_swizzle(®
, chan
);
100 nv50_ir::Modifier
getMod(int chan
) const;
102 SrcRegister
getIndirect(int dim
) const
104 assert(fsr
&& isIndirect(dim
));
106 return SrcRegister(fsr
->DimIndirect
);
107 return SrcRegister(fsr
->Indirect
);
110 uint32_t getValueU32(int c
, const struct nv50_ir_prog_info
*info
) const
112 assert(reg
.File
== TGSI_FILE_IMMEDIATE
);
113 assert(!reg
.Absolute
);
115 return info
->immd
.data
[reg
.Index
* 4 + getSwizzle(c
)];
119 const struct tgsi_src_register reg
;
120 const struct tgsi_full_src_register
*fsr
;
126 DstRegister(const struct tgsi_full_dst_register
*dst
)
127 : reg(dst
->Register
),
131 DstRegister(const struct tgsi_dst_register
& dst
) : reg(dst
), fdr(NULL
) { }
133 uint
getFile() const { return reg
.File
; }
135 bool is2D() const { return reg
.Dimension
; }
137 bool isIndirect(int dim
) const
139 return (dim
&& fdr
) ? fdr
->Dimension
.Indirect
: reg
.Indirect
;
142 int getIndex(int dim
) const
144 return (dim
&& fdr
) ? fdr
->Dimension
.Dimension
: reg
.Index
;
147 unsigned int getMask() const { return reg
.WriteMask
; }
149 bool isMasked(int chan
) const { return !(getMask() & (1 << chan
)); }
151 SrcRegister
getIndirect(int dim
) const
153 assert(fdr
&& isIndirect(dim
));
155 return SrcRegister(fdr
->DimIndirect
);
156 return SrcRegister(fdr
->Indirect
);
160 const struct tgsi_dst_register reg
;
161 const struct tgsi_full_dst_register
*fdr
;
164 inline uint
getOpcode() const { return insn
->Instruction
.Opcode
; }
166 unsigned int srcCount() const { return insn
->Instruction
.NumSrcRegs
; }
167 unsigned int dstCount() const { return insn
->Instruction
.NumDstRegs
; }
169 // mask of used components of source s
170 unsigned int srcMask(unsigned int s
) const;
172 SrcRegister
getSrc(unsigned int s
) const
174 assert(s
< srcCount());
175 return SrcRegister(&insn
->Src
[s
]);
178 DstRegister
getDst(unsigned int d
) const
180 assert(d
< dstCount());
181 return DstRegister(&insn
->Dst
[d
]);
184 SrcRegister
getTexOffset(unsigned int i
) const
186 assert(i
< TGSI_FULL_MAX_TEX_OFFSETS
);
187 return SrcRegister(insn
->TexOffsets
[i
]);
190 unsigned int getNumTexOffsets() const { return insn
->Texture
.NumOffsets
; }
192 bool checkDstSrcAliasing() const;
194 inline nv50_ir::operation
getOP() const {
195 return translateOpcode(getOpcode()); }
197 nv50_ir::DataType
inferSrcType() const;
198 nv50_ir::DataType
inferDstType() const;
200 nv50_ir::CondCode
getSetCond() const;
202 nv50_ir::TexInstruction::Target
getTexture(const Source
*, int s
) const;
204 inline uint
getLabel() { return insn
->Label
.Label
; }
206 unsigned getSaturate() const { return insn
->Instruction
.Saturate
; }
210 tgsi_dump_instruction(insn
, 1);
214 const struct tgsi_full_instruction
*insn
;
217 unsigned int Instruction::srcMask(unsigned int s
) const
219 unsigned int mask
= insn
->Dst
[0].Register
.WriteMask
;
221 switch (insn
->Instruction
.Opcode
) {
222 case TGSI_OPCODE_COS
:
223 case TGSI_OPCODE_SIN
:
224 return (mask
& 0x8) | ((mask
& 0x7) ? 0x1 : 0x0);
225 case TGSI_OPCODE_DP2
:
227 case TGSI_OPCODE_DP3
:
229 case TGSI_OPCODE_DP4
:
230 case TGSI_OPCODE_DPH
:
231 case TGSI_OPCODE_KIL
: /* WriteMask ignored */
233 case TGSI_OPCODE_DST
:
234 return mask
& (s
? 0xa : 0x6);
235 case TGSI_OPCODE_EX2
:
236 case TGSI_OPCODE_EXP
:
237 case TGSI_OPCODE_LG2
:
238 case TGSI_OPCODE_LOG
:
239 case TGSI_OPCODE_POW
:
240 case TGSI_OPCODE_RCP
:
241 case TGSI_OPCODE_RSQ
:
242 case TGSI_OPCODE_SCS
:
245 case TGSI_OPCODE_UIF
:
247 case TGSI_OPCODE_LIT
:
249 case TGSI_OPCODE_TEX2
:
250 case TGSI_OPCODE_TXB2
:
251 case TGSI_OPCODE_TXL2
:
252 return (s
== 0) ? 0xf : 0x3;
253 case TGSI_OPCODE_TEX
:
254 case TGSI_OPCODE_TXB
:
255 case TGSI_OPCODE_TXD
:
256 case TGSI_OPCODE_TXL
:
257 case TGSI_OPCODE_TXP
:
259 const struct tgsi_instruction_texture
*tex
= &insn
->Texture
;
261 assert(insn
->Instruction
.Texture
);
264 if (insn
->Instruction
.Opcode
!= TGSI_OPCODE_TEX
&&
265 insn
->Instruction
.Opcode
!= TGSI_OPCODE_TXD
)
266 mask
|= 0x8; /* bias, lod or proj */
268 switch (tex
->Texture
) {
269 case TGSI_TEXTURE_1D
:
272 case TGSI_TEXTURE_SHADOW1D
:
275 case TGSI_TEXTURE_1D_ARRAY
:
276 case TGSI_TEXTURE_2D
:
277 case TGSI_TEXTURE_RECT
:
280 case TGSI_TEXTURE_CUBE_ARRAY
:
281 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
282 case TGSI_TEXTURE_SHADOWCUBE
:
283 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
291 case TGSI_OPCODE_XPD
:
294 if (mask
& 1) x
|= 0x6;
295 if (mask
& 2) x
|= 0x5;
296 if (mask
& 4) x
|= 0x3;
306 nv50_ir::Modifier
Instruction::SrcRegister::getMod(int chan
) const
308 nv50_ir::Modifier
m(0);
311 m
= m
| nv50_ir::Modifier(NV50_IR_MOD_ABS
);
313 m
= m
| nv50_ir::Modifier(NV50_IR_MOD_NEG
);
317 static nv50_ir::DataFile
translateFile(uint file
)
320 case TGSI_FILE_CONSTANT
: return nv50_ir::FILE_MEMORY_CONST
;
321 case TGSI_FILE_INPUT
: return nv50_ir::FILE_SHADER_INPUT
;
322 case TGSI_FILE_OUTPUT
: return nv50_ir::FILE_SHADER_OUTPUT
;
323 case TGSI_FILE_TEMPORARY
: return nv50_ir::FILE_GPR
;
324 case TGSI_FILE_ADDRESS
: return nv50_ir::FILE_ADDRESS
;
325 case TGSI_FILE_PREDICATE
: return nv50_ir::FILE_PREDICATE
;
326 case TGSI_FILE_IMMEDIATE
: return nv50_ir::FILE_IMMEDIATE
;
327 case TGSI_FILE_SYSTEM_VALUE
: return nv50_ir::FILE_SYSTEM_VALUE
;
328 case TGSI_FILE_RESOURCE
: return nv50_ir::FILE_MEMORY_GLOBAL
;
329 case TGSI_FILE_SAMPLER
:
332 return nv50_ir::FILE_NULL
;
336 static nv50_ir::SVSemantic
translateSysVal(uint sysval
)
339 case TGSI_SEMANTIC_FACE
: return nv50_ir::SV_FACE
;
340 case TGSI_SEMANTIC_PSIZE
: return nv50_ir::SV_POINT_SIZE
;
341 case TGSI_SEMANTIC_PRIMID
: return nv50_ir::SV_PRIMITIVE_ID
;
342 case TGSI_SEMANTIC_INSTANCEID
: return nv50_ir::SV_INSTANCE_ID
;
343 case TGSI_SEMANTIC_VERTEXID
: return nv50_ir::SV_VERTEX_ID
;
344 case TGSI_SEMANTIC_GRID_SIZE
: return nv50_ir::SV_NCTAID
;
345 case TGSI_SEMANTIC_BLOCK_ID
: return nv50_ir::SV_CTAID
;
346 case TGSI_SEMANTIC_BLOCK_SIZE
: return nv50_ir::SV_NTID
;
347 case TGSI_SEMANTIC_THREAD_ID
: return nv50_ir::SV_TID
;
350 return nv50_ir::SV_CLOCK
;
354 #define NV50_IR_TEX_TARG_CASE(a, b) \
355 case TGSI_TEXTURE_##a: return nv50_ir::TEX_TARGET_##b;
357 static nv50_ir::TexTarget
translateTexture(uint tex
)
360 NV50_IR_TEX_TARG_CASE(1D
, 1D
);
361 NV50_IR_TEX_TARG_CASE(2D
, 2D
);
362 NV50_IR_TEX_TARG_CASE(2D_MSAA
, 2D_MS
);
363 NV50_IR_TEX_TARG_CASE(3D
, 3D
);
364 NV50_IR_TEX_TARG_CASE(CUBE
, CUBE
);
365 NV50_IR_TEX_TARG_CASE(RECT
, RECT
);
366 NV50_IR_TEX_TARG_CASE(1D_ARRAY
, 1D_ARRAY
);
367 NV50_IR_TEX_TARG_CASE(2D_ARRAY
, 2D_ARRAY
);
368 NV50_IR_TEX_TARG_CASE(2D_ARRAY_MSAA
, 2D_MS_ARRAY
);
369 NV50_IR_TEX_TARG_CASE(CUBE_ARRAY
, CUBE_ARRAY
);
370 NV50_IR_TEX_TARG_CASE(SHADOW1D
, 1D_SHADOW
);
371 NV50_IR_TEX_TARG_CASE(SHADOW2D
, 2D_SHADOW
);
372 NV50_IR_TEX_TARG_CASE(SHADOWCUBE
, CUBE_SHADOW
);
373 NV50_IR_TEX_TARG_CASE(SHADOWRECT
, RECT_SHADOW
);
374 NV50_IR_TEX_TARG_CASE(SHADOW1D_ARRAY
, 1D_ARRAY_SHADOW
);
375 NV50_IR_TEX_TARG_CASE(SHADOW2D_ARRAY
, 2D_ARRAY_SHADOW
);
376 NV50_IR_TEX_TARG_CASE(SHADOWCUBE_ARRAY
, CUBE_ARRAY_SHADOW
);
377 NV50_IR_TEX_TARG_CASE(BUFFER
, BUFFER
);
379 case TGSI_TEXTURE_UNKNOWN
:
381 assert(!"invalid texture target");
382 return nv50_ir::TEX_TARGET_2D
;
386 nv50_ir::DataType
Instruction::inferSrcType() const
388 switch (getOpcode()) {
389 case TGSI_OPCODE_UIF
:
390 case TGSI_OPCODE_AND
:
392 case TGSI_OPCODE_XOR
:
393 case TGSI_OPCODE_NOT
:
394 case TGSI_OPCODE_U2F
:
395 case TGSI_OPCODE_UADD
:
396 case TGSI_OPCODE_UDIV
:
397 case TGSI_OPCODE_UMOD
:
398 case TGSI_OPCODE_UMAD
:
399 case TGSI_OPCODE_UMUL
:
400 case TGSI_OPCODE_UMAX
:
401 case TGSI_OPCODE_UMIN
:
402 case TGSI_OPCODE_USEQ
:
403 case TGSI_OPCODE_USGE
:
404 case TGSI_OPCODE_USLT
:
405 case TGSI_OPCODE_USNE
:
406 case TGSI_OPCODE_USHR
:
407 case TGSI_OPCODE_UCMP
:
408 case TGSI_OPCODE_ATOMUADD
:
409 case TGSI_OPCODE_ATOMXCHG
:
410 case TGSI_OPCODE_ATOMCAS
:
411 case TGSI_OPCODE_ATOMAND
:
412 case TGSI_OPCODE_ATOMOR
:
413 case TGSI_OPCODE_ATOMXOR
:
414 case TGSI_OPCODE_ATOMUMIN
:
415 case TGSI_OPCODE_ATOMUMAX
:
416 return nv50_ir::TYPE_U32
;
417 case TGSI_OPCODE_I2F
:
418 case TGSI_OPCODE_IDIV
:
419 case TGSI_OPCODE_IMAX
:
420 case TGSI_OPCODE_IMIN
:
421 case TGSI_OPCODE_IABS
:
422 case TGSI_OPCODE_INEG
:
423 case TGSI_OPCODE_ISGE
:
424 case TGSI_OPCODE_ISHR
:
425 case TGSI_OPCODE_ISLT
:
426 case TGSI_OPCODE_ISSG
:
427 case TGSI_OPCODE_SAD
: // not sure about SAD, but no one has a float version
428 case TGSI_OPCODE_MOD
:
429 case TGSI_OPCODE_UARL
:
430 case TGSI_OPCODE_ATOMIMIN
:
431 case TGSI_OPCODE_ATOMIMAX
:
432 return nv50_ir::TYPE_S32
;
434 return nv50_ir::TYPE_F32
;
438 nv50_ir::DataType
Instruction::inferDstType() const
440 switch (getOpcode()) {
441 case TGSI_OPCODE_F2U
: return nv50_ir::TYPE_U32
;
442 case TGSI_OPCODE_F2I
: return nv50_ir::TYPE_S32
;
443 case TGSI_OPCODE_I2F
:
444 case TGSI_OPCODE_U2F
:
445 return nv50_ir::TYPE_F32
;
447 return inferSrcType();
451 nv50_ir::CondCode
Instruction::getSetCond() const
453 using namespace nv50_ir
;
455 switch (getOpcode()) {
456 case TGSI_OPCODE_SLT
:
457 case TGSI_OPCODE_ISLT
:
458 case TGSI_OPCODE_USLT
:
460 case TGSI_OPCODE_SLE
:
462 case TGSI_OPCODE_SGE
:
463 case TGSI_OPCODE_ISGE
:
464 case TGSI_OPCODE_USGE
:
466 case TGSI_OPCODE_SGT
:
468 case TGSI_OPCODE_SEQ
:
469 case TGSI_OPCODE_USEQ
:
471 case TGSI_OPCODE_SNE
:
473 case TGSI_OPCODE_USNE
:
475 case TGSI_OPCODE_SFL
:
477 case TGSI_OPCODE_STR
:
483 #define NV50_IR_OPCODE_CASE(a, b) case TGSI_OPCODE_##a: return nv50_ir::OP_##b
485 static nv50_ir::operation
translateOpcode(uint opcode
)
488 NV50_IR_OPCODE_CASE(ARL
, SHL
);
489 NV50_IR_OPCODE_CASE(MOV
, MOV
);
491 NV50_IR_OPCODE_CASE(RCP
, RCP
);
492 NV50_IR_OPCODE_CASE(RSQ
, RSQ
);
494 NV50_IR_OPCODE_CASE(MUL
, MUL
);
495 NV50_IR_OPCODE_CASE(ADD
, ADD
);
497 NV50_IR_OPCODE_CASE(MIN
, MIN
);
498 NV50_IR_OPCODE_CASE(MAX
, MAX
);
499 NV50_IR_OPCODE_CASE(SLT
, SET
);
500 NV50_IR_OPCODE_CASE(SGE
, SET
);
501 NV50_IR_OPCODE_CASE(MAD
, MAD
);
502 NV50_IR_OPCODE_CASE(SUB
, SUB
);
504 NV50_IR_OPCODE_CASE(FLR
, FLOOR
);
505 NV50_IR_OPCODE_CASE(ROUND
, CVT
);
506 NV50_IR_OPCODE_CASE(EX2
, EX2
);
507 NV50_IR_OPCODE_CASE(LG2
, LG2
);
508 NV50_IR_OPCODE_CASE(POW
, POW
);
510 NV50_IR_OPCODE_CASE(ABS
, ABS
);
512 NV50_IR_OPCODE_CASE(COS
, COS
);
513 NV50_IR_OPCODE_CASE(DDX
, DFDX
);
514 NV50_IR_OPCODE_CASE(DDY
, DFDY
);
515 NV50_IR_OPCODE_CASE(KILP
, DISCARD
);
517 NV50_IR_OPCODE_CASE(SEQ
, SET
);
518 NV50_IR_OPCODE_CASE(SFL
, SET
);
519 NV50_IR_OPCODE_CASE(SGT
, SET
);
520 NV50_IR_OPCODE_CASE(SIN
, SIN
);
521 NV50_IR_OPCODE_CASE(SLE
, SET
);
522 NV50_IR_OPCODE_CASE(SNE
, SET
);
523 NV50_IR_OPCODE_CASE(STR
, SET
);
524 NV50_IR_OPCODE_CASE(TEX
, TEX
);
525 NV50_IR_OPCODE_CASE(TXD
, TXD
);
526 NV50_IR_OPCODE_CASE(TXP
, TEX
);
528 NV50_IR_OPCODE_CASE(BRA
, BRA
);
529 NV50_IR_OPCODE_CASE(CAL
, CALL
);
530 NV50_IR_OPCODE_CASE(RET
, RET
);
531 NV50_IR_OPCODE_CASE(CMP
, SLCT
);
533 NV50_IR_OPCODE_CASE(TXB
, TXB
);
535 NV50_IR_OPCODE_CASE(DIV
, DIV
);
537 NV50_IR_OPCODE_CASE(TXL
, TXL
);
539 NV50_IR_OPCODE_CASE(CEIL
, CEIL
);
540 NV50_IR_OPCODE_CASE(I2F
, CVT
);
541 NV50_IR_OPCODE_CASE(NOT
, NOT
);
542 NV50_IR_OPCODE_CASE(TRUNC
, TRUNC
);
543 NV50_IR_OPCODE_CASE(SHL
, SHL
);
545 NV50_IR_OPCODE_CASE(AND
, AND
);
546 NV50_IR_OPCODE_CASE(OR
, OR
);
547 NV50_IR_OPCODE_CASE(MOD
, MOD
);
548 NV50_IR_OPCODE_CASE(XOR
, XOR
);
549 NV50_IR_OPCODE_CASE(SAD
, SAD
);
550 NV50_IR_OPCODE_CASE(TXF
, TXF
);
551 NV50_IR_OPCODE_CASE(TXQ
, TXQ
);
553 NV50_IR_OPCODE_CASE(EMIT
, EMIT
);
554 NV50_IR_OPCODE_CASE(ENDPRIM
, RESTART
);
556 NV50_IR_OPCODE_CASE(KIL
, DISCARD
);
558 NV50_IR_OPCODE_CASE(F2I
, CVT
);
559 NV50_IR_OPCODE_CASE(IDIV
, DIV
);
560 NV50_IR_OPCODE_CASE(IMAX
, MAX
);
561 NV50_IR_OPCODE_CASE(IMIN
, MIN
);
562 NV50_IR_OPCODE_CASE(IABS
, ABS
);
563 NV50_IR_OPCODE_CASE(INEG
, NEG
);
564 NV50_IR_OPCODE_CASE(ISGE
, SET
);
565 NV50_IR_OPCODE_CASE(ISHR
, SHR
);
566 NV50_IR_OPCODE_CASE(ISLT
, SET
);
567 NV50_IR_OPCODE_CASE(F2U
, CVT
);
568 NV50_IR_OPCODE_CASE(U2F
, CVT
);
569 NV50_IR_OPCODE_CASE(UADD
, ADD
);
570 NV50_IR_OPCODE_CASE(UDIV
, DIV
);
571 NV50_IR_OPCODE_CASE(UMAD
, MAD
);
572 NV50_IR_OPCODE_CASE(UMAX
, MAX
);
573 NV50_IR_OPCODE_CASE(UMIN
, MIN
);
574 NV50_IR_OPCODE_CASE(UMOD
, MOD
);
575 NV50_IR_OPCODE_CASE(UMUL
, MUL
);
576 NV50_IR_OPCODE_CASE(USEQ
, SET
);
577 NV50_IR_OPCODE_CASE(USGE
, SET
);
578 NV50_IR_OPCODE_CASE(USHR
, SHR
);
579 NV50_IR_OPCODE_CASE(USLT
, SET
);
580 NV50_IR_OPCODE_CASE(USNE
, SET
);
582 NV50_IR_OPCODE_CASE(SAMPLE
, TEX
);
583 NV50_IR_OPCODE_CASE(SAMPLE_B
, TXB
);
584 NV50_IR_OPCODE_CASE(SAMPLE_C
, TEX
);
585 NV50_IR_OPCODE_CASE(SAMPLE_C_LZ
, TEX
);
586 NV50_IR_OPCODE_CASE(SAMPLE_D
, TXD
);
587 NV50_IR_OPCODE_CASE(SAMPLE_L
, TXL
);
588 NV50_IR_OPCODE_CASE(SAMPLE_I
, TXF
);
589 NV50_IR_OPCODE_CASE(SAMPLE_I_MS
, TXF
);
590 NV50_IR_OPCODE_CASE(GATHER4
, TXG
);
591 NV50_IR_OPCODE_CASE(SVIEWINFO
, TXQ
);
593 NV50_IR_OPCODE_CASE(ATOMUADD
, ATOM
);
594 NV50_IR_OPCODE_CASE(ATOMXCHG
, ATOM
);
595 NV50_IR_OPCODE_CASE(ATOMCAS
, ATOM
);
596 NV50_IR_OPCODE_CASE(ATOMAND
, ATOM
);
597 NV50_IR_OPCODE_CASE(ATOMOR
, ATOM
);
598 NV50_IR_OPCODE_CASE(ATOMXOR
, ATOM
);
599 NV50_IR_OPCODE_CASE(ATOMUMIN
, ATOM
);
600 NV50_IR_OPCODE_CASE(ATOMUMAX
, ATOM
);
601 NV50_IR_OPCODE_CASE(ATOMIMIN
, ATOM
);
602 NV50_IR_OPCODE_CASE(ATOMIMAX
, ATOM
);
604 NV50_IR_OPCODE_CASE(TEX2
, TEX
);
605 NV50_IR_OPCODE_CASE(TXB2
, TXB
);
606 NV50_IR_OPCODE_CASE(TXL2
, TXL
);
608 NV50_IR_OPCODE_CASE(END
, EXIT
);
611 return nv50_ir::OP_NOP
;
615 static uint16_t opcodeToSubOp(uint opcode
)
618 case TGSI_OPCODE_LFENCE
: return NV50_IR_SUBOP_MEMBAR(L
, GL
);
619 case TGSI_OPCODE_SFENCE
: return NV50_IR_SUBOP_MEMBAR(S
, GL
);
620 case TGSI_OPCODE_MFENCE
: return NV50_IR_SUBOP_MEMBAR(M
, GL
);
621 case TGSI_OPCODE_ATOMUADD
: return NV50_IR_SUBOP_ATOM_ADD
;
622 case TGSI_OPCODE_ATOMXCHG
: return NV50_IR_SUBOP_ATOM_EXCH
;
623 case TGSI_OPCODE_ATOMCAS
: return NV50_IR_SUBOP_ATOM_CAS
;
624 case TGSI_OPCODE_ATOMAND
: return NV50_IR_SUBOP_ATOM_AND
;
625 case TGSI_OPCODE_ATOMOR
: return NV50_IR_SUBOP_ATOM_OR
;
626 case TGSI_OPCODE_ATOMXOR
: return NV50_IR_SUBOP_ATOM_XOR
;
627 case TGSI_OPCODE_ATOMUMIN
: return NV50_IR_SUBOP_ATOM_MIN
;
628 case TGSI_OPCODE_ATOMIMIN
: return NV50_IR_SUBOP_ATOM_MIN
;
629 case TGSI_OPCODE_ATOMUMAX
: return NV50_IR_SUBOP_ATOM_MAX
;
630 case TGSI_OPCODE_ATOMIMAX
: return NV50_IR_SUBOP_ATOM_MAX
;
636 bool Instruction::checkDstSrcAliasing() const
638 if (insn
->Dst
[0].Register
.Indirect
) // no danger if indirect, using memory
641 for (int s
= 0; s
< TGSI_FULL_MAX_SRC_REGISTERS
; ++s
) {
642 if (insn
->Src
[s
].Register
.File
== TGSI_FILE_NULL
)
644 if (insn
->Src
[s
].Register
.File
== insn
->Dst
[0].Register
.File
&&
645 insn
->Src
[s
].Register
.Index
== insn
->Dst
[0].Register
.Index
)
654 Source(struct nv50_ir_prog_info
*);
659 unsigned fileSize(unsigned file
) const { return scan
.file_max
[file
] + 1; }
662 struct tgsi_shader_info scan
;
663 struct tgsi_full_instruction
*insns
;
664 const struct tgsi_token
*tokens
;
665 struct nv50_ir_prog_info
*info
;
667 nv50_ir::DynArray tempArrays
;
668 nv50_ir::DynArray immdArrays
;
670 typedef nv50_ir::BuildUtil::Location Location
;
671 // these registers are per-subroutine, cannot be used for parameter passing
672 std::set
<Location
> locals
;
674 bool mainTempsInLMem
;
676 int clipVertexOutput
;
679 uint8_t target
; // TGSI_TEXTURE_*
681 std::vector
<TextureView
> textureViews
;
684 uint8_t target
; // TGSI_TEXTURE_*
686 uint8_t slot
; // $surface index
688 std::vector
<Resource
> resources
;
691 int inferSysValDirection(unsigned sn
) const;
692 bool scanDeclaration(const struct tgsi_full_declaration
*);
693 bool scanInstruction(const struct tgsi_full_instruction
*);
694 void scanProperty(const struct tgsi_full_property
*);
695 void scanImmediate(const struct tgsi_full_immediate
*);
697 inline bool isEdgeFlagPassthrough(const Instruction
&) const;
700 Source::Source(struct nv50_ir_prog_info
*prog
) : info(prog
)
702 tokens
= (const struct tgsi_token
*)info
->bin
.source
;
704 if (prog
->dbgFlags
& NV50_IR_DEBUG_BASIC
)
705 tgsi_dump(tokens
, 0);
707 mainTempsInLMem
= FALSE
;
716 FREE(info
->immd
.data
);
718 FREE(info
->immd
.type
);
721 bool Source::scanSource()
723 unsigned insnCount
= 0;
724 struct tgsi_parse_context parse
;
726 tgsi_scan_shader(tokens
, &scan
);
728 insns
= (struct tgsi_full_instruction
*)MALLOC(scan
.num_instructions
*
733 clipVertexOutput
= -1;
735 textureViews
.resize(scan
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1);
736 resources
.resize(scan
.file_max
[TGSI_FILE_RESOURCE
] + 1);
738 info
->immd
.bufSize
= 0;
740 info
->numInputs
= scan
.file_max
[TGSI_FILE_INPUT
] + 1;
741 info
->numOutputs
= scan
.file_max
[TGSI_FILE_OUTPUT
] + 1;
742 info
->numSysVals
= scan
.file_max
[TGSI_FILE_SYSTEM_VALUE
] + 1;
744 if (info
->type
== PIPE_SHADER_FRAGMENT
) {
745 info
->prop
.fp
.writesDepth
= scan
.writes_z
;
746 info
->prop
.fp
.usesDiscard
= scan
.uses_kill
;
748 if (info
->type
== PIPE_SHADER_GEOMETRY
) {
749 info
->prop
.gp
.instanceCount
= 1; // default value
752 info
->immd
.data
= (uint32_t *)MALLOC(scan
.immediate_count
* 16);
753 info
->immd
.type
= (ubyte
*)MALLOC(scan
.immediate_count
* sizeof(ubyte
));
755 tgsi_parse_init(&parse
, tokens
);
756 while (!tgsi_parse_end_of_tokens(&parse
)) {
757 tgsi_parse_token(&parse
);
759 switch (parse
.FullToken
.Token
.Type
) {
760 case TGSI_TOKEN_TYPE_IMMEDIATE
:
761 scanImmediate(&parse
.FullToken
.FullImmediate
);
763 case TGSI_TOKEN_TYPE_DECLARATION
:
764 scanDeclaration(&parse
.FullToken
.FullDeclaration
);
766 case TGSI_TOKEN_TYPE_INSTRUCTION
:
767 insns
[insnCount
++] = parse
.FullToken
.FullInstruction
;
768 scanInstruction(&parse
.FullToken
.FullInstruction
);
770 case TGSI_TOKEN_TYPE_PROPERTY
:
771 scanProperty(&parse
.FullToken
.FullProperty
);
774 INFO("unknown TGSI token type: %d\n", parse
.FullToken
.Token
.Type
);
778 tgsi_parse_free(&parse
);
781 info
->bin
.tlsSpace
+= (scan
.file_max
[TGSI_FILE_TEMPORARY
] + 1) * 16;
783 if (info
->io
.genUserClip
> 0) {
784 info
->io
.clipDistanceMask
= (1 << info
->io
.genUserClip
) - 1;
786 const unsigned int nOut
= (info
->io
.genUserClip
+ 3) / 4;
788 for (unsigned int n
= 0; n
< nOut
; ++n
) {
789 unsigned int i
= info
->numOutputs
++;
791 info
->out
[i
].sn
= TGSI_SEMANTIC_CLIPDIST
;
793 info
->out
[i
].mask
= info
->io
.clipDistanceMask
>> (n
* 4);
797 return info
->assignSlots(info
) == 0;
800 void Source::scanProperty(const struct tgsi_full_property
*prop
)
802 switch (prop
->Property
.PropertyName
) {
803 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
804 info
->prop
.gp
.outputPrim
= prop
->u
[0].Data
;
806 case TGSI_PROPERTY_GS_INPUT_PRIM
:
807 info
->prop
.gp
.inputPrim
= prop
->u
[0].Data
;
809 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
:
810 info
->prop
.gp
.maxVertices
= prop
->u
[0].Data
;
813 case TGSI_PROPERTY_GS_INSTANCE_COUNT
:
814 info
->prop
.gp
.instanceCount
= prop
->u
[0].Data
;
817 case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
:
818 info
->prop
.fp
.separateFragData
= TRUE
;
820 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
821 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
824 case TGSI_PROPERTY_VS_PROHIBIT_UCPS
:
825 info
->io
.genUserClip
= -1;
828 INFO("unhandled TGSI property %d\n", prop
->Property
.PropertyName
);
833 void Source::scanImmediate(const struct tgsi_full_immediate
*imm
)
835 const unsigned n
= info
->immd
.count
++;
837 assert(n
< scan
.immediate_count
);
839 for (int c
= 0; c
< 4; ++c
)
840 info
->immd
.data
[n
* 4 + c
] = imm
->u
[c
].Uint
;
842 info
->immd
.type
[n
] = imm
->Immediate
.DataType
;
845 int Source::inferSysValDirection(unsigned sn
) const
848 case TGSI_SEMANTIC_INSTANCEID
:
849 case TGSI_SEMANTIC_VERTEXID
:
852 case TGSI_SEMANTIC_LAYER
:
853 case TGSI_SEMANTIC_VIEWPORTINDEX
:
856 case TGSI_SEMANTIC_PRIMID
:
857 return (info
->type
== PIPE_SHADER_FRAGMENT
) ? 1 : 0;
863 bool Source::scanDeclaration(const struct tgsi_full_declaration
*decl
)
866 unsigned sn
= TGSI_SEMANTIC_GENERIC
;
868 const unsigned first
= decl
->Range
.First
, last
= decl
->Range
.Last
;
870 if (decl
->Declaration
.Semantic
) {
871 sn
= decl
->Semantic
.Name
;
872 si
= decl
->Semantic
.Index
;
875 if (decl
->Declaration
.Local
) {
876 for (i
= first
; i
<= last
; ++i
) {
877 for (c
= 0; c
< 4; ++c
) {
879 Location(decl
->Declaration
.File
, decl
->Dim
.Index2D
, i
, c
));
884 switch (decl
->Declaration
.File
) {
885 case TGSI_FILE_INPUT
:
886 if (info
->type
== PIPE_SHADER_VERTEX
) {
887 // all vertex attributes are equal
888 for (i
= first
; i
<= last
; ++i
) {
889 info
->in
[i
].sn
= TGSI_SEMANTIC_GENERIC
;
893 for (i
= first
; i
<= last
; ++i
, ++si
) {
897 if (info
->type
== PIPE_SHADER_FRAGMENT
) {
898 // translate interpolation mode
899 switch (decl
->Interp
.Interpolate
) {
900 case TGSI_INTERPOLATE_CONSTANT
:
901 info
->in
[i
].flat
= 1;
903 case TGSI_INTERPOLATE_COLOR
:
906 case TGSI_INTERPOLATE_LINEAR
:
907 info
->in
[i
].linear
= 1;
912 if (decl
->Interp
.Centroid
)
913 info
->in
[i
].centroid
= 1;
918 case TGSI_FILE_OUTPUT
:
919 for (i
= first
; i
<= last
; ++i
, ++si
) {
921 case TGSI_SEMANTIC_POSITION
:
922 if (info
->type
== PIPE_SHADER_FRAGMENT
)
923 info
->io
.fragDepth
= i
;
925 if (clipVertexOutput
< 0)
926 clipVertexOutput
= i
;
928 case TGSI_SEMANTIC_COLOR
:
929 if (info
->type
== PIPE_SHADER_FRAGMENT
)
930 info
->prop
.fp
.numColourResults
++;
932 case TGSI_SEMANTIC_EDGEFLAG
:
933 info
->io
.edgeFlagOut
= i
;
935 case TGSI_SEMANTIC_CLIPVERTEX
:
936 clipVertexOutput
= i
;
938 case TGSI_SEMANTIC_CLIPDIST
:
939 info
->io
.clipDistanceMask
|=
940 decl
->Declaration
.UsageMask
<< (si
* 4);
941 info
->io
.genUserClip
= -1;
947 info
->out
[i
].sn
= sn
;
948 info
->out
[i
].si
= si
;
951 case TGSI_FILE_SYSTEM_VALUE
:
953 case TGSI_SEMANTIC_INSTANCEID
:
954 info
->io
.instanceId
= first
;
956 case TGSI_SEMANTIC_VERTEXID
:
957 info
->io
.vertexId
= first
;
962 for (i
= first
; i
<= last
; ++i
, ++si
) {
965 info
->sv
[i
].input
= inferSysValDirection(sn
);
968 case TGSI_FILE_RESOURCE
:
969 for (i
= first
; i
<= last
; ++i
) {
970 resources
[i
].target
= decl
->Resource
.Resource
;
971 resources
[i
].raw
= decl
->Resource
.Raw
;
972 resources
[i
].slot
= i
;
975 case TGSI_FILE_SAMPLER_VIEW
:
976 for (i
= first
; i
<= last
; ++i
)
977 textureViews
[i
].target
= decl
->SamplerView
.Resource
;
980 case TGSI_FILE_TEMPORARY
:
981 case TGSI_FILE_ADDRESS
:
982 case TGSI_FILE_CONSTANT
:
983 case TGSI_FILE_IMMEDIATE
:
984 case TGSI_FILE_PREDICATE
:
985 case TGSI_FILE_SAMPLER
:
988 ERROR("unhandled TGSI_FILE %d\n", decl
->Declaration
.File
);
994 inline bool Source::isEdgeFlagPassthrough(const Instruction
& insn
) const
996 return insn
.getOpcode() == TGSI_OPCODE_MOV
&&
997 insn
.getDst(0).getIndex(0) == info
->io
.edgeFlagOut
&&
998 insn
.getSrc(0).getFile() == TGSI_FILE_INPUT
;
1001 bool Source::scanInstruction(const struct tgsi_full_instruction
*inst
)
1003 Instruction
insn(inst
);
1005 if (insn
.getOpcode() == TGSI_OPCODE_BARRIER
)
1006 info
->numBarriers
= 1;
1008 if (insn
.dstCount()) {
1009 if (insn
.getDst(0).getFile() == TGSI_FILE_OUTPUT
) {
1010 Instruction::DstRegister dst
= insn
.getDst(0);
1012 if (dst
.isIndirect(0))
1013 for (unsigned i
= 0; i
< info
->numOutputs
; ++i
)
1014 info
->out
[i
].mask
= 0xf;
1016 info
->out
[dst
.getIndex(0)].mask
|= dst
.getMask();
1018 if (info
->out
[dst
.getIndex(0)].sn
== TGSI_SEMANTIC_PSIZE
||
1019 info
->out
[dst
.getIndex(0)].sn
== TGSI_SEMANTIC_PRIMID
||
1020 info
->out
[dst
.getIndex(0)].sn
== TGSI_SEMANTIC_FOG
)
1021 info
->out
[dst
.getIndex(0)].mask
&= 1;
1023 if (isEdgeFlagPassthrough(insn
))
1024 info
->io
.edgeFlagIn
= insn
.getSrc(0).getIndex(0);
1026 if (insn
.getDst(0).getFile() == TGSI_FILE_TEMPORARY
) {
1027 if (insn
.getDst(0).isIndirect(0))
1028 mainTempsInLMem
= TRUE
;
1032 for (unsigned s
= 0; s
< insn
.srcCount(); ++s
) {
1033 Instruction::SrcRegister src
= insn
.getSrc(s
);
1034 if (src
.getFile() == TGSI_FILE_TEMPORARY
) {
1035 if (src
.isIndirect(0))
1036 mainTempsInLMem
= TRUE
;
1038 if (src
.getFile() == TGSI_FILE_RESOURCE
) {
1039 if (src
.getIndex(0) == TGSI_RESOURCE_GLOBAL
)
1040 info
->io
.globalAccess
|= (insn
.getOpcode() == TGSI_OPCODE_LOAD
) ?
1043 if (src
.getFile() != TGSI_FILE_INPUT
)
1045 unsigned mask
= insn
.srcMask(s
);
1047 if (src
.isIndirect(0)) {
1048 for (unsigned i
= 0; i
< info
->numInputs
; ++i
)
1049 info
->in
[i
].mask
= 0xf;
1051 const int i
= src
.getIndex(0);
1052 for (unsigned c
= 0; c
< 4; ++c
) {
1053 if (!(mask
& (1 << c
)))
1055 int k
= src
.getSwizzle(c
);
1056 if (k
<= TGSI_SWIZZLE_W
)
1057 info
->in
[i
].mask
|= 1 << k
;
1059 switch (info
->in
[i
].sn
) {
1060 case TGSI_SEMANTIC_PSIZE
:
1061 case TGSI_SEMANTIC_PRIMID
:
1062 case TGSI_SEMANTIC_FOG
:
1063 info
->in
[i
].mask
&= 0x1;
1065 case TGSI_SEMANTIC_PCOORD
:
1066 info
->in
[i
].mask
&= 0x3;
1076 nv50_ir::TexInstruction::Target
1077 Instruction::getTexture(const tgsi::Source
*code
, int s
) const
1079 // XXX: indirect access
1082 switch (getSrc(s
).getFile()) {
1083 case TGSI_FILE_RESOURCE
:
1084 r
= getSrc(s
).getIndex(0);
1085 return translateTexture(code
->resources
.at(r
).target
);
1086 case TGSI_FILE_SAMPLER_VIEW
:
1087 r
= getSrc(s
).getIndex(0);
1088 return translateTexture(code
->textureViews
.at(r
).target
);
1090 return translateTexture(insn
->Texture
.Texture
);
1098 using namespace nv50_ir
;
1100 class Converter
: public BuildUtil
1103 Converter(Program
*, const tgsi::Source
*);
1111 Subroutine(Function
*f
) : f(f
) { }
1116 Value
*getVertexBase(int s
);
1117 DataArray
*getArrayForFile(unsigned file
, int idx
);
1118 Value
*fetchSrc(int s
, int c
);
1119 Value
*acquireDst(int d
, int c
);
1120 void storeDst(int d
, int c
, Value
*);
1122 Value
*fetchSrc(const tgsi::Instruction::SrcRegister src
, int c
, Value
*ptr
);
1123 void storeDst(const tgsi::Instruction::DstRegister dst
, int c
,
1124 Value
*val
, Value
*ptr
);
1126 Value
*applySrcMod(Value
*, int s
, int c
);
1128 Symbol
*makeSym(uint file
, int fileIndex
, int idx
, int c
, uint32_t addr
);
1129 Symbol
*srcToSym(tgsi::Instruction::SrcRegister
, int c
);
1130 Symbol
*dstToSym(tgsi::Instruction::DstRegister
, int c
);
1132 bool handleInstruction(const struct tgsi_full_instruction
*);
1133 void exportOutputs();
1134 inline Subroutine
*getSubroutine(unsigned ip
);
1135 inline Subroutine
*getSubroutine(Function
*);
1136 inline bool isEndOfSubroutine(uint ip
);
1138 void loadProjTexCoords(Value
*dst
[4], Value
*src
[4], unsigned int mask
);
1140 // R,S,L,C,Dx,Dy encode TGSI sources for respective values (0xSf for auto)
1141 void setTexRS(TexInstruction
*, unsigned int& s
, int R
, int S
);
1142 void handleTEX(Value
*dst0
[4], int R
, int S
, int L
, int C
, int Dx
, int Dy
);
1143 void handleTXF(Value
*dst0
[4], int R
, int L_M
);
1144 void handleTXQ(Value
*dst0
[4], enum TexQuery
);
1145 void handleLIT(Value
*dst0
[4]);
1146 void handleUserClipPlanes();
1148 Symbol
*getResourceBase(int r
);
1149 void getResourceCoords(std::vector
<Value
*>&, int r
, int s
);
1151 void handleLOAD(Value
*dst0
[4]);
1153 void handleATOM(Value
*dst0
[4], DataType
, uint16_t subOp
);
1155 Value
*interpolate(tgsi::Instruction::SrcRegister
, int c
, Value
*ptr
);
1157 void insertConvergenceOps(BasicBlock
*conv
, BasicBlock
*fork
);
1159 Value
*buildDot(int dim
);
1161 class BindArgumentsPass
: public Pass
{
1163 BindArgumentsPass(Converter
&conv
) : conv(conv
) { }
1169 inline const Location
*getValueLocation(Subroutine
*, Value
*);
1171 template<typename T
> inline void
1172 updateCallArgs(Instruction
*i
, void (Instruction::*setArg
)(int, Value
*),
1173 T (Function::*proto
));
1175 template<typename T
> inline void
1176 updatePrototype(BitSet
*set
, void (Function::*updateSet
)(),
1177 T (Function::*proto
));
1180 bool visit(Function
*);
1181 bool visit(BasicBlock
*bb
) { return false; }
1185 const struct tgsi::Source
*code
;
1186 const struct nv50_ir_prog_info
*info
;
1189 std::map
<unsigned, Subroutine
> map
;
1193 uint ip
; // instruction pointer
1195 tgsi::Instruction tgsi
;
1200 DataArray tData
; // TGSI_FILE_TEMPORARY
1201 DataArray aData
; // TGSI_FILE_ADDRESS
1202 DataArray pData
; // TGSI_FILE_PREDICATE
1203 DataArray oData
; // TGSI_FILE_OUTPUT (if outputs in registers)
1206 Value
*fragCoord
[4];
1209 Value
*vtxBase
[5]; // base address of vertex in primitive (for TP/GP)
1210 uint8_t vtxBaseValid
;
1212 Stack condBBs
; // fork BB, then else clause BB
1213 Stack joinBBs
; // fork BB, for inserting join ops on ENDIF
1214 Stack loopBBs
; // loop headers
1215 Stack breakBBs
; // end of / after loop
1219 Converter::srcToSym(tgsi::Instruction::SrcRegister src
, int c
)
1221 const int swz
= src
.getSwizzle(c
);
1223 return makeSym(src
.getFile(),
1224 src
.is2D() ? src
.getIndex(1) : 0,
1225 src
.isIndirect(0) ? -1 : src
.getIndex(0), swz
,
1226 src
.getIndex(0) * 16 + swz
* 4);
1230 Converter::dstToSym(tgsi::Instruction::DstRegister dst
, int c
)
1232 return makeSym(dst
.getFile(),
1233 dst
.is2D() ? dst
.getIndex(1) : 0,
1234 dst
.isIndirect(0) ? -1 : dst
.getIndex(0), c
,
1235 dst
.getIndex(0) * 16 + c
* 4);
1239 Converter::makeSym(uint tgsiFile
, int fileIdx
, int idx
, int c
, uint32_t address
)
1241 Symbol
*sym
= new_Symbol(prog
, tgsi::translateFile(tgsiFile
));
1243 sym
->reg
.fileIndex
= fileIdx
;
1246 if (sym
->reg
.file
== FILE_SHADER_INPUT
)
1247 sym
->setOffset(info
->in
[idx
].slot
[c
] * 4);
1249 if (sym
->reg
.file
== FILE_SHADER_OUTPUT
)
1250 sym
->setOffset(info
->out
[idx
].slot
[c
] * 4);
1252 if (sym
->reg
.file
== FILE_SYSTEM_VALUE
)
1253 sym
->setSV(tgsi::translateSysVal(info
->sv
[idx
].sn
), c
);
1255 sym
->setOffset(address
);
1257 sym
->setOffset(address
);
1262 static inline uint8_t
1263 translateInterpMode(const struct nv50_ir_varying
*var
, operation
& op
)
1265 uint8_t mode
= NV50_IR_INTERP_PERSPECTIVE
;
1268 mode
= NV50_IR_INTERP_FLAT
;
1271 mode
= NV50_IR_INTERP_LINEAR
;
1274 mode
= NV50_IR_INTERP_SC
;
1276 op
= (mode
== NV50_IR_INTERP_PERSPECTIVE
|| mode
== NV50_IR_INTERP_SC
)
1277 ? OP_PINTERP
: OP_LINTERP
;
1280 mode
|= NV50_IR_INTERP_CENTROID
;
1286 Converter::interpolate(tgsi::Instruction::SrcRegister src
, int c
, Value
*ptr
)
1290 // XXX: no way to know interpolation mode if we don't know what's accessed
1291 const uint8_t mode
= translateInterpMode(&info
->in
[ptr
? 0 :
1292 src
.getIndex(0)], op
);
1294 Instruction
*insn
= new_Instruction(func
, op
, TYPE_F32
);
1296 insn
->setDef(0, getScratch());
1297 insn
->setSrc(0, srcToSym(src
, c
));
1298 if (op
== OP_PINTERP
)
1299 insn
->setSrc(1, fragCoord
[3]);
1301 insn
->setIndirect(0, 0, ptr
);
1303 insn
->setInterpolate(mode
);
1305 bb
->insertTail(insn
);
1306 return insn
->getDef(0);
1310 Converter::applySrcMod(Value
*val
, int s
, int c
)
1312 Modifier m
= tgsi
.getSrc(s
).getMod(c
);
1313 DataType ty
= tgsi
.inferSrcType();
1315 if (m
& Modifier(NV50_IR_MOD_ABS
))
1316 val
= mkOp1v(OP_ABS
, ty
, getScratch(), val
);
1318 if (m
& Modifier(NV50_IR_MOD_NEG
))
1319 val
= mkOp1v(OP_NEG
, ty
, getScratch(), val
);
1325 Converter::getVertexBase(int s
)
1328 if (!(vtxBaseValid
& (1 << s
))) {
1329 const int index
= tgsi
.getSrc(s
).getIndex(1);
1331 if (tgsi
.getSrc(s
).isIndirect(1))
1332 rel
= fetchSrc(tgsi
.getSrc(s
).getIndirect(1), 0, NULL
);
1333 vtxBaseValid
|= 1 << s
;
1334 vtxBase
[s
] = mkOp2v(OP_PFETCH
, TYPE_U32
, getSSA(), mkImm(index
), rel
);
1340 Converter::fetchSrc(int s
, int c
)
1343 Value
*ptr
= NULL
, *dimRel
= NULL
;
1345 tgsi::Instruction::SrcRegister src
= tgsi
.getSrc(s
);
1347 if (src
.isIndirect(0))
1348 ptr
= fetchSrc(src
.getIndirect(0), 0, NULL
);
1351 switch (src
.getFile()) {
1352 case TGSI_FILE_INPUT
:
1353 dimRel
= getVertexBase(s
);
1355 case TGSI_FILE_CONSTANT
:
1356 // on NVC0, this is valid and c{I+J}[k] == cI[(J << 16) + k]
1357 if (src
.isIndirect(1))
1358 dimRel
= fetchSrc(src
.getIndirect(1), 0, 0);
1365 res
= fetchSrc(src
, c
, ptr
);
1368 res
->getInsn()->setIndirect(0, 1, dimRel
);
1370 return applySrcMod(res
, s
, c
);
1373 Converter::DataArray
*
1374 Converter::getArrayForFile(unsigned file
, int idx
)
1377 case TGSI_FILE_TEMPORARY
:
1379 case TGSI_FILE_PREDICATE
:
1381 case TGSI_FILE_ADDRESS
:
1383 case TGSI_FILE_OUTPUT
:
1384 assert(prog
->getType() == Program::TYPE_FRAGMENT
);
1387 assert(!"invalid/unhandled TGSI source file");
1393 Converter::fetchSrc(tgsi::Instruction::SrcRegister src
, int c
, Value
*ptr
)
1395 const int idx2d
= src
.is2D() ? src
.getIndex(1) : 0;
1396 const int idx
= src
.getIndex(0);
1397 const int swz
= src
.getSwizzle(c
);
1399 switch (src
.getFile()) {
1400 case TGSI_FILE_IMMEDIATE
:
1402 return loadImm(NULL
, info
->immd
.data
[idx
* 4 + swz
]);
1403 case TGSI_FILE_CONSTANT
:
1404 return mkLoadv(TYPE_U32
, srcToSym(src
, c
), ptr
);
1405 case TGSI_FILE_INPUT
:
1406 if (prog
->getType() == Program::TYPE_FRAGMENT
) {
1407 // don't load masked inputs, won't be assigned a slot
1408 if (!ptr
&& !(info
->in
[idx
].mask
& (1 << swz
)))
1409 return loadImm(NULL
, swz
== TGSI_SWIZZLE_W
? 1.0f
: 0.0f
);
1410 if (!ptr
&& info
->in
[idx
].sn
== TGSI_SEMANTIC_FACE
)
1411 return mkOp1v(OP_RDSV
, TYPE_F32
, getSSA(), mkSysVal(SV_FACE
, 0));
1412 return interpolate(src
, c
, ptr
);
1414 return mkLoadv(TYPE_U32
, srcToSym(src
, c
), ptr
);
1415 case TGSI_FILE_OUTPUT
:
1416 assert(!"load from output file");
1418 case TGSI_FILE_SYSTEM_VALUE
:
1420 return mkOp1v(OP_RDSV
, TYPE_U32
, getSSA(), srcToSym(src
, c
));
1422 return getArrayForFile(src
.getFile(), idx2d
)->load(
1423 sub
.cur
->values
, idx
, swz
, ptr
);
1428 Converter::acquireDst(int d
, int c
)
1430 const tgsi::Instruction::DstRegister dst
= tgsi
.getDst(d
);
1431 const unsigned f
= dst
.getFile();
1432 const int idx
= dst
.getIndex(0);
1433 const int idx2d
= dst
.is2D() ? dst
.getIndex(1) : 0;
1435 if (dst
.isMasked(c
) || f
== TGSI_FILE_RESOURCE
)
1438 if (dst
.isIndirect(0) ||
1439 f
== TGSI_FILE_SYSTEM_VALUE
||
1440 (f
== TGSI_FILE_OUTPUT
&& prog
->getType() != Program::TYPE_FRAGMENT
))
1441 return getScratch();
1443 return getArrayForFile(f
, idx2d
)-> acquire(sub
.cur
->values
, idx
, c
);
1447 Converter::storeDst(int d
, int c
, Value
*val
)
1449 const tgsi::Instruction::DstRegister dst
= tgsi
.getDst(d
);
1451 switch (tgsi
.getSaturate()) {
1454 case TGSI_SAT_ZERO_ONE
:
1455 mkOp1(OP_SAT
, dstTy
, val
, val
);
1457 case TGSI_SAT_MINUS_PLUS_ONE
:
1458 mkOp2(OP_MAX
, dstTy
, val
, val
, mkImm(-1.0f
));
1459 mkOp2(OP_MIN
, dstTy
, val
, val
, mkImm(+1.0f
));
1462 assert(!"invalid saturation mode");
1466 Value
*ptr
= dst
.isIndirect(0) ?
1467 fetchSrc(dst
.getIndirect(0), 0, NULL
) : NULL
;
1469 if (info
->io
.genUserClip
> 0 &&
1470 dst
.getFile() == TGSI_FILE_OUTPUT
&&
1471 !dst
.isIndirect(0) && dst
.getIndex(0) == code
->clipVertexOutput
) {
1472 mkMov(clipVtx
[c
], val
);
1476 storeDst(dst
, c
, val
, ptr
);
1480 Converter::storeDst(const tgsi::Instruction::DstRegister dst
, int c
,
1481 Value
*val
, Value
*ptr
)
1483 const unsigned f
= dst
.getFile();
1484 const int idx
= dst
.getIndex(0);
1485 const int idx2d
= dst
.is2D() ? dst
.getIndex(1) : 0;
1487 if (f
== TGSI_FILE_SYSTEM_VALUE
) {
1489 mkOp2(OP_WRSV
, TYPE_U32
, NULL
, dstToSym(dst
, c
), val
);
1491 if (f
== TGSI_FILE_OUTPUT
&& prog
->getType() != Program::TYPE_FRAGMENT
) {
1492 if (ptr
|| (info
->out
[idx
].mask
& (1 << c
)))
1493 mkStore(OP_EXPORT
, TYPE_U32
, dstToSym(dst
, c
), ptr
, val
);
1495 if (f
== TGSI_FILE_TEMPORARY
||
1496 f
== TGSI_FILE_PREDICATE
||
1497 f
== TGSI_FILE_ADDRESS
||
1498 f
== TGSI_FILE_OUTPUT
) {
1499 getArrayForFile(f
, idx2d
)->store(sub
.cur
->values
, idx
, c
, ptr
, val
);
1501 assert(!"invalid dst file");
1505 #define FOR_EACH_DST_ENABLED_CHANNEL(d, chan, inst) \
1506 for (chan = 0; chan < 4; ++chan) \
1507 if (!inst.getDst(d).isMasked(chan))
1510 Converter::buildDot(int dim
)
1514 Value
*src0
= fetchSrc(0, 0), *src1
= fetchSrc(1, 0);
1515 Value
*dotp
= getScratch();
1517 mkOp2(OP_MUL
, TYPE_F32
, dotp
, src0
, src1
);
1519 for (int c
= 1; c
< dim
; ++c
) {
1520 src0
= fetchSrc(0, c
);
1521 src1
= fetchSrc(1, c
);
1522 mkOp3(OP_MAD
, TYPE_F32
, dotp
, src0
, src1
, dotp
);
1528 Converter::insertConvergenceOps(BasicBlock
*conv
, BasicBlock
*fork
)
1530 FlowInstruction
*join
= new_FlowInstruction(func
, OP_JOIN
, NULL
);
1532 conv
->insertHead(join
);
1534 fork
->joinAt
= new_FlowInstruction(func
, OP_JOINAT
, conv
);
1535 fork
->insertBefore(fork
->getExit(), fork
->joinAt
);
1539 Converter::setTexRS(TexInstruction
*tex
, unsigned int& s
, int R
, int S
)
1541 unsigned rIdx
= 0, sIdx
= 0;
1544 rIdx
= tgsi
.getSrc(R
).getIndex(0);
1546 sIdx
= tgsi
.getSrc(S
).getIndex(0);
1548 tex
->setTexture(tgsi
.getTexture(code
, R
), rIdx
, sIdx
);
1550 if (tgsi
.getSrc(R
).isIndirect(0)) {
1551 tex
->tex
.rIndirectSrc
= s
;
1552 tex
->setSrc(s
++, fetchSrc(tgsi
.getSrc(R
).getIndirect(0), 0, NULL
));
1554 if (S
>= 0 && tgsi
.getSrc(S
).isIndirect(0)) {
1555 tex
->tex
.sIndirectSrc
= s
;
1556 tex
->setSrc(s
++, fetchSrc(tgsi
.getSrc(S
).getIndirect(0), 0, NULL
));
1561 Converter::handleTXQ(Value
*dst0
[4], enum TexQuery query
)
1563 TexInstruction
*tex
= new_TexInstruction(func
, OP_TXQ
);
1564 tex
->tex
.query
= query
;
1567 for (d
= 0, c
= 0; c
< 4; ++c
) {
1570 tex
->tex
.mask
|= 1 << c
;
1571 tex
->setDef(d
++, dst0
[c
]);
1573 tex
->setSrc((c
= 0), fetchSrc(0, 0)); // mip level
1575 setTexRS(tex
, c
, 1, -1);
1577 bb
->insertTail(tex
);
1581 Converter::loadProjTexCoords(Value
*dst
[4], Value
*src
[4], unsigned int mask
)
1583 Value
*proj
= fetchSrc(0, 3);
1584 Instruction
*insn
= proj
->getUniqueInsn();
1587 if (insn
->op
== OP_PINTERP
) {
1588 bb
->insertTail(insn
= cloneForward(func
, insn
));
1589 insn
->op
= OP_LINTERP
;
1590 insn
->setInterpolate(NV50_IR_INTERP_LINEAR
| insn
->getSampleMode());
1591 insn
->setSrc(1, NULL
);
1592 proj
= insn
->getDef(0);
1594 proj
= mkOp1v(OP_RCP
, TYPE_F32
, getSSA(), proj
);
1596 for (c
= 0; c
< 4; ++c
) {
1597 if (!(mask
& (1 << c
)))
1599 if ((insn
= src
[c
]->getUniqueInsn())->op
!= OP_PINTERP
)
1603 bb
->insertTail(insn
= cloneForward(func
, insn
));
1604 insn
->setInterpolate(NV50_IR_INTERP_PERSPECTIVE
| insn
->getSampleMode());
1605 insn
->setSrc(1, proj
);
1606 dst
[c
] = insn
->getDef(0);
1611 proj
= mkOp1v(OP_RCP
, TYPE_F32
, getSSA(), fetchSrc(0, 3));
1613 for (c
= 0; c
< 4; ++c
)
1614 if (mask
& (1 << c
))
1615 dst
[c
] = mkOp2v(OP_MUL
, TYPE_F32
, getSSA(), src
[c
], proj
);
1618 // order of nv50 ir sources: x y z layer lod/bias shadow
1619 // order of TGSI TEX sources: x y z layer shadow lod/bias
1620 // lowering will finally set the hw specific order (like array first on nvc0)
1622 Converter::handleTEX(Value
*dst
[4], int R
, int S
, int L
, int C
, int Dx
, int Dy
)
1625 Value
*arg
[4], *src
[8];
1626 Value
*lod
= NULL
, *shd
= NULL
;
1627 unsigned int s
, c
, d
;
1628 TexInstruction
*texi
= new_TexInstruction(func
, tgsi
.getOP());
1630 TexInstruction::Target tgt
= tgsi
.getTexture(code
, R
);
1632 for (s
= 0; s
< tgt
.getArgCount(); ++s
)
1633 arg
[s
] = src
[s
] = fetchSrc(0, s
);
1635 if (texi
->op
== OP_TXL
|| texi
->op
== OP_TXB
)
1636 lod
= fetchSrc(L
>> 4, L
& 3);
1639 C
= 0x00 | MAX2(tgt
.getArgCount(), 2); // guess DC src
1642 shd
= fetchSrc(C
>> 4, C
& 3);
1644 if (texi
->op
== OP_TXD
) {
1645 for (c
= 0; c
< tgt
.getDim(); ++c
) {
1646 texi
->dPdx
[c
].set(fetchSrc(Dx
>> 4, (Dx
& 3) + c
));
1647 texi
->dPdy
[c
].set(fetchSrc(Dy
>> 4, (Dy
& 3) + c
));
1651 // cube textures don't care about projection value, it's divided out
1652 if (tgsi
.getOpcode() == TGSI_OPCODE_TXP
&& !tgt
.isCube() && !tgt
.isArray()) {
1653 unsigned int n
= tgt
.getDim();
1657 assert(tgt
.getDim() == tgt
.getArgCount());
1659 loadProjTexCoords(src
, arg
, (1 << n
) - 1);
1665 for (c
= 0; c
< 3; ++c
)
1666 src
[c
] = mkOp1v(OP_ABS
, TYPE_F32
, getSSA(), arg
[c
]);
1668 mkOp2(OP_MAX
, TYPE_F32
, val
, src
[0], src
[1]);
1669 mkOp2(OP_MAX
, TYPE_F32
, val
, src
[2], val
);
1670 mkOp1(OP_RCP
, TYPE_F32
, val
, val
);
1671 for (c
= 0; c
< 3; ++c
)
1672 src
[c
] = mkOp2v(OP_MUL
, TYPE_F32
, getSSA(), arg
[c
], val
);
1675 for (c
= 0, d
= 0; c
< 4; ++c
) {
1677 texi
->setDef(d
++, dst
[c
]);
1678 texi
->tex
.mask
|= 1 << c
;
1680 // NOTE: maybe hook up def too, for CSE
1683 for (s
= 0; s
< tgt
.getArgCount(); ++s
)
1684 texi
->setSrc(s
, src
[s
]);
1686 texi
->setSrc(s
++, lod
);
1688 texi
->setSrc(s
++, shd
);
1690 setTexRS(texi
, s
, R
, S
);
1692 if (tgsi
.getOpcode() == TGSI_OPCODE_SAMPLE_C_LZ
)
1693 texi
->tex
.levelZero
= true;
1695 bb
->insertTail(texi
);
1698 // 1st source: xyz = coordinates, w = lod/sample
1699 // 2nd source: offset
1701 Converter::handleTXF(Value
*dst
[4], int R
, int L_M
)
1703 TexInstruction
*texi
= new_TexInstruction(func
, tgsi
.getOP());
1705 unsigned int c
, d
, s
;
1707 texi
->tex
.target
= tgsi
.getTexture(code
, R
);
1709 ms
= texi
->tex
.target
.isMS() ? 1 : 0;
1710 texi
->tex
.levelZero
= ms
; /* MS textures don't have mip-maps */
1712 for (c
= 0, d
= 0; c
< 4; ++c
) {
1714 texi
->setDef(d
++, dst
[c
]);
1715 texi
->tex
.mask
|= 1 << c
;
1718 for (c
= 0; c
< (texi
->tex
.target
.getArgCount() - ms
); ++c
)
1719 texi
->setSrc(c
, fetchSrc(0, c
));
1720 texi
->setSrc(c
++, fetchSrc(L_M
>> 4, L_M
& 3)); // lod or ms
1722 setTexRS(texi
, c
, R
, -1);
1724 for (s
= 0; s
< tgsi
.getNumTexOffsets(); ++s
) {
1725 for (c
= 0; c
< 3; ++c
) {
1726 texi
->tex
.offset
[s
][c
] = tgsi
.getTexOffset(s
).getValueU32(c
, info
);
1727 if (texi
->tex
.offset
[s
][c
])
1728 texi
->tex
.useOffsets
= s
+ 1;
1732 bb
->insertTail(texi
);
1736 Converter::handleLIT(Value
*dst0
[4])
1739 unsigned int mask
= tgsi
.getDst(0).getMask();
1741 if (mask
& (1 << 0))
1742 loadImm(dst0
[0], 1.0f
);
1744 if (mask
& (1 << 3))
1745 loadImm(dst0
[3], 1.0f
);
1747 if (mask
& (3 << 1)) {
1748 val0
= getScratch();
1749 mkOp2(OP_MAX
, TYPE_F32
, val0
, fetchSrc(0, 0), zero
);
1750 if (mask
& (1 << 1))
1751 mkMov(dst0
[1], val0
);
1754 if (mask
& (1 << 2)) {
1755 Value
*src1
= fetchSrc(0, 1), *src3
= fetchSrc(0, 3);
1756 Value
*val1
= getScratch(), *val3
= getScratch();
1758 Value
*pos128
= loadImm(NULL
, +127.999999f
);
1759 Value
*neg128
= loadImm(NULL
, -127.999999f
);
1761 mkOp2(OP_MAX
, TYPE_F32
, val1
, src1
, zero
);
1762 mkOp2(OP_MAX
, TYPE_F32
, val3
, src3
, neg128
);
1763 mkOp2(OP_MIN
, TYPE_F32
, val3
, val3
, pos128
);
1764 mkOp2(OP_POW
, TYPE_F32
, val3
, val1
, val3
);
1766 mkCmp(OP_SLCT
, CC_GT
, TYPE_F32
, dst0
[2], val3
, zero
, val0
);
1771 isResourceSpecial(const int r
)
1773 return (r
== TGSI_RESOURCE_GLOBAL
||
1774 r
== TGSI_RESOURCE_LOCAL
||
1775 r
== TGSI_RESOURCE_PRIVATE
||
1776 r
== TGSI_RESOURCE_INPUT
);
1780 isResourceRaw(const struct tgsi::Source
*code
, const int r
)
1782 return isResourceSpecial(r
) || code
->resources
[r
].raw
;
1785 static inline nv50_ir::TexTarget
1786 getResourceTarget(const struct tgsi::Source
*code
, int r
)
1788 if (isResourceSpecial(r
))
1789 return nv50_ir::TEX_TARGET_BUFFER
;
1790 return tgsi::translateTexture(code
->resources
.at(r
).target
);
1794 Converter::getResourceBase(const int r
)
1799 case TGSI_RESOURCE_GLOBAL
:
1800 sym
= new_Symbol(prog
, nv50_ir::FILE_MEMORY_GLOBAL
, 15);
1802 case TGSI_RESOURCE_LOCAL
:
1803 assert(prog
->getType() == Program::TYPE_COMPUTE
);
1804 sym
= mkSymbol(nv50_ir::FILE_MEMORY_SHARED
, 0, TYPE_U32
,
1805 info
->prop
.cp
.sharedOffset
);
1807 case TGSI_RESOURCE_PRIVATE
:
1808 sym
= mkSymbol(nv50_ir::FILE_MEMORY_LOCAL
, 0, TYPE_U32
,
1809 info
->bin
.tlsSpace
);
1811 case TGSI_RESOURCE_INPUT
:
1812 assert(prog
->getType() == Program::TYPE_COMPUTE
);
1813 sym
= mkSymbol(nv50_ir::FILE_SHADER_INPUT
, 0, TYPE_U32
,
1814 info
->prop
.cp
.inputOffset
);
1817 sym
= new_Symbol(prog
,
1818 nv50_ir::FILE_MEMORY_GLOBAL
, code
->resources
.at(r
).slot
);
1825 Converter::getResourceCoords(std::vector
<Value
*> &coords
, int r
, int s
)
1828 TexInstruction::Target(getResourceTarget(code
, r
)).getArgCount();
1830 for (int c
= 0; c
< arg
; ++c
)
1831 coords
.push_back(fetchSrc(s
, c
));
1833 // NOTE: TGSI_RESOURCE_GLOBAL needs FILE_GPR; this is an nv50 quirk
1834 if (r
== TGSI_RESOURCE_LOCAL
||
1835 r
== TGSI_RESOURCE_PRIVATE
||
1836 r
== TGSI_RESOURCE_INPUT
)
1837 coords
[0] = mkOp1v(OP_MOV
, TYPE_U32
, getScratch(4, FILE_ADDRESS
),
1842 partitionLoadStore(uint8_t comp
[2], uint8_t size
[2], uint8_t mask
)
1851 comp
[n
= 1] = size
[0] + 1;
1859 size
[0] = (comp
[0] == 1) ? 1 : 2;
1860 size
[1] = 3 - size
[0];
1861 comp
[1] = comp
[0] + size
[0];
1866 // For raw loads, granularity is 4 byte.
1867 // Usage of the texture read mask on OP_SULDP is not allowed.
1869 Converter::handleLOAD(Value
*dst0
[4])
1871 const int r
= tgsi
.getSrc(0).getIndex(0);
1873 std::vector
<Value
*> off
, src
, ldv
, def
;
1875 getResourceCoords(off
, r
, 1);
1877 if (isResourceRaw(code
, r
)) {
1879 uint8_t comp
[2] = { 0, 0 };
1880 uint8_t size
[2] = { 0, 0 };
1882 Symbol
*base
= getResourceBase(r
);
1884 // determine the base and size of the at most 2 load ops
1885 for (c
= 0; c
< 4; ++c
)
1886 if (!tgsi
.getDst(0).isMasked(c
))
1887 mask
|= 1 << (tgsi
.getSrc(0).getSwizzle(c
) - TGSI_SWIZZLE_X
);
1889 int n
= partitionLoadStore(comp
, size
, mask
);
1893 def
.resize(4); // index by component, the ones we need will be non-NULL
1894 for (c
= 0; c
< 4; ++c
) {
1895 if (dst0
[c
] && tgsi
.getSrc(0).getSwizzle(c
) == (TGSI_SWIZZLE_X
+ c
))
1898 if (mask
& (1 << c
))
1899 def
[c
] = getScratch();
1902 const bool useLd
= isResourceSpecial(r
) ||
1903 (info
->io
.nv50styleSurfaces
&&
1904 code
->resources
[r
].target
== TGSI_TEXTURE_BUFFER
);
1906 for (int i
= 0; i
< n
; ++i
) {
1907 ldv
.assign(def
.begin() + comp
[i
], def
.begin() + comp
[i
] + size
[i
]);
1909 if (comp
[i
]) // adjust x component of source address if necessary
1910 src
[0] = mkOp2v(OP_ADD
, TYPE_U32
, getSSA(4, off
[0]->reg
.file
),
1911 off
[0], mkImm(comp
[i
] * 4));
1917 mkLoad(typeOfSize(size
[i
] * 4), ldv
[0], base
, src
[0]);
1918 for (size_t c
= 1; c
< ldv
.size(); ++c
)
1919 ld
->setDef(c
, ldv
[c
]);
1921 mkTex(OP_SULDB
, getResourceTarget(code
, r
), code
->resources
[r
].slot
,
1922 0, ldv
, src
)->dType
= typeOfSize(size
[i
] * 4);
1927 for (c
= 0; c
< 4; ++c
) {
1928 if (!dst0
[c
] || tgsi
.getSrc(0).getSwizzle(c
) != (TGSI_SWIZZLE_X
+ c
))
1929 def
[c
] = getScratch();
1934 mkTex(OP_SULDP
, getResourceTarget(code
, r
), code
->resources
[r
].slot
, 0,
1937 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
)
1938 if (dst0
[c
] != def
[c
])
1939 mkMov(dst0
[c
], def
[tgsi
.getSrc(0).getSwizzle(c
)]);
1942 // For formatted stores, the write mask on OP_SUSTP can be used.
1943 // Raw stores have to be split.
1945 Converter::handleSTORE()
1947 const int r
= tgsi
.getDst(0).getIndex(0);
1949 std::vector
<Value
*> off
, src
, dummy
;
1951 getResourceCoords(off
, r
, 0);
1953 const int s
= src
.size();
1955 if (isResourceRaw(code
, r
)) {
1956 uint8_t comp
[2] = { 0, 0 };
1957 uint8_t size
[2] = { 0, 0 };
1959 int n
= partitionLoadStore(comp
, size
, tgsi
.getDst(0).getMask());
1961 Symbol
*base
= getResourceBase(r
);
1963 const bool useSt
= isResourceSpecial(r
) ||
1964 (info
->io
.nv50styleSurfaces
&&
1965 code
->resources
[r
].target
== TGSI_TEXTURE_BUFFER
);
1967 for (int i
= 0; i
< n
; ++i
) {
1968 if (comp
[i
]) // adjust x component of source address if necessary
1969 src
[0] = mkOp2v(OP_ADD
, TYPE_U32
, getSSA(4, off
[0]->reg
.file
),
1970 off
[0], mkImm(comp
[i
] * 4));
1974 const DataType stTy
= typeOfSize(size
[i
] * 4);
1978 mkStore(OP_STORE
, stTy
, base
, NULL
, fetchSrc(1, comp
[i
]));
1979 for (c
= 1; c
< size
[i
]; ++c
)
1980 st
->setSrc(1 + c
, fetchSrc(1, comp
[i
] + c
));
1981 st
->setIndirect(0, 0, src
[0]);
1983 // attach values to be stored
1984 src
.resize(s
+ size
[i
]);
1985 for (c
= 0; c
< size
[i
]; ++c
)
1986 src
[s
+ c
] = fetchSrc(1, comp
[i
] + c
);
1987 mkTex(OP_SUSTB
, getResourceTarget(code
, r
), code
->resources
[r
].slot
,
1988 0, dummy
, src
)->setType(stTy
);
1992 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
)
1993 src
.push_back(fetchSrc(1, c
));
1995 mkTex(OP_SUSTP
, getResourceTarget(code
, r
), code
->resources
[r
].slot
, 0,
1996 dummy
, src
)->tex
.mask
= tgsi
.getDst(0).getMask();
2000 // XXX: These only work on resources with the single-component u32/s32 formats.
2001 // Therefore the result is replicated. This might not be intended by TGSI, but
2002 // operating on more than 1 component would produce undefined results because
2003 // they do not exist.
2005 Converter::handleATOM(Value
*dst0
[4], DataType ty
, uint16_t subOp
)
2007 const int r
= tgsi
.getSrc(0).getIndex(0);
2008 std::vector
<Value
*> srcv
;
2009 std::vector
<Value
*> defv
;
2010 LValue
*dst
= getScratch();
2012 getResourceCoords(srcv
, r
, 1);
2014 if (isResourceSpecial(r
)) {
2015 assert(r
!= TGSI_RESOURCE_INPUT
);
2017 insn
= mkOp2(OP_ATOM
, ty
, dst
, getResourceBase(r
), fetchSrc(2, 0));
2018 insn
->subOp
= subOp
;
2019 if (subOp
== NV50_IR_SUBOP_ATOM_CAS
)
2020 insn
->setSrc(2, fetchSrc(3, 0));
2021 insn
->setIndirect(0, 0, srcv
.at(0));
2023 operation op
= isResourceRaw(code
, r
) ? OP_SUREDB
: OP_SUREDP
;
2024 TexTarget targ
= getResourceTarget(code
, r
);
2025 int idx
= code
->resources
[r
].slot
;
2026 defv
.push_back(dst
);
2027 srcv
.push_back(fetchSrc(2, 0));
2028 if (subOp
== NV50_IR_SUBOP_ATOM_CAS
)
2029 srcv
.push_back(fetchSrc(3, 0));
2030 TexInstruction
*tex
= mkTex(op
, targ
, idx
, 0, defv
, srcv
);
2036 for (int c
= 0; c
< 4; ++c
)
2038 dst0
[c
] = dst
; // not equal to rDst so handleInstruction will do mkMov
2041 Converter::Subroutine
*
2042 Converter::getSubroutine(unsigned ip
)
2044 std::map
<unsigned, Subroutine
>::iterator it
= sub
.map
.find(ip
);
2046 if (it
== sub
.map
.end())
2047 it
= sub
.map
.insert(std::make_pair(
2048 ip
, Subroutine(new Function(prog
, "SUB", ip
)))).first
;
2053 Converter::Subroutine
*
2054 Converter::getSubroutine(Function
*f
)
2056 unsigned ip
= f
->getLabel();
2057 std::map
<unsigned, Subroutine
>::iterator it
= sub
.map
.find(ip
);
2059 if (it
== sub
.map
.end())
2060 it
= sub
.map
.insert(std::make_pair(ip
, Subroutine(f
))).first
;
2066 Converter::isEndOfSubroutine(uint ip
)
2068 assert(ip
< code
->scan
.num_instructions
);
2069 tgsi::Instruction
insn(&code
->insns
[ip
]);
2070 return (insn
.getOpcode() == TGSI_OPCODE_END
||
2071 insn
.getOpcode() == TGSI_OPCODE_ENDSUB
||
2072 // does END occur at end of main or the very end ?
2073 insn
.getOpcode() == TGSI_OPCODE_BGNSUB
);
2077 Converter::handleInstruction(const struct tgsi_full_instruction
*insn
)
2081 Value
*dst0
[4], *rDst0
[4];
2082 Value
*src0
, *src1
, *src2
;
2086 tgsi
= tgsi::Instruction(insn
);
2088 bool useScratchDst
= tgsi
.checkDstSrcAliasing();
2090 operation op
= tgsi
.getOP();
2091 dstTy
= tgsi
.inferDstType();
2092 srcTy
= tgsi
.inferSrcType();
2094 unsigned int mask
= tgsi
.dstCount() ? tgsi
.getDst(0).getMask() : 0;
2096 if (tgsi
.dstCount()) {
2097 for (c
= 0; c
< 4; ++c
) {
2098 rDst0
[c
] = acquireDst(0, c
);
2099 dst0
[c
] = (useScratchDst
&& rDst0
[c
]) ? getScratch() : rDst0
[c
];
2103 switch (tgsi
.getOpcode()) {
2104 case TGSI_OPCODE_ADD
:
2105 case TGSI_OPCODE_UADD
:
2106 case TGSI_OPCODE_AND
:
2107 case TGSI_OPCODE_DIV
:
2108 case TGSI_OPCODE_IDIV
:
2109 case TGSI_OPCODE_UDIV
:
2110 case TGSI_OPCODE_MAX
:
2111 case TGSI_OPCODE_MIN
:
2112 case TGSI_OPCODE_IMAX
:
2113 case TGSI_OPCODE_IMIN
:
2114 case TGSI_OPCODE_UMAX
:
2115 case TGSI_OPCODE_UMIN
:
2116 case TGSI_OPCODE_MOD
:
2117 case TGSI_OPCODE_UMOD
:
2118 case TGSI_OPCODE_MUL
:
2119 case TGSI_OPCODE_UMUL
:
2120 case TGSI_OPCODE_OR
:
2121 case TGSI_OPCODE_POW
:
2122 case TGSI_OPCODE_SHL
:
2123 case TGSI_OPCODE_ISHR
:
2124 case TGSI_OPCODE_USHR
:
2125 case TGSI_OPCODE_SUB
:
2126 case TGSI_OPCODE_XOR
:
2127 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
) {
2128 src0
= fetchSrc(0, c
);
2129 src1
= fetchSrc(1, c
);
2130 mkOp2(op
, dstTy
, dst0
[c
], src0
, src1
);
2133 case TGSI_OPCODE_MAD
:
2134 case TGSI_OPCODE_UMAD
:
2135 case TGSI_OPCODE_SAD
:
2136 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
) {
2137 src0
= fetchSrc(0, c
);
2138 src1
= fetchSrc(1, c
);
2139 src2
= fetchSrc(2, c
);
2140 mkOp3(op
, dstTy
, dst0
[c
], src0
, src1
, src2
);
2143 case TGSI_OPCODE_MOV
:
2144 case TGSI_OPCODE_ABS
:
2145 case TGSI_OPCODE_CEIL
:
2146 case TGSI_OPCODE_FLR
:
2147 case TGSI_OPCODE_TRUNC
:
2148 case TGSI_OPCODE_RCP
:
2149 case TGSI_OPCODE_IABS
:
2150 case TGSI_OPCODE_INEG
:
2151 case TGSI_OPCODE_NOT
:
2152 case TGSI_OPCODE_DDX
:
2153 case TGSI_OPCODE_DDY
:
2154 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
)
2155 mkOp1(op
, dstTy
, dst0
[c
], fetchSrc(0, c
));
2157 case TGSI_OPCODE_RSQ
:
2158 src0
= fetchSrc(0, 0);
2159 val0
= getScratch();
2160 mkOp1(OP_ABS
, TYPE_F32
, val0
, src0
);
2161 mkOp1(OP_RSQ
, TYPE_F32
, val0
, val0
);
2162 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
)
2163 mkMov(dst0
[c
], val0
);
2165 case TGSI_OPCODE_ARL
:
2166 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
) {
2167 src0
= fetchSrc(0, c
);
2168 mkCvt(OP_CVT
, TYPE_S32
, dst0
[c
], TYPE_F32
, src0
)->rnd
= ROUND_M
;
2169 mkOp2(OP_SHL
, TYPE_U32
, dst0
[c
], dst0
[c
], mkImm(4));
2172 case TGSI_OPCODE_UARL
:
2173 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
)
2174 mkOp2(OP_SHL
, TYPE_U32
, dst0
[c
], fetchSrc(0, c
), mkImm(4));
2176 case TGSI_OPCODE_EX2
:
2177 case TGSI_OPCODE_LG2
:
2178 val0
= mkOp1(op
, TYPE_F32
, getScratch(), fetchSrc(0, 0))->getDef(0);
2179 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
)
2180 mkOp1(OP_MOV
, TYPE_F32
, dst0
[c
], val0
);
2182 case TGSI_OPCODE_COS
:
2183 case TGSI_OPCODE_SIN
:
2184 val0
= getScratch();
2186 mkOp1(OP_PRESIN
, TYPE_F32
, val0
, fetchSrc(0, 0));
2187 mkOp1(op
, TYPE_F32
, val0
, val0
);
2188 for (c
= 0; c
< 3; ++c
)
2190 mkMov(dst0
[c
], val0
);
2193 mkOp1(OP_PRESIN
, TYPE_F32
, val0
, fetchSrc(0, 3));
2194 mkOp1(op
, TYPE_F32
, dst0
[3], val0
);
2197 case TGSI_OPCODE_SCS
:
2199 val0
= mkOp1v(OP_PRESIN
, TYPE_F32
, getSSA(), fetchSrc(0, 0));
2201 mkOp1(OP_COS
, TYPE_F32
, dst0
[0], val0
);
2203 mkOp1(OP_SIN
, TYPE_F32
, dst0
[1], val0
);
2206 loadImm(dst0
[2], 0.0f
);
2208 loadImm(dst0
[3], 1.0f
);
2210 case TGSI_OPCODE_EXP
:
2211 src0
= fetchSrc(0, 0);
2212 val0
= mkOp1v(OP_FLOOR
, TYPE_F32
, getSSA(), src0
);
2214 mkOp2(OP_SUB
, TYPE_F32
, dst0
[1], src0
, val0
);
2216 mkOp1(OP_EX2
, TYPE_F32
, dst0
[0], val0
);
2218 mkOp1(OP_EX2
, TYPE_F32
, dst0
[2], src0
);
2220 loadImm(dst0
[3], 1.0f
);
2222 case TGSI_OPCODE_LOG
:
2223 src0
= mkOp1v(OP_ABS
, TYPE_F32
, getSSA(), fetchSrc(0, 0));
2224 val0
= mkOp1v(OP_LG2
, TYPE_F32
, dst0
[2] ? dst0
[2] : getSSA(), src0
);
2225 if (dst0
[0] || dst0
[1])
2226 val1
= mkOp1v(OP_FLOOR
, TYPE_F32
, dst0
[0] ? dst0
[0] : getSSA(), val0
);
2228 mkOp1(OP_EX2
, TYPE_F32
, dst0
[1], val1
);
2229 mkOp1(OP_RCP
, TYPE_F32
, dst0
[1], dst0
[1]);
2230 mkOp2(OP_MUL
, TYPE_F32
, dst0
[1], dst0
[1], src0
);
2233 loadImm(dst0
[3], 1.0f
);
2235 case TGSI_OPCODE_DP2
:
2237 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
)
2238 mkMov(dst0
[c
], val0
);
2240 case TGSI_OPCODE_DP3
:
2242 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
)
2243 mkMov(dst0
[c
], val0
);
2245 case TGSI_OPCODE_DP4
:
2247 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
)
2248 mkMov(dst0
[c
], val0
);
2250 case TGSI_OPCODE_DPH
:
2252 src1
= fetchSrc(1, 3);
2253 mkOp2(OP_ADD
, TYPE_F32
, val0
, val0
, src1
);
2254 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
)
2255 mkMov(dst0
[c
], val0
);
2257 case TGSI_OPCODE_DST
:
2259 loadImm(dst0
[0], 1.0f
);
2261 src0
= fetchSrc(0, 1);
2262 src1
= fetchSrc(1, 1);
2263 mkOp2(OP_MUL
, TYPE_F32
, dst0
[1], src0
, src1
);
2266 mkMov(dst0
[2], fetchSrc(0, 2));
2268 mkMov(dst0
[3], fetchSrc(1, 3));
2270 case TGSI_OPCODE_LRP
:
2271 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
) {
2272 src0
= fetchSrc(0, c
);
2273 src1
= fetchSrc(1, c
);
2274 src2
= fetchSrc(2, c
);
2275 mkOp3(OP_MAD
, TYPE_F32
, dst0
[c
],
2276 mkOp2v(OP_SUB
, TYPE_F32
, getSSA(), src1
, src2
), src0
, src2
);
2279 case TGSI_OPCODE_LIT
:
2282 case TGSI_OPCODE_XPD
:
2283 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
) {
2286 src0
= fetchSrc(1, (c
+ 1) % 3);
2287 src1
= fetchSrc(0, (c
+ 2) % 3);
2288 mkOp2(OP_MUL
, TYPE_F32
, val0
, src0
, src1
);
2289 mkOp1(OP_NEG
, TYPE_F32
, val0
, val0
);
2291 src0
= fetchSrc(0, (c
+ 1) % 3);
2292 src1
= fetchSrc(1, (c
+ 2) % 3);
2293 mkOp3(OP_MAD
, TYPE_F32
, dst0
[c
], src0
, src1
, val0
);
2295 loadImm(dst0
[c
], 1.0f
);
2299 case TGSI_OPCODE_ISSG
:
2300 case TGSI_OPCODE_SSG
:
2301 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
) {
2302 src0
= fetchSrc(0, c
);
2303 val0
= getScratch();
2304 val1
= getScratch();
2305 mkCmp(OP_SET
, CC_GT
, srcTy
, val0
, src0
, zero
);
2306 mkCmp(OP_SET
, CC_LT
, srcTy
, val1
, src0
, zero
);
2307 if (srcTy
== TYPE_F32
)
2308 mkOp2(OP_SUB
, TYPE_F32
, dst0
[c
], val0
, val1
);
2310 mkOp2(OP_SUB
, TYPE_S32
, dst0
[c
], val1
, val0
);
2313 case TGSI_OPCODE_UCMP
:
2314 case TGSI_OPCODE_CMP
:
2315 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
) {
2316 src0
= fetchSrc(0, c
);
2317 src1
= fetchSrc(1, c
);
2318 src2
= fetchSrc(2, c
);
2320 mkMov(dst0
[c
], src1
);
2322 mkCmp(OP_SLCT
, (srcTy
== TYPE_F32
) ? CC_LT
: CC_NE
,
2323 srcTy
, dst0
[c
], src1
, src2
, src0
);
2326 case TGSI_OPCODE_FRC
:
2327 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
) {
2328 src0
= fetchSrc(0, c
);
2329 val0
= getScratch();
2330 mkOp1(OP_FLOOR
, TYPE_F32
, val0
, src0
);
2331 mkOp2(OP_SUB
, TYPE_F32
, dst0
[c
], src0
, val0
);
2334 case TGSI_OPCODE_ROUND
:
2335 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
)
2336 mkCvt(OP_CVT
, TYPE_F32
, dst0
[c
], TYPE_F32
, fetchSrc(0, c
))
2339 case TGSI_OPCODE_CLAMP
:
2340 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
) {
2341 src0
= fetchSrc(0, c
);
2342 src1
= fetchSrc(1, c
);
2343 src2
= fetchSrc(2, c
);
2344 val0
= getScratch();
2345 mkOp2(OP_MIN
, TYPE_F32
, val0
, src0
, src1
);
2346 mkOp2(OP_MAX
, TYPE_F32
, dst0
[c
], val0
, src2
);
2349 case TGSI_OPCODE_SLT
:
2350 case TGSI_OPCODE_SGE
:
2351 case TGSI_OPCODE_SEQ
:
2352 case TGSI_OPCODE_SFL
:
2353 case TGSI_OPCODE_SGT
:
2354 case TGSI_OPCODE_SLE
:
2355 case TGSI_OPCODE_SNE
:
2356 case TGSI_OPCODE_STR
:
2357 case TGSI_OPCODE_ISGE
:
2358 case TGSI_OPCODE_ISLT
:
2359 case TGSI_OPCODE_USEQ
:
2360 case TGSI_OPCODE_USGE
:
2361 case TGSI_OPCODE_USLT
:
2362 case TGSI_OPCODE_USNE
:
2363 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
) {
2364 src0
= fetchSrc(0, c
);
2365 src1
= fetchSrc(1, c
);
2366 mkCmp(op
, tgsi
.getSetCond(), dstTy
, dst0
[c
], src0
, src1
);
2369 case TGSI_OPCODE_KIL
:
2370 val0
= new_LValue(func
, FILE_PREDICATE
);
2371 for (c
= 0; c
< 4; ++c
) {
2372 mkCmp(OP_SET
, CC_LT
, TYPE_F32
, val0
, fetchSrc(0, c
), zero
);
2373 mkOp(OP_DISCARD
, TYPE_NONE
, NULL
)->setPredicate(CC_P
, val0
);
2376 case TGSI_OPCODE_KILP
:
2377 mkOp(OP_DISCARD
, TYPE_NONE
, NULL
);
2379 case TGSI_OPCODE_TEX
:
2380 case TGSI_OPCODE_TXB
:
2381 case TGSI_OPCODE_TXL
:
2382 case TGSI_OPCODE_TXP
:
2384 handleTEX(dst0
, 1, 1, 0x03, 0x0f, 0x00, 0x00);
2386 case TGSI_OPCODE_TXD
:
2387 handleTEX(dst0
, 3, 3, 0x03, 0x0f, 0x10, 0x20);
2389 case TGSI_OPCODE_TEX2
:
2390 handleTEX(dst0
, 2, 2, 0x03, 0x10, 0x00, 0x00);
2392 case TGSI_OPCODE_TXB2
:
2393 case TGSI_OPCODE_TXL2
:
2394 handleTEX(dst0
, 2, 2, 0x10, 0x11, 0x00, 0x00);
2396 case TGSI_OPCODE_SAMPLE
:
2397 case TGSI_OPCODE_SAMPLE_B
:
2398 case TGSI_OPCODE_SAMPLE_D
:
2399 case TGSI_OPCODE_SAMPLE_L
:
2400 case TGSI_OPCODE_SAMPLE_C
:
2401 case TGSI_OPCODE_SAMPLE_C_LZ
:
2402 handleTEX(dst0
, 1, 2, 0x30, 0x30, 0x30, 0x40);
2404 case TGSI_OPCODE_TXF
:
2405 handleTXF(dst0
, 1, 0x03);
2407 case TGSI_OPCODE_SAMPLE_I
:
2408 handleTXF(dst0
, 1, 0x03);
2410 case TGSI_OPCODE_SAMPLE_I_MS
:
2411 handleTXF(dst0
, 1, 0x20);
2413 case TGSI_OPCODE_TXQ
:
2414 case TGSI_OPCODE_SVIEWINFO
:
2415 handleTXQ(dst0
, TXQ_DIMS
);
2417 case TGSI_OPCODE_F2I
:
2418 case TGSI_OPCODE_F2U
:
2419 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
)
2420 mkCvt(OP_CVT
, dstTy
, dst0
[c
], srcTy
, fetchSrc(0, c
))->rnd
= ROUND_Z
;
2422 case TGSI_OPCODE_I2F
:
2423 case TGSI_OPCODE_U2F
:
2424 FOR_EACH_DST_ENABLED_CHANNEL(0, c
, tgsi
)
2425 mkCvt(OP_CVT
, dstTy
, dst0
[c
], srcTy
, fetchSrc(0, c
));
2427 case TGSI_OPCODE_EMIT
:
2428 case TGSI_OPCODE_ENDPRIM
:
2429 // get vertex stream if specified (must be immediate)
2430 src0
= tgsi
.srcCount() ?
2431 mkImm(tgsi
.getSrc(0).getValueU32(0, info
)) : zero
;
2432 mkOp1(op
, TYPE_U32
, NULL
, src0
)->fixed
= 1;
2434 case TGSI_OPCODE_IF
:
2435 case TGSI_OPCODE_UIF
:
2437 BasicBlock
*ifBB
= new BasicBlock(func
);
2439 bb
->cfg
.attach(&ifBB
->cfg
, Graph::Edge::TREE
);
2443 mkFlow(OP_BRA
, NULL
, CC_NOT_P
, fetchSrc(0, 0))->setType(srcTy
);
2445 setPosition(ifBB
, true);
2448 case TGSI_OPCODE_ELSE
:
2450 BasicBlock
*elseBB
= new BasicBlock(func
);
2451 BasicBlock
*forkBB
= reinterpret_cast<BasicBlock
*>(condBBs
.pop().u
.p
);
2453 forkBB
->cfg
.attach(&elseBB
->cfg
, Graph::Edge::TREE
);
2456 forkBB
->getExit()->asFlow()->target
.bb
= elseBB
;
2457 if (!bb
->isTerminated())
2458 mkFlow(OP_BRA
, NULL
, CC_ALWAYS
, NULL
);
2460 setPosition(elseBB
, true);
2463 case TGSI_OPCODE_ENDIF
:
2465 BasicBlock
*convBB
= new BasicBlock(func
);
2466 BasicBlock
*prevBB
= reinterpret_cast<BasicBlock
*>(condBBs
.pop().u
.p
);
2467 BasicBlock
*forkBB
= reinterpret_cast<BasicBlock
*>(joinBBs
.pop().u
.p
);
2469 if (!bb
->isTerminated()) {
2470 // we only want join if none of the clauses ended with CONT/BREAK/RET
2471 if (prevBB
->getExit()->op
== OP_BRA
&& joinBBs
.getSize() < 6)
2472 insertConvergenceOps(convBB
, forkBB
);
2473 mkFlow(OP_BRA
, convBB
, CC_ALWAYS
, NULL
);
2474 bb
->cfg
.attach(&convBB
->cfg
, Graph::Edge::FORWARD
);
2477 if (prevBB
->getExit()->op
== OP_BRA
) {
2478 prevBB
->cfg
.attach(&convBB
->cfg
, Graph::Edge::FORWARD
);
2479 prevBB
->getExit()->asFlow()->target
.bb
= convBB
;
2481 setPosition(convBB
, true);
2484 case TGSI_OPCODE_BGNLOOP
:
2486 BasicBlock
*lbgnBB
= new BasicBlock(func
);
2487 BasicBlock
*lbrkBB
= new BasicBlock(func
);
2489 loopBBs
.push(lbgnBB
);
2490 breakBBs
.push(lbrkBB
);
2491 if (loopBBs
.getSize() > func
->loopNestingBound
)
2492 func
->loopNestingBound
++;
2494 mkFlow(OP_PREBREAK
, lbrkBB
, CC_ALWAYS
, NULL
);
2496 bb
->cfg
.attach(&lbgnBB
->cfg
, Graph::Edge::TREE
);
2497 setPosition(lbgnBB
, true);
2498 mkFlow(OP_PRECONT
, lbgnBB
, CC_ALWAYS
, NULL
);
2501 case TGSI_OPCODE_ENDLOOP
:
2503 BasicBlock
*loopBB
= reinterpret_cast<BasicBlock
*>(loopBBs
.pop().u
.p
);
2505 if (!bb
->isTerminated()) {
2506 mkFlow(OP_CONT
, loopBB
, CC_ALWAYS
, NULL
);
2507 bb
->cfg
.attach(&loopBB
->cfg
, Graph::Edge::BACK
);
2509 setPosition(reinterpret_cast<BasicBlock
*>(breakBBs
.pop().u
.p
), true);
2512 case TGSI_OPCODE_BRK
:
2514 if (bb
->isTerminated())
2516 BasicBlock
*brkBB
= reinterpret_cast<BasicBlock
*>(breakBBs
.peek().u
.p
);
2517 mkFlow(OP_BREAK
, brkBB
, CC_ALWAYS
, NULL
);
2518 bb
->cfg
.attach(&brkBB
->cfg
, Graph::Edge::CROSS
);
2521 case TGSI_OPCODE_CONT
:
2523 if (bb
->isTerminated())
2525 BasicBlock
*contBB
= reinterpret_cast<BasicBlock
*>(loopBBs
.peek().u
.p
);
2526 mkFlow(OP_CONT
, contBB
, CC_ALWAYS
, NULL
);
2527 contBB
->explicitCont
= true;
2528 bb
->cfg
.attach(&contBB
->cfg
, Graph::Edge::BACK
);
2531 case TGSI_OPCODE_BGNSUB
:
2533 Subroutine
*s
= getSubroutine(ip
);
2534 BasicBlock
*entry
= new BasicBlock(s
->f
);
2535 BasicBlock
*leave
= new BasicBlock(s
->f
);
2537 // multiple entrypoints possible, keep the graph connected
2538 if (prog
->getType() == Program::TYPE_COMPUTE
)
2539 prog
->main
->call
.attach(&s
->f
->call
, Graph::Edge::TREE
);
2542 s
->f
->setEntry(entry
);
2543 s
->f
->setExit(leave
);
2544 setPosition(entry
, true);
2547 case TGSI_OPCODE_ENDSUB
:
2549 sub
.cur
= getSubroutine(prog
->main
);
2550 setPosition(BasicBlock::get(sub
.cur
->f
->cfg
.getRoot()), true);
2553 case TGSI_OPCODE_CAL
:
2555 Subroutine
*s
= getSubroutine(tgsi
.getLabel());
2556 mkFlow(OP_CALL
, s
->f
, CC_ALWAYS
, NULL
);
2557 func
->call
.attach(&s
->f
->call
, Graph::Edge::TREE
);
2560 case TGSI_OPCODE_RET
:
2562 if (bb
->isTerminated())
2564 BasicBlock
*leave
= BasicBlock::get(func
->cfgExit
);
2566 if (!isEndOfSubroutine(ip
+ 1)) {
2567 // insert a PRERET at the entry if this is an early return
2568 // (only needed for sharing code in the epilogue)
2569 BasicBlock
*pos
= getBB();
2570 setPosition(BasicBlock::get(func
->cfg
.getRoot()), false);
2571 mkFlow(OP_PRERET
, leave
, CC_ALWAYS
, NULL
)->fixed
= 1;
2572 setPosition(pos
, true);
2574 mkFlow(OP_RET
, NULL
, CC_ALWAYS
, NULL
)->fixed
= 1;
2575 bb
->cfg
.attach(&leave
->cfg
, Graph::Edge::CROSS
);
2578 case TGSI_OPCODE_END
:
2580 // attach and generate epilogue code
2581 BasicBlock
*epilogue
= BasicBlock::get(func
->cfgExit
);
2582 bb
->cfg
.attach(&epilogue
->cfg
, Graph::Edge::TREE
);
2583 setPosition(epilogue
, true);
2584 if (prog
->getType() == Program::TYPE_FRAGMENT
)
2586 if (info
->io
.genUserClip
> 0)
2587 handleUserClipPlanes();
2588 mkOp(OP_EXIT
, TYPE_NONE
, NULL
)->terminator
= 1;
2591 case TGSI_OPCODE_SWITCH
:
2592 case TGSI_OPCODE_CASE
:
2593 ERROR("switch/case opcode encountered, should have been lowered\n");
2596 case TGSI_OPCODE_LOAD
:
2599 case TGSI_OPCODE_STORE
:
2602 case TGSI_OPCODE_BARRIER
:
2603 geni
= mkOp2(OP_BAR
, TYPE_U32
, NULL
, mkImm(0), mkImm(0));
2605 geni
->subOp
= NV50_IR_SUBOP_BAR_SYNC
;
2607 case TGSI_OPCODE_MFENCE
:
2608 case TGSI_OPCODE_LFENCE
:
2609 case TGSI_OPCODE_SFENCE
:
2610 geni
= mkOp(OP_MEMBAR
, TYPE_NONE
, NULL
);
2612 geni
->subOp
= tgsi::opcodeToSubOp(tgsi
.getOpcode());
2614 case TGSI_OPCODE_ATOMUADD
:
2615 case TGSI_OPCODE_ATOMXCHG
:
2616 case TGSI_OPCODE_ATOMCAS
:
2617 case TGSI_OPCODE_ATOMAND
:
2618 case TGSI_OPCODE_ATOMOR
:
2619 case TGSI_OPCODE_ATOMXOR
:
2620 case TGSI_OPCODE_ATOMUMIN
:
2621 case TGSI_OPCODE_ATOMIMIN
:
2622 case TGSI_OPCODE_ATOMUMAX
:
2623 case TGSI_OPCODE_ATOMIMAX
:
2624 handleATOM(dst0
, dstTy
, tgsi::opcodeToSubOp(tgsi
.getOpcode()));
2627 ERROR("unhandled TGSI opcode: %u\n", tgsi
.getOpcode());
2632 if (tgsi
.dstCount()) {
2633 for (c
= 0; c
< 4; ++c
) {
2636 if (dst0
[c
] != rDst0
[c
])
2637 mkMov(rDst0
[c
], dst0
[c
]);
2638 storeDst(0, c
, rDst0
[c
]);
2647 Converter::handleUserClipPlanes()
2652 for (c
= 0; c
< 4; ++c
) {
2653 for (i
= 0; i
< info
->io
.genUserClip
; ++i
) {
2654 Symbol
*sym
= mkSymbol(FILE_MEMORY_CONST
, info
->io
.ucpCBSlot
,
2655 TYPE_F32
, info
->io
.ucpBase
+ i
* 16 + c
* 4);
2656 Value
*ucp
= mkLoadv(TYPE_F32
, sym
, NULL
);
2658 res
[i
] = mkOp2v(OP_MUL
, TYPE_F32
, getScratch(), clipVtx
[c
], ucp
);
2660 mkOp3(OP_MAD
, TYPE_F32
, res
[i
], clipVtx
[c
], ucp
, res
[i
]);
2664 const int first
= info
->numOutputs
- (info
->io
.genUserClip
+ 3) / 4;
2666 for (i
= 0; i
< info
->io
.genUserClip
; ++i
) {
2670 mkSymbol(FILE_SHADER_OUTPUT
, 0, TYPE_F32
, info
->out
[n
].slot
[c
] * 4);
2671 mkStore(OP_EXPORT
, TYPE_F32
, sym
, NULL
, res
[i
]);
2676 Converter::exportOutputs()
2678 for (unsigned int i
= 0; i
< info
->numOutputs
; ++i
) {
2679 for (unsigned int c
= 0; c
< 4; ++c
) {
2680 if (!oData
.exists(sub
.cur
->values
, i
, c
))
2682 Symbol
*sym
= mkSymbol(FILE_SHADER_OUTPUT
, 0, TYPE_F32
,
2683 info
->out
[i
].slot
[c
] * 4);
2684 Value
*val
= oData
.load(sub
.cur
->values
, i
, c
, NULL
);
2686 mkStore(OP_EXPORT
, TYPE_F32
, sym
, NULL
, val
);
2691 Converter::Converter(Program
*ir
, const tgsi::Source
*code
) : BuildUtil(ir
),
2694 tData(this), aData(this), pData(this), oData(this)
2698 const DataFile tFile
= code
->mainTempsInLMem
? FILE_MEMORY_LOCAL
: FILE_GPR
;
2700 const unsigned tSize
= code
->fileSize(TGSI_FILE_TEMPORARY
);
2701 const unsigned pSize
= code
->fileSize(TGSI_FILE_PREDICATE
);
2702 const unsigned aSize
= code
->fileSize(TGSI_FILE_ADDRESS
);
2703 const unsigned oSize
= code
->fileSize(TGSI_FILE_OUTPUT
);
2705 tData
.setup(TGSI_FILE_TEMPORARY
, 0, 0, tSize
, 4, 4, tFile
, 0);
2706 pData
.setup(TGSI_FILE_PREDICATE
, 0, 0, pSize
, 4, 4, FILE_PREDICATE
, 0);
2707 aData
.setup(TGSI_FILE_ADDRESS
, 0, 0, aSize
, 4, 4, FILE_ADDRESS
, 0);
2708 oData
.setup(TGSI_FILE_OUTPUT
, 0, 0, oSize
, 4, 4, FILE_GPR
, 0);
2710 zero
= mkImm((uint32_t)0);
2715 Converter::~Converter()
2719 inline const Converter::Location
*
2720 Converter::BindArgumentsPass::getValueLocation(Subroutine
*s
, Value
*v
)
2722 ValueMap::l_iterator it
= s
->values
.l
.find(v
);
2723 return it
== s
->values
.l
.end() ? NULL
: &it
->second
;
2726 template<typename T
> inline void
2727 Converter::BindArgumentsPass::updateCallArgs(
2728 Instruction
*i
, void (Instruction::*setArg
)(int, Value
*),
2729 T (Function::*proto
))
2731 Function
*g
= i
->asFlow()->target
.fn
;
2732 Subroutine
*subg
= conv
.getSubroutine(g
);
2734 for (unsigned a
= 0; a
< (g
->*proto
).size(); ++a
) {
2735 Value
*v
= (g
->*proto
)[a
].get();
2736 const Converter::Location
&l
= *getValueLocation(subg
, v
);
2737 Converter::DataArray
*array
= conv
.getArrayForFile(l
.array
, l
.arrayIdx
);
2739 (i
->*setArg
)(a
, array
->acquire(sub
->values
, l
.i
, l
.c
));
2743 template<typename T
> inline void
2744 Converter::BindArgumentsPass::updatePrototype(
2745 BitSet
*set
, void (Function::*updateSet
)(), T (Function::*proto
))
2747 (func
->*updateSet
)();
2749 for (unsigned i
= 0; i
< set
->getSize(); ++i
) {
2750 Value
*v
= func
->getLValue(i
);
2751 const Converter::Location
*l
= getValueLocation(sub
, v
);
2753 // only include values with a matching TGSI register
2754 if (set
->test(i
) && l
&& !conv
.code
->locals
.count(*l
))
2755 (func
->*proto
).push_back(v
);
2760 Converter::BindArgumentsPass::visit(Function
*f
)
2762 sub
= conv
.getSubroutine(f
);
2764 for (ArrayList::Iterator bi
= f
->allBBlocks
.iterator();
2765 !bi
.end(); bi
.next()) {
2766 for (Instruction
*i
= BasicBlock::get(bi
)->getFirst();
2768 if (i
->op
== OP_CALL
&& !i
->asFlow()->builtin
) {
2769 updateCallArgs(i
, &Instruction::setSrc
, &Function::ins
);
2770 updateCallArgs(i
, &Instruction::setDef
, &Function::outs
);
2775 if (func
== prog
->main
&& prog
->getType() != Program::TYPE_COMPUTE
)
2777 updatePrototype(&BasicBlock::get(f
->cfg
.getRoot())->liveSet
,
2778 &Function::buildLiveSets
, &Function::ins
);
2779 updatePrototype(&BasicBlock::get(f
->cfgExit
)->defSet
,
2780 &Function::buildDefSets
, &Function::outs
);
2788 BasicBlock
*entry
= new BasicBlock(prog
->main
);
2789 BasicBlock
*leave
= new BasicBlock(prog
->main
);
2791 prog
->main
->setEntry(entry
);
2792 prog
->main
->setExit(leave
);
2794 setPosition(entry
, true);
2795 sub
.cur
= getSubroutine(prog
->main
);
2797 if (info
->io
.genUserClip
> 0) {
2798 for (int c
= 0; c
< 4; ++c
)
2799 clipVtx
[c
] = getScratch();
2802 if (prog
->getType() == Program::TYPE_FRAGMENT
) {
2803 Symbol
*sv
= mkSysVal(SV_POSITION
, 3);
2804 fragCoord
[3] = mkOp1v(OP_RDSV
, TYPE_F32
, getSSA(), sv
);
2805 mkOp1(OP_RCP
, TYPE_F32
, fragCoord
[3], fragCoord
[3]);
2808 for (ip
= 0; ip
< code
->scan
.num_instructions
; ++ip
) {
2809 if (!handleInstruction(&code
->insns
[ip
]))
2813 if (!BindArgumentsPass(*this).run(prog
))
2819 } // unnamed namespace
2824 Program::makeFromTGSI(struct nv50_ir_prog_info
*info
)
2826 tgsi::Source
src(info
);
2827 if (!src
.scanSource())
2829 tlsSize
= info
->bin
.tlsSpace
;
2831 Converter
builder(this, &src
);
2832 return builder
.run();
2835 } // namespace nv50_ir