1 /**************************************************************************
3 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
34 #include "pipe/p_compiler.h"
35 #include "pipe/p_context.h"
36 #include "pipe/p_screen.h"
37 #include "pipe/p_shader_tokens.h"
38 #include "pipe/p_state.h"
39 #include "tgsi/tgsi_ureg.h"
40 #include "st_mesa_to_tgsi.h"
41 #include "st_context.h"
42 #include "program/prog_instruction.h"
43 #include "program/prog_parameter.h"
44 #include "util/u_debug.h"
45 #include "util/u_math.h"
46 #include "util/u_memory.h"
49 #define PROGRAM_ANY_CONST ((1 << PROGRAM_LOCAL_PARAM) | \
50 (1 << PROGRAM_ENV_PARAM) | \
51 (1 << PROGRAM_STATE_VAR) | \
52 (1 << PROGRAM_CONSTANT) | \
53 (1 << PROGRAM_UNIFORM))
57 unsigned branch_target
;
63 * Intermediate state used during shader translation.
66 struct ureg_program
*ureg
;
68 struct ureg_dst temps
[MAX_PROGRAM_TEMPS
];
69 struct ureg_src
*constants
;
70 struct ureg_dst outputs
[PIPE_MAX_SHADER_OUTPUTS
];
71 struct ureg_src inputs
[PIPE_MAX_SHADER_INPUTS
];
72 struct ureg_dst address
[1];
73 struct ureg_src samplers
[PIPE_MAX_SAMPLERS
];
74 struct ureg_src systemValues
[SYSTEM_VALUE_MAX
];
76 const GLuint
*inputMapping
;
77 const GLuint
*outputMapping
;
79 /* For every instruction that contains a label (eg CALL), keep
80 * details so that we can go back afterwards and emit the correct
81 * tgsi instruction number for each label.
85 unsigned labels_count
;
87 /* Keep a record of the tgsi instruction number that each mesa
88 * instruction starts at, will be used to fix up labels after
95 unsigned procType
; /**< TGSI_PROCESSOR_VERTEX/FRAGMENT */
101 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
102 static unsigned mesa_sysval_to_semantic
[SYSTEM_VALUE_MAX
] = {
104 TGSI_SEMANTIC_VERTEXID
,
105 TGSI_SEMANTIC_INSTANCEID
110 * Make note of a branch to a label in the TGSI code.
111 * After we've emitted all instructions, we'll go over the list
112 * of labels built here and patch the TGSI code with the actual
113 * location of each label.
115 static unsigned *get_label( struct st_translate
*t
,
116 unsigned branch_target
)
120 if (t
->labels_count
+ 1 >= t
->labels_size
) {
121 t
->labels_size
= 1 << (util_logbase2(t
->labels_size
) + 1);
122 t
->labels
= realloc(t
->labels
, t
->labels_size
* sizeof t
->labels
[0]);
123 if (t
->labels
== NULL
) {
124 static unsigned dummy
;
130 i
= t
->labels_count
++;
131 t
->labels
[i
].branch_target
= branch_target
;
132 return &t
->labels
[i
].token
;
137 * Called prior to emitting the TGSI code for each Mesa instruction.
138 * Allocate additional space for instructions if needed.
139 * Update the insn[] array so the next Mesa instruction points to
140 * the next TGSI instruction.
142 static void set_insn_start( struct st_translate
*t
,
145 if (t
->insn_count
+ 1 >= t
->insn_size
) {
146 t
->insn_size
= 1 << (util_logbase2(t
->insn_size
) + 1);
147 t
->insn
= realloc(t
->insn
, t
->insn_size
* sizeof t
->insn
[0]);
148 if (t
->insn
== NULL
) {
154 t
->insn
[t
->insn_count
++] = start
;
159 * Map a Mesa dst register to a TGSI ureg_dst register.
161 static struct ureg_dst
162 dst_register( struct st_translate
*t
,
163 gl_register_file file
,
167 case PROGRAM_UNDEFINED
:
168 return ureg_dst_undef();
170 case PROGRAM_TEMPORARY
:
171 if (ureg_dst_is_undef(t
->temps
[index
]))
172 t
->temps
[index
] = ureg_DECL_temporary( t
->ureg
);
174 return t
->temps
[index
];
177 if (t
->procType
== TGSI_PROCESSOR_VERTEX
)
178 assert(index
< VARYING_SLOT_MAX
);
179 else if (t
->procType
== TGSI_PROCESSOR_FRAGMENT
)
180 assert(index
< FRAG_RESULT_MAX
);
182 assert(index
< VARYING_SLOT_MAX
);
184 assert(t
->outputMapping
[index
] < Elements(t
->outputs
));
186 return t
->outputs
[t
->outputMapping
[index
]];
188 case PROGRAM_ADDRESS
:
189 return t
->address
[index
];
193 return ureg_dst_undef();
199 * Map a Mesa src register to a TGSI ureg_src register.
201 static struct ureg_src
202 src_register( struct st_translate
*t
,
203 gl_register_file file
,
207 case PROGRAM_UNDEFINED
:
208 return ureg_src_undef();
210 case PROGRAM_TEMPORARY
:
212 assert(index
< Elements(t
->temps
));
213 if (ureg_dst_is_undef(t
->temps
[index
]))
214 t
->temps
[index
] = ureg_DECL_temporary( t
->ureg
);
215 return ureg_src(t
->temps
[index
]);
217 case PROGRAM_ENV_PARAM
:
218 case PROGRAM_LOCAL_PARAM
:
219 case PROGRAM_UNIFORM
:
221 return t
->constants
[index
];
222 case PROGRAM_STATE_VAR
:
223 case PROGRAM_CONSTANT
: /* ie, immediate */
225 return ureg_DECL_constant( t
->ureg
, 0 );
227 return t
->constants
[index
];
230 assert(t
->inputMapping
[index
] < Elements(t
->inputs
));
231 return t
->inputs
[t
->inputMapping
[index
]];
234 assert(t
->outputMapping
[index
] < Elements(t
->outputs
));
235 return ureg_src(t
->outputs
[t
->outputMapping
[index
]]); /* not needed? */
237 case PROGRAM_ADDRESS
:
238 return ureg_src(t
->address
[index
]);
240 case PROGRAM_SYSTEM_VALUE
:
241 assert(index
< Elements(t
->systemValues
));
242 return t
->systemValues
[index
];
246 return ureg_src_undef();
252 * Map mesa texture target to TGSI texture target.
255 st_translate_texture_target( GLuint textarget
,
259 switch( textarget
) {
260 case TEXTURE_1D_INDEX
: return TGSI_TEXTURE_SHADOW1D
;
261 case TEXTURE_2D_INDEX
: return TGSI_TEXTURE_SHADOW2D
;
262 case TEXTURE_RECT_INDEX
: return TGSI_TEXTURE_SHADOWRECT
;
263 case TEXTURE_1D_ARRAY_INDEX
: return TGSI_TEXTURE_SHADOW1D_ARRAY
;
264 case TEXTURE_2D_ARRAY_INDEX
: return TGSI_TEXTURE_SHADOW2D_ARRAY
;
265 case TEXTURE_CUBE_INDEX
: return TGSI_TEXTURE_SHADOWCUBE
;
266 case TEXTURE_CUBE_ARRAY_INDEX
: return TGSI_TEXTURE_SHADOWCUBE_ARRAY
;
271 switch( textarget
) {
272 case TEXTURE_BUFFER_INDEX
: return TGSI_TEXTURE_BUFFER
;
273 case TEXTURE_1D_INDEX
: return TGSI_TEXTURE_1D
;
274 case TEXTURE_2D_INDEX
: return TGSI_TEXTURE_2D
;
275 case TEXTURE_3D_INDEX
: return TGSI_TEXTURE_3D
;
276 case TEXTURE_CUBE_INDEX
: return TGSI_TEXTURE_CUBE
;
277 case TEXTURE_CUBE_ARRAY_INDEX
: return TGSI_TEXTURE_CUBE_ARRAY
;
278 case TEXTURE_RECT_INDEX
: return TGSI_TEXTURE_RECT
;
279 case TEXTURE_1D_ARRAY_INDEX
: return TGSI_TEXTURE_1D_ARRAY
;
280 case TEXTURE_2D_ARRAY_INDEX
: return TGSI_TEXTURE_2D_ARRAY
;
281 case TEXTURE_EXTERNAL_INDEX
: return TGSI_TEXTURE_2D
;
284 return TGSI_TEXTURE_1D
;
290 * Create a TGSI ureg_dst register from a Mesa dest register.
292 static struct ureg_dst
293 translate_dst( struct st_translate
*t
,
294 const struct prog_dst_register
*DstReg
,
298 struct ureg_dst dst
= dst_register( t
,
302 dst
= ureg_writemask( dst
,
306 dst
= ureg_saturate( dst
);
307 else if (clamp_color
&& DstReg
->File
== PROGRAM_OUTPUT
) {
308 /* Clamp colors for ARB_color_buffer_float. */
309 switch (t
->procType
) {
310 case TGSI_PROCESSOR_VERTEX
:
311 /* XXX if the geometry shader is present, this must be done there
312 * instead of here. */
313 if (DstReg
->Index
== VARYING_SLOT_COL0
||
314 DstReg
->Index
== VARYING_SLOT_COL1
||
315 DstReg
->Index
== VARYING_SLOT_BFC0
||
316 DstReg
->Index
== VARYING_SLOT_BFC1
) {
317 dst
= ureg_saturate(dst
);
321 case TGSI_PROCESSOR_FRAGMENT
:
322 if (DstReg
->Index
>= FRAG_RESULT_COLOR
) {
323 dst
= ureg_saturate(dst
);
330 dst
= ureg_dst_indirect( dst
, ureg_src(t
->address
[0]) );
337 * Create a TGSI ureg_src register from a Mesa src register.
339 static struct ureg_src
340 translate_src( struct st_translate
*t
,
341 const struct prog_src_register
*SrcReg
)
343 struct ureg_src src
= src_register( t
, SrcReg
->File
, SrcReg
->Index
);
345 if (t
->procType
== TGSI_PROCESSOR_GEOMETRY
&& SrcReg
->HasIndex2
) {
346 src
= src_register( t
, SrcReg
->File
, SrcReg
->Index2
);
347 if (SrcReg
->RelAddr2
)
348 src
= ureg_src_dimension_indirect( src
, ureg_src(t
->address
[0]),
351 src
= ureg_src_dimension( src
, SrcReg
->Index
);
354 src
= ureg_swizzle( src
,
355 GET_SWZ( SrcReg
->Swizzle
, 0 ) & 0x3,
356 GET_SWZ( SrcReg
->Swizzle
, 1 ) & 0x3,
357 GET_SWZ( SrcReg
->Swizzle
, 2 ) & 0x3,
358 GET_SWZ( SrcReg
->Swizzle
, 3 ) & 0x3);
360 if (SrcReg
->Negate
== NEGATE_XYZW
)
361 src
= ureg_negate(src
);
366 if (SrcReg
->RelAddr
) {
367 src
= ureg_src_indirect( src
, ureg_src(t
->address
[0]));
368 if (SrcReg
->File
!= PROGRAM_INPUT
&&
369 SrcReg
->File
!= PROGRAM_OUTPUT
) {
370 /* If SrcReg->Index was negative, it was set to zero in
371 * src_register(). Reassign it now. But don't do this
372 * for input/output regs since they get remapped while
373 * const buffers don't.
375 src
.Index
= SrcReg
->Index
;
383 static struct ureg_src
swizzle_4v( struct ureg_src src
,
384 const unsigned *swz
)
386 return ureg_swizzle( src
, swz
[0], swz
[1], swz
[2], swz
[3] );
391 * Translate a SWZ instruction into a MOV, MUL or MAD instruction. EG:
397 * MAD dst {1,-1,0,0}, src.xyxx, {0,0,1,0}
399 static void emit_swz( struct st_translate
*t
,
401 const struct prog_src_register
*SrcReg
)
403 struct ureg_program
*ureg
= t
->ureg
;
404 struct ureg_src src
= src_register( t
, SrcReg
->File
, SrcReg
->Index
);
406 unsigned negate_mask
= SrcReg
->Negate
;
408 unsigned one_mask
= ((GET_SWZ(SrcReg
->Swizzle
, 0) == SWIZZLE_ONE
) << 0 |
409 (GET_SWZ(SrcReg
->Swizzle
, 1) == SWIZZLE_ONE
) << 1 |
410 (GET_SWZ(SrcReg
->Swizzle
, 2) == SWIZZLE_ONE
) << 2 |
411 (GET_SWZ(SrcReg
->Swizzle
, 3) == SWIZZLE_ONE
) << 3);
413 unsigned zero_mask
= ((GET_SWZ(SrcReg
->Swizzle
, 0) == SWIZZLE_ZERO
) << 0 |
414 (GET_SWZ(SrcReg
->Swizzle
, 1) == SWIZZLE_ZERO
) << 1 |
415 (GET_SWZ(SrcReg
->Swizzle
, 2) == SWIZZLE_ZERO
) << 2 |
416 (GET_SWZ(SrcReg
->Swizzle
, 3) == SWIZZLE_ZERO
) << 3);
418 unsigned negative_one_mask
= one_mask
& negate_mask
;
419 unsigned positive_one_mask
= one_mask
& ~negate_mask
;
423 unsigned mul_swizzle
[4] = {0,0,0,0};
424 unsigned add_swizzle
[4] = {0,0,0,0};
425 unsigned src_swizzle
[4] = {0,0,0,0};
426 boolean need_add
= FALSE
;
427 boolean need_mul
= FALSE
;
429 if (dst
.WriteMask
== 0)
432 /* Is this just a MOV?
434 if (zero_mask
== 0 &&
436 (negate_mask
== 0 || negate_mask
== TGSI_WRITEMASK_XYZW
))
438 ureg_MOV( ureg
, dst
, translate_src( t
, SrcReg
));
444 #define IMM_NEG_ONE 2
446 imm
= ureg_imm3f( ureg
, 0, 1, -1 );
448 for (i
= 0; i
< 4; i
++) {
449 unsigned bit
= 1 << i
;
451 if (dst
.WriteMask
& bit
) {
452 if (positive_one_mask
& bit
) {
453 mul_swizzle
[i
] = IMM_ZERO
;
454 add_swizzle
[i
] = IMM_ONE
;
457 else if (negative_one_mask
& bit
) {
458 mul_swizzle
[i
] = IMM_ZERO
;
459 add_swizzle
[i
] = IMM_NEG_ONE
;
462 else if (zero_mask
& bit
) {
463 mul_swizzle
[i
] = IMM_ZERO
;
464 add_swizzle
[i
] = IMM_ZERO
;
468 add_swizzle
[i
] = IMM_ZERO
;
469 src_swizzle
[i
] = GET_SWZ(SrcReg
->Swizzle
, i
);
471 if (negate_mask
& bit
) {
472 mul_swizzle
[i
] = IMM_NEG_ONE
;
475 mul_swizzle
[i
] = IMM_ONE
;
481 if (need_mul
&& need_add
) {
484 swizzle_4v( src
, src_swizzle
),
485 swizzle_4v( imm
, mul_swizzle
),
486 swizzle_4v( imm
, add_swizzle
) );
491 swizzle_4v( src
, src_swizzle
),
492 swizzle_4v( imm
, mul_swizzle
) );
497 swizzle_4v( imm
, add_swizzle
) );
510 * Negate the value of DDY to match GL semantics where (0,0) is the
511 * lower-left corner of the window.
512 * Note that the GL_ARB_fragment_coord_conventions extension will
513 * effect this someday.
515 static void emit_ddy( struct st_translate
*t
,
517 const struct prog_src_register
*SrcReg
)
519 struct ureg_program
*ureg
= t
->ureg
;
520 struct ureg_src src
= translate_src( t
, SrcReg
);
521 src
= ureg_negate( src
);
522 ureg_DDY( ureg
, dst
, src
);
528 translate_opcode( unsigned op
)
532 return TGSI_OPCODE_ARL
;
534 return TGSI_OPCODE_ABS
;
536 return TGSI_OPCODE_ADD
;
538 return TGSI_OPCODE_BGNLOOP
;
540 return TGSI_OPCODE_BGNSUB
;
542 return TGSI_OPCODE_BRK
;
544 return TGSI_OPCODE_CAL
;
546 return TGSI_OPCODE_CMP
;
548 return TGSI_OPCODE_CONT
;
550 return TGSI_OPCODE_COS
;
552 return TGSI_OPCODE_DDX
;
554 return TGSI_OPCODE_DDY
;
556 return TGSI_OPCODE_DP2
;
558 return TGSI_OPCODE_DP2A
;
560 return TGSI_OPCODE_DP3
;
562 return TGSI_OPCODE_DP4
;
564 return TGSI_OPCODE_DPH
;
566 return TGSI_OPCODE_DST
;
568 return TGSI_OPCODE_ELSE
;
570 return TGSI_OPCODE_ENDIF
;
572 return TGSI_OPCODE_ENDLOOP
;
574 return TGSI_OPCODE_ENDSUB
;
576 return TGSI_OPCODE_EX2
;
578 return TGSI_OPCODE_EXP
;
580 return TGSI_OPCODE_FLR
;
582 return TGSI_OPCODE_FRC
;
584 return TGSI_OPCODE_IF
;
586 return TGSI_OPCODE_TRUNC
;
588 return TGSI_OPCODE_KIL
;
590 return TGSI_OPCODE_KILP
;
592 return TGSI_OPCODE_LG2
;
594 return TGSI_OPCODE_LOG
;
596 return TGSI_OPCODE_LIT
;
598 return TGSI_OPCODE_LRP
;
600 return TGSI_OPCODE_MAD
;
602 return TGSI_OPCODE_MAX
;
604 return TGSI_OPCODE_MIN
;
606 return TGSI_OPCODE_MOV
;
608 return TGSI_OPCODE_MUL
;
610 return TGSI_OPCODE_NOP
;
612 return TGSI_OPCODE_NRM
;
614 return TGSI_OPCODE_NRM4
;
616 return TGSI_OPCODE_POW
;
618 return TGSI_OPCODE_RCP
;
620 return TGSI_OPCODE_RET
;
622 return TGSI_OPCODE_RSQ
;
624 return TGSI_OPCODE_SCS
;
626 return TGSI_OPCODE_SEQ
;
628 return TGSI_OPCODE_SGE
;
630 return TGSI_OPCODE_SGT
;
632 return TGSI_OPCODE_SIN
;
634 return TGSI_OPCODE_SLE
;
636 return TGSI_OPCODE_SLT
;
638 return TGSI_OPCODE_SNE
;
640 return TGSI_OPCODE_SSG
;
642 return TGSI_OPCODE_SUB
;
644 return TGSI_OPCODE_TEX
;
646 return TGSI_OPCODE_TXB
;
648 return TGSI_OPCODE_TXD
;
650 return TGSI_OPCODE_TXL
;
652 return TGSI_OPCODE_TXP
;
654 return TGSI_OPCODE_XPD
;
656 return TGSI_OPCODE_END
;
659 return TGSI_OPCODE_NOP
;
666 struct st_translate
*t
,
667 const struct prog_instruction
*inst
,
668 boolean clamp_dst_color_output
)
670 struct ureg_program
*ureg
= t
->ureg
;
672 struct ureg_dst dst
[1] = { { 0 } };
673 struct ureg_src src
[4];
677 num_dst
= _mesa_num_inst_dst_regs( inst
->Opcode
);
678 num_src
= _mesa_num_inst_src_regs( inst
->Opcode
);
681 dst
[0] = translate_dst( t
,
684 clamp_dst_color_output
);
686 for (i
= 0; i
< num_src
; i
++)
687 src
[i
] = translate_src( t
, &inst
->SrcReg
[i
] );
689 switch( inst
->Opcode
) {
691 emit_swz( t
, dst
[0], &inst
->SrcReg
[0] );
699 debug_assert(num_dst
== 0);
700 ureg_label_insn( ureg
,
701 translate_opcode( inst
->Opcode
),
703 get_label( t
, inst
->BranchTarget
));
711 src
[num_src
++] = t
->samplers
[inst
->TexSrcUnit
];
713 translate_opcode( inst
->Opcode
),
715 st_translate_texture_target( inst
->TexSrcTarget
,
722 dst
[0] = ureg_writemask(dst
[0], TGSI_WRITEMASK_XY
);
724 translate_opcode( inst
->Opcode
),
730 dst
[0] = ureg_writemask(dst
[0], TGSI_WRITEMASK_XYZ
);
732 translate_opcode( inst
->Opcode
),
741 /* At some point, a motivated person could add a better
742 * implementation of noise. Currently not even the nvidia
743 * binary drivers do anything more than this. In any case, the
744 * place to do this is in the GL state tracker, not the poor
747 ureg_MOV( ureg
, dst
[0], ureg_imm1f(ureg
, 0.5) );
751 emit_ddy( t
, dst
[0], &inst
->SrcReg
[0] );
756 translate_opcode( inst
->Opcode
),
765 * Emit the TGSI instructions for inverting and adjusting WPOS.
766 * This code is unavoidable because it also depends on whether
767 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
770 emit_wpos_adjustment( struct st_translate
*t
,
771 const struct gl_program
*program
,
773 GLfloat adjX
, GLfloat adjY
[2])
775 struct ureg_program
*ureg
= t
->ureg
;
777 /* Fragment program uses fragment position input.
778 * Need to replace instances of INPUT[WPOS] with temp T
779 * where T = INPUT[WPOS] by y is inverted.
781 static const gl_state_index wposTransformState
[STATE_LENGTH
]
782 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
, 0, 0, 0 };
784 /* XXX: note we are modifying the incoming shader here! Need to
785 * do this before emitting the constant decls below, or this
788 unsigned wposTransConst
= _mesa_add_state_reference(program
->Parameters
,
791 struct ureg_src wpostrans
= ureg_DECL_constant( ureg
, wposTransConst
);
792 struct ureg_dst wpos_temp
= ureg_DECL_temporary( ureg
);
793 struct ureg_src wpos_input
= t
->inputs
[t
->inputMapping
[VARYING_SLOT_POS
]];
795 /* First, apply the coordinate shift: */
796 if (adjX
|| adjY
[0] || adjY
[1]) {
797 if (adjY
[0] != adjY
[1]) {
798 /* Adjust the y coordinate by adjY[1] or adjY[0] respectively
799 * depending on whether inversion is actually going to be applied
800 * or not, which is determined by testing against the inversion
801 * state variable used below, which will be either +1 or -1.
803 struct ureg_dst adj_temp
= ureg_DECL_temporary(ureg
);
805 ureg_CMP(ureg
, adj_temp
,
806 ureg_scalar(wpostrans
, invert
? 2 : 0),
807 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
),
808 ureg_imm4f(ureg
, adjX
, adjY
[1], 0.0f
, 0.0f
));
809 ureg_ADD(ureg
, wpos_temp
, wpos_input
, ureg_src(adj_temp
));
811 ureg_ADD(ureg
, wpos_temp
, wpos_input
,
812 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
));
814 wpos_input
= ureg_src(wpos_temp
);
816 /* MOV wpos_temp, input[wpos]
818 ureg_MOV( ureg
, wpos_temp
, wpos_input
);
821 /* Now the conditional y flip: STATE_FB_WPOS_Y_TRANSFORM.xy/zw will be
822 * inversion/identity, or the other way around if we're drawing to an FBO.
825 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
828 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
830 ureg_scalar(wpostrans
, 0),
831 ureg_scalar(wpostrans
, 1));
833 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
836 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
838 ureg_scalar(wpostrans
, 2),
839 ureg_scalar(wpostrans
, 3));
842 /* Use wpos_temp as position input from here on:
844 t
->inputs
[t
->inputMapping
[VARYING_SLOT_POS
]] = ureg_src(wpos_temp
);
849 * Emit fragment position/ooordinate code.
852 emit_wpos(struct st_context
*st
,
853 struct st_translate
*t
,
854 const struct gl_program
*program
,
855 struct ureg_program
*ureg
)
857 const struct gl_fragment_program
*fp
=
858 (const struct gl_fragment_program
*) program
;
859 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
861 GLfloat adjY
[2] = { 0.0f
, 0.0f
};
862 boolean invert
= FALSE
;
864 /* Query the pixel center conventions supported by the pipe driver and set
865 * adjX, adjY to help out if it cannot handle the requested one internally.
867 * The bias of the y-coordinate depends on whether y-inversion takes place
868 * (adjY[1]) or not (adjY[0]), which is in turn dependent on whether we are
869 * drawing to an FBO (causes additional inversion), and whether the the pipe
870 * driver origin and the requested origin differ (the latter condition is
871 * stored in the 'invert' variable).
873 * For height = 100 (i = integer, h = half-integer, l = lower, u = upper):
880 * l,i -> u,i: ( 0.0 + 1.0) * -1 + 100 = 99
881 * l,h -> u,h: ( 0.5 + 0.0) * -1 + 100 = 99.5
882 * u,i -> l,i: (99.0 + 1.0) * -1 + 100 = 0
883 * u,h -> l,h: (99.5 + 0.0) * -1 + 100 = 0.5
885 * inversion and center shift:
886 * l,i -> u,h: ( 0.0 + 0.5) * -1 + 100 = 99.5
887 * l,h -> u,i: ( 0.5 + 0.5) * -1 + 100 = 99
888 * u,i -> l,h: (99.0 + 0.5) * -1 + 100 = 0.5
889 * u,h -> l,i: (99.5 + 0.5) * -1 + 100 = 0
891 if (fp
->OriginUpperLeft
) {
892 /* Fragment shader wants origin in upper-left */
893 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
)) {
894 /* the driver supports upper-left origin */
896 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
)) {
897 /* the driver supports lower-left origin, need to invert Y */
898 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
905 /* Fragment shader wants origin in lower-left */
906 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
))
907 /* the driver supports lower-left origin */
908 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
909 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
))
910 /* the driver supports upper-left origin, need to invert Y */
916 if (fp
->PixelCenterInteger
) {
917 /* Fragment shader wants pixel center integer */
918 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
919 /* the driver supports pixel center integer */
921 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
923 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
924 /* the driver supports pixel center half integer, need to bias X,Y */
933 /* Fragment shader wants pixel center half integer */
934 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
935 /* the driver supports pixel center half integer */
937 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
938 /* the driver supports pixel center integer, need to bias X,Y */
939 adjX
= adjY
[0] = adjY
[1] = 0.5f
;
940 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
946 /* we invert after adjustment so that we avoid the MOV to temporary,
947 * and reuse the adjustment ADD instead */
948 emit_wpos_adjustment(t
, program
, invert
, adjX
, adjY
);
953 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
954 * TGSI uses +1 for front, -1 for back.
955 * This function converts the TGSI value to the GL value. Simply clamping/
956 * saturating the value to [0,1] does the job.
959 emit_face_var( struct st_translate
*t
,
960 const struct gl_program
*program
)
962 struct ureg_program
*ureg
= t
->ureg
;
963 struct ureg_dst face_temp
= ureg_DECL_temporary( ureg
);
964 struct ureg_src face_input
= t
->inputs
[t
->inputMapping
[VARYING_SLOT_FACE
]];
966 /* MOV_SAT face_temp, input[face]
968 face_temp
= ureg_saturate( face_temp
);
969 ureg_MOV( ureg
, face_temp
, face_input
);
971 /* Use face_temp as face input from here on:
973 t
->inputs
[t
->inputMapping
[VARYING_SLOT_FACE
]] = ureg_src(face_temp
);
978 emit_edgeflags( struct st_translate
*t
,
979 const struct gl_program
*program
)
981 struct ureg_program
*ureg
= t
->ureg
;
982 struct ureg_dst edge_dst
= t
->outputs
[t
->outputMapping
[VARYING_SLOT_EDGE
]];
983 struct ureg_src edge_src
= t
->inputs
[t
->inputMapping
[VERT_ATTRIB_EDGEFLAG
]];
985 ureg_MOV( ureg
, edge_dst
, edge_src
);
990 * Translate Mesa program to TGSI format.
991 * \param program the program to translate
992 * \param numInputs number of input registers used
993 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
995 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
996 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
998 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
999 * \param numOutputs number of output registers used
1000 * \param outputMapping maps Mesa fragment program outputs to TGSI
1002 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
1003 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
1006 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
1009 st_translate_mesa_program(
1010 struct gl_context
*ctx
,
1012 struct ureg_program
*ureg
,
1013 const struct gl_program
*program
,
1015 const GLuint inputMapping
[],
1016 const ubyte inputSemanticName
[],
1017 const ubyte inputSemanticIndex
[],
1018 const GLuint interpMode
[],
1020 const GLuint outputMapping
[],
1021 const ubyte outputSemanticName
[],
1022 const ubyte outputSemanticIndex
[],
1023 boolean passthrough_edgeflags
,
1024 boolean clamp_color
)
1026 struct st_translate translate
, *t
;
1028 enum pipe_error ret
= PIPE_OK
;
1030 assert(numInputs
<= Elements(t
->inputs
));
1031 assert(numOutputs
<= Elements(t
->outputs
));
1034 memset(t
, 0, sizeof *t
);
1036 t
->procType
= procType
;
1037 t
->inputMapping
= inputMapping
;
1038 t
->outputMapping
= outputMapping
;
1041 /*_mesa_print_program(program);*/
1044 * Declare input attributes.
1046 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
1047 for (i
= 0; i
< numInputs
; i
++) {
1048 t
->inputs
[i
] = ureg_DECL_fs_input(ureg
,
1049 inputSemanticName
[i
],
1050 inputSemanticIndex
[i
],
1054 if (program
->InputsRead
& VARYING_BIT_POS
) {
1055 /* Must do this after setting up t->inputs, and before
1056 * emitting constant references, below:
1058 emit_wpos(st_context(ctx
), t
, program
, ureg
);
1061 if (program
->InputsRead
& VARYING_BIT_FACE
) {
1062 emit_face_var( t
, program
);
1066 * Declare output attributes.
1068 for (i
= 0; i
< numOutputs
; i
++) {
1069 switch (outputSemanticName
[i
]) {
1070 case TGSI_SEMANTIC_POSITION
:
1071 t
->outputs
[i
] = ureg_DECL_output( ureg
,
1072 TGSI_SEMANTIC_POSITION
, /* Z / Depth */
1073 outputSemanticIndex
[i
] );
1075 t
->outputs
[i
] = ureg_writemask( t
->outputs
[i
],
1078 case TGSI_SEMANTIC_STENCIL
:
1079 t
->outputs
[i
] = ureg_DECL_output( ureg
,
1080 TGSI_SEMANTIC_STENCIL
, /* Stencil */
1081 outputSemanticIndex
[i
] );
1082 t
->outputs
[i
] = ureg_writemask( t
->outputs
[i
],
1085 case TGSI_SEMANTIC_COLOR
:
1086 t
->outputs
[i
] = ureg_DECL_output( ureg
,
1087 TGSI_SEMANTIC_COLOR
,
1088 outputSemanticIndex
[i
] );
1096 else if (procType
== TGSI_PROCESSOR_GEOMETRY
) {
1097 for (i
= 0; i
< numInputs
; i
++) {
1098 t
->inputs
[i
] = ureg_DECL_gs_input(ureg
,
1100 inputSemanticName
[i
],
1101 inputSemanticIndex
[i
]);
1104 for (i
= 0; i
< numOutputs
; i
++) {
1105 t
->outputs
[i
] = ureg_DECL_output( ureg
,
1106 outputSemanticName
[i
],
1107 outputSemanticIndex
[i
] );
1111 assert(procType
== TGSI_PROCESSOR_VERTEX
);
1113 for (i
= 0; i
< numInputs
; i
++) {
1114 t
->inputs
[i
] = ureg_DECL_vs_input(ureg
, i
);
1117 for (i
= 0; i
< numOutputs
; i
++) {
1118 t
->outputs
[i
] = ureg_DECL_output( ureg
,
1119 outputSemanticName
[i
],
1120 outputSemanticIndex
[i
] );
1122 if (passthrough_edgeflags
)
1123 emit_edgeflags( t
, program
);
1126 /* Declare address register.
1128 if (program
->NumAddressRegs
> 0) {
1129 debug_assert( program
->NumAddressRegs
== 1 );
1130 t
->address
[0] = ureg_DECL_address( ureg
);
1133 /* Declare misc input registers
1136 GLbitfield sysInputs
= program
->SystemValuesRead
;
1137 unsigned numSys
= 0;
1138 for (i
= 0; sysInputs
; i
++) {
1139 if (sysInputs
& (1 << i
)) {
1140 unsigned semName
= mesa_sysval_to_semantic
[i
];
1141 t
->systemValues
[i
] = ureg_DECL_system_value(ureg
, numSys
, semName
, 0);
1142 if (semName
== TGSI_SEMANTIC_INSTANCEID
||
1143 semName
== TGSI_SEMANTIC_VERTEXID
) {
1144 /* From Gallium perspective, these system values are always
1145 * integer, and require native integer support. However, if
1146 * native integer is supported on the vertex stage but not the
1147 * pixel stage (e.g, i915g + draw), Mesa will generate IR that
1148 * assumes these system values are floats. To resolve the
1149 * inconsistency, we insert a U2F.
1151 struct st_context
*st
= st_context(ctx
);
1152 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
1153 assert(procType
== TGSI_PROCESSOR_VERTEX
);
1154 assert(pscreen
->get_shader_param(pscreen
, PIPE_SHADER_VERTEX
, PIPE_SHADER_CAP_INTEGERS
));
1155 (void) pscreen
; /* silence non-debug build warnings */
1156 if (!ctx
->Const
.NativeIntegers
) {
1157 struct ureg_dst temp
= ureg_DECL_local_temporary(t
->ureg
);
1158 ureg_U2F( t
->ureg
, ureg_writemask(temp
, TGSI_WRITEMASK_X
), t
->systemValues
[i
]);
1159 t
->systemValues
[i
] = ureg_scalar(ureg_src(temp
), 0);
1163 sysInputs
&= ~(1 << i
);
1168 if (program
->IndirectRegisterFiles
& (1 << PROGRAM_TEMPORARY
)) {
1169 /* If temps are accessed with indirect addressing, declare temporaries
1170 * in sequential order. Else, we declare them on demand elsewhere.
1172 for (i
= 0; i
< program
->NumTemporaries
; i
++) {
1173 /* XXX use TGSI_FILE_TEMPORARY_ARRAY when it's supported by ureg */
1174 t
->temps
[i
] = ureg_DECL_temporary( t
->ureg
);
1178 /* Emit constants and immediates. Mesa uses a single index space
1179 * for these, so we put all the translated regs in t->constants.
1181 if (program
->Parameters
) {
1182 t
->constants
= calloc( program
->Parameters
->NumParameters
,
1183 sizeof t
->constants
[0] );
1184 if (t
->constants
== NULL
) {
1185 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
1189 for (i
= 0; i
< program
->Parameters
->NumParameters
; i
++) {
1190 switch (program
->Parameters
->Parameters
[i
].Type
) {
1191 case PROGRAM_ENV_PARAM
:
1192 case PROGRAM_LOCAL_PARAM
:
1193 case PROGRAM_STATE_VAR
:
1194 case PROGRAM_UNIFORM
:
1195 t
->constants
[i
] = ureg_DECL_constant( ureg
, i
);
1198 /* Emit immediates only when there's no indirect addressing of
1200 * FIXME: Be smarter and recognize param arrays:
1201 * indirect addressing is only valid within the referenced
1204 case PROGRAM_CONSTANT
:
1205 if (program
->IndirectRegisterFiles
& PROGRAM_ANY_CONST
)
1206 t
->constants
[i
] = ureg_DECL_constant( ureg
, i
);
1209 ureg_DECL_immediate( ureg
,
1210 (const float*) program
->Parameters
->ParameterValues
[i
],
1219 /* texture samplers */
1220 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
1221 if (program
->SamplersUsed
& (1 << i
)) {
1222 t
->samplers
[i
] = ureg_DECL_sampler( ureg
, i
);
1226 /* Emit each instruction in turn:
1228 for (i
= 0; i
< program
->NumInstructions
; i
++) {
1229 set_insn_start( t
, ureg_get_instruction_number( ureg
));
1230 compile_instruction( t
, &program
->Instructions
[i
], clamp_color
);
1233 /* Fix up all emitted labels:
1235 for (i
= 0; i
< t
->labels_count
; i
++) {
1236 ureg_fixup_label( ureg
,
1238 t
->insn
[t
->labels
[i
].branch_target
] );
1247 debug_printf("%s: translate error flag set\n", __FUNCTION__
);
1255 * Tokens cannot be free with free otherwise the builtin gallium
1256 * malloc debugging will get confused.
1259 st_free_tokens(const struct tgsi_token
*tokens
)
1261 ureg_free_tokens(tokens
);