1 #include "tgsitollvm.h"
7 #include "instructions.h"
8 #include "storagesoa.h"
9 #include "instructionssoa.h"
11 #include "pipe/p_shader_tokens.h"
13 #include "tgsi/tgsi_parse.h"
14 #include "tgsi/tgsi_exec.h"
15 #include "tgsi/tgsi_util.h"
16 #include "tgsi/tgsi_build.h"
17 #include "tgsi/tgsi_dump.h"
20 #include <llvm/Module.h>
21 #include <llvm/CallingConv.h>
22 #include <llvm/Constants.h>
23 #include <llvm/DerivedTypes.h>
24 #include <llvm/Instructions.h>
25 #include <llvm/ModuleProvider.h>
26 #include <llvm/Pass.h>
27 #include <llvm/PassManager.h>
28 #include <llvm/ParameterAttributes.h>
29 #include <llvm/Support/PatternMatch.h>
30 #include <llvm/ExecutionEngine/JIT.h>
31 #include <llvm/ExecutionEngine/Interpreter.h>
32 #include <llvm/ExecutionEngine/GenericValue.h>
33 #include <llvm/Support/MemoryBuffer.h>
34 #include <llvm/LinkAllPasses.h>
35 #include <llvm/Analysis/Verifier.h>
36 #include <llvm/Analysis/LoopPass.h>
37 #include <llvm/Target/TargetData.h>
38 #include <llvm/Bitcode/ReaderWriter.h>
39 #include <llvm/Transforms/Utils/Cloning.h>
48 static inline FunctionType
*vertexShaderFunctionType()
50 //Function takes three arguments,
51 // the calling code has to make sure the types it will
52 // pass are castable to the following:
53 // [4 x <4 x float>] inputs,
54 // [4 x <4 x float>] output,
55 // [4 x [4 x float]] consts,
56 // [4 x <4 x float>] temps
58 std::vector
<const Type
*> funcArgs
;
59 VectorType
*vectorType
= VectorType::get(Type::FloatTy
, 4);
60 ArrayType
*vectorArray
= ArrayType::get(vectorType
, 4);
61 PointerType
*vectorArrayPtr
= PointerType::get(vectorArray
, 0);
63 ArrayType
*floatArray
= ArrayType::get(Type::FloatTy
, 4);
64 ArrayType
*constsArray
= ArrayType::get(floatArray
, 4);
65 PointerType
*constsArrayPtr
= PointerType::get(constsArray
, 0);
67 funcArgs
.push_back(vectorArrayPtr
);//inputs
68 funcArgs
.push_back(vectorArrayPtr
);//output
69 funcArgs
.push_back(constsArrayPtr
);//consts
70 funcArgs
.push_back(vectorArrayPtr
);//temps
72 FunctionType
*functionType
= FunctionType::get(
73 /*Result=*/Type::VoidTy
,
81 add_interpolator(struct gallivm_ir
*ir
,
82 struct gallivm_interpolate
*interp
)
84 ir
->interpolators
[ir
->num_interp
] = *interp
;
89 translate_declaration(struct gallivm_ir
*prog
,
92 struct tgsi_full_declaration
*decl
,
93 struct tgsi_full_declaration
*fd
)
95 if (decl
->Declaration
.File
== TGSI_FILE_INPUT
) {
96 unsigned first
, last
, mask
;
99 first
= decl
->DeclarationRange
.First
;
100 last
= decl
->DeclarationRange
.Last
;
101 mask
= decl
->Declaration
.UsageMask
;
103 /* Do not touch WPOS.xy */
105 mask
&= ~TGSI_WRITEMASK_XY
;
106 if (mask
== TGSI_WRITEMASK_NONE
) {
114 interp_method
= decl
->Declaration
.Interpolate
;
116 if (mask
== TGSI_WRITEMASK_XYZW
) {
119 for (i
= first
; i
<= last
; i
++) {
120 for (j
= 0; j
< NUM_CHANNELS
; j
++) {
121 //interp( mach, i, j );
122 struct gallivm_interpolate interp
;
123 interp
.type
= interp_method
;
126 add_interpolator(prog
, &interp
);
131 for( j
= 0; j
< NUM_CHANNELS
; j
++ ) {
132 if( mask
& (1 << j
) ) {
133 for( i
= first
; i
<= last
; i
++ ) {
134 struct gallivm_interpolate interp
;
135 interp
.type
= interp_method
;
138 add_interpolator(prog
, &interp
);
147 translate_declarationir(struct gallivm_ir
*,
150 struct tgsi_full_declaration
*decl
,
151 struct tgsi_full_declaration
*)
153 if (decl
->Declaration
.File
== TGSI_FILE_ADDRESS
) {
154 int idx
= decl
->DeclarationRange
.First
;
155 storage
->addAddress(idx
);
160 translate_immediate(Storage
*storage
,
161 struct tgsi_full_immediate
*imm
)
165 for (i
= 0; i
< imm
->Immediate
.Size
- 1; ++i
) {
166 switch (imm
->Immediate
.DataType
) {
167 case TGSI_IMM_FLOAT32
:
168 vec
[i
] = imm
->u
.ImmediateFloat32
[i
].Float
;
174 storage
->addImmediate(vec
);
179 translate_immediateir(StorageSoa
*storage
,
180 struct tgsi_full_immediate
*imm
)
184 for (i
= 0; i
< imm
->Immediate
.Size
- 1; ++i
) {
185 switch (imm
->Immediate
.DataType
) {
186 case TGSI_IMM_FLOAT32
:
187 vec
[i
] = imm
->u
.ImmediateFloat32
[i
].Float
;
193 storage
->addImmediate(vec
);
197 swizzleInt(struct tgsi_full_src_register
*src
)
202 for (int k
= 0; k
< 4; ++k
) {
203 swizzle
+= tgsi_util_get_full_src_register_extswizzle(src
, k
) * start
;
209 static inline llvm::Value
*
210 swizzleVector(llvm::Value
*val
, struct tgsi_full_src_register
*src
,
213 int swizzle
= swizzleInt(src
);
215 if (gallivm_is_swizzle(swizzle
)) {
216 /*fprintf(stderr, "XXXXXXXX swizzle = %d\n", swizzle);*/
217 val
= storage
->shuffleVector(val
, swizzle
);
223 translate_instruction(llvm::Module
*module
,
226 struct tgsi_full_instruction
*inst
,
227 struct tgsi_full_instruction
*fi
,
230 llvm::Value
*inputs
[4];
236 for (int i
= 0; i
< inst
->Instruction
.NumSrcRegs
; ++i
) {
237 struct tgsi_full_src_register
*src
= &inst
->FullSrcRegisters
[i
];
238 llvm::Value
*val
= 0;
239 llvm::Value
*indIdx
= 0;
241 if (src
->SrcRegister
.Indirect
) {
242 indIdx
= storage
->addrElement(src
->SrcRegisterInd
.Index
);
243 indIdx
= storage
->extractIndex(indIdx
);
245 if (src
->SrcRegister
.File
== TGSI_FILE_CONSTANT
) {
246 val
= storage
->constElement(src
->SrcRegister
.Index
, indIdx
);
247 } else if (src
->SrcRegister
.File
== TGSI_FILE_INPUT
) {
248 val
= storage
->inputElement(src
->SrcRegister
.Index
, indIdx
);
249 } else if (src
->SrcRegister
.File
== TGSI_FILE_TEMPORARY
) {
250 val
= storage
->tempElement(src
->SrcRegister
.Index
);
251 } else if (src
->SrcRegister
.File
== TGSI_FILE_OUTPUT
) {
252 val
= storage
->outputElement(src
->SrcRegister
.Index
, indIdx
);
253 } else if (src
->SrcRegister
.File
== TGSI_FILE_IMMEDIATE
) {
254 val
= storage
->immediateElement(src
->SrcRegister
.Index
);
256 fprintf(stderr
, "ERROR: not supported llvm source %d\n", src
->SrcRegister
.File
);
260 inputs
[i
] = swizzleVector(val
, src
, storage
);
264 instr->printVector(inputs[0]);
266 instr->printVector(inputs[1]);*/
267 llvm::Value
*out
= 0;
268 switch (inst
->Instruction
.Opcode
) {
269 case TGSI_OPCODE_ARL
: {
270 out
= instr
->arl(inputs
[0]);
273 case TGSI_OPCODE_MOV
: {
277 case TGSI_OPCODE_LIT
: {
278 out
= instr
->lit(inputs
[0]);
281 case TGSI_OPCODE_RCP
: {
282 out
= instr
->rcp(inputs
[0]);
285 case TGSI_OPCODE_RSQ
: {
286 out
= instr
->rsq(inputs
[0]);
289 case TGSI_OPCODE_EXP
: {
290 out
= instr
->exp(inputs
[0]);
293 case TGSI_OPCODE_LOG
: {
294 out
= instr
->log(inputs
[0]);
297 case TGSI_OPCODE_MUL
: {
298 out
= instr
->mul(inputs
[0], inputs
[1]);
301 case TGSI_OPCODE_ADD
: {
302 out
= instr
->add(inputs
[0], inputs
[1]);
305 case TGSI_OPCODE_DP3
: {
306 out
= instr
->dp3(inputs
[0], inputs
[1]);
309 case TGSI_OPCODE_DP4
: {
310 out
= instr
->dp4(inputs
[0], inputs
[1]);
313 case TGSI_OPCODE_DST
: {
314 out
= instr
->dst(inputs
[0], inputs
[1]);
317 case TGSI_OPCODE_MIN
: {
318 out
= instr
->min(inputs
[0], inputs
[1]);
321 case TGSI_OPCODE_MAX
: {
322 out
= instr
->max(inputs
[0], inputs
[1]);
325 case TGSI_OPCODE_SLT
: {
326 out
= instr
->slt(inputs
[0], inputs
[1]);
329 case TGSI_OPCODE_SGE
: {
330 out
= instr
->sge(inputs
[0], inputs
[1]);
333 case TGSI_OPCODE_MAD
: {
334 out
= instr
->madd(inputs
[0], inputs
[1], inputs
[2]);
337 case TGSI_OPCODE_SUB
: {
338 out
= instr
->sub(inputs
[0], inputs
[1]);
341 case TGSI_OPCODE_LERP
: {
342 out
= instr
->lerp(inputs
[0], inputs
[1], inputs
[2]);
345 case TGSI_OPCODE_CND
: {
346 out
= instr
->cnd(inputs
[0], inputs
[1], inputs
[2]);
349 case TGSI_OPCODE_CND0
: {
350 out
= instr
->cnd0(inputs
[0], inputs
[1], inputs
[2]);
353 case TGSI_OPCODE_DOT2ADD
: {
354 out
= instr
->dot2add(inputs
[0], inputs
[1], inputs
[2]);
357 case TGSI_OPCODE_INDEX
:
359 case TGSI_OPCODE_NEGATE
: {
360 out
= instr
->neg(inputs
[0]);
363 case TGSI_OPCODE_FRAC
: {
364 out
= instr
->frc(inputs
[0]);
367 case TGSI_OPCODE_CLAMP
: {
368 out
= instr
->clamp(inputs
[0]);
371 case TGSI_OPCODE_FLOOR
: {
372 out
= instr
->floor(inputs
[0]);
375 case TGSI_OPCODE_ROUND
:
377 case TGSI_OPCODE_EXPBASE2
: {
378 out
= instr
->ex2(inputs
[0]);
381 case TGSI_OPCODE_LOGBASE2
: {
382 out
= instr
->lg2(inputs
[0]);
385 case TGSI_OPCODE_POWER
: {
386 out
= instr
->pow(inputs
[0], inputs
[1]);
389 case TGSI_OPCODE_CROSSPRODUCT
: {
390 out
= instr
->cross(inputs
[0], inputs
[1]);
393 case TGSI_OPCODE_MULTIPLYMATRIX
:
395 case TGSI_OPCODE_ABS
: {
396 out
= instr
->abs(inputs
[0]);
399 case TGSI_OPCODE_RCC
:
401 case TGSI_OPCODE_DPH
: {
402 out
= instr
->dph(inputs
[0], inputs
[1]);
405 case TGSI_OPCODE_COS
: {
406 out
= instr
->cos(inputs
[0]);
409 case TGSI_OPCODE_DDX
: {
410 out
= instr
->ddx(inputs
[0]);
413 case TGSI_OPCODE_DDY
: {
414 out
= instr
->ddy(inputs
[0]);
417 case TGSI_OPCODE_KILP
:
419 case TGSI_OPCODE_PK2H
:
421 case TGSI_OPCODE_PK2US
:
423 case TGSI_OPCODE_PK4B
:
425 case TGSI_OPCODE_PK4UB
:
427 case TGSI_OPCODE_RFL
:
429 case TGSI_OPCODE_SEQ
: {
430 out
= instr
->seq(inputs
[0], inputs
[1]);
433 case TGSI_OPCODE_SFL
: {
434 out
= instr
->sfl(inputs
[0], inputs
[1]);
437 case TGSI_OPCODE_SGT
: {
438 out
= instr
->sgt(inputs
[0], inputs
[1]);
441 case TGSI_OPCODE_SIN
: {
442 out
= instr
->sin(inputs
[0]);
445 case TGSI_OPCODE_SLE
: {
446 out
= instr
->sle(inputs
[0], inputs
[1]);
449 case TGSI_OPCODE_SNE
: {
450 out
= instr
->sne(inputs
[0], inputs
[1]);
453 case TGSI_OPCODE_STR
: {
454 out
= instr
->str(inputs
[0], inputs
[1]);
457 case TGSI_OPCODE_TEX
:
459 case TGSI_OPCODE_TXD
:
461 case TGSI_OPCODE_UP2H
:
463 case TGSI_OPCODE_UP2US
:
465 case TGSI_OPCODE_UP4B
:
467 case TGSI_OPCODE_UP4UB
:
469 case TGSI_OPCODE_X2D
: {
470 out
= instr
->x2d(inputs
[0], inputs
[1], inputs
[2]);
473 case TGSI_OPCODE_ARA
:
475 case TGSI_OPCODE_ARR
:
477 case TGSI_OPCODE_BRA
:
479 case TGSI_OPCODE_CAL
: {
480 instr
->cal(inst
->InstructionExtLabel
.Label
, storage
->inputPtr());
484 case TGSI_OPCODE_RET
: {
489 case TGSI_OPCODE_SSG
:
491 case TGSI_OPCODE_CMP
: {
492 out
= instr
->cmp(inputs
[0], inputs
[1], inputs
[2]);
495 case TGSI_OPCODE_SCS
: {
496 out
= instr
->scs(inputs
[0]);
499 case TGSI_OPCODE_TXB
:
501 case TGSI_OPCODE_NRM4
:
502 case TGSI_OPCODE_NRM
: {
503 out
= instr
->nrm(inputs
[0]);
506 case TGSI_OPCODE_DIV
: {
507 out
= instr
->div(inputs
[0], inputs
[1]);
510 case TGSI_OPCODE_DP2
: {
511 out
= instr
->dp2(inputs
[0], inputs
[1]);
514 case TGSI_OPCODE_TXL
:
516 case TGSI_OPCODE_BRK
: {
521 case TGSI_OPCODE_IF
: {
522 instr
->ifop(inputs
[0]);
523 storage
->setCurrentBlock(instr
->currentBlock());
524 return; //just update the state
527 case TGSI_OPCODE_LOOP
:
529 case TGSI_OPCODE_REP
:
531 case TGSI_OPCODE_ELSE
: {
533 storage
->setCurrentBlock(instr
->currentBlock());
534 return; //only state update
537 case TGSI_OPCODE_ENDIF
: {
539 storage
->setCurrentBlock(instr
->currentBlock());
540 return; //just update the state
543 case TGSI_OPCODE_ENDLOOP
:
545 case TGSI_OPCODE_ENDREP
:
547 case TGSI_OPCODE_PUSHA
:
549 case TGSI_OPCODE_POPA
:
551 case TGSI_OPCODE_CEIL
:
553 case TGSI_OPCODE_I2F
:
555 case TGSI_OPCODE_NOT
:
557 case TGSI_OPCODE_TRUNC
: {
558 out
= instr
->trunc(inputs
[0]);
561 case TGSI_OPCODE_SHL
:
563 case TGSI_OPCODE_SHR
:
565 case TGSI_OPCODE_AND
:
569 case TGSI_OPCODE_MOD
:
571 case TGSI_OPCODE_XOR
:
573 case TGSI_OPCODE_SAD
:
575 case TGSI_OPCODE_TXF
:
577 case TGSI_OPCODE_TXQ
:
579 case TGSI_OPCODE_CONT
:
581 case TGSI_OPCODE_EMIT
:
583 case TGSI_OPCODE_ENDPRIM
:
585 case TGSI_OPCODE_BGNLOOP2
: {
587 storage
->setCurrentBlock(instr
->currentBlock());
591 case TGSI_OPCODE_BGNSUB
: {
592 instr
->bgnSub(instno
);
593 storage
->setCurrentBlock(instr
->currentBlock());
594 storage
->pushTemps();
598 case TGSI_OPCODE_ENDLOOP2
: {
600 storage
->setCurrentBlock(instr
->currentBlock());
604 case TGSI_OPCODE_ENDSUB
: {
606 storage
->setCurrentBlock(instr
->currentBlock());
607 storage
->popArguments();
612 case TGSI_OPCODE_NOISE1
:
614 case TGSI_OPCODE_NOISE2
:
616 case TGSI_OPCODE_NOISE3
:
618 case TGSI_OPCODE_NOISE4
:
620 case TGSI_OPCODE_NOP
:
622 case TGSI_OPCODE_M4X3
:
624 case TGSI_OPCODE_M3X4
:
626 case TGSI_OPCODE_M3X3
:
628 case TGSI_OPCODE_M3X2
:
630 case TGSI_OPCODE_CALLNZ
:
632 case TGSI_OPCODE_IFC
:
634 case TGSI_OPCODE_BREAKC
:
636 case TGSI_OPCODE_KIL
: {
637 out
= instr
->kil(inputs
[0]);
638 storage
->setKilElement(out
);
642 case TGSI_OPCODE_END
:
647 fprintf(stderr
, "ERROR: Unknown opcode %d\n",
648 inst
->Instruction
.Opcode
);
654 fprintf(stderr
, "ERROR: unsupported opcode %d\n",
655 inst
->Instruction
.Opcode
);
656 assert(!"Unsupported opcode");
659 /* # not sure if we need this */
660 switch( inst
->Instruction
.Saturate
) {
663 case TGSI_SAT_ZERO_ONE
:
666 case TGSI_SAT_MINUS_PLUS_ONE
:
667 /*TXT( "_SAT[-1,1]" );*/
674 for (int i
= 0; i
< inst
->Instruction
.NumDstRegs
; ++i
) {
675 struct tgsi_full_dst_register
*dst
= &inst
->FullDstRegisters
[i
];
677 if (dst
->DstRegister
.File
== TGSI_FILE_OUTPUT
) {
678 storage
->setOutputElement(dst
->DstRegister
.Index
, out
, dst
->DstRegister
.WriteMask
);
679 } else if (dst
->DstRegister
.File
== TGSI_FILE_TEMPORARY
) {
680 storage
->setTempElement(dst
->DstRegister
.Index
, out
, dst
->DstRegister
.WriteMask
);
681 } else if (dst
->DstRegister
.File
== TGSI_FILE_ADDRESS
) {
682 storage
->setAddrElement(dst
->DstRegister
.Index
, out
, dst
->DstRegister
.WriteMask
);
684 fprintf(stderr
, "ERROR: unsupported LLVM destination!");
685 assert(!"wrong destination");
692 translate_instructionir(llvm::Module
*module
,
694 InstructionsSoa
*instr
,
695 struct tgsi_full_instruction
*inst
,
696 struct tgsi_full_instruction
*fi
,
699 std::vector
< std::vector
<llvm::Value
*> > inputs(inst
->Instruction
.NumSrcRegs
);
701 for (int i
= 0; i
< inst
->Instruction
.NumSrcRegs
; ++i
) {
702 struct tgsi_full_src_register
*src
= &inst
->FullSrcRegisters
[i
];
703 std::vector
<llvm::Value
*> val
;
704 llvm::Value
*indIdx
= 0;
705 int swizzle
= swizzleInt(src
);
707 if (src
->SrcRegister
.Indirect
) {
708 indIdx
= storage
->addrElement(src
->SrcRegisterInd
.Index
);
711 val
= storage
->load((enum tgsi_file_type
)src
->SrcRegister
.File
,
712 src
->SrcRegister
.Index
, swizzle
, indIdx
);
717 std::vector
<llvm::Value
*> out(4);
718 switch (inst
->Instruction
.Opcode
) {
719 case TGSI_OPCODE_ARL
: {
720 out
= instr
->arl(inputs
[0]);
723 case TGSI_OPCODE_MOV
: {
727 case TGSI_OPCODE_LIT
: {
728 out
= instr
->lit(inputs
[0]);
731 case TGSI_OPCODE_RCP
: {
734 case TGSI_OPCODE_RSQ
: {
735 out
= instr
->rsq(inputs
[0]);
738 case TGSI_OPCODE_EXP
:
740 case TGSI_OPCODE_LOG
:
742 case TGSI_OPCODE_MUL
: {
743 out
= instr
->mul(inputs
[0], inputs
[1]);
746 case TGSI_OPCODE_ADD
: {
747 out
= instr
->add(inputs
[0], inputs
[1]);
750 case TGSI_OPCODE_DP3
: {
751 out
= instr
->dp3(inputs
[0], inputs
[1]);
754 case TGSI_OPCODE_DP4
: {
755 out
= instr
->dp4(inputs
[0], inputs
[1]);
758 case TGSI_OPCODE_DST
: {
761 case TGSI_OPCODE_MIN
: {
762 out
= instr
->min(inputs
[0], inputs
[1]);
765 case TGSI_OPCODE_MAX
: {
766 out
= instr
->max(inputs
[0], inputs
[1]);
769 case TGSI_OPCODE_SLT
: {
770 out
= instr
->slt(inputs
[0], inputs
[1]);
773 case TGSI_OPCODE_SGE
: {
776 case TGSI_OPCODE_MAD
: {
777 out
= instr
->madd(inputs
[0], inputs
[1], inputs
[2]);
780 case TGSI_OPCODE_SUB
: {
781 out
= instr
->sub(inputs
[0], inputs
[1]);
784 case TGSI_OPCODE_LERP
: {
787 case TGSI_OPCODE_CND
:
789 case TGSI_OPCODE_CND0
:
791 case TGSI_OPCODE_DOT2ADD
:
793 case TGSI_OPCODE_INDEX
:
795 case TGSI_OPCODE_NEGATE
:
797 case TGSI_OPCODE_FRAC
: {
800 case TGSI_OPCODE_CLAMP
:
802 case TGSI_OPCODE_FLOOR
: {
805 case TGSI_OPCODE_ROUND
:
807 case TGSI_OPCODE_EXPBASE2
: {
810 case TGSI_OPCODE_LOGBASE2
: {
813 case TGSI_OPCODE_POWER
: {
814 out
= instr
->pow(inputs
[0], inputs
[1]);
817 case TGSI_OPCODE_CROSSPRODUCT
: {
820 case TGSI_OPCODE_MULTIPLYMATRIX
:
822 case TGSI_OPCODE_ABS
: {
823 out
= instr
->abs(inputs
[0]);
826 case TGSI_OPCODE_RCC
:
828 case TGSI_OPCODE_DPH
: {
831 case TGSI_OPCODE_COS
: {
834 case TGSI_OPCODE_DDX
:
836 case TGSI_OPCODE_DDY
:
838 case TGSI_OPCODE_KILP
:
840 case TGSI_OPCODE_PK2H
:
842 case TGSI_OPCODE_PK2US
:
844 case TGSI_OPCODE_PK4B
:
846 case TGSI_OPCODE_PK4UB
:
848 case TGSI_OPCODE_RFL
:
850 case TGSI_OPCODE_SEQ
:
852 case TGSI_OPCODE_SFL
:
854 case TGSI_OPCODE_SGT
: {
857 case TGSI_OPCODE_SIN
: {
860 case TGSI_OPCODE_SLE
:
862 case TGSI_OPCODE_SNE
:
864 case TGSI_OPCODE_STR
:
866 case TGSI_OPCODE_TEX
:
868 case TGSI_OPCODE_TXD
:
870 case TGSI_OPCODE_UP2H
:
872 case TGSI_OPCODE_UP2US
:
874 case TGSI_OPCODE_UP4B
:
876 case TGSI_OPCODE_UP4UB
:
878 case TGSI_OPCODE_X2D
:
880 case TGSI_OPCODE_ARA
:
882 case TGSI_OPCODE_ARR
:
884 case TGSI_OPCODE_BRA
:
886 case TGSI_OPCODE_CAL
: {
889 case TGSI_OPCODE_RET
: {
892 case TGSI_OPCODE_SSG
:
894 case TGSI_OPCODE_CMP
: {
897 case TGSI_OPCODE_SCS
: {
900 case TGSI_OPCODE_TXB
:
902 case TGSI_OPCODE_NRM
:
904 case TGSI_OPCODE_DIV
:
906 case TGSI_OPCODE_DP2
:
908 case TGSI_OPCODE_TXL
:
910 case TGSI_OPCODE_BRK
: {
913 case TGSI_OPCODE_IF
: {
916 case TGSI_OPCODE_LOOP
:
918 case TGSI_OPCODE_REP
:
920 case TGSI_OPCODE_ELSE
: {
923 case TGSI_OPCODE_ENDIF
: {
926 case TGSI_OPCODE_ENDLOOP
:
928 case TGSI_OPCODE_ENDREP
:
930 case TGSI_OPCODE_PUSHA
:
932 case TGSI_OPCODE_POPA
:
934 case TGSI_OPCODE_CEIL
:
936 case TGSI_OPCODE_I2F
:
938 case TGSI_OPCODE_NOT
:
940 case TGSI_OPCODE_TRUNC
: {
943 case TGSI_OPCODE_SHL
:
945 case TGSI_OPCODE_SHR
:
947 case TGSI_OPCODE_AND
:
951 case TGSI_OPCODE_MOD
:
953 case TGSI_OPCODE_XOR
:
955 case TGSI_OPCODE_SAD
:
957 case TGSI_OPCODE_TXF
:
959 case TGSI_OPCODE_TXQ
:
961 case TGSI_OPCODE_CONT
:
963 case TGSI_OPCODE_EMIT
:
965 case TGSI_OPCODE_ENDPRIM
:
967 case TGSI_OPCODE_BGNLOOP2
: {
970 case TGSI_OPCODE_BGNSUB
: {
973 case TGSI_OPCODE_ENDLOOP2
: {
976 case TGSI_OPCODE_ENDSUB
: {
979 case TGSI_OPCODE_NOISE1
:
981 case TGSI_OPCODE_NOISE2
:
983 case TGSI_OPCODE_NOISE3
:
985 case TGSI_OPCODE_NOISE4
:
987 case TGSI_OPCODE_NOP
:
989 case TGSI_OPCODE_M4X3
:
991 case TGSI_OPCODE_M3X4
:
993 case TGSI_OPCODE_M3X3
:
995 case TGSI_OPCODE_M3X2
:
997 case TGSI_OPCODE_NRM4
:
999 case TGSI_OPCODE_CALLNZ
:
1001 case TGSI_OPCODE_IFC
:
1003 case TGSI_OPCODE_BREAKC
:
1005 case TGSI_OPCODE_KIL
: {
1008 case TGSI_OPCODE_END
:
1013 fprintf(stderr
, "ERROR: Unknown opcode %d\n",
1014 inst
->Instruction
.Opcode
);
1020 fprintf(stderr
, "ERROR: unsupported opcode %d\n",
1021 inst
->Instruction
.Opcode
);
1022 assert(!"Unsupported opcode");
1026 for (int i
= 0; i
< inst
->Instruction
.NumDstRegs
; ++i
) {
1027 struct tgsi_full_dst_register
*dst
= &inst
->FullDstRegisters
[i
];
1029 storage
->store((enum tgsi_file_type
)dst
->DstRegister
.File
,
1030 dst
->DstRegister
.Index
, out
, dst
->DstRegister
.WriteMask
);
1035 tgsi_to_llvm(struct gallivm_ir
*ir
, const struct tgsi_token
*tokens
)
1037 llvm::Module
*mod
= new Module("shader");
1038 struct tgsi_parse_context parse
;
1039 struct tgsi_full_instruction fi
;
1040 struct tgsi_full_declaration fd
;
1041 unsigned instno
= 0;
1042 Function
* shader
= mod
->getFunction("execute_shader");
1043 std::ostringstream stream
;
1044 if (ir
->type
== GALLIVM_VS
) {
1045 stream
<< "vs_shader";
1047 stream
<< "fs_shader";
1050 std::string func_name
= stream
.str();
1051 shader
->setName(func_name
.c_str());
1053 Function::arg_iterator args
= shader
->arg_begin();
1054 Value
*ptr_INPUT
= args
++;
1055 ptr_INPUT
->setName("input");
1057 BasicBlock
*label_entry
= BasicBlock::Create("entry", shader
, 0);
1059 tgsi_parse_init(&parse
, tokens
);
1061 fi
= tgsi_default_full_instruction();
1062 fd
= tgsi_default_full_declaration();
1063 Storage
storage(label_entry
, ptr_INPUT
);
1064 Instructions
instr(mod
, shader
, label_entry
, &storage
);
1065 while(!tgsi_parse_end_of_tokens(&parse
)) {
1066 tgsi_parse_token(&parse
);
1068 switch (parse
.FullToken
.Token
.Type
) {
1069 case TGSI_TOKEN_TYPE_DECLARATION
:
1070 translate_declaration(ir
, mod
, &storage
,
1071 &parse
.FullToken
.FullDeclaration
,
1075 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1076 translate_immediate(&storage
,
1077 &parse
.FullToken
.FullImmediate
);
1080 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1081 translate_instruction(mod
, &storage
, &instr
,
1082 &parse
.FullToken
.FullInstruction
,
1092 tgsi_parse_free(&parse
);
1094 ir
->num_consts
= storage
.numConsts();
1098 llvm::Module
* tgsi_to_llvmir(struct gallivm_ir
*ir
,
1099 const struct tgsi_token
*tokens
)
1101 llvm::Module
*mod
= new Module("shader");
1102 struct tgsi_parse_context parse
;
1103 struct tgsi_full_instruction fi
;
1104 struct tgsi_full_declaration fd
;
1105 unsigned instno
= 0;
1106 std::ostringstream stream
;
1107 if (ir
->type
== GALLIVM_VS
) {
1108 stream
<< "vs_shader";
1110 stream
<< "fs_shader";
1113 std::string func_name
= stream
.str();
1114 Function
*shader
= llvm::cast
<Function
>(mod
->getOrInsertFunction(
1116 vertexShaderFunctionType()));
1118 Function::arg_iterator args
= shader
->arg_begin();
1119 Value
*input
= args
++;
1120 input
->setName("inputs");
1121 Value
*output
= args
++;
1122 output
->setName("outputs");
1123 Value
*consts
= args
++;
1124 consts
->setName("consts");
1125 Value
*temps
= args
++;
1126 temps
->setName("temps");
1128 BasicBlock
*label_entry
= BasicBlock::Create("entry", shader
, 0);
1130 tgsi_parse_init(&parse
, tokens
);
1132 fi
= tgsi_default_full_instruction();
1133 fd
= tgsi_default_full_declaration();
1135 StorageSoa
storage(label_entry
, input
, output
, consts
, temps
);
1136 InstructionsSoa
instr(mod
, shader
, label_entry
, &storage
);
1138 while(!tgsi_parse_end_of_tokens(&parse
)) {
1139 tgsi_parse_token(&parse
);
1141 switch (parse
.FullToken
.Token
.Type
) {
1142 case TGSI_TOKEN_TYPE_DECLARATION
:
1143 translate_declarationir(ir
, mod
, &storage
,
1144 &parse
.FullToken
.FullDeclaration
,
1148 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1149 translate_immediateir(&storage
,
1150 &parse
.FullToken
.FullImmediate
);
1153 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1154 storage
.declareImmediates();
1155 translate_instructionir(mod
, &storage
, &instr
,
1156 &parse
.FullToken
.FullInstruction
,
1166 tgsi_parse_free(&parse
);