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_2D_MULTISAMPLE_INDEX
: return TGSI_TEXTURE_2D_MSAA
;
273 case TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: return TGSI_TEXTURE_2D_ARRAY_MSAA
;
274 case TEXTURE_BUFFER_INDEX
: return TGSI_TEXTURE_BUFFER
;
275 case TEXTURE_1D_INDEX
: return TGSI_TEXTURE_1D
;
276 case TEXTURE_2D_INDEX
: return TGSI_TEXTURE_2D
;
277 case TEXTURE_3D_INDEX
: return TGSI_TEXTURE_3D
;
278 case TEXTURE_CUBE_INDEX
: return TGSI_TEXTURE_CUBE
;
279 case TEXTURE_CUBE_ARRAY_INDEX
: return TGSI_TEXTURE_CUBE_ARRAY
;
280 case TEXTURE_RECT_INDEX
: return TGSI_TEXTURE_RECT
;
281 case TEXTURE_1D_ARRAY_INDEX
: return TGSI_TEXTURE_1D_ARRAY
;
282 case TEXTURE_2D_ARRAY_INDEX
: return TGSI_TEXTURE_2D_ARRAY
;
283 case TEXTURE_EXTERNAL_INDEX
: return TGSI_TEXTURE_2D
;
286 return TGSI_TEXTURE_1D
;
292 * Create a TGSI ureg_dst register from a Mesa dest register.
294 static struct ureg_dst
295 translate_dst( struct st_translate
*t
,
296 const struct prog_dst_register
*DstReg
,
300 struct ureg_dst dst
= dst_register( t
,
304 dst
= ureg_writemask( dst
,
308 dst
= ureg_saturate( dst
);
309 else if (clamp_color
&& DstReg
->File
== PROGRAM_OUTPUT
) {
310 /* Clamp colors for ARB_color_buffer_float. */
311 switch (t
->procType
) {
312 case TGSI_PROCESSOR_VERTEX
:
313 /* XXX if the geometry shader is present, this must be done there
314 * instead of here. */
315 if (DstReg
->Index
== VARYING_SLOT_COL0
||
316 DstReg
->Index
== VARYING_SLOT_COL1
||
317 DstReg
->Index
== VARYING_SLOT_BFC0
||
318 DstReg
->Index
== VARYING_SLOT_BFC1
) {
319 dst
= ureg_saturate(dst
);
323 case TGSI_PROCESSOR_FRAGMENT
:
324 if (DstReg
->Index
>= FRAG_RESULT_COLOR
) {
325 dst
= ureg_saturate(dst
);
332 dst
= ureg_dst_indirect( dst
, ureg_src(t
->address
[0]) );
339 * Create a TGSI ureg_src register from a Mesa src register.
341 static struct ureg_src
342 translate_src( struct st_translate
*t
,
343 const struct prog_src_register
*SrcReg
)
345 struct ureg_src src
= src_register( t
, SrcReg
->File
, SrcReg
->Index
);
347 if (t
->procType
== TGSI_PROCESSOR_GEOMETRY
&& SrcReg
->HasIndex2
) {
348 src
= src_register( t
, SrcReg
->File
, SrcReg
->Index2
);
349 if (SrcReg
->RelAddr2
)
350 src
= ureg_src_dimension_indirect( src
, ureg_src(t
->address
[0]),
353 src
= ureg_src_dimension( src
, SrcReg
->Index
);
356 src
= ureg_swizzle( src
,
357 GET_SWZ( SrcReg
->Swizzle
, 0 ) & 0x3,
358 GET_SWZ( SrcReg
->Swizzle
, 1 ) & 0x3,
359 GET_SWZ( SrcReg
->Swizzle
, 2 ) & 0x3,
360 GET_SWZ( SrcReg
->Swizzle
, 3 ) & 0x3);
362 if (SrcReg
->Negate
== NEGATE_XYZW
)
363 src
= ureg_negate(src
);
368 if (SrcReg
->RelAddr
) {
369 src
= ureg_src_indirect( src
, ureg_src(t
->address
[0]));
370 if (SrcReg
->File
!= PROGRAM_INPUT
&&
371 SrcReg
->File
!= PROGRAM_OUTPUT
) {
372 /* If SrcReg->Index was negative, it was set to zero in
373 * src_register(). Reassign it now. But don't do this
374 * for input/output regs since they get remapped while
375 * const buffers don't.
377 src
.Index
= SrcReg
->Index
;
385 static struct ureg_src
swizzle_4v( struct ureg_src src
,
386 const unsigned *swz
)
388 return ureg_swizzle( src
, swz
[0], swz
[1], swz
[2], swz
[3] );
393 * Translate a SWZ instruction into a MOV, MUL or MAD instruction. EG:
399 * MAD dst {1,-1,0,0}, src.xyxx, {0,0,1,0}
401 static void emit_swz( struct st_translate
*t
,
403 const struct prog_src_register
*SrcReg
)
405 struct ureg_program
*ureg
= t
->ureg
;
406 struct ureg_src src
= src_register( t
, SrcReg
->File
, SrcReg
->Index
);
408 unsigned negate_mask
= SrcReg
->Negate
;
410 unsigned one_mask
= ((GET_SWZ(SrcReg
->Swizzle
, 0) == SWIZZLE_ONE
) << 0 |
411 (GET_SWZ(SrcReg
->Swizzle
, 1) == SWIZZLE_ONE
) << 1 |
412 (GET_SWZ(SrcReg
->Swizzle
, 2) == SWIZZLE_ONE
) << 2 |
413 (GET_SWZ(SrcReg
->Swizzle
, 3) == SWIZZLE_ONE
) << 3);
415 unsigned zero_mask
= ((GET_SWZ(SrcReg
->Swizzle
, 0) == SWIZZLE_ZERO
) << 0 |
416 (GET_SWZ(SrcReg
->Swizzle
, 1) == SWIZZLE_ZERO
) << 1 |
417 (GET_SWZ(SrcReg
->Swizzle
, 2) == SWIZZLE_ZERO
) << 2 |
418 (GET_SWZ(SrcReg
->Swizzle
, 3) == SWIZZLE_ZERO
) << 3);
420 unsigned negative_one_mask
= one_mask
& negate_mask
;
421 unsigned positive_one_mask
= one_mask
& ~negate_mask
;
425 unsigned mul_swizzle
[4] = {0,0,0,0};
426 unsigned add_swizzle
[4] = {0,0,0,0};
427 unsigned src_swizzle
[4] = {0,0,0,0};
428 boolean need_add
= FALSE
;
429 boolean need_mul
= FALSE
;
431 if (dst
.WriteMask
== 0)
434 /* Is this just a MOV?
436 if (zero_mask
== 0 &&
438 (negate_mask
== 0 || negate_mask
== TGSI_WRITEMASK_XYZW
))
440 ureg_MOV( ureg
, dst
, translate_src( t
, SrcReg
));
446 #define IMM_NEG_ONE 2
448 imm
= ureg_imm3f( ureg
, 0, 1, -1 );
450 for (i
= 0; i
< 4; i
++) {
451 unsigned bit
= 1 << i
;
453 if (dst
.WriteMask
& bit
) {
454 if (positive_one_mask
& bit
) {
455 mul_swizzle
[i
] = IMM_ZERO
;
456 add_swizzle
[i
] = IMM_ONE
;
459 else if (negative_one_mask
& bit
) {
460 mul_swizzle
[i
] = IMM_ZERO
;
461 add_swizzle
[i
] = IMM_NEG_ONE
;
464 else if (zero_mask
& bit
) {
465 mul_swizzle
[i
] = IMM_ZERO
;
466 add_swizzle
[i
] = IMM_ZERO
;
470 add_swizzle
[i
] = IMM_ZERO
;
471 src_swizzle
[i
] = GET_SWZ(SrcReg
->Swizzle
, i
);
473 if (negate_mask
& bit
) {
474 mul_swizzle
[i
] = IMM_NEG_ONE
;
477 mul_swizzle
[i
] = IMM_ONE
;
483 if (need_mul
&& need_add
) {
486 swizzle_4v( src
, src_swizzle
),
487 swizzle_4v( imm
, mul_swizzle
),
488 swizzle_4v( imm
, add_swizzle
) );
493 swizzle_4v( src
, src_swizzle
),
494 swizzle_4v( imm
, mul_swizzle
) );
499 swizzle_4v( imm
, add_swizzle
) );
512 * Negate the value of DDY to match GL semantics where (0,0) is the
513 * lower-left corner of the window.
514 * Note that the GL_ARB_fragment_coord_conventions extension will
515 * effect this someday.
517 static void emit_ddy( struct st_translate
*t
,
519 const struct prog_src_register
*SrcReg
)
521 struct ureg_program
*ureg
= t
->ureg
;
522 struct ureg_src src
= translate_src( t
, SrcReg
);
523 src
= ureg_negate( src
);
524 ureg_DDY( ureg
, dst
, src
);
530 translate_opcode( unsigned op
)
534 return TGSI_OPCODE_ARL
;
536 return TGSI_OPCODE_ABS
;
538 return TGSI_OPCODE_ADD
;
540 return TGSI_OPCODE_BGNLOOP
;
542 return TGSI_OPCODE_BGNSUB
;
544 return TGSI_OPCODE_BRK
;
546 return TGSI_OPCODE_CAL
;
548 return TGSI_OPCODE_CMP
;
550 return TGSI_OPCODE_CONT
;
552 return TGSI_OPCODE_COS
;
554 return TGSI_OPCODE_DDX
;
556 return TGSI_OPCODE_DDY
;
558 return TGSI_OPCODE_DP2
;
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_KILL_IF
;
590 /* XXX we don't support condition codes in TGSI */
591 return TGSI_OPCODE_KILL
;
593 return TGSI_OPCODE_LG2
;
595 return TGSI_OPCODE_LOG
;
597 return TGSI_OPCODE_LIT
;
599 return TGSI_OPCODE_LRP
;
601 return TGSI_OPCODE_MAD
;
603 return TGSI_OPCODE_MAX
;
605 return TGSI_OPCODE_MIN
;
607 return TGSI_OPCODE_MOV
;
609 return TGSI_OPCODE_MUL
;
611 return TGSI_OPCODE_NOP
;
613 return TGSI_OPCODE_POW
;
615 return TGSI_OPCODE_RCP
;
617 return TGSI_OPCODE_RET
;
619 return TGSI_OPCODE_SCS
;
621 return TGSI_OPCODE_SEQ
;
623 return TGSI_OPCODE_SGE
;
625 return TGSI_OPCODE_SGT
;
627 return TGSI_OPCODE_SIN
;
629 return TGSI_OPCODE_SLE
;
631 return TGSI_OPCODE_SLT
;
633 return TGSI_OPCODE_SNE
;
635 return TGSI_OPCODE_SSG
;
637 return TGSI_OPCODE_SUB
;
639 return TGSI_OPCODE_TEX
;
641 return TGSI_OPCODE_TXB
;
643 return TGSI_OPCODE_TXD
;
645 return TGSI_OPCODE_TXL
;
647 return TGSI_OPCODE_TXP
;
649 return TGSI_OPCODE_XPD
;
651 return TGSI_OPCODE_END
;
654 return TGSI_OPCODE_NOP
;
661 struct gl_context
*ctx
,
662 struct st_translate
*t
,
663 const struct prog_instruction
*inst
,
664 boolean clamp_dst_color_output
)
666 struct ureg_program
*ureg
= t
->ureg
;
668 struct ureg_dst dst
[1] = { { 0 } };
669 struct ureg_src src
[4];
673 num_dst
= _mesa_num_inst_dst_regs( inst
->Opcode
);
674 num_src
= _mesa_num_inst_src_regs( inst
->Opcode
);
677 dst
[0] = translate_dst( t
,
680 clamp_dst_color_output
);
682 for (i
= 0; i
< num_src
; i
++)
683 src
[i
] = translate_src( t
, &inst
->SrcReg
[i
] );
685 switch( inst
->Opcode
) {
687 emit_swz( t
, dst
[0], &inst
->SrcReg
[0] );
694 debug_assert(num_dst
== 0);
695 ureg_label_insn( ureg
,
696 translate_opcode( inst
->Opcode
),
698 get_label( t
, inst
->BranchTarget
));
702 debug_assert(num_dst
== 0);
703 ureg_label_insn( ureg
,
704 ctx
->Const
.NativeIntegers
? TGSI_OPCODE_UIF
: TGSI_OPCODE_IF
,
706 get_label( t
, inst
->BranchTarget
));
714 src
[num_src
++] = t
->samplers
[inst
->TexSrcUnit
];
716 translate_opcode( inst
->Opcode
),
718 st_translate_texture_target( inst
->TexSrcTarget
,
725 dst
[0] = ureg_writemask(dst
[0], TGSI_WRITEMASK_XY
);
727 translate_opcode( inst
->Opcode
),
733 dst
[0] = ureg_writemask(dst
[0], TGSI_WRITEMASK_XYZ
);
735 translate_opcode( inst
->Opcode
),
744 /* At some point, a motivated person could add a better
745 * implementation of noise. Currently not even the nvidia
746 * binary drivers do anything more than this. In any case, the
747 * place to do this is in the GL state tracker, not the poor
750 ureg_MOV( ureg
, dst
[0], ureg_imm1f(ureg
, 0.5) );
754 emit_ddy( t
, dst
[0], &inst
->SrcReg
[0] );
758 ureg_RSQ( ureg
, dst
[0], ureg_abs(src
[0]) );
763 translate_opcode( inst
->Opcode
),
772 * Emit the TGSI instructions for inverting and adjusting WPOS.
773 * This code is unavoidable because it also depends on whether
774 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
777 emit_wpos_adjustment( struct st_translate
*t
,
778 const struct gl_program
*program
,
780 GLfloat adjX
, GLfloat adjY
[2])
782 struct ureg_program
*ureg
= t
->ureg
;
784 /* Fragment program uses fragment position input.
785 * Need to replace instances of INPUT[WPOS] with temp T
786 * where T = INPUT[WPOS] by y is inverted.
788 static const gl_state_index wposTransformState
[STATE_LENGTH
]
789 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
, 0, 0, 0 };
791 /* XXX: note we are modifying the incoming shader here! Need to
792 * do this before emitting the constant decls below, or this
795 unsigned wposTransConst
= _mesa_add_state_reference(program
->Parameters
,
798 struct ureg_src wpostrans
= ureg_DECL_constant( ureg
, wposTransConst
);
799 struct ureg_dst wpos_temp
= ureg_DECL_temporary( ureg
);
800 struct ureg_src wpos_input
= t
->inputs
[t
->inputMapping
[VARYING_SLOT_POS
]];
802 /* First, apply the coordinate shift: */
803 if (adjX
|| adjY
[0] || adjY
[1]) {
804 if (adjY
[0] != adjY
[1]) {
805 /* Adjust the y coordinate by adjY[1] or adjY[0] respectively
806 * depending on whether inversion is actually going to be applied
807 * or not, which is determined by testing against the inversion
808 * state variable used below, which will be either +1 or -1.
810 struct ureg_dst adj_temp
= ureg_DECL_temporary(ureg
);
812 ureg_CMP(ureg
, adj_temp
,
813 ureg_scalar(wpostrans
, invert
? 2 : 0),
814 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
),
815 ureg_imm4f(ureg
, adjX
, adjY
[1], 0.0f
, 0.0f
));
816 ureg_ADD(ureg
, wpos_temp
, wpos_input
, ureg_src(adj_temp
));
818 ureg_ADD(ureg
, wpos_temp
, wpos_input
,
819 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
));
821 wpos_input
= ureg_src(wpos_temp
);
823 /* MOV wpos_temp, input[wpos]
825 ureg_MOV( ureg
, wpos_temp
, wpos_input
);
828 /* Now the conditional y flip: STATE_FB_WPOS_Y_TRANSFORM.xy/zw will be
829 * inversion/identity, or the other way around if we're drawing to an FBO.
832 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
835 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
837 ureg_scalar(wpostrans
, 0),
838 ureg_scalar(wpostrans
, 1));
840 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
843 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
845 ureg_scalar(wpostrans
, 2),
846 ureg_scalar(wpostrans
, 3));
849 /* Use wpos_temp as position input from here on:
851 t
->inputs
[t
->inputMapping
[VARYING_SLOT_POS
]] = ureg_src(wpos_temp
);
856 * Emit fragment position/ooordinate code.
859 emit_wpos(struct st_context
*st
,
860 struct st_translate
*t
,
861 const struct gl_program
*program
,
862 struct ureg_program
*ureg
)
864 const struct gl_fragment_program
*fp
=
865 (const struct gl_fragment_program
*) program
;
866 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
868 GLfloat adjY
[2] = { 0.0f
, 0.0f
};
869 boolean invert
= FALSE
;
871 /* Query the pixel center conventions supported by the pipe driver and set
872 * adjX, adjY to help out if it cannot handle the requested one internally.
874 * The bias of the y-coordinate depends on whether y-inversion takes place
875 * (adjY[1]) or not (adjY[0]), which is in turn dependent on whether we are
876 * drawing to an FBO (causes additional inversion), and whether the the pipe
877 * driver origin and the requested origin differ (the latter condition is
878 * stored in the 'invert' variable).
880 * For height = 100 (i = integer, h = half-integer, l = lower, u = upper):
887 * l,i -> u,i: ( 0.0 + 1.0) * -1 + 100 = 99
888 * l,h -> u,h: ( 0.5 + 0.0) * -1 + 100 = 99.5
889 * u,i -> l,i: (99.0 + 1.0) * -1 + 100 = 0
890 * u,h -> l,h: (99.5 + 0.0) * -1 + 100 = 0.5
892 * inversion and center shift:
893 * l,i -> u,h: ( 0.0 + 0.5) * -1 + 100 = 99.5
894 * l,h -> u,i: ( 0.5 + 0.5) * -1 + 100 = 99
895 * u,i -> l,h: (99.0 + 0.5) * -1 + 100 = 0.5
896 * u,h -> l,i: (99.5 + 0.5) * -1 + 100 = 0
898 if (fp
->OriginUpperLeft
) {
899 /* Fragment shader wants origin in upper-left */
900 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
)) {
901 /* the driver supports upper-left origin */
903 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
)) {
904 /* the driver supports lower-left origin, need to invert Y */
905 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
912 /* Fragment shader wants origin in lower-left */
913 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
))
914 /* the driver supports lower-left origin */
915 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
916 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
))
917 /* the driver supports upper-left origin, need to invert Y */
923 if (fp
->PixelCenterInteger
) {
924 /* Fragment shader wants pixel center integer */
925 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
926 /* the driver supports pixel center integer */
928 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
930 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
931 /* the driver supports pixel center half integer, need to bias X,Y */
940 /* Fragment shader wants pixel center half integer */
941 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
942 /* the driver supports pixel center half integer */
944 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
945 /* the driver supports pixel center integer, need to bias X,Y */
946 adjX
= adjY
[0] = adjY
[1] = 0.5f
;
947 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
953 /* we invert after adjustment so that we avoid the MOV to temporary,
954 * and reuse the adjustment ADD instead */
955 emit_wpos_adjustment(t
, program
, invert
, adjX
, adjY
);
960 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
961 * TGSI uses +1 for front, -1 for back.
962 * This function converts the TGSI value to the GL value. Simply clamping/
963 * saturating the value to [0,1] does the job.
966 emit_face_var( struct st_translate
*t
,
967 const struct gl_program
*program
)
969 struct ureg_program
*ureg
= t
->ureg
;
970 struct ureg_dst face_temp
= ureg_DECL_temporary( ureg
);
971 struct ureg_src face_input
= t
->inputs
[t
->inputMapping
[VARYING_SLOT_FACE
]];
973 /* MOV_SAT face_temp, input[face]
975 face_temp
= ureg_saturate( face_temp
);
976 ureg_MOV( ureg
, face_temp
, face_input
);
978 /* Use face_temp as face input from here on:
980 t
->inputs
[t
->inputMapping
[VARYING_SLOT_FACE
]] = ureg_src(face_temp
);
985 emit_edgeflags( struct st_translate
*t
,
986 const struct gl_program
*program
)
988 struct ureg_program
*ureg
= t
->ureg
;
989 struct ureg_dst edge_dst
= t
->outputs
[t
->outputMapping
[VARYING_SLOT_EDGE
]];
990 struct ureg_src edge_src
= t
->inputs
[t
->inputMapping
[VERT_ATTRIB_EDGEFLAG
]];
992 ureg_MOV( ureg
, edge_dst
, edge_src
);
997 * Translate Mesa program to TGSI format.
998 * \param program the program to translate
999 * \param numInputs number of input registers used
1000 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
1002 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
1003 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
1005 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
1006 * \param numOutputs number of output registers used
1007 * \param outputMapping maps Mesa fragment program outputs to TGSI
1009 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
1010 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
1013 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
1016 st_translate_mesa_program(
1017 struct gl_context
*ctx
,
1019 struct ureg_program
*ureg
,
1020 const struct gl_program
*program
,
1022 const GLuint inputMapping
[],
1023 const ubyte inputSemanticName
[],
1024 const ubyte inputSemanticIndex
[],
1025 const GLuint interpMode
[],
1027 const GLuint outputMapping
[],
1028 const ubyte outputSemanticName
[],
1029 const ubyte outputSemanticIndex
[],
1030 boolean passthrough_edgeflags
,
1031 boolean clamp_color
)
1033 struct st_translate translate
, *t
;
1035 enum pipe_error ret
= PIPE_OK
;
1037 assert(numInputs
<= Elements(t
->inputs
));
1038 assert(numOutputs
<= Elements(t
->outputs
));
1041 memset(t
, 0, sizeof *t
);
1043 t
->procType
= procType
;
1044 t
->inputMapping
= inputMapping
;
1045 t
->outputMapping
= outputMapping
;
1048 /*_mesa_print_program(program);*/
1051 * Declare input attributes.
1053 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
1054 for (i
= 0; i
< numInputs
; i
++) {
1055 t
->inputs
[i
] = ureg_DECL_fs_input(ureg
,
1056 inputSemanticName
[i
],
1057 inputSemanticIndex
[i
],
1061 if (program
->InputsRead
& VARYING_BIT_POS
) {
1062 /* Must do this after setting up t->inputs, and before
1063 * emitting constant references, below:
1065 emit_wpos(st_context(ctx
), t
, program
, ureg
);
1068 if (program
->InputsRead
& VARYING_BIT_FACE
) {
1069 emit_face_var( t
, program
);
1073 * Declare output attributes.
1075 for (i
= 0; i
< numOutputs
; i
++) {
1076 switch (outputSemanticName
[i
]) {
1077 case TGSI_SEMANTIC_POSITION
:
1078 t
->outputs
[i
] = ureg_DECL_output( ureg
,
1079 TGSI_SEMANTIC_POSITION
, /* Z / Depth */
1080 outputSemanticIndex
[i
] );
1082 t
->outputs
[i
] = ureg_writemask( t
->outputs
[i
],
1085 case TGSI_SEMANTIC_STENCIL
:
1086 t
->outputs
[i
] = ureg_DECL_output( ureg
,
1087 TGSI_SEMANTIC_STENCIL
, /* Stencil */
1088 outputSemanticIndex
[i
] );
1089 t
->outputs
[i
] = ureg_writemask( t
->outputs
[i
],
1092 case TGSI_SEMANTIC_COLOR
:
1093 t
->outputs
[i
] = ureg_DECL_output( ureg
,
1094 TGSI_SEMANTIC_COLOR
,
1095 outputSemanticIndex
[i
] );
1103 else if (procType
== TGSI_PROCESSOR_GEOMETRY
) {
1104 for (i
= 0; i
< numInputs
; i
++) {
1105 t
->inputs
[i
] = ureg_DECL_gs_input(ureg
,
1107 inputSemanticName
[i
],
1108 inputSemanticIndex
[i
]);
1111 for (i
= 0; i
< numOutputs
; i
++) {
1112 t
->outputs
[i
] = ureg_DECL_output( ureg
,
1113 outputSemanticName
[i
],
1114 outputSemanticIndex
[i
] );
1118 assert(procType
== TGSI_PROCESSOR_VERTEX
);
1120 for (i
= 0; i
< numInputs
; i
++) {
1121 t
->inputs
[i
] = ureg_DECL_vs_input(ureg
, i
);
1124 for (i
= 0; i
< numOutputs
; i
++) {
1125 t
->outputs
[i
] = ureg_DECL_output( ureg
,
1126 outputSemanticName
[i
],
1127 outputSemanticIndex
[i
] );
1129 if (passthrough_edgeflags
)
1130 emit_edgeflags( t
, program
);
1133 /* Declare address register.
1135 if (program
->NumAddressRegs
> 0) {
1136 debug_assert( program
->NumAddressRegs
== 1 );
1137 t
->address
[0] = ureg_DECL_address( ureg
);
1140 /* Declare misc input registers
1143 GLbitfield sysInputs
= program
->SystemValuesRead
;
1144 unsigned numSys
= 0;
1145 for (i
= 0; sysInputs
; i
++) {
1146 if (sysInputs
& (1 << i
)) {
1147 unsigned semName
= mesa_sysval_to_semantic
[i
];
1148 t
->systemValues
[i
] = ureg_DECL_system_value(ureg
, numSys
, semName
, 0);
1149 if (semName
== TGSI_SEMANTIC_INSTANCEID
||
1150 semName
== TGSI_SEMANTIC_VERTEXID
) {
1151 /* From Gallium perspective, these system values are always
1152 * integer, and require native integer support. However, if
1153 * native integer is supported on the vertex stage but not the
1154 * pixel stage (e.g, i915g + draw), Mesa will generate IR that
1155 * assumes these system values are floats. To resolve the
1156 * inconsistency, we insert a U2F.
1158 struct st_context
*st
= st_context(ctx
);
1159 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
1160 assert(procType
== TGSI_PROCESSOR_VERTEX
);
1161 assert(pscreen
->get_shader_param(pscreen
, PIPE_SHADER_VERTEX
, PIPE_SHADER_CAP_INTEGERS
));
1162 (void) pscreen
; /* silence non-debug build warnings */
1163 if (!ctx
->Const
.NativeIntegers
) {
1164 struct ureg_dst temp
= ureg_DECL_local_temporary(t
->ureg
);
1165 ureg_U2F( t
->ureg
, ureg_writemask(temp
, TGSI_WRITEMASK_X
), t
->systemValues
[i
]);
1166 t
->systemValues
[i
] = ureg_scalar(ureg_src(temp
), 0);
1170 sysInputs
&= ~(1 << i
);
1175 if (program
->IndirectRegisterFiles
& (1 << PROGRAM_TEMPORARY
)) {
1176 /* If temps are accessed with indirect addressing, declare temporaries
1177 * in sequential order. Else, we declare them on demand elsewhere.
1179 for (i
= 0; i
< program
->NumTemporaries
; i
++) {
1180 /* XXX use TGSI_FILE_TEMPORARY_ARRAY when it's supported by ureg */
1181 t
->temps
[i
] = ureg_DECL_temporary( t
->ureg
);
1185 /* Emit constants and immediates. Mesa uses a single index space
1186 * for these, so we put all the translated regs in t->constants.
1188 if (program
->Parameters
) {
1189 t
->constants
= calloc( program
->Parameters
->NumParameters
,
1190 sizeof t
->constants
[0] );
1191 if (t
->constants
== NULL
) {
1192 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
1196 for (i
= 0; i
< program
->Parameters
->NumParameters
; i
++) {
1197 switch (program
->Parameters
->Parameters
[i
].Type
) {
1198 case PROGRAM_ENV_PARAM
:
1199 case PROGRAM_LOCAL_PARAM
:
1200 case PROGRAM_STATE_VAR
:
1201 case PROGRAM_UNIFORM
:
1202 t
->constants
[i
] = ureg_DECL_constant( ureg
, i
);
1205 /* Emit immediates only when there's no indirect addressing of
1207 * FIXME: Be smarter and recognize param arrays:
1208 * indirect addressing is only valid within the referenced
1211 case PROGRAM_CONSTANT
:
1212 if (program
->IndirectRegisterFiles
& PROGRAM_ANY_CONST
)
1213 t
->constants
[i
] = ureg_DECL_constant( ureg
, i
);
1216 ureg_DECL_immediate( ureg
,
1217 (const float*) program
->Parameters
->ParameterValues
[i
],
1226 /* texture samplers */
1227 for (i
= 0; i
< ctx
->Const
.FragmentProgram
.MaxTextureImageUnits
; i
++) {
1228 if (program
->SamplersUsed
& (1 << i
)) {
1229 t
->samplers
[i
] = ureg_DECL_sampler( ureg
, i
);
1233 /* Emit each instruction in turn:
1235 for (i
= 0; i
< program
->NumInstructions
; i
++) {
1236 set_insn_start( t
, ureg_get_instruction_number( ureg
));
1237 compile_instruction( ctx
, t
, &program
->Instructions
[i
], clamp_color
);
1240 /* Fix up all emitted labels:
1242 for (i
= 0; i
< t
->labels_count
; i
++) {
1243 ureg_fixup_label( ureg
,
1245 t
->insn
[t
->labels
[i
].branch_target
] );
1254 debug_printf("%s: translate error flag set\n", __FUNCTION__
);
1262 * Tokens cannot be free with free otherwise the builtin gallium
1263 * malloc debugging will get confused.
1266 st_free_tokens(const struct tgsi_token
*tokens
)
1268 ureg_free_tokens(tokens
);