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/util/tgsi_parse.h"
14 #include "tgsi/exec/tgsi_exec.h"
15 #include "tgsi/util/tgsi_util.h"
16 #include "tgsi/util/tgsi_build.h"
17 #include "tgsi/util/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 assert(decl
->Declaration
.Declare
== TGSI_DECLARE_RANGE
);
101 first
= decl
->u
.DeclarationRange
.First
;
102 last
= decl
->u
.DeclarationRange
.Last
;
103 mask
= decl
->Declaration
.UsageMask
;
105 /* Do not touch WPOS.xy */
107 mask
&= ~TGSI_WRITEMASK_XY
;
108 if (mask
== TGSI_WRITEMASK_NONE
) {
116 interp_method
= decl
->Interpolation
.Interpolate
;
118 if (mask
== TGSI_WRITEMASK_XYZW
) {
121 for (i
= first
; i
<= last
; i
++) {
122 for (j
= 0; j
< NUM_CHANNELS
; j
++) {
123 //interp( mach, i, j );
124 struct gallivm_interpolate interp
;
125 interp
.type
= interp_method
;
128 add_interpolator(prog
, &interp
);
133 for( j
= 0; j
< NUM_CHANNELS
; j
++ ) {
134 if( mask
& (1 << j
) ) {
135 for( i
= first
; i
<= last
; i
++ ) {
136 struct gallivm_interpolate interp
;
137 interp
.type
= interp_method
;
140 add_interpolator(prog
, &interp
);
149 translate_declarationir(struct gallivm_ir
*,
152 struct tgsi_full_declaration
*decl
,
153 struct tgsi_full_declaration
*)
155 if (decl
->Declaration
.File
== TGSI_FILE_ADDRESS
) {
156 int idx
= decl
->u
.DeclarationRange
.First
;
157 storage
->addAddress(idx
);
162 translate_immediate(Storage
*storage
,
163 struct tgsi_full_immediate
*imm
)
167 for (i
= 0; i
< imm
->Immediate
.Size
- 1; ++i
) {
168 switch (imm
->Immediate
.DataType
) {
169 case TGSI_IMM_FLOAT32
:
170 vec
[i
] = imm
->u
.ImmediateFloat32
[i
].Float
;
176 storage
->addImmediate(vec
);
181 translate_immediateir(StorageSoa
*storage
,
182 struct tgsi_full_immediate
*imm
)
186 for (i
= 0; i
< imm
->Immediate
.Size
- 1; ++i
) {
187 switch (imm
->Immediate
.DataType
) {
188 case TGSI_IMM_FLOAT32
:
189 vec
[i
] = imm
->u
.ImmediateFloat32
[i
].Float
;
195 storage
->addImmediate(vec
);
199 swizzleInt(struct tgsi_full_src_register
*src
)
204 for (int k
= 0; k
< 4; ++k
) {
205 swizzle
+= tgsi_util_get_full_src_register_extswizzle(src
, k
) * start
;
211 static inline llvm::Value
*
212 swizzleVector(llvm::Value
*val
, struct tgsi_full_src_register
*src
,
215 int swizzle
= swizzleInt(src
);
217 if (gallivm_is_swizzle(swizzle
)) {
218 /*fprintf(stderr, "XXXXXXXX swizzle = %d\n", swizzle);*/
219 val
= storage
->shuffleVector(val
, swizzle
);
225 translate_instruction(llvm::Module
*module
,
228 struct tgsi_full_instruction
*inst
,
229 struct tgsi_full_instruction
*fi
,
232 llvm::Value
*inputs
[4];
238 for (int i
= 0; i
< inst
->Instruction
.NumSrcRegs
; ++i
) {
239 struct tgsi_full_src_register
*src
= &inst
->FullSrcRegisters
[i
];
240 llvm::Value
*val
= 0;
241 llvm::Value
*indIdx
= 0;
243 if (src
->SrcRegister
.Indirect
) {
244 indIdx
= storage
->addrElement(src
->SrcRegisterInd
.Index
);
245 indIdx
= storage
->extractIndex(indIdx
);
247 if (src
->SrcRegister
.File
== TGSI_FILE_CONSTANT
) {
248 val
= storage
->constElement(src
->SrcRegister
.Index
, indIdx
);
249 } else if (src
->SrcRegister
.File
== TGSI_FILE_INPUT
) {
250 val
= storage
->inputElement(src
->SrcRegister
.Index
, indIdx
);
251 } else if (src
->SrcRegister
.File
== TGSI_FILE_TEMPORARY
) {
252 val
= storage
->tempElement(src
->SrcRegister
.Index
);
253 } else if (src
->SrcRegister
.File
== TGSI_FILE_OUTPUT
) {
254 val
= storage
->outputElement(src
->SrcRegister
.Index
, indIdx
);
255 } else if (src
->SrcRegister
.File
== TGSI_FILE_IMMEDIATE
) {
256 val
= storage
->immediateElement(src
->SrcRegister
.Index
);
258 fprintf(stderr
, "ERROR: not supported llvm source %d\n", src
->SrcRegister
.File
);
262 inputs
[i
] = swizzleVector(val
, src
, storage
);
266 instr->printVector(inputs[0]);
268 instr->printVector(inputs[1]);*/
269 llvm::Value
*out
= 0;
270 switch (inst
->Instruction
.Opcode
) {
271 case TGSI_OPCODE_ARL
: {
272 out
= instr
->arl(inputs
[0]);
275 case TGSI_OPCODE_MOV
: {
279 case TGSI_OPCODE_LIT
: {
280 out
= instr
->lit(inputs
[0]);
283 case TGSI_OPCODE_RCP
: {
284 out
= instr
->rcp(inputs
[0]);
287 case TGSI_OPCODE_RSQ
: {
288 out
= instr
->rsq(inputs
[0]);
291 case TGSI_OPCODE_EXP
:
293 case TGSI_OPCODE_LOG
:
295 case TGSI_OPCODE_MUL
: {
296 out
= instr
->mul(inputs
[0], inputs
[1]);
299 case TGSI_OPCODE_ADD
: {
300 out
= instr
->add(inputs
[0], inputs
[1]);
303 case TGSI_OPCODE_DP3
: {
304 out
= instr
->dp3(inputs
[0], inputs
[1]);
307 case TGSI_OPCODE_DP4
: {
308 out
= instr
->dp4(inputs
[0], inputs
[1]);
311 case TGSI_OPCODE_DST
: {
312 out
= instr
->dst(inputs
[0], inputs
[1]);
315 case TGSI_OPCODE_MIN
: {
316 out
= instr
->min(inputs
[0], inputs
[1]);
319 case TGSI_OPCODE_MAX
: {
320 out
= instr
->max(inputs
[0], inputs
[1]);
323 case TGSI_OPCODE_SLT
: {
324 out
= instr
->slt(inputs
[0], inputs
[1]);
327 case TGSI_OPCODE_SGE
: {
328 out
= instr
->sge(inputs
[0], inputs
[1]);
331 case TGSI_OPCODE_MAD
: {
332 out
= instr
->madd(inputs
[0], inputs
[1], inputs
[2]);
335 case TGSI_OPCODE_SUB
: {
336 out
= instr
->sub(inputs
[0], inputs
[1]);
339 case TGSI_OPCODE_LERP
: {
340 out
= instr
->lerp(inputs
[0], inputs
[1], inputs
[2]);
343 case TGSI_OPCODE_CND
:
345 case TGSI_OPCODE_CND0
:
347 case TGSI_OPCODE_DOT2ADD
:
349 case TGSI_OPCODE_INDEX
:
351 case TGSI_OPCODE_NEGATE
:
353 case TGSI_OPCODE_FRAC
: {
354 out
= instr
->frc(inputs
[0]);
357 case TGSI_OPCODE_CLAMP
:
359 case TGSI_OPCODE_FLOOR
: {
360 out
= instr
->floor(inputs
[0]);
363 case TGSI_OPCODE_ROUND
:
365 case TGSI_OPCODE_EXPBASE2
: {
366 out
= instr
->ex2(inputs
[0]);
369 case TGSI_OPCODE_LOGBASE2
: {
370 out
= instr
->lg2(inputs
[0]);
373 case TGSI_OPCODE_POWER
: {
374 out
= instr
->pow(inputs
[0], inputs
[1]);
377 case TGSI_OPCODE_CROSSPRODUCT
: {
378 out
= instr
->cross(inputs
[0], inputs
[1]);
381 case TGSI_OPCODE_MULTIPLYMATRIX
:
383 case TGSI_OPCODE_ABS
: {
384 out
= instr
->abs(inputs
[0]);
387 case TGSI_OPCODE_RCC
:
389 case TGSI_OPCODE_DPH
: {
390 out
= instr
->dph(inputs
[0], inputs
[1]);
393 case TGSI_OPCODE_COS
: {
394 out
= instr
->cos(inputs
[0]);
397 case TGSI_OPCODE_DDX
:
399 case TGSI_OPCODE_DDY
:
401 case TGSI_OPCODE_KILP
: {
402 out
= instr
->kilp(inputs
[0]);
403 storage
->setKilElement(out
);
407 case TGSI_OPCODE_PK2H
:
409 case TGSI_OPCODE_PK2US
:
411 case TGSI_OPCODE_PK4B
:
413 case TGSI_OPCODE_PK4UB
:
415 case TGSI_OPCODE_RFL
:
417 case TGSI_OPCODE_SEQ
:
419 case TGSI_OPCODE_SFL
:
421 case TGSI_OPCODE_SGT
: {
422 out
= instr
->sgt(inputs
[0], inputs
[1]);
425 case TGSI_OPCODE_SIN
: {
426 out
= instr
->sin(inputs
[0]);
429 case TGSI_OPCODE_SLE
:
431 case TGSI_OPCODE_SNE
:
433 case TGSI_OPCODE_STR
:
435 case TGSI_OPCODE_TEX
:
437 case TGSI_OPCODE_TXD
:
439 case TGSI_OPCODE_UP2H
:
441 case TGSI_OPCODE_UP2US
:
443 case TGSI_OPCODE_UP4B
:
445 case TGSI_OPCODE_UP4UB
:
447 case TGSI_OPCODE_X2D
:
449 case TGSI_OPCODE_ARA
:
451 case TGSI_OPCODE_ARR
:
453 case TGSI_OPCODE_BRA
:
455 case TGSI_OPCODE_CAL
: {
456 instr
->cal(inst
->InstructionExtLabel
.Label
, storage
->inputPtr());
460 case TGSI_OPCODE_RET
: {
465 case TGSI_OPCODE_SSG
:
467 case TGSI_OPCODE_CMP
: {
468 out
= instr
->cmp(inputs
[0], inputs
[1], inputs
[2]);
471 case TGSI_OPCODE_SCS
: {
472 out
= instr
->scs(inputs
[0]);
475 case TGSI_OPCODE_TXB
:
477 case TGSI_OPCODE_NRM
:
479 case TGSI_OPCODE_DIV
:
481 case TGSI_OPCODE_DP2
:
483 case TGSI_OPCODE_TXL
:
485 case TGSI_OPCODE_BRK
: {
490 case TGSI_OPCODE_IF
: {
491 instr
->ifop(inputs
[0]);
492 storage
->setCurrentBlock(instr
->currentBlock());
493 return; //just update the state
496 case TGSI_OPCODE_LOOP
:
498 case TGSI_OPCODE_REP
:
500 case TGSI_OPCODE_ELSE
: {
502 storage
->setCurrentBlock(instr
->currentBlock());
503 return; //only state update
506 case TGSI_OPCODE_ENDIF
: {
508 storage
->setCurrentBlock(instr
->currentBlock());
509 return; //just update the state
512 case TGSI_OPCODE_ENDLOOP
:
514 case TGSI_OPCODE_ENDREP
:
516 case TGSI_OPCODE_PUSHA
:
518 case TGSI_OPCODE_POPA
:
520 case TGSI_OPCODE_CEIL
:
522 case TGSI_OPCODE_I2F
:
524 case TGSI_OPCODE_NOT
:
526 case TGSI_OPCODE_TRUNC
: {
527 out
= instr
->trunc(inputs
[0]);
530 case TGSI_OPCODE_SHL
:
532 case TGSI_OPCODE_SHR
:
534 case TGSI_OPCODE_AND
:
538 case TGSI_OPCODE_MOD
:
540 case TGSI_OPCODE_XOR
:
542 case TGSI_OPCODE_SAD
:
544 case TGSI_OPCODE_TXF
:
546 case TGSI_OPCODE_TXQ
:
548 case TGSI_OPCODE_CONT
:
550 case TGSI_OPCODE_EMIT
:
552 case TGSI_OPCODE_ENDPRIM
:
554 case TGSI_OPCODE_BGNLOOP2
: {
556 storage
->setCurrentBlock(instr
->currentBlock());
560 case TGSI_OPCODE_BGNSUB
: {
561 instr
->bgnSub(instno
);
562 storage
->setCurrentBlock(instr
->currentBlock());
563 storage
->pushTemps();
567 case TGSI_OPCODE_ENDLOOP2
: {
569 storage
->setCurrentBlock(instr
->currentBlock());
573 case TGSI_OPCODE_ENDSUB
: {
575 storage
->setCurrentBlock(instr
->currentBlock());
576 storage
->popArguments();
581 case TGSI_OPCODE_NOISE1
:
583 case TGSI_OPCODE_NOISE2
:
585 case TGSI_OPCODE_NOISE3
:
587 case TGSI_OPCODE_NOISE4
:
589 case TGSI_OPCODE_NOP
:
591 case TGSI_OPCODE_M4X3
:
593 case TGSI_OPCODE_M3X4
:
595 case TGSI_OPCODE_M3X3
:
597 case TGSI_OPCODE_M3X2
:
599 case TGSI_OPCODE_NRM4
:
601 case TGSI_OPCODE_CALLNZ
:
603 case TGSI_OPCODE_IFC
:
605 case TGSI_OPCODE_BREAKC
:
607 case TGSI_OPCODE_KIL
:
609 case TGSI_OPCODE_END
:
614 fprintf(stderr
, "ERROR: Unknown opcode %d\n",
615 inst
->Instruction
.Opcode
);
621 fprintf(stderr
, "ERROR: unsupported opcode %d\n",
622 inst
->Instruction
.Opcode
);
623 assert(!"Unsupported opcode");
626 /* # not sure if we need this */
627 switch( inst
->Instruction
.Saturate
) {
630 case TGSI_SAT_ZERO_ONE
:
633 case TGSI_SAT_MINUS_PLUS_ONE
:
634 /*TXT( "_SAT[-1,1]" );*/
641 for (int i
= 0; i
< inst
->Instruction
.NumDstRegs
; ++i
) {
642 struct tgsi_full_dst_register
*dst
= &inst
->FullDstRegisters
[i
];
644 if (dst
->DstRegister
.File
== TGSI_FILE_OUTPUT
) {
645 storage
->setOutputElement(dst
->DstRegister
.Index
, out
, dst
->DstRegister
.WriteMask
);
646 } else if (dst
->DstRegister
.File
== TGSI_FILE_TEMPORARY
) {
647 storage
->setTempElement(dst
->DstRegister
.Index
, out
, dst
->DstRegister
.WriteMask
);
648 } else if (dst
->DstRegister
.File
== TGSI_FILE_ADDRESS
) {
649 storage
->setAddrElement(dst
->DstRegister
.Index
, out
, dst
->DstRegister
.WriteMask
);
651 fprintf(stderr
, "ERROR: unsupported LLVM destination!");
652 assert(!"wrong destination");
659 translate_instructionir(llvm::Module
*module
,
661 InstructionsSoa
*instr
,
662 struct tgsi_full_instruction
*inst
,
663 struct tgsi_full_instruction
*fi
,
666 std::vector
< std::vector
<llvm::Value
*> > inputs(inst
->Instruction
.NumSrcRegs
);
668 for (int i
= 0; i
< inst
->Instruction
.NumSrcRegs
; ++i
) {
669 struct tgsi_full_src_register
*src
= &inst
->FullSrcRegisters
[i
];
670 std::vector
<llvm::Value
*> val
;
671 llvm::Value
*indIdx
= 0;
672 int swizzle
= swizzleInt(src
);
674 if (src
->SrcRegister
.Indirect
) {
675 indIdx
= storage
->addrElement(src
->SrcRegisterInd
.Index
);
678 val
= storage
->load((enum tgsi_file_type
)src
->SrcRegister
.File
,
679 src
->SrcRegister
.Index
, swizzle
, indIdx
);
684 std::vector
<llvm::Value
*> out(4);
685 switch (inst
->Instruction
.Opcode
) {
686 case TGSI_OPCODE_ARL
: {
687 out
= instr
->arl(inputs
[0]);
690 case TGSI_OPCODE_MOV
: {
694 case TGSI_OPCODE_LIT
: {
697 case TGSI_OPCODE_RCP
: {
700 case TGSI_OPCODE_RSQ
: {
703 case TGSI_OPCODE_EXP
:
705 case TGSI_OPCODE_LOG
:
707 case TGSI_OPCODE_MUL
: {
708 out
= instr
->mul(inputs
[0], inputs
[1]);
711 case TGSI_OPCODE_ADD
: {
712 out
= instr
->add(inputs
[0], inputs
[1]);
715 case TGSI_OPCODE_DP3
: {
716 out
= instr
->dp3(inputs
[0], inputs
[1]);
719 case TGSI_OPCODE_DP4
: {
720 out
= instr
->dp4(inputs
[0], inputs
[1]);
723 case TGSI_OPCODE_DST
: {
726 case TGSI_OPCODE_MIN
: {
729 case TGSI_OPCODE_MAX
: {
732 case TGSI_OPCODE_SLT
: {
735 case TGSI_OPCODE_SGE
: {
738 case TGSI_OPCODE_MAD
: {
739 out
= instr
->madd(inputs
[0], inputs
[1], inputs
[2]);
742 case TGSI_OPCODE_SUB
: {
745 case TGSI_OPCODE_LERP
: {
748 case TGSI_OPCODE_CND
:
750 case TGSI_OPCODE_CND0
:
752 case TGSI_OPCODE_DOT2ADD
:
754 case TGSI_OPCODE_INDEX
:
756 case TGSI_OPCODE_NEGATE
:
758 case TGSI_OPCODE_FRAC
: {
761 case TGSI_OPCODE_CLAMP
:
763 case TGSI_OPCODE_FLOOR
: {
766 case TGSI_OPCODE_ROUND
:
768 case TGSI_OPCODE_EXPBASE2
: {
771 case TGSI_OPCODE_LOGBASE2
: {
774 case TGSI_OPCODE_POWER
: {
775 out
= instr
->pow(inputs
[0], inputs
[1]);
778 case TGSI_OPCODE_CROSSPRODUCT
: {
781 case TGSI_OPCODE_MULTIPLYMATRIX
:
783 case TGSI_OPCODE_ABS
: {
786 case TGSI_OPCODE_RCC
:
788 case TGSI_OPCODE_DPH
: {
791 case TGSI_OPCODE_COS
: {
794 case TGSI_OPCODE_DDX
:
796 case TGSI_OPCODE_DDY
:
798 case TGSI_OPCODE_KILP
: {
801 case TGSI_OPCODE_PK2H
:
803 case TGSI_OPCODE_PK2US
:
805 case TGSI_OPCODE_PK4B
:
807 case TGSI_OPCODE_PK4UB
:
809 case TGSI_OPCODE_RFL
:
811 case TGSI_OPCODE_SEQ
:
813 case TGSI_OPCODE_SFL
:
815 case TGSI_OPCODE_SGT
: {
818 case TGSI_OPCODE_SIN
: {
821 case TGSI_OPCODE_SLE
:
823 case TGSI_OPCODE_SNE
:
825 case TGSI_OPCODE_STR
:
827 case TGSI_OPCODE_TEX
:
829 case TGSI_OPCODE_TXD
:
831 case TGSI_OPCODE_UP2H
:
833 case TGSI_OPCODE_UP2US
:
835 case TGSI_OPCODE_UP4B
:
837 case TGSI_OPCODE_UP4UB
:
839 case TGSI_OPCODE_X2D
:
841 case TGSI_OPCODE_ARA
:
843 case TGSI_OPCODE_ARR
:
845 case TGSI_OPCODE_BRA
:
847 case TGSI_OPCODE_CAL
: {
850 case TGSI_OPCODE_RET
: {
853 case TGSI_OPCODE_SSG
:
855 case TGSI_OPCODE_CMP
: {
858 case TGSI_OPCODE_SCS
: {
861 case TGSI_OPCODE_TXB
:
863 case TGSI_OPCODE_NRM
:
865 case TGSI_OPCODE_DIV
:
867 case TGSI_OPCODE_DP2
:
869 case TGSI_OPCODE_TXL
:
871 case TGSI_OPCODE_BRK
: {
874 case TGSI_OPCODE_IF
: {
877 case TGSI_OPCODE_LOOP
:
879 case TGSI_OPCODE_REP
:
881 case TGSI_OPCODE_ELSE
: {
884 case TGSI_OPCODE_ENDIF
: {
887 case TGSI_OPCODE_ENDLOOP
:
889 case TGSI_OPCODE_ENDREP
:
891 case TGSI_OPCODE_PUSHA
:
893 case TGSI_OPCODE_POPA
:
895 case TGSI_OPCODE_CEIL
:
897 case TGSI_OPCODE_I2F
:
899 case TGSI_OPCODE_NOT
:
901 case TGSI_OPCODE_TRUNC
: {
904 case TGSI_OPCODE_SHL
:
906 case TGSI_OPCODE_SHR
:
908 case TGSI_OPCODE_AND
:
912 case TGSI_OPCODE_MOD
:
914 case TGSI_OPCODE_XOR
:
916 case TGSI_OPCODE_SAD
:
918 case TGSI_OPCODE_TXF
:
920 case TGSI_OPCODE_TXQ
:
922 case TGSI_OPCODE_CONT
:
924 case TGSI_OPCODE_EMIT
:
926 case TGSI_OPCODE_ENDPRIM
:
928 case TGSI_OPCODE_BGNLOOP2
: {
931 case TGSI_OPCODE_BGNSUB
: {
934 case TGSI_OPCODE_ENDLOOP2
: {
937 case TGSI_OPCODE_ENDSUB
: {
940 case TGSI_OPCODE_NOISE1
:
942 case TGSI_OPCODE_NOISE2
:
944 case TGSI_OPCODE_NOISE3
:
946 case TGSI_OPCODE_NOISE4
:
948 case TGSI_OPCODE_NOP
:
950 case TGSI_OPCODE_M4X3
:
952 case TGSI_OPCODE_M3X4
:
954 case TGSI_OPCODE_M3X3
:
956 case TGSI_OPCODE_M3X2
:
958 case TGSI_OPCODE_NRM4
:
960 case TGSI_OPCODE_CALLNZ
:
962 case TGSI_OPCODE_IFC
:
964 case TGSI_OPCODE_BREAKC
:
966 case TGSI_OPCODE_KIL
:
968 case TGSI_OPCODE_END
:
973 fprintf(stderr
, "ERROR: Unknown opcode %d\n",
974 inst
->Instruction
.Opcode
);
980 fprintf(stderr
, "ERROR: unsupported opcode %d\n",
981 inst
->Instruction
.Opcode
);
982 assert(!"Unsupported opcode");
986 for (int i
= 0; i
< inst
->Instruction
.NumDstRegs
; ++i
) {
987 struct tgsi_full_dst_register
*dst
= &inst
->FullDstRegisters
[i
];
989 storage
->store((enum tgsi_file_type
)dst
->DstRegister
.File
,
990 dst
->DstRegister
.Index
, out
, dst
->DstRegister
.WriteMask
);
995 tgsi_to_llvm(struct gallivm_ir
*ir
, const struct tgsi_token
*tokens
)
997 llvm::Module
*mod
= new Module("shader");
998 struct tgsi_parse_context parse
;
999 struct tgsi_full_instruction fi
;
1000 struct tgsi_full_declaration fd
;
1001 unsigned instno
= 0;
1002 Function
* shader
= mod
->getFunction("execute_shader");
1003 std::ostringstream stream
;
1004 if (ir
->type
== GALLIVM_VS
) {
1005 stream
<< "vs_shader";
1007 stream
<< "fs_shader";
1010 std::string func_name
= stream
.str();
1011 shader
->setName(func_name
.c_str());
1013 Function::arg_iterator args
= shader
->arg_begin();
1014 Value
*ptr_INPUT
= args
++;
1015 ptr_INPUT
->setName("input");
1017 BasicBlock
*label_entry
= new BasicBlock("entry", shader
, 0);
1019 tgsi_parse_init(&parse
, tokens
);
1021 fi
= tgsi_default_full_instruction();
1022 fd
= tgsi_default_full_declaration();
1023 Storage
storage(label_entry
, ptr_INPUT
);
1024 Instructions
instr(mod
, shader
, label_entry
, &storage
);
1025 while(!tgsi_parse_end_of_tokens(&parse
)) {
1026 tgsi_parse_token(&parse
);
1028 switch (parse
.FullToken
.Token
.Type
) {
1029 case TGSI_TOKEN_TYPE_DECLARATION
:
1030 translate_declaration(ir
, mod
, &storage
,
1031 &parse
.FullToken
.FullDeclaration
,
1035 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1036 translate_immediate(&storage
,
1037 &parse
.FullToken
.FullImmediate
);
1040 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1041 translate_instruction(mod
, &storage
, &instr
,
1042 &parse
.FullToken
.FullInstruction
,
1052 tgsi_parse_free(&parse
);
1054 ir
->num_consts
= storage
.numConsts();
1058 llvm::Module
* tgsi_to_llvmir(struct gallivm_ir
*ir
,
1059 const struct tgsi_token
*tokens
)
1061 llvm::Module
*mod
= new Module("shader");
1062 struct tgsi_parse_context parse
;
1063 struct tgsi_full_instruction fi
;
1064 struct tgsi_full_declaration fd
;
1065 unsigned instno
= 0;
1066 std::ostringstream stream
;
1067 if (ir
->type
== GALLIVM_VS
) {
1068 stream
<< "vs_shader";
1070 stream
<< "fs_shader";
1073 std::string func_name
= stream
.str();
1074 Function
*shader
= llvm::cast
<Function
>(mod
->getOrInsertFunction(
1076 vertexShaderFunctionType()));
1078 Function::arg_iterator args
= shader
->arg_begin();
1079 Value
*input
= args
++;
1080 input
->setName("inputs");
1081 Value
*output
= args
++;
1082 output
->setName("outputs");
1083 Value
*consts
= args
++;
1084 consts
->setName("consts");
1085 Value
*temps
= args
++;
1086 temps
->setName("temps");
1088 BasicBlock
*label_entry
= new BasicBlock("entry", shader
, 0);
1090 tgsi_parse_init(&parse
, tokens
);
1092 fi
= tgsi_default_full_instruction();
1093 fd
= tgsi_default_full_declaration();
1095 StorageSoa
storage(label_entry
, input
, output
, consts
, temps
);
1096 InstructionsSoa
instr(mod
, shader
, label_entry
, &storage
);
1098 while(!tgsi_parse_end_of_tokens(&parse
)) {
1099 tgsi_parse_token(&parse
);
1101 switch (parse
.FullToken
.Token
.Type
) {
1102 case TGSI_TOKEN_TYPE_DECLARATION
:
1103 translate_declarationir(ir
, mod
, &storage
,
1104 &parse
.FullToken
.FullDeclaration
,
1108 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1109 translate_immediateir(&storage
,
1110 &parse
.FullToken
.FullImmediate
);
1113 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1114 storage
.declareImmediates();
1115 translate_instructionir(mod
, &storage
, &instr
,
1116 &parse
.FullToken
.FullInstruction
,
1126 tgsi_parse_free(&parse
);