1 #include "tgsi_platform.h"
5 * Map mesa register file to SBIR register file.
9 enum register_file file
)
12 case PROGRAM_UNDEFINED
:
13 return TGSI_FILE_NULL
;
14 case PROGRAM_TEMPORARY
:
15 return TGSI_FILE_TEMPORARY
;
16 //case PROGRAM_LOCAL_PARAM:
17 //case PROGRAM_ENV_PARAM:
18 case PROGRAM_STATE_VAR
:
19 case PROGRAM_NAMED_PARAM
:
20 case PROGRAM_CONSTANT
:
22 return TGSI_FILE_CONSTANT
;
24 return TGSI_FILE_INPUT
;
26 return TGSI_FILE_OUTPUT
;
28 return TGSI_FILE_ADDRESS
;
31 return TGSI_FILE_NULL
;
36 * Map mesa register file index to SBIR index.
37 * Take special care when processing input and output indices.
38 * \param processor either TGSI_PROCESSOR_FRAGMENT or TGSI_PROCESSOR_VERTEX
39 * \param file one of TGSI_FILE_x
40 * \param index the mesa register file index
41 * \param usage_bitmask ???
44 map_register_file_index(
48 GLbitfield usage_bitmask
)
56 * The fragment/vertex program input indexes (FRAG/VERT_ATTRIB_x) get
57 * mapped to a packed sequence of integers.
58 * If a program uses one input attribute, the mapped index will be 1.
59 * If a program uses two input attribs, the mapped indexes will be 1,2.
60 * If a program uses 3 input attribs, the mapped indexes will be 1,2,3.
64 assert( usage_bitmask
& (1 << index
) );
66 for( i
= 0; i
< index
; i
++ ) {
67 if( usage_bitmask
& (1 << i
) ) {
71 printf("Map input %d to %d\n", index
, mapped_index
);
74 case TGSI_FILE_OUTPUT
:
75 assert( usage_bitmask
== 0x0 );
76 if( processor
== TGSI_PROCESSOR_FRAGMENT
) {
77 /* depth result -> index 0
78 * color results -> index 1, 2, ...
80 if( index
== FRAG_RESULT_DEPR
) {
84 assert( index
== FRAG_RESULT_COLR
);
85 mapped_index
= index
+ 1;
89 /* mapped_index = VERT_RESULT_x */
102 * Map mesa texture target to SBIR texture target.
108 switch( textarget
) {
109 case TEXTURE_1D_INDEX
:
110 return TGSI_TEXTURE_1D
;
111 case TEXTURE_2D_INDEX
:
112 return TGSI_TEXTURE_2D
;
113 case TEXTURE_3D_INDEX
:
114 return TGSI_TEXTURE_3D
;
115 case TEXTURE_CUBE_INDEX
:
116 return TGSI_TEXTURE_CUBE
;
117 case TEXTURE_RECT_INDEX
:
118 return TGSI_TEXTURE_RECT
;
123 return TGSI_TEXTURE_1D
;
132 return TGSI_SAT_NONE
;
133 case SATURATE_ZERO_ONE
:
134 return TGSI_SAT_ZERO_ONE
;
135 case SATURATE_PLUS_MINUS_ONE
:
136 return TGSI_SAT_MINUS_PLUS_ONE
;
139 return TGSI_SAT_NONE
;
147 assert( WRITEMASK_X
== TGSI_WRITEMASK_X
);
148 assert( WRITEMASK_Y
== TGSI_WRITEMASK_Y
);
149 assert( WRITEMASK_Z
== TGSI_WRITEMASK_Z
);
150 assert( WRITEMASK_W
== TGSI_WRITEMASK_W
);
151 assert( (writemask
& ~TGSI_WRITEMASK_XYZW
) == 0 );
158 const struct prog_instruction
*inst
,
159 struct tgsi_full_instruction
*fullinst
,
164 struct tgsi_full_dst_register
*fulldst
;
165 struct tgsi_full_src_register
*fullsrc
;
167 *fullinst
= tgsi_default_full_instruction();
169 fullinst
->Instruction
.Saturate
= convert_sat( inst
->SaturateMode
);
170 fullinst
->Instruction
.NumDstRegs
= 1;
171 fullinst
->Instruction
.NumSrcRegs
= _mesa_num_inst_src_regs( inst
->Opcode
);
173 fulldst
= &fullinst
->FullDstRegisters
[0];
174 fulldst
->DstRegister
.File
= map_register_file( inst
->DstReg
.File
);
175 fulldst
->DstRegister
.Index
= map_register_file_index(
177 fulldst
->DstRegister
.File
,
180 fulldst
->DstRegister
.WriteMask
= convert_writemask( inst
->DstReg
.WriteMask
);
182 for( i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++ ) {
185 fullsrc
= &fullinst
->FullSrcRegisters
[i
];
186 fullsrc
->SrcRegister
.File
= map_register_file( inst
->SrcReg
[i
].File
);
187 fullsrc
->SrcRegister
.Index
= map_register_file_index(
189 fullsrc
->SrcRegister
.File
,
190 inst
->SrcReg
[i
].Index
,
193 for( j
= 0; j
< 4; j
++ ) {
196 swz
= GET_SWZ( inst
->SrcReg
[i
].Swizzle
, j
);
197 if( swz
> SWIZZLE_W
) {
198 tgsi_util_set_src_register_extswizzle(
199 &fullsrc
->SrcRegisterExtSwz
,
204 tgsi_util_set_src_register_swizzle(
205 &fullsrc
->SrcRegister
,
211 if( inst
->SrcReg
[i
].NegateBase
== NEGATE_XYZW
) {
212 fullsrc
->SrcRegister
.Negate
= 1;
214 else if( inst
->SrcReg
[i
].NegateBase
!= NEGATE_NONE
) {
215 if( inst
->SrcReg
[i
].NegateBase
& NEGATE_X
) {
216 fullsrc
->SrcRegisterExtSwz
.NegateX
= 1;
218 if( inst
->SrcReg
[i
].NegateBase
& NEGATE_Y
) {
219 fullsrc
->SrcRegisterExtSwz
.NegateY
= 1;
221 if( inst
->SrcReg
[i
].NegateBase
& NEGATE_Z
) {
222 fullsrc
->SrcRegisterExtSwz
.NegateZ
= 1;
224 if( inst
->SrcReg
[i
].NegateBase
& NEGATE_W
) {
225 fullsrc
->SrcRegisterExtSwz
.NegateW
= 1;
229 if( inst
->SrcReg
[i
].Abs
) {
230 fullsrc
->SrcRegisterExtMod
.Absolute
= 1;
233 if( inst
->SrcReg
[i
].NegateAbs
) {
234 fullsrc
->SrcRegisterExtMod
.Negate
= 1;
237 if( inst
->SrcReg
[i
].RelAddr
) {
238 fullsrc
->SrcRegister
.Indirect
= 1;
240 fullsrc
->SrcRegisterInd
.File
= TGSI_FILE_ADDRESS
;
241 fullsrc
->SrcRegisterInd
.Index
= 0;
245 switch( inst
->Opcode
) {
247 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ARL
;
250 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ABS
;
253 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ADD
;
256 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_CMP
;
259 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_COS
;
262 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP3
;
265 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DDX
;
268 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DDY
;
271 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP4
;
274 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DPH
;
277 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DST
;
280 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_EX2
;
283 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_FLR
;
286 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_FRC
;
289 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_KIL
;
292 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LG2
;
295 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LIT
;
298 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LRP
;
301 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MAD
;
304 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MAX
;
307 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MIN
;
310 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MOV
;
313 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MUL
;
316 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_POW
;
319 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_RCP
;
322 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_RSQ
;
323 tgsi_util_set_full_src_register_sign_mode(
324 &fullinst
->FullSrcRegisters
[0],
325 TGSI_UTIL_SIGN_CLEAR
);
328 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SCS
;
329 fulldst
->DstRegister
.WriteMask
&= TGSI_WRITEMASK_XY
;
332 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SGE
;
335 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SIN
;
338 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SLT
;
341 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SUB
;
344 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SWZ
;
347 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TEX
;
348 fullinst
->Instruction
.NumSrcRegs
= 2;
349 fullinst
->InstructionExtTexture
.Texture
= map_texture_target( inst
->TexSrcTarget
);
350 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
351 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
354 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXB
;
355 fullinst
->Instruction
.NumSrcRegs
= 2;
356 fullinst
->InstructionExtTexture
.Texture
= map_texture_target( inst
->TexSrcTarget
);
357 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
358 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
361 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TEX
;
362 fullinst
->Instruction
.NumSrcRegs
= 2;
363 fullinst
->InstructionExtTexture
.Texture
= map_texture_target( inst
->TexSrcTarget
);
364 fullinst
->FullSrcRegisters
[0].SrcRegisterExtSwz
.ExtDivide
= TGSI_EXTSWIZZLE_W
;
365 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
366 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
369 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_XPD
;
370 fulldst
->DstRegister
.WriteMask
&= TGSI_WRITEMASK_XYZ
;
373 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOP
;
376 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BGNLOOP2
;
379 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BGNSUB
;
382 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ENDLOOP2
;
385 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ENDSUB
;
388 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE1
;
391 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE2
;
394 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE3
;
397 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE4
;
409 tgsi_mesa_compile_fp_program(
410 const struct gl_fragment_program
*program
,
411 struct tgsi_token
*tokens
,
415 struct tgsi_header
*header
;
416 struct tgsi_full_declaration fulldecl
;
417 struct tgsi_full_instruction fullinst
;
418 struct tgsi_full_dst_register
*fulldst
;
419 struct tgsi_full_src_register
*fullsrc
;
422 *(struct tgsi_version
*) &tokens
[0] = tgsi_build_version();
424 header
= (struct tgsi_header
*) &tokens
[1];
425 *header
= tgsi_build_header();
430 * Input 0 is always read, at least implicitly by the instruction generated
431 * above, so mark it as used.
433 inputs_read
= program
->Base
.InputsRead
| 1;
436 * Declare input attributes.
438 fulldecl
= tgsi_default_full_declaration();
440 fulldecl
.Declaration
.File
= TGSI_FILE_INPUT
;
441 fulldecl
.Declaration
.Declare
= TGSI_DECLARE_RANGE
;
442 fulldecl
.Declaration
.Interpolate
= 1;
445 * Do not interpolate fragment position.
447 fulldecl
.u
.DeclarationRange
.First
= 0;
448 fulldecl
.u
.DeclarationRange
.Last
= 0;
450 fulldecl
.Interpolation
.Interpolate
= TGSI_INTERPOLATE_CONSTANT
;
452 ti
+= tgsi_build_full_declaration(
459 * Interpolate generic attributes.
461 fulldecl
.u
.DeclarationRange
.First
= 1;
462 fulldecl
.u
.DeclarationRange
.Last
= 1;
463 for( i
= 1; i
< 32; i
++ ) {
464 if( inputs_read
& (1 << i
) ) {
465 fulldecl
.u
.DeclarationRange
.Last
++;
469 fulldecl
.Interpolation
.Interpolate
= TGSI_INTERPOLATE_LINEAR
;
471 ti
+= tgsi_build_full_declaration(
478 * Copy input fragment xyz to output xyz.
479 * If the shader writes depth, do not copy the z component.
482 fullinst
= tgsi_default_full_instruction();
484 fullinst
.Instruction
.Opcode
= TGSI_OPCODE_MOV
;
485 fullinst
.Instruction
.NumDstRegs
= 1;
486 fullinst
.Instruction
.NumSrcRegs
= 1;
488 fulldst
= &fullinst
.FullDstRegisters
[0];
489 fulldst
->DstRegister
.File
= TGSI_FILE_OUTPUT
;
490 fulldst
->DstRegister
.Index
= 0;
491 if( program
->Base
.OutputsWritten
& (1 << FRAG_RESULT_DEPR
) ) {
492 fulldst
->DstRegister
.WriteMask
= TGSI_WRITEMASK_XY
;
495 fulldst
->DstRegister
.WriteMask
= TGSI_WRITEMASK_XYZ
;
498 fullsrc
= &fullinst
.FullSrcRegisters
[0];
499 fullsrc
->SrcRegister
.File
= TGSI_FILE_INPUT
;
500 fullsrc
->SrcRegister
.Index
= 0;
502 ti
+= tgsi_build_full_instruction(
508 for( i
= 0; i
< program
->Base
.NumInstructions
; i
++ ) {
509 if( compile_instruction(
510 &program
->Base
.Instructions
[i
],
513 TGSI_PROCESSOR_FRAGMENT
) ) {
514 assert( i
== program
->Base
.NumInstructions
- 1 );
517 TGSI_DUMP_NO_IGNORED
| TGSI_DUMP_NO_DEFAULT
);
521 ti
+= tgsi_build_full_instruction(
532 tgsi_mesa_compile_vp_program(
533 const struct gl_vertex_program
*program
,
534 struct tgsi_token
*tokens
,
538 struct tgsi_header
*header
;
539 struct tgsi_processor
*processor
;
540 struct tgsi_full_instruction fullinst
;
541 GLuint inputs_read
= ~0;
543 *(struct tgsi_version
*) &tokens
[0] = tgsi_build_version();
545 header
= (struct tgsi_header
*) &tokens
[1];
546 *header
= tgsi_build_header();
548 processor
= (struct tgsi_processor
*) &tokens
[2];
549 *processor
= tgsi_build_processor( TGSI_PROCESSOR_VERTEX
, header
);
553 for( ii
= 0; ii
< program
->Base
.NumInstructions
; ii
++ ) {
554 if( compile_instruction(
555 &program
->Base
.Instructions
[ii
],
558 TGSI_PROCESSOR_VERTEX
) ) {
559 assert( ii
== program
->Base
.NumInstructions
- 1 );
560 tgsi_dump( tokens
, TGSI_DUMP_NO_IGNORED
| TGSI_DUMP_NO_DEFAULT
);
564 ti
+= tgsi_build_full_instruction(