1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 **********************************************************/
27 #include "pipe/p_shader_tokens.h"
28 #include "tgsi/tgsi_parse.h"
29 #include "util/u_memory.h"
31 #include "svga_tgsi_emit.h"
32 #include "svga_context.h"
35 static boolean
emit_vs_postamble( struct svga_shader_emitter
*emit
);
36 static boolean
emit_ps_postamble( struct svga_shader_emitter
*emit
);
46 case TGSI_OPCODE_ABS
: return SVGA3DOP_ABS
;
47 case TGSI_OPCODE_ADD
: return SVGA3DOP_ADD
;
48 case TGSI_OPCODE_BREAKC
: return SVGA3DOP_BREAKC
;
49 case TGSI_OPCODE_DP2A
: return SVGA3DOP_DP2ADD
;
50 case TGSI_OPCODE_DP3
: return SVGA3DOP_DP3
;
51 case TGSI_OPCODE_DP4
: return SVGA3DOP_DP4
;
52 case TGSI_OPCODE_ENDFOR
: return SVGA3DOP_ENDLOOP
;
53 case TGSI_OPCODE_FRC
: return SVGA3DOP_FRC
;
54 case TGSI_OPCODE_BGNFOR
: return SVGA3DOP_LOOP
;
55 case TGSI_OPCODE_MAD
: return SVGA3DOP_MAD
;
56 case TGSI_OPCODE_MAX
: return SVGA3DOP_MAX
;
57 case TGSI_OPCODE_MIN
: return SVGA3DOP_MIN
;
58 case TGSI_OPCODE_MOV
: return SVGA3DOP_MOV
;
59 case TGSI_OPCODE_MUL
: return SVGA3DOP_MUL
;
60 case TGSI_OPCODE_NOP
: return SVGA3DOP_NOP
;
61 case TGSI_OPCODE_NRM4
: return SVGA3DOP_NRM
;
62 case TGSI_OPCODE_SSG
: return SVGA3DOP_SGN
;
64 debug_printf("Unkown opcode %u\n", opcode
);
66 return SVGA3DOP_LAST_INST
;
71 static unsigned translate_file( unsigned file
)
74 case TGSI_FILE_TEMPORARY
: return SVGA3DREG_TEMP
;
75 case TGSI_FILE_INPUT
: return SVGA3DREG_INPUT
;
76 case TGSI_FILE_OUTPUT
: return SVGA3DREG_OUTPUT
; /* VS3.0+ only */
77 case TGSI_FILE_IMMEDIATE
: return SVGA3DREG_CONST
;
78 case TGSI_FILE_CONSTANT
: return SVGA3DREG_CONST
;
79 case TGSI_FILE_SAMPLER
: return SVGA3DREG_SAMPLER
;
80 case TGSI_FILE_ADDRESS
: return SVGA3DREG_ADDR
;
83 return SVGA3DREG_TEMP
;
92 static SVGA3dShaderDestToken
93 translate_dst_register( struct svga_shader_emitter
*emit
,
94 const struct tgsi_full_instruction
*insn
,
97 const struct tgsi_full_dst_register
*reg
= &insn
->Dst
[idx
];
98 SVGA3dShaderDestToken dest
;
100 switch (reg
->Register
.File
) {
101 case TGSI_FILE_OUTPUT
:
102 /* Output registers encode semantic information in their name.
103 * Need to lookup a table built at decl time:
105 dest
= emit
->output_map
[reg
->Register
.Index
];
109 dest
= dst_register( translate_file( reg
->Register
.File
),
110 reg
->Register
.Index
);
114 dest
.mask
= reg
->Register
.WriteMask
;
117 if (insn
->Instruction
.Saturate
)
118 dest
.dstMod
= SVGA3DDSTMOD_SATURATE
;
124 static struct src_register
125 swizzle( struct src_register src
,
131 x
= (src
.base
.swizzle
>> (x
* 2)) & 0x3;
132 y
= (src
.base
.swizzle
>> (y
* 2)) & 0x3;
133 z
= (src
.base
.swizzle
>> (z
* 2)) & 0x3;
134 w
= (src
.base
.swizzle
>> (w
* 2)) & 0x3;
136 src
.base
.swizzle
= TRANSLATE_SWIZZLE(x
,y
,z
,w
);
141 static struct src_register
142 scalar( struct src_register src
,
145 return swizzle( src
, comp
, comp
, comp
, comp
);
148 static INLINE boolean
149 svga_arl_needs_adjustment( const struct svga_shader_emitter
*emit
)
153 for (i
= 0; i
< emit
->num_arl_consts
; ++i
) {
154 if (emit
->arl_consts
[i
].arl_num
== emit
->current_arl
)
161 svga_arl_adjustment( const struct svga_shader_emitter
*emit
)
165 for (i
= 0; i
< emit
->num_arl_consts
; ++i
) {
166 if (emit
->arl_consts
[i
].arl_num
== emit
->current_arl
)
167 return emit
->arl_consts
[i
].number
;
172 static struct src_register
173 translate_src_register( const struct svga_shader_emitter
*emit
,
174 const struct tgsi_full_src_register
*reg
)
176 struct src_register src
;
178 switch (reg
->Register
.File
) {
179 case TGSI_FILE_INPUT
:
180 /* Input registers are referred to by their semantic name rather
181 * than by index. Use the mapping build up from the decls:
183 src
= emit
->input_map
[reg
->Register
.Index
];
186 case TGSI_FILE_IMMEDIATE
:
187 /* Immediates are appended after TGSI constants in the D3D
190 src
= src_register( translate_file( reg
->Register
.File
),
191 reg
->Register
.Index
+
196 src
= src_register( translate_file( reg
->Register
.File
),
197 reg
->Register
.Index
);
202 /* Indirect addressing (for coninstant buffer lookups only)
204 if (reg
->Register
.Indirect
)
206 /* we shift the offset towards the minimum */
207 if (svga_arl_needs_adjustment( emit
)) {
208 src
.base
.num
-= svga_arl_adjustment( emit
);
210 src
.base
.relAddr
= 1;
212 /* Not really sure what should go in the second token:
214 src
.indirect
= src_token( SVGA3DREG_ADDR
,
215 reg
->Indirect
.Index
);
217 src
.indirect
.swizzle
= SWIZZLE_XXXX
;
221 reg
->Register
.SwizzleX
,
222 reg
->Register
.SwizzleY
,
223 reg
->Register
.SwizzleZ
,
224 reg
->Register
.SwizzleW
);
226 /* src.mod isn't a bitfield, unfortunately:
227 * See tgsi_util_get_full_src_register_sign_mode for implementation details.
229 if (reg
->Register
.Absolute
) {
230 if (reg
->Register
.Negate
)
231 src
.base
.srcMod
= SVGA3DSRCMOD_ABSNEG
;
233 src
.base
.srcMod
= SVGA3DSRCMOD_ABS
;
236 if (reg
->Register
.Negate
)
237 src
.base
.srcMod
= SVGA3DSRCMOD_NEG
;
239 src
.base
.srcMod
= SVGA3DSRCMOD_NONE
;
247 * Get a temporary register, return -1 if none available
249 static INLINE SVGA3dShaderDestToken
250 get_temp( struct svga_shader_emitter
*emit
)
252 int i
= emit
->nr_hw_temp
+ emit
->internal_temp_count
++;
254 return dst_register( SVGA3DREG_TEMP
, i
);
257 /* Release a single temp. Currently only effective if it was the last
258 * allocated temp, otherwise release will be delayed until the next
259 * call to reset_temp_regs().
262 release_temp( struct svga_shader_emitter
*emit
,
263 SVGA3dShaderDestToken temp
)
265 if (temp
.num
== emit
->internal_temp_count
- 1)
266 emit
->internal_temp_count
--;
269 static void reset_temp_regs( struct svga_shader_emitter
*emit
)
271 emit
->internal_temp_count
= 0;
275 static boolean
submit_op0( struct svga_shader_emitter
*emit
,
276 SVGA3dShaderInstToken inst
,
277 SVGA3dShaderDestToken dest
)
279 return (emit_instruction( emit
, inst
) &&
280 emit_dst( emit
, dest
));
283 static boolean
submit_op1( struct svga_shader_emitter
*emit
,
284 SVGA3dShaderInstToken inst
,
285 SVGA3dShaderDestToken dest
,
286 struct src_register src0
)
288 return emit_op1( emit
, inst
, dest
, src0
);
292 /* SVGA shaders may not refer to >1 constant register in a single
293 * instruction. This function checks for that usage and inserts a
294 * move to temporary if detected.
296 * The same applies to input registers -- at most a single input
297 * register may be read by any instruction.
299 static boolean
submit_op2( struct svga_shader_emitter
*emit
,
300 SVGA3dShaderInstToken inst
,
301 SVGA3dShaderDestToken dest
,
302 struct src_register src0
,
303 struct src_register src1
)
305 SVGA3dShaderDestToken temp
;
306 SVGA3dShaderRegType type0
, type1
;
307 boolean need_temp
= FALSE
;
310 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
311 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
313 if (type0
== SVGA3DREG_CONST
&&
314 type1
== SVGA3DREG_CONST
&&
315 src0
.base
.num
!= src1
.base
.num
)
318 if (type0
== SVGA3DREG_INPUT
&&
319 type1
== SVGA3DREG_INPUT
&&
320 src0
.base
.num
!= src1
.base
.num
)
325 temp
= get_temp( emit
);
327 if (!emit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp
, src0
))
333 if (!emit_op2( emit
, inst
, dest
, src0
, src1
))
337 release_temp( emit
, temp
);
343 /* SVGA shaders may not refer to >1 constant register in a single
344 * instruction. This function checks for that usage and inserts a
345 * move to temporary if detected.
347 static boolean
submit_op3( struct svga_shader_emitter
*emit
,
348 SVGA3dShaderInstToken inst
,
349 SVGA3dShaderDestToken dest
,
350 struct src_register src0
,
351 struct src_register src1
,
352 struct src_register src2
)
354 SVGA3dShaderDestToken temp0
;
355 SVGA3dShaderDestToken temp1
;
356 boolean need_temp0
= FALSE
;
357 boolean need_temp1
= FALSE
;
358 SVGA3dShaderRegType type0
, type1
, type2
;
362 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
363 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
364 type2
= SVGA3dShaderGetRegType( src2
.base
.value
);
366 if (inst
.op
!= SVGA3DOP_SINCOS
) {
367 if (type0
== SVGA3DREG_CONST
&&
368 ((type1
== SVGA3DREG_CONST
&& src0
.base
.num
!= src1
.base
.num
) ||
369 (type2
== SVGA3DREG_CONST
&& src0
.base
.num
!= src2
.base
.num
)))
372 if (type1
== SVGA3DREG_CONST
&&
373 (type2
== SVGA3DREG_CONST
&& src1
.base
.num
!= src2
.base
.num
))
377 if (type0
== SVGA3DREG_INPUT
&&
378 ((type1
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src1
.base
.num
) ||
379 (type2
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src2
.base
.num
)))
382 if (type1
== SVGA3DREG_INPUT
&&
383 (type2
== SVGA3DREG_INPUT
&& src1
.base
.num
!= src2
.base
.num
))
388 temp0
= get_temp( emit
);
390 if (!emit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp0
, src0
))
398 temp1
= get_temp( emit
);
400 if (!emit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp1
, src1
))
406 if (!emit_op3( emit
, inst
, dest
, src0
, src1
, src2
))
410 release_temp( emit
, temp1
);
412 release_temp( emit
, temp0
);
419 /* SVGA shaders may not refer to >1 constant register in a single
420 * instruction. This function checks for that usage and inserts a
421 * move to temporary if detected.
423 static boolean
submit_op4( struct svga_shader_emitter
*emit
,
424 SVGA3dShaderInstToken inst
,
425 SVGA3dShaderDestToken dest
,
426 struct src_register src0
,
427 struct src_register src1
,
428 struct src_register src2
,
429 struct src_register src3
)
431 SVGA3dShaderDestToken temp0
;
432 SVGA3dShaderDestToken temp3
;
433 boolean need_temp0
= FALSE
;
434 boolean need_temp3
= FALSE
;
435 SVGA3dShaderRegType type0
, type1
, type2
, type3
;
439 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
440 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
441 type2
= SVGA3dShaderGetRegType( src2
.base
.value
);
442 type3
= SVGA3dShaderGetRegType( src2
.base
.value
);
444 /* Make life a little easier - this is only used by the TXD
445 * instruction which is guaranteed not to have a constant/input reg
446 * in one slot at least:
448 assert(type1
== SVGA3DREG_SAMPLER
);
450 if (type0
== SVGA3DREG_CONST
&&
451 ((type3
== SVGA3DREG_CONST
&& src0
.base
.num
!= src3
.base
.num
) ||
452 (type2
== SVGA3DREG_CONST
&& src0
.base
.num
!= src2
.base
.num
)))
455 if (type3
== SVGA3DREG_CONST
&&
456 (type2
== SVGA3DREG_CONST
&& src3
.base
.num
!= src2
.base
.num
))
459 if (type0
== SVGA3DREG_INPUT
&&
460 ((type3
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src3
.base
.num
) ||
461 (type2
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src2
.base
.num
)))
464 if (type3
== SVGA3DREG_INPUT
&&
465 (type2
== SVGA3DREG_INPUT
&& src3
.base
.num
!= src2
.base
.num
))
470 temp0
= get_temp( emit
);
472 if (!emit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp0
, src0
))
480 temp3
= get_temp( emit
);
482 if (!emit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp3
, src3
))
488 if (!emit_op4( emit
, inst
, dest
, src0
, src1
, src2
, src3
))
492 release_temp( emit
, temp3
);
494 release_temp( emit
, temp0
);
499 static boolean
emit_def_const( struct svga_shader_emitter
*emit
,
500 SVGA3dShaderConstType type
,
508 SVGA3dShaderInstToken opcode
;
511 case SVGA3D_CONST_TYPE_FLOAT
:
512 opcode
= inst_token( SVGA3DOP_DEF
);
513 def
.dst
= dst_register( SVGA3DREG_CONST
, idx
);
514 def
.constValues
[0] = a
;
515 def
.constValues
[1] = b
;
516 def
.constValues
[2] = c
;
517 def
.constValues
[3] = d
;
519 case SVGA3D_CONST_TYPE_INT
:
520 opcode
= inst_token( SVGA3DOP_DEFI
);
521 def
.dst
= dst_register( SVGA3DREG_CONSTINT
, idx
);
522 def
.constIValues
[0] = (int)a
;
523 def
.constIValues
[1] = (int)b
;
524 def
.constIValues
[2] = (int)c
;
525 def
.constIValues
[3] = (int)d
;
529 opcode
= inst_token( SVGA3DOP_NOP
);
533 if (!emit_instruction(emit
, opcode
) ||
534 !svga_shader_emit_dwords( emit
, def
.values
, Elements(def
.values
)))
540 static INLINE boolean
541 create_zero_immediate( struct svga_shader_emitter
*emit
)
543 unsigned idx
= emit
->nr_hw_const
++;
545 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
549 emit
->zero_immediate_idx
= idx
;
550 emit
->created_zero_immediate
= TRUE
;
555 static INLINE boolean
556 create_loop_const( struct svga_shader_emitter
*emit
)
558 unsigned idx
= emit
->nr_hw_const
++;
560 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_INT
, idx
,
561 255, /* iteration count */
562 0, /* initial value */
564 0 /* not used, must be 0 */))
567 emit
->loop_const_idx
= idx
;
568 emit
->created_loop_const
= TRUE
;
573 static INLINE boolean
574 create_sincos_consts( struct svga_shader_emitter
*emit
)
576 unsigned idx
= emit
->nr_hw_const
++;
578 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
, idx
,
585 emit
->sincos_consts_idx
= idx
;
586 idx
= emit
->nr_hw_const
++;
588 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
, idx
,
595 emit
->created_sincos_consts
= TRUE
;
600 static INLINE boolean
601 create_arl_consts( struct svga_shader_emitter
*emit
)
605 for (i
= 0; i
< emit
->num_arl_consts
; i
+= 4) {
607 unsigned idx
= emit
->nr_hw_const
++;
609 for (j
= 0; j
< 4 && (j
+ i
) < emit
->num_arl_consts
; ++j
) {
610 vals
[j
] = emit
->arl_consts
[i
+ j
].number
;
611 emit
->arl_consts
[i
+ j
].idx
= idx
;
614 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_X
;
617 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_Y
;
620 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_Z
;
623 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_W
;
630 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
, idx
,
639 static INLINE
struct src_register
640 get_vface( struct svga_shader_emitter
*emit
)
642 assert(emit
->emitted_vface
);
643 return src_register(SVGA3DREG_MISCTYPE
,
647 /* returns {0, 0, 0, 1} immediate */
648 static INLINE
struct src_register
649 get_zero_immediate( struct svga_shader_emitter
*emit
)
651 assert(emit
->created_zero_immediate
);
652 assert(emit
->zero_immediate_idx
>= 0);
653 return src_register( SVGA3DREG_CONST
,
654 emit
->zero_immediate_idx
);
657 /* returns the loop const */
658 static INLINE
struct src_register
659 get_loop_const( struct svga_shader_emitter
*emit
)
661 assert(emit
->created_loop_const
);
662 assert(emit
->loop_const_idx
>= 0);
663 return src_register( SVGA3DREG_CONSTINT
,
664 emit
->loop_const_idx
);
667 /* returns a sincos const */
668 static INLINE
struct src_register
669 get_sincos_const( struct svga_shader_emitter
*emit
,
672 assert(emit
->created_sincos_consts
);
673 assert(emit
->sincos_consts_idx
>= 0);
674 assert(index
== 0 || index
== 1);
675 return src_register( SVGA3DREG_CONST
,
676 emit
->sincos_consts_idx
+ index
);
679 static INLINE
struct src_register
680 get_fake_arl_const( struct svga_shader_emitter
*emit
)
682 struct src_register reg
;
683 int idx
= 0, swizzle
= 0, i
;
685 for (i
= 0; i
< emit
->num_arl_consts
; ++ i
) {
686 if (emit
->arl_consts
[i
].arl_num
== emit
->current_arl
) {
687 idx
= emit
->arl_consts
[i
].idx
;
688 swizzle
= emit
->arl_consts
[i
].swizzle
;
692 reg
= src_register( SVGA3DREG_CONST
, idx
);
693 return scalar(reg
, swizzle
);
696 static INLINE
struct src_register
697 get_tex_dimensions( struct svga_shader_emitter
*emit
, int sampler_num
)
700 struct src_register reg
;
702 /* the width/height indexes start right after constants */
703 idx
= emit
->key
.fkey
.tex
[sampler_num
].width_height_idx
+
704 emit
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
706 reg
= src_register( SVGA3DREG_CONST
, idx
);
710 static boolean
emit_fake_arl(struct svga_shader_emitter
*emit
,
711 const struct tgsi_full_instruction
*insn
)
713 const struct src_register src0
= translate_src_register(
714 emit
, &insn
->Src
[0] );
715 struct src_register src1
= get_fake_arl_const( emit
);
716 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
717 SVGA3dShaderDestToken tmp
= get_temp( emit
);
719 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), tmp
, src0
))
722 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), tmp
, src( tmp
),
726 /* replicate the original swizzle */
728 src1
.base
.swizzle
= src0
.base
.swizzle
;
730 return submit_op1( emit
, inst_token( SVGA3DOP_MOVA
),
734 static boolean
emit_if(struct svga_shader_emitter
*emit
,
735 const struct tgsi_full_instruction
*insn
)
737 const struct src_register src
= translate_src_register(
738 emit
, &insn
->Src
[0] );
739 struct src_register zero
= get_zero_immediate( emit
);
740 SVGA3dShaderInstToken if_token
= inst_token( SVGA3DOP_IFC
);
742 if_token
.control
= SVGA3DOPCOMPC_NE
;
743 zero
= scalar(zero
, TGSI_SWIZZLE_X
);
745 emit
->dynamic_branching_level
++;
747 return (emit_instruction( emit
, if_token
) &&
748 emit_src( emit
, src
) &&
749 emit_src( emit
, zero
) );
752 static boolean
emit_endif(struct svga_shader_emitter
*emit
,
753 const struct tgsi_full_instruction
*insn
)
755 emit
->dynamic_branching_level
--;
757 return (emit_instruction( emit
,
758 inst_token( SVGA3DOP_ENDIF
)));
761 static boolean
emit_else(struct svga_shader_emitter
*emit
,
762 const struct tgsi_full_instruction
*insn
)
764 return (emit_instruction( emit
,
765 inst_token( SVGA3DOP_ELSE
)));
768 /* Translate the following TGSI FLR instruction.
770 * To the following SVGA3D instruction sequence.
774 static boolean
emit_floor(struct svga_shader_emitter
*emit
,
775 const struct tgsi_full_instruction
*insn
)
777 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
778 const struct src_register src0
= translate_src_register(
779 emit
, &insn
->Src
[0] );
780 SVGA3dShaderDestToken temp
= get_temp( emit
);
783 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
), temp
, src0
))
786 /* SUB DST, SRC, TMP */
787 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
, src0
,
788 negate( src( temp
) ) ))
795 /* Translate the following TGSI CMP instruction.
796 * CMP DST, SRC0, SRC1, SRC2
797 * To the following SVGA3D instruction sequence.
798 * CMP DST, SRC0, SRC2, SRC1
800 static boolean
emit_cmp(struct svga_shader_emitter
*emit
,
801 const struct tgsi_full_instruction
*insn
)
803 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
804 const struct src_register src0
= translate_src_register(
805 emit
, &insn
->Src
[0] );
806 const struct src_register src1
= translate_src_register(
807 emit
, &insn
->Src
[1] );
808 const struct src_register src2
= translate_src_register(
809 emit
, &insn
->Src
[2] );
811 /* CMP DST, SRC0, SRC2, SRC1 */
812 return submit_op3( emit
, inst_token( SVGA3DOP_CMP
), dst
, src0
, src2
, src1
);
817 /* Translate the following TGSI DIV instruction.
818 * DIV DST.xy, SRC0, SRC1
819 * To the following SVGA3D instruction sequence.
820 * RCP TMP.x, SRC1.xxxx
821 * RCP TMP.y, SRC1.yyyy
822 * MUL DST.xy, SRC0, TMP
824 static boolean
emit_div(struct svga_shader_emitter
*emit
,
825 const struct tgsi_full_instruction
*insn
)
827 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
828 const struct src_register src0
= translate_src_register(
829 emit
, &insn
->Src
[0] );
830 const struct src_register src1
= translate_src_register(
831 emit
, &insn
->Src
[1] );
832 SVGA3dShaderDestToken temp
= get_temp( emit
);
835 /* For each enabled element, perform a RCP instruction. Note that
836 * RCP is scalar in SVGA3D:
838 for (i
= 0; i
< 4; i
++) {
839 unsigned channel
= 1 << i
;
840 if (dst
.mask
& channel
) {
841 /* RCP TMP.?, SRC1.???? */
842 if (!submit_op1( emit
, inst_token( SVGA3DOP_RCP
),
843 writemask(temp
, channel
),
849 /* Then multiply them out with a single mul:
853 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
), dst
, src0
,
860 /* Translate the following TGSI DP2 instruction.
861 * DP2 DST, SRC1, SRC2
862 * To the following SVGA3D instruction sequence.
863 * MUL TMP, SRC1, SRC2
864 * ADD DST, TMP.xxxx, TMP.yyyy
866 static boolean
emit_dp2(struct svga_shader_emitter
*emit
,
867 const struct tgsi_full_instruction
*insn
)
869 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
870 const struct src_register src0
= translate_src_register(
871 emit
, &insn
->Src
[0] );
872 const struct src_register src1
= translate_src_register(
873 emit
, &insn
->Src
[1] );
874 SVGA3dShaderDestToken temp
= get_temp( emit
);
875 struct src_register temp_src0
, temp_src1
;
877 /* MUL TMP, SRC1, SRC2 */
878 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
), temp
, src0
, src1
))
881 temp_src0
= scalar(src( temp
), TGSI_SWIZZLE_X
);
882 temp_src1
= scalar(src( temp
), TGSI_SWIZZLE_Y
);
884 /* ADD DST, TMP.xxxx, TMP.yyyy */
885 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
,
886 temp_src0
, temp_src1
))
893 /* Translate the following TGSI DPH instruction.
894 * DPH DST, SRC1, SRC2
895 * To the following SVGA3D instruction sequence.
896 * DP3 TMP, SRC1, SRC2
897 * ADD DST, TMP, SRC2.wwww
899 static boolean
emit_dph(struct svga_shader_emitter
*emit
,
900 const struct tgsi_full_instruction
*insn
)
902 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
903 const struct src_register src0
= translate_src_register(
904 emit
, &insn
->Src
[0] );
905 struct src_register src1
= translate_src_register(
906 emit
, &insn
->Src
[1] );
907 SVGA3dShaderDestToken temp
= get_temp( emit
);
909 /* DP3 TMP, SRC1, SRC2 */
910 if (!submit_op2( emit
, inst_token( SVGA3DOP_DP3
), temp
, src0
, src1
))
913 src1
= scalar(src1
, TGSI_SWIZZLE_W
);
915 /* ADD DST, TMP, SRC2.wwww */
916 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
,
923 /* Translate the following TGSI DST instruction.
925 * To the following SVGA3D instruction sequence.
930 static boolean
emit_nrm(struct svga_shader_emitter
*emit
,
931 const struct tgsi_full_instruction
*insn
)
933 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
934 const struct src_register src0
= translate_src_register(
935 emit
, &insn
->Src
[0] );
936 SVGA3dShaderDestToken temp
= get_temp( emit
);
938 /* DP3 TMP, SRC, SRC */
939 if (!submit_op2( emit
, inst_token( SVGA3DOP_DP3
), temp
, src0
, src0
))
943 if (!submit_op1( emit
, inst_token( SVGA3DOP_RSQ
), temp
, src( temp
)))
946 /* MUL DST, SRC, TMP */
947 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
), dst
,
955 static boolean
do_emit_sincos(struct svga_shader_emitter
*emit
,
956 SVGA3dShaderDestToken dst
,
957 struct src_register src0
)
959 src0
= scalar(src0
, TGSI_SWIZZLE_X
);
961 if (emit
->use_sm30
) {
962 return submit_op1( emit
, inst_token( SVGA3DOP_SINCOS
),
965 struct src_register const1
= get_sincos_const( emit
, 0 );
966 struct src_register const2
= get_sincos_const( emit
, 1 );
968 return submit_op3( emit
, inst_token( SVGA3DOP_SINCOS
),
969 dst
, src0
, const1
, const2
);
973 static boolean
emit_sincos(struct svga_shader_emitter
*emit
,
974 const struct tgsi_full_instruction
*insn
)
976 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
977 struct src_register src0
= translate_src_register(
978 emit
, &insn
->Src
[0] );
979 SVGA3dShaderDestToken temp
= get_temp( emit
);
982 if (!do_emit_sincos(emit
, writemask(temp
, TGSI_WRITEMASK_XY
), src0
))
986 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src( temp
) ))
996 static boolean
emit_sin(struct svga_shader_emitter
*emit
,
997 const struct tgsi_full_instruction
*insn
)
999 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1000 struct src_register src0
= translate_src_register(
1001 emit
, &insn
->Src
[0] );
1002 SVGA3dShaderDestToken temp
= get_temp( emit
);
1005 if (!do_emit_sincos(emit
, writemask(temp
, TGSI_WRITEMASK_Y
), src0
))
1008 src0
= scalar(src( temp
), TGSI_SWIZZLE_Y
);
1010 /* MOV DST TMP.yyyy */
1011 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src0
))
1021 static boolean
emit_cos(struct svga_shader_emitter
*emit
,
1022 const struct tgsi_full_instruction
*insn
)
1024 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1025 struct src_register src0
= translate_src_register(
1026 emit
, &insn
->Src
[0] );
1027 SVGA3dShaderDestToken temp
= get_temp( emit
);
1030 if (!do_emit_sincos( emit
, writemask(temp
, TGSI_WRITEMASK_X
), src0
))
1033 src0
= scalar(src( temp
), TGSI_SWIZZLE_X
);
1035 /* MOV DST TMP.xxxx */
1036 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src0
))
1044 * ADD DST SRC0, negate(SRC0)
1046 static boolean
emit_sub(struct svga_shader_emitter
*emit
,
1047 const struct tgsi_full_instruction
*insn
)
1049 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1050 struct src_register src0
= translate_src_register(
1051 emit
, &insn
->Src
[0] );
1052 struct src_register src1
= translate_src_register(
1053 emit
, &insn
->Src
[1] );
1055 src1
= negate(src1
);
1057 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
,
1065 static boolean
emit_kil(struct svga_shader_emitter
*emit
,
1066 const struct tgsi_full_instruction
*insn
)
1068 SVGA3dShaderInstToken inst
;
1069 const struct tgsi_full_src_register
*reg
= &insn
->Src
[0];
1070 struct src_register src0
;
1072 inst
= inst_token( SVGA3DOP_TEXKILL
);
1073 src0
= translate_src_register( emit
, reg
);
1075 if (reg
->Register
.Absolute
||
1076 reg
->Register
.Negate
||
1077 reg
->Register
.Indirect
||
1078 reg
->Register
.SwizzleX
!= 0 ||
1079 reg
->Register
.SwizzleY
!= 1 ||
1080 reg
->Register
.SwizzleZ
!= 2 ||
1081 reg
->Register
.File
!= TGSI_FILE_TEMPORARY
)
1083 SVGA3dShaderDestToken temp
= get_temp( emit
);
1085 submit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp
, src0
);
1089 return submit_op0( emit
, inst
, dst(src0
) );
1093 /* mesa state tracker always emits kilp as an unconditional
1095 static boolean
emit_kilp(struct svga_shader_emitter
*emit
,
1096 const struct tgsi_full_instruction
*insn
)
1098 SVGA3dShaderInstToken inst
;
1099 SVGA3dShaderDestToken temp
;
1100 struct src_register one
= scalar( get_zero_immediate( emit
),
1103 inst
= inst_token( SVGA3DOP_TEXKILL
);
1105 /* texkill doesn't allow negation on the operand so lets move
1106 * negation of {1} to a temp register */
1107 temp
= get_temp( emit
);
1108 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp
,
1112 return submit_op0( emit
, inst
, temp
);
1115 /* Implement conditionals by initializing destination reg to 'fail',
1116 * then set predicate reg with UFOP_SETP, then move 'pass' to dest
1117 * based on predicate reg.
1119 * SETP src0, cmp, src1 -- do this first to avoid aliasing problems.
1124 emit_conditional(struct svga_shader_emitter
*emit
,
1125 unsigned compare_func
,
1126 SVGA3dShaderDestToken dst
,
1127 struct src_register src0
,
1128 struct src_register src1
,
1129 struct src_register pass
,
1130 struct src_register fail
)
1132 SVGA3dShaderDestToken pred_reg
= dst_register( SVGA3DREG_PREDICATE
, 0 );
1133 SVGA3dShaderInstToken setp_token
, mov_token
;
1134 setp_token
= inst_token( SVGA3DOP_SETP
);
1136 switch (compare_func
) {
1137 case PIPE_FUNC_NEVER
:
1138 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1141 case PIPE_FUNC_LESS
:
1142 setp_token
.control
= SVGA3DOPCOMP_LT
;
1144 case PIPE_FUNC_EQUAL
:
1145 setp_token
.control
= SVGA3DOPCOMP_EQ
;
1147 case PIPE_FUNC_LEQUAL
:
1148 setp_token
.control
= SVGA3DOPCOMP_LE
;
1150 case PIPE_FUNC_GREATER
:
1151 setp_token
.control
= SVGA3DOPCOMP_GT
;
1153 case PIPE_FUNC_NOTEQUAL
:
1154 setp_token
.control
= SVGA3DOPCOMPC_NE
;
1156 case PIPE_FUNC_GEQUAL
:
1157 setp_token
.control
= SVGA3DOPCOMP_GE
;
1159 case PIPE_FUNC_ALWAYS
:
1160 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1165 /* SETP src0, COMPOP, src1 */
1166 if (!submit_op2( emit
, setp_token
, pred_reg
,
1170 mov_token
= inst_token( SVGA3DOP_MOV
);
1173 if (!submit_op1( emit
, mov_token
, dst
,
1177 /* MOV dst, pass (predicated)
1179 * Note that the predicate reg (and possible modifiers) is passed
1180 * as the first source argument.
1182 mov_token
.predicated
= 1;
1183 if (!submit_op2( emit
, mov_token
, dst
,
1184 src( pred_reg
), pass
))
1192 emit_select(struct svga_shader_emitter
*emit
,
1193 unsigned compare_func
,
1194 SVGA3dShaderDestToken dst
,
1195 struct src_register src0
,
1196 struct src_register src1
)
1198 /* There are some SVGA instructions which implement some selects
1199 * directly, but they are only available in the vertex shader.
1201 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1202 switch (compare_func
) {
1203 case PIPE_FUNC_GEQUAL
:
1204 return submit_op2( emit
, inst_token( SVGA3DOP_SGE
), dst
, src0
, src1
);
1205 case PIPE_FUNC_LEQUAL
:
1206 return submit_op2( emit
, inst_token( SVGA3DOP_SGE
), dst
, src1
, src0
);
1207 case PIPE_FUNC_GREATER
:
1208 return submit_op2( emit
, inst_token( SVGA3DOP_SLT
), dst
, src1
, src0
);
1209 case PIPE_FUNC_LESS
:
1210 return submit_op2( emit
, inst_token( SVGA3DOP_SLT
), dst
, src0
, src1
);
1217 /* Otherwise, need to use the setp approach:
1220 struct src_register one
, zero
;
1221 /* zero immediate is 0,0,0,1 */
1222 zero
= get_zero_immediate( emit
);
1223 one
= scalar( zero
, TGSI_SWIZZLE_W
);
1224 zero
= scalar( zero
, TGSI_SWIZZLE_X
);
1226 return emit_conditional(
1237 static boolean
emit_select_op(struct svga_shader_emitter
*emit
,
1239 const struct tgsi_full_instruction
*insn
)
1241 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1242 struct src_register src0
= translate_src_register(
1243 emit
, &insn
->Src
[0] );
1244 struct src_register src1
= translate_src_register(
1245 emit
, &insn
->Src
[1] );
1247 return emit_select( emit
, compare
, dst
, src0
, src1
);
1251 /* Translate texture instructions to SVGA3D representation.
1253 static boolean
emit_tex2(struct svga_shader_emitter
*emit
,
1254 const struct tgsi_full_instruction
*insn
,
1255 SVGA3dShaderDestToken dst
)
1257 SVGA3dShaderInstToken inst
;
1258 struct src_register texcoord
;
1259 struct src_register sampler
;
1260 SVGA3dShaderDestToken tmp
;
1264 switch (insn
->Instruction
.Opcode
) {
1265 case TGSI_OPCODE_TEX
:
1266 inst
.op
= SVGA3DOP_TEX
;
1268 case TGSI_OPCODE_TXP
:
1269 inst
.op
= SVGA3DOP_TEX
;
1270 inst
.control
= SVGA3DOPCONT_PROJECT
;
1272 case TGSI_OPCODE_TXB
:
1273 inst
.op
= SVGA3DOP_TEX
;
1274 inst
.control
= SVGA3DOPCONT_BIAS
;
1276 case TGSI_OPCODE_TXL
:
1277 inst
.op
= SVGA3DOP_TEXLDL
;
1284 texcoord
= translate_src_register( emit
, &insn
->Src
[0] );
1285 sampler
= translate_src_register( emit
, &insn
->Src
[1] );
1287 if (emit
->key
.fkey
.tex
[sampler
.base
.num
].unnormalized
||
1288 emit
->dynamic_branching_level
> 0)
1289 tmp
= get_temp( emit
);
1291 /* Can't do mipmapping inside dynamic branch constructs. Force LOD
1292 * zero in that case.
1294 if (emit
->dynamic_branching_level
> 0 &&
1295 inst
.op
== SVGA3DOP_TEX
&&
1296 SVGA3dShaderGetRegType(texcoord
.base
.value
) == SVGA3DREG_TEMP
) {
1297 struct src_register zero
= get_zero_immediate( emit
);
1299 /* MOV tmp, texcoord */
1300 if (!submit_op1( emit
,
1301 inst_token( SVGA3DOP_MOV
),
1306 /* MOV tmp.w, zero */
1307 if (!submit_op1( emit
,
1308 inst_token( SVGA3DOP_MOV
),
1309 writemask( tmp
, TGSI_WRITEMASK_W
),
1310 scalar( zero
, TGSI_SWIZZLE_X
)))
1313 texcoord
= src( tmp
);
1314 inst
.op
= SVGA3DOP_TEXLDL
;
1317 /* Explicit normalization of texcoords:
1319 if (emit
->key
.fkey
.tex
[sampler
.base
.num
].unnormalized
) {
1320 struct src_register wh
= get_tex_dimensions( emit
, sampler
.base
.num
);
1322 /* MUL tmp, SRC0, WH */
1323 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
1324 tmp
, texcoord
, wh
))
1327 texcoord
= src( tmp
);
1330 return submit_op2( emit
, inst
, dst
, texcoord
, sampler
);
1336 /* Translate texture instructions to SVGA3D representation.
1338 static boolean
emit_tex4(struct svga_shader_emitter
*emit
,
1339 const struct tgsi_full_instruction
*insn
,
1340 SVGA3dShaderDestToken dst
)
1342 SVGA3dShaderInstToken inst
;
1343 struct src_register texcoord
;
1344 struct src_register ddx
;
1345 struct src_register ddy
;
1346 struct src_register sampler
;
1348 texcoord
= translate_src_register( emit
, &insn
->Src
[0] );
1349 ddx
= translate_src_register( emit
, &insn
->Src
[1] );
1350 ddy
= translate_src_register( emit
, &insn
->Src
[2] );
1351 sampler
= translate_src_register( emit
, &insn
->Src
[3] );
1355 switch (insn
->Instruction
.Opcode
) {
1356 case TGSI_OPCODE_TXD
:
1357 inst
.op
= SVGA3DOP_TEXLDD
; /* 4 args! */
1364 return submit_op4( emit
, inst
, dst
, texcoord
, sampler
, ddx
, ddy
);
1368 static boolean
emit_tex(struct svga_shader_emitter
*emit
,
1369 const struct tgsi_full_instruction
*insn
)
1371 SVGA3dShaderDestToken dst
=
1372 translate_dst_register( emit
, insn
, 0 );
1373 struct src_register src0
=
1374 translate_src_register( emit
, &insn
->Src
[0] );
1375 struct src_register src1
=
1376 translate_src_register( emit
, &insn
->Src
[1] );
1378 SVGA3dShaderDestToken tex_result
;
1380 /* check for shadow samplers */
1381 boolean compare
= (emit
->key
.fkey
.tex
[src1
.base
.num
].compare_mode
==
1382 PIPE_TEX_COMPARE_R_TO_TEXTURE
);
1385 /* If doing compare processing, need to put this value into a
1386 * temporary so it can be used as a source later on.
1389 (!emit
->use_sm30
&& dst
.mask
!= TGSI_WRITEMASK_XYZW
) ) {
1390 tex_result
= get_temp( emit
);
1396 switch(insn
->Instruction
.Opcode
) {
1397 case TGSI_OPCODE_TEX
:
1398 case TGSI_OPCODE_TXB
:
1399 case TGSI_OPCODE_TXP
:
1400 case TGSI_OPCODE_TXL
:
1401 if (!emit_tex2( emit
, insn
, tex_result
))
1404 case TGSI_OPCODE_TXD
:
1405 if (!emit_tex4( emit
, insn
, tex_result
))
1414 if (dst
.mask
& TGSI_WRITEMASK_XYZ
) {
1415 SVGA3dShaderDestToken src0_zdivw
= get_temp( emit
);
1416 struct src_register tex_src_x
= scalar(src(tex_result
), TGSI_SWIZZLE_Y
);
1418 /* Divide texcoord R by Q */
1419 if (!submit_op1( emit
, inst_token( SVGA3DOP_RCP
),
1420 writemask(src0_zdivw
, TGSI_WRITEMASK_X
),
1421 scalar(src0
, TGSI_SWIZZLE_W
) ))
1424 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
1425 writemask(src0_zdivw
, TGSI_WRITEMASK_X
),
1426 scalar(src0
, TGSI_SWIZZLE_Z
),
1427 scalar(src(src0_zdivw
), TGSI_SWIZZLE_X
) ))
1432 emit
->key
.fkey
.tex
[src1
.base
.num
].compare_func
,
1433 writemask( dst
, TGSI_WRITEMASK_XYZ
),
1434 scalar(src(src0_zdivw
), TGSI_SWIZZLE_X
),
1439 if (dst
.mask
& TGSI_WRITEMASK_W
) {
1440 struct src_register one
=
1441 scalar( get_zero_immediate( emit
), TGSI_SWIZZLE_W
);
1443 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1444 writemask( dst
, TGSI_WRITEMASK_W
),
1451 else if (!emit
->use_sm30
&& dst
.mask
!= TGSI_WRITEMASK_XYZW
)
1453 if (!emit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src(tex_result
) ))
1460 static boolean
emit_bgnloop2( struct svga_shader_emitter
*emit
,
1461 const struct tgsi_full_instruction
*insn
)
1463 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_LOOP
);
1464 struct src_register loop_reg
= src_register( SVGA3DREG_LOOP
, 0 );
1465 struct src_register const_int
= get_loop_const( emit
);
1467 emit
->dynamic_branching_level
++;
1469 return (emit_instruction( emit
, inst
) &&
1470 emit_src( emit
, loop_reg
) &&
1471 emit_src( emit
, const_int
) );
1474 static boolean
emit_endloop2( struct svga_shader_emitter
*emit
,
1475 const struct tgsi_full_instruction
*insn
)
1477 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_ENDLOOP
);
1479 emit
->dynamic_branching_level
--;
1481 return emit_instruction( emit
, inst
);
1484 static boolean
emit_brk( struct svga_shader_emitter
*emit
,
1485 const struct tgsi_full_instruction
*insn
)
1487 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_BREAK
);
1488 return emit_instruction( emit
, inst
);
1491 static boolean
emit_scalar_op1( struct svga_shader_emitter
*emit
,
1493 const struct tgsi_full_instruction
*insn
)
1495 SVGA3dShaderInstToken inst
;
1496 SVGA3dShaderDestToken dst
;
1497 struct src_register src
;
1499 inst
= inst_token( opcode
);
1500 dst
= translate_dst_register( emit
, insn
, 0 );
1501 src
= translate_src_register( emit
, &insn
->Src
[0] );
1502 src
= scalar( src
, TGSI_SWIZZLE_X
);
1504 return submit_op1( emit
, inst
, dst
, src
);
1508 static boolean
emit_simple_instruction(struct svga_shader_emitter
*emit
,
1510 const struct tgsi_full_instruction
*insn
)
1512 const struct tgsi_full_src_register
*src
= insn
->Src
;
1513 SVGA3dShaderInstToken inst
;
1514 SVGA3dShaderDestToken dst
;
1516 inst
= inst_token( opcode
);
1517 dst
= translate_dst_register( emit
, insn
, 0 );
1519 switch (insn
->Instruction
.NumSrcRegs
) {
1521 return submit_op0( emit
, inst
, dst
);
1523 return submit_op1( emit
, inst
, dst
,
1524 translate_src_register( emit
, &src
[0] ));
1526 return submit_op2( emit
, inst
, dst
,
1527 translate_src_register( emit
, &src
[0] ),
1528 translate_src_register( emit
, &src
[1] ) );
1530 return submit_op3( emit
, inst
, dst
,
1531 translate_src_register( emit
, &src
[0] ),
1532 translate_src_register( emit
, &src
[1] ),
1533 translate_src_register( emit
, &src
[2] ) );
1541 static boolean
emit_deriv(struct svga_shader_emitter
*emit
,
1542 const struct tgsi_full_instruction
*insn
)
1544 if (emit
->dynamic_branching_level
> 0 &&
1545 insn
->Src
[0].Register
.File
== TGSI_FILE_TEMPORARY
)
1547 struct src_register zero
= get_zero_immediate( emit
);
1548 SVGA3dShaderDestToken dst
=
1549 translate_dst_register( emit
, insn
, 0 );
1551 /* Deriv opcodes not valid inside dynamic branching, workaround
1552 * by zeroing out the destination.
1554 if (!submit_op1(emit
,
1555 inst_token( SVGA3DOP_MOV
),
1557 scalar(zero
, TGSI_SWIZZLE_X
)))
1565 switch (insn
->Instruction
.Opcode
) {
1566 case TGSI_OPCODE_DDX
:
1567 opcode
= SVGA3DOP_DSX
;
1569 case TGSI_OPCODE_DDY
:
1570 opcode
= SVGA3DOP_DSY
;
1576 return emit_simple_instruction( emit
, opcode
, insn
);
1580 static boolean
emit_arl(struct svga_shader_emitter
*emit
,
1581 const struct tgsi_full_instruction
*insn
)
1583 ++emit
->current_arl
;
1584 if (svga_arl_needs_adjustment( emit
)) {
1585 return emit_fake_arl( emit
, insn
);
1587 /* no need to adjust, just emit straight arl */
1588 return emit_simple_instruction(emit
, SVGA3DOP_MOVA
, insn
);
1592 static boolean
alias_src_dst( struct src_register src
,
1593 SVGA3dShaderDestToken dst
)
1595 if (src
.base
.num
!= dst
.num
)
1598 if (SVGA3dShaderGetRegType(dst
.value
) !=
1599 SVGA3dShaderGetRegType(src
.base
.value
))
1605 static boolean
emit_pow(struct svga_shader_emitter
*emit
,
1606 const struct tgsi_full_instruction
*insn
)
1608 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1609 struct src_register src0
= translate_src_register(
1610 emit
, &insn
->Src
[0] );
1611 struct src_register src1
= translate_src_register(
1612 emit
, &insn
->Src
[1] );
1613 boolean need_tmp
= FALSE
;
1615 /* POW can only output to a temporary */
1616 if (insn
->Dst
[0].Register
.File
!= TGSI_FILE_TEMPORARY
)
1619 /* POW src1 must not be the same register as dst */
1620 if (alias_src_dst( src1
, dst
))
1623 /* it's a scalar op */
1624 src0
= scalar( src0
, TGSI_SWIZZLE_X
);
1625 src1
= scalar( src1
, TGSI_SWIZZLE_X
);
1628 SVGA3dShaderDestToken tmp
= writemask(get_temp( emit
), TGSI_WRITEMASK_X
);
1630 if (!submit_op2(emit
, inst_token( SVGA3DOP_POW
), tmp
, src0
, src1
))
1633 return submit_op1(emit
, inst_token( SVGA3DOP_MOV
), dst
, scalar(src(tmp
), 0) );
1636 return submit_op2(emit
, inst_token( SVGA3DOP_POW
), dst
, src0
, src1
);
1640 static boolean
emit_xpd(struct svga_shader_emitter
*emit
,
1641 const struct tgsi_full_instruction
*insn
)
1643 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1644 const struct src_register src0
= translate_src_register(
1645 emit
, &insn
->Src
[0] );
1646 const struct src_register src1
= translate_src_register(
1647 emit
, &insn
->Src
[1] );
1648 boolean need_dst_tmp
= FALSE
;
1650 /* XPD can only output to a temporary */
1651 if (SVGA3dShaderGetRegType(dst
.value
) != SVGA3DREG_TEMP
)
1652 need_dst_tmp
= TRUE
;
1654 /* The dst reg must not be the same as src0 or src1*/
1655 if (alias_src_dst(src0
, dst
) ||
1656 alias_src_dst(src1
, dst
))
1657 need_dst_tmp
= TRUE
;
1660 SVGA3dShaderDestToken tmp
= get_temp( emit
);
1662 /* Obey DX9 restrictions on mask:
1664 tmp
.mask
= dst
.mask
& TGSI_WRITEMASK_XYZ
;
1666 if (!submit_op2(emit
, inst_token( SVGA3DOP_CRS
), tmp
, src0
, src1
))
1669 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), dst
, src( tmp
)))
1673 if (!submit_op2(emit
, inst_token( SVGA3DOP_CRS
), dst
, src0
, src1
))
1677 /* Need to emit 1.0 to dst.w?
1679 if (dst
.mask
& TGSI_WRITEMASK_W
) {
1680 struct src_register zero
= get_zero_immediate( emit
);
1682 if (!submit_op1(emit
,
1683 inst_token( SVGA3DOP_MOV
),
1684 writemask(dst
, TGSI_WRITEMASK_W
),
1693 static boolean
emit_lrp(struct svga_shader_emitter
*emit
,
1694 const struct tgsi_full_instruction
*insn
)
1696 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1697 SVGA3dShaderDestToken tmp
;
1698 const struct src_register src0
= translate_src_register(
1699 emit
, &insn
->Src
[0] );
1700 const struct src_register src1
= translate_src_register(
1701 emit
, &insn
->Src
[1] );
1702 const struct src_register src2
= translate_src_register(
1703 emit
, &insn
->Src
[2] );
1704 boolean need_dst_tmp
= FALSE
;
1706 /* The dst reg must not be the same as src0 or src2 */
1707 if (alias_src_dst(src0
, dst
) ||
1708 alias_src_dst(src2
, dst
))
1709 need_dst_tmp
= TRUE
;
1712 tmp
= get_temp( emit
);
1713 tmp
.mask
= dst
.mask
;
1719 if (!submit_op3(emit
, inst_token( SVGA3DOP_LRP
), tmp
, src0
, src1
, src2
))
1723 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), dst
, src( tmp
)))
1731 static boolean
emit_dst_insn(struct svga_shader_emitter
*emit
,
1732 const struct tgsi_full_instruction
*insn
)
1734 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1735 /* SVGA/DX9 has a DST instruction, but only for vertex shaders:
1737 return emit_simple_instruction(emit
, SVGA3DOP_DST
, insn
);
1741 /* result[0] = 1 * 1;
1742 * result[1] = a[1] * b[1];
1743 * result[2] = a[2] * 1;
1744 * result[3] = 1 * b[3];
1747 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1748 SVGA3dShaderDestToken tmp
;
1749 const struct src_register src0
= translate_src_register(
1750 emit
, &insn
->Src
[0] );
1751 const struct src_register src1
= translate_src_register(
1752 emit
, &insn
->Src
[1] );
1753 struct src_register zero
= get_zero_immediate( emit
);
1754 boolean need_tmp
= FALSE
;
1756 if (SVGA3dShaderGetRegType(dst
.value
) != SVGA3DREG_TEMP
||
1757 alias_src_dst(src0
, dst
) ||
1758 alias_src_dst(src1
, dst
))
1762 tmp
= get_temp( emit
);
1770 if (tmp
.mask
& TGSI_WRITEMASK_XW
) {
1771 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1772 writemask(tmp
, TGSI_WRITEMASK_XW
),
1779 if (tmp
.mask
& TGSI_WRITEMASK_YZ
) {
1780 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1781 writemask(tmp
, TGSI_WRITEMASK_YZ
),
1786 /* tmp.yw = tmp * src1
1788 if (tmp
.mask
& TGSI_WRITEMASK_YW
) {
1789 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
1790 writemask(tmp
, TGSI_WRITEMASK_YW
),
1799 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1810 static boolean
emit_exp(struct svga_shader_emitter
*emit
,
1811 const struct tgsi_full_instruction
*insn
)
1813 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1814 struct src_register src0
=
1815 translate_src_register( emit
, &insn
->Src
[0] );
1816 struct src_register zero
= get_zero_immediate( emit
);
1817 SVGA3dShaderDestToken fraction
;
1819 if (dst
.mask
& TGSI_WRITEMASK_Y
)
1821 else if (dst
.mask
& TGSI_WRITEMASK_X
)
1822 fraction
= get_temp( emit
);
1826 /* If y is being written, fill it with src0 - floor(src0).
1828 if (dst
.mask
& TGSI_WRITEMASK_XY
) {
1829 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
),
1830 writemask( fraction
, TGSI_WRITEMASK_Y
),
1835 /* If x is being written, fill it with 2 ^ floor(src0).
1837 if (dst
.mask
& TGSI_WRITEMASK_X
) {
1838 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
),
1839 writemask( dst
, TGSI_WRITEMASK_X
),
1841 scalar( negate( src( fraction
) ), TGSI_SWIZZLE_Y
) ) )
1844 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXP
),
1845 writemask( dst
, TGSI_WRITEMASK_X
),
1846 scalar( src( dst
), TGSI_SWIZZLE_X
) ) )
1849 if (!(dst
.mask
& TGSI_WRITEMASK_Y
))
1850 release_temp( emit
, fraction
);
1853 /* If z is being written, fill it with 2 ^ src0 (partial precision).
1855 if (dst
.mask
& TGSI_WRITEMASK_Z
) {
1856 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXPP
),
1857 writemask( dst
, TGSI_WRITEMASK_Z
),
1862 /* If w is being written, fill it with one.
1864 if (dst
.mask
& TGSI_WRITEMASK_W
) {
1865 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1866 writemask(dst
, TGSI_WRITEMASK_W
),
1867 scalar( zero
, TGSI_SWIZZLE_W
) ))
1874 static boolean
emit_lit(struct svga_shader_emitter
*emit
,
1875 const struct tgsi_full_instruction
*insn
)
1877 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1878 /* SVGA/DX9 has a LIT instruction, but only for vertex shaders:
1880 return emit_simple_instruction(emit
, SVGA3DOP_LIT
, insn
);
1884 /* D3D vs. GL semantics can be fairly easily accomodated by
1885 * variations on this sequence.
1889 * tmp.z = pow(src.y,src.w)
1890 * p0 = src0.xxxx > 0
1891 * result = zero.wxxw
1892 * (p0) result.yz = tmp
1896 * tmp.z = pow(src.y,src.w)
1897 * p0 = src0.xxyy > 0
1898 * result = zero.wxxw
1899 * (p0) result.yz = tmp
1901 * Will implement the GL version for now.
1904 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1905 SVGA3dShaderDestToken tmp
= get_temp( emit
);
1906 const struct src_register src0
= translate_src_register(
1907 emit
, &insn
->Src
[0] );
1908 struct src_register zero
= get_zero_immediate( emit
);
1910 /* tmp = pow(src.y, src.w)
1912 if (dst
.mask
& TGSI_WRITEMASK_Z
) {
1913 if (!submit_op2(emit
, inst_token( SVGA3DOP_POW
),
1922 if (dst
.mask
& TGSI_WRITEMASK_Y
) {
1923 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1924 writemask(tmp
, TGSI_WRITEMASK_Y
),
1929 /* Can't quite do this with emit conditional due to the extra
1930 * writemask on the predicated mov:
1933 SVGA3dShaderDestToken pred_reg
= dst_register( SVGA3DREG_PREDICATE
, 0 );
1934 SVGA3dShaderInstToken setp_token
, mov_token
;
1935 struct src_register predsrc
;
1937 setp_token
= inst_token( SVGA3DOP_SETP
);
1938 mov_token
= inst_token( SVGA3DOP_MOV
);
1940 setp_token
.control
= SVGA3DOPCOMP_GT
;
1942 /* D3D vs GL semantics:
1945 predsrc
= swizzle(src0
, 0, 0, 1, 1); /* D3D */
1947 predsrc
= swizzle(src0
, 0, 0, 0, 0); /* GL */
1949 /* SETP src0.xxyy, GT, {0}.x */
1950 if (!submit_op2( emit
, setp_token
, pred_reg
,
1952 swizzle(zero
, 0, 0, 0, 0) ))
1956 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
,
1957 swizzle(zero
, 3, 0, 0, 3 )))
1960 /* MOV dst.yz, tmp (predicated)
1962 * Note that the predicate reg (and possible modifiers) is passed
1963 * as the first source argument.
1965 if (dst
.mask
& TGSI_WRITEMASK_YZ
) {
1966 mov_token
.predicated
= 1;
1967 if (!submit_op2( emit
, mov_token
,
1968 writemask(dst
, TGSI_WRITEMASK_YZ
),
1969 src( pred_reg
), src( tmp
) ))
1981 static boolean
emit_ex2( struct svga_shader_emitter
*emit
,
1982 const struct tgsi_full_instruction
*insn
)
1984 SVGA3dShaderInstToken inst
;
1985 SVGA3dShaderDestToken dst
;
1986 struct src_register src0
;
1988 inst
= inst_token( SVGA3DOP_EXP
);
1989 dst
= translate_dst_register( emit
, insn
, 0 );
1990 src0
= translate_src_register( emit
, &insn
->Src
[0] );
1991 src0
= scalar( src0
, TGSI_SWIZZLE_X
);
1993 if (dst
.mask
!= TGSI_WRITEMASK_XYZW
) {
1994 SVGA3dShaderDestToken tmp
= get_temp( emit
);
1996 if (!submit_op1( emit
, inst
, tmp
, src0
))
1999 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2001 scalar( src( tmp
), TGSI_SWIZZLE_X
) );
2004 return submit_op1( emit
, inst
, dst
, src0
);
2008 static boolean
emit_log(struct svga_shader_emitter
*emit
,
2009 const struct tgsi_full_instruction
*insn
)
2011 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2012 struct src_register src0
=
2013 translate_src_register( emit
, &insn
->Src
[0] );
2014 struct src_register zero
= get_zero_immediate( emit
);
2015 SVGA3dShaderDestToken abs_tmp
;
2016 struct src_register abs_src0
;
2017 SVGA3dShaderDestToken log2_abs
;
2021 if (dst
.mask
& TGSI_WRITEMASK_Z
)
2023 else if (dst
.mask
& TGSI_WRITEMASK_XY
)
2024 log2_abs
= get_temp( emit
);
2028 /* If z is being written, fill it with log2( abs( src0 ) ).
2030 if (dst
.mask
& TGSI_WRITEMASK_XYZ
) {
2031 if (!src0
.base
.srcMod
|| src0
.base
.srcMod
== SVGA3DSRCMOD_ABS
)
2034 abs_tmp
= get_temp( emit
);
2036 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2041 abs_src0
= src( abs_tmp
);
2044 abs_src0
= absolute( scalar( abs_src0
, TGSI_SWIZZLE_X
) );
2046 if (!submit_op1( emit
, inst_token( SVGA3DOP_LOG
),
2047 writemask( log2_abs
, TGSI_WRITEMASK_Z
),
2052 if (dst
.mask
& TGSI_WRITEMASK_XY
) {
2053 SVGA3dShaderDestToken floor_log2
;
2055 if (dst
.mask
& TGSI_WRITEMASK_X
)
2058 floor_log2
= get_temp( emit
);
2060 /* If x is being written, fill it with floor( log2( abs( src0 ) ) ).
2062 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
),
2063 writemask( floor_log2
, TGSI_WRITEMASK_X
),
2064 scalar( src( log2_abs
), TGSI_SWIZZLE_Z
) ) )
2067 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
),
2068 writemask( floor_log2
, TGSI_WRITEMASK_X
),
2069 scalar( src( log2_abs
), TGSI_SWIZZLE_Z
),
2070 negate( src( floor_log2
) ) ) )
2073 /* If y is being written, fill it with
2074 * abs ( src0 ) / ( 2 ^ floor( log2( abs( src0 ) ) ) ).
2076 if (dst
.mask
& TGSI_WRITEMASK_Y
) {
2077 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXP
),
2078 writemask( dst
, TGSI_WRITEMASK_Y
),
2079 negate( scalar( src( floor_log2
),
2080 TGSI_SWIZZLE_X
) ) ) )
2083 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
2084 writemask( dst
, TGSI_WRITEMASK_Y
),
2090 if (!(dst
.mask
& TGSI_WRITEMASK_X
))
2091 release_temp( emit
, floor_log2
);
2093 if (!(dst
.mask
& TGSI_WRITEMASK_Z
))
2094 release_temp( emit
, log2_abs
);
2097 if (dst
.mask
& TGSI_WRITEMASK_XYZ
&& src0
.base
.srcMod
&&
2098 src0
.base
.srcMod
!= SVGA3DSRCMOD_ABS
)
2099 release_temp( emit
, abs_tmp
);
2101 /* If w is being written, fill it with one.
2103 if (dst
.mask
& TGSI_WRITEMASK_W
) {
2104 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2105 writemask(dst
, TGSI_WRITEMASK_W
),
2106 scalar( zero
, TGSI_SWIZZLE_W
) ))
2114 static boolean
emit_bgnsub( struct svga_shader_emitter
*emit
,
2116 const struct tgsi_full_instruction
*insn
)
2120 /* Note that we've finished the main function and are now emitting
2121 * subroutines. This affects how we terminate the generated
2124 emit
->in_main_func
= FALSE
;
2126 for (i
= 0; i
< emit
->nr_labels
; i
++) {
2127 if (emit
->label
[i
] == position
) {
2128 return (emit_instruction( emit
, inst_token( SVGA3DOP_RET
) ) &&
2129 emit_instruction( emit
, inst_token( SVGA3DOP_LABEL
) ) &&
2130 emit_src( emit
, src_register( SVGA3DREG_LABEL
, i
)));
2138 static boolean
emit_call( struct svga_shader_emitter
*emit
,
2139 const struct tgsi_full_instruction
*insn
)
2141 unsigned position
= insn
->Label
.Label
;
2144 for (i
= 0; i
< emit
->nr_labels
; i
++) {
2145 if (emit
->label
[i
] == position
)
2149 if (emit
->nr_labels
== Elements(emit
->label
))
2152 if (i
== emit
->nr_labels
) {
2153 emit
->label
[i
] = position
;
2157 return (emit_instruction( emit
, inst_token( SVGA3DOP_CALL
) ) &&
2158 emit_src( emit
, src_register( SVGA3DREG_LABEL
, i
)));
2162 static boolean
emit_end( struct svga_shader_emitter
*emit
)
2164 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2165 return emit_vs_postamble( emit
);
2168 return emit_ps_postamble( emit
);
2174 static boolean
svga_emit_instruction( struct svga_shader_emitter
*emit
,
2176 const struct tgsi_full_instruction
*insn
)
2178 switch (insn
->Instruction
.Opcode
) {
2180 case TGSI_OPCODE_ARL
:
2181 return emit_arl( emit
, insn
);
2183 case TGSI_OPCODE_TEX
:
2184 case TGSI_OPCODE_TXB
:
2185 case TGSI_OPCODE_TXP
:
2186 case TGSI_OPCODE_TXL
:
2187 case TGSI_OPCODE_TXD
:
2188 return emit_tex( emit
, insn
);
2190 case TGSI_OPCODE_DDX
:
2191 case TGSI_OPCODE_DDY
:
2192 return emit_deriv( emit
, insn
);
2194 case TGSI_OPCODE_BGNSUB
:
2195 return emit_bgnsub( emit
, position
, insn
);
2197 case TGSI_OPCODE_ENDSUB
:
2200 case TGSI_OPCODE_CAL
:
2201 return emit_call( emit
, insn
);
2203 case TGSI_OPCODE_FLR
:
2204 case TGSI_OPCODE_TRUNC
: /* should be TRUNC, not FLR */
2205 return emit_floor( emit
, insn
);
2207 case TGSI_OPCODE_CMP
:
2208 return emit_cmp( emit
, insn
);
2210 case TGSI_OPCODE_DIV
:
2211 return emit_div( emit
, insn
);
2213 case TGSI_OPCODE_DP2
:
2214 return emit_dp2( emit
, insn
);
2216 case TGSI_OPCODE_DPH
:
2217 return emit_dph( emit
, insn
);
2219 case TGSI_OPCODE_NRM
:
2220 return emit_nrm( emit
, insn
);
2222 case TGSI_OPCODE_COS
:
2223 return emit_cos( emit
, insn
);
2225 case TGSI_OPCODE_SIN
:
2226 return emit_sin( emit
, insn
);
2228 case TGSI_OPCODE_SCS
:
2229 return emit_sincos( emit
, insn
);
2231 case TGSI_OPCODE_END
:
2232 /* TGSI always finishes the main func with an END */
2233 return emit_end( emit
);
2235 case TGSI_OPCODE_KIL
:
2236 return emit_kil( emit
, insn
);
2238 /* Selection opcodes. The underlying language is fairly
2239 * non-orthogonal about these.
2241 case TGSI_OPCODE_SEQ
:
2242 return emit_select_op( emit
, PIPE_FUNC_EQUAL
, insn
);
2244 case TGSI_OPCODE_SNE
:
2245 return emit_select_op( emit
, PIPE_FUNC_NOTEQUAL
, insn
);
2247 case TGSI_OPCODE_SGT
:
2248 return emit_select_op( emit
, PIPE_FUNC_GREATER
, insn
);
2250 case TGSI_OPCODE_SGE
:
2251 return emit_select_op( emit
, PIPE_FUNC_GEQUAL
, insn
);
2253 case TGSI_OPCODE_SLT
:
2254 return emit_select_op( emit
, PIPE_FUNC_LESS
, insn
);
2256 case TGSI_OPCODE_SLE
:
2257 return emit_select_op( emit
, PIPE_FUNC_LEQUAL
, insn
);
2259 case TGSI_OPCODE_SUB
:
2260 return emit_sub( emit
, insn
);
2262 case TGSI_OPCODE_POW
:
2263 return emit_pow( emit
, insn
);
2265 case TGSI_OPCODE_EX2
:
2266 return emit_ex2( emit
, insn
);
2268 case TGSI_OPCODE_EXP
:
2269 return emit_exp( emit
, insn
);
2271 case TGSI_OPCODE_LOG
:
2272 return emit_log( emit
, insn
);
2274 case TGSI_OPCODE_LG2
:
2275 return emit_scalar_op1( emit
, SVGA3DOP_LOG
, insn
);
2277 case TGSI_OPCODE_RSQ
:
2278 return emit_scalar_op1( emit
, SVGA3DOP_RSQ
, insn
);
2280 case TGSI_OPCODE_RCP
:
2281 return emit_scalar_op1( emit
, SVGA3DOP_RCP
, insn
);
2283 case TGSI_OPCODE_CONT
:
2284 case TGSI_OPCODE_RET
:
2285 /* This is a noop -- we tell mesa that we can't support RET
2286 * within a function (early return), so this will always be
2287 * followed by an ENDSUB.
2291 /* These aren't actually used by any of the frontends we care
2294 case TGSI_OPCODE_CLAMP
:
2295 case TGSI_OPCODE_ROUND
:
2296 case TGSI_OPCODE_AND
:
2297 case TGSI_OPCODE_OR
:
2298 case TGSI_OPCODE_I2F
:
2299 case TGSI_OPCODE_NOT
:
2300 case TGSI_OPCODE_SHL
:
2301 case TGSI_OPCODE_ISHR
:
2302 case TGSI_OPCODE_XOR
:
2305 case TGSI_OPCODE_IF
:
2306 return emit_if( emit
, insn
);
2307 case TGSI_OPCODE_ELSE
:
2308 return emit_else( emit
, insn
);
2309 case TGSI_OPCODE_ENDIF
:
2310 return emit_endif( emit
, insn
);
2312 case TGSI_OPCODE_BGNLOOP
:
2313 return emit_bgnloop2( emit
, insn
);
2314 case TGSI_OPCODE_ENDLOOP
:
2315 return emit_endloop2( emit
, insn
);
2316 case TGSI_OPCODE_BRK
:
2317 return emit_brk( emit
, insn
);
2319 case TGSI_OPCODE_XPD
:
2320 return emit_xpd( emit
, insn
);
2322 case TGSI_OPCODE_KILP
:
2323 return emit_kilp( emit
, insn
);
2325 case TGSI_OPCODE_DST
:
2326 return emit_dst_insn( emit
, insn
);
2328 case TGSI_OPCODE_LIT
:
2329 return emit_lit( emit
, insn
);
2331 case TGSI_OPCODE_LRP
:
2332 return emit_lrp( emit
, insn
);
2335 unsigned opcode
= translate_opcode(insn
->Instruction
.Opcode
);
2337 if (opcode
== SVGA3DOP_LAST_INST
)
2340 if (!emit_simple_instruction( emit
, opcode
, insn
))
2349 static boolean
svga_emit_immediate( struct svga_shader_emitter
*emit
,
2350 struct tgsi_full_immediate
*imm
)
2352 static const float id
[4] = {0,0,0,1};
2356 assert(1 <= imm
->Immediate
.NrTokens
&& imm
->Immediate
.NrTokens
<= 5);
2357 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++)
2358 value
[i
] = imm
->u
[i
].Float
;
2360 for ( ; i
< 4; i
++ )
2363 return emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
2364 emit
->imm_start
+ emit
->internal_imm_count
++,
2365 value
[0], value
[1], value
[2], value
[3]);
2368 static boolean
make_immediate( struct svga_shader_emitter
*emit
,
2373 struct src_register
*out
)
2375 unsigned idx
= emit
->nr_hw_const
++;
2377 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
2381 *out
= src_register( SVGA3DREG_CONST
, idx
);
2386 static boolean
emit_vs_preamble( struct svga_shader_emitter
*emit
)
2388 if (!emit
->key
.vkey
.need_prescale
) {
2389 if (!make_immediate( emit
, 0, 0, .5, .5,
2397 static boolean
emit_ps_preamble( struct svga_shader_emitter
*emit
)
2401 /* For SM20, need to initialize the temporaries we're using to hold
2402 * color outputs to some value. Shaders which don't set all of
2403 * these values are likely to be rejected by the DX9 runtime.
2405 if (!emit
->use_sm30
) {
2406 struct src_register zero
= get_zero_immediate( emit
);
2407 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
2408 if (SVGA3dShaderGetRegType(emit
->true_col
[i
].value
) != 0) {
2410 if (!submit_op1( emit
,
2411 inst_token(SVGA3DOP_MOV
),
2422 static boolean
emit_ps_postamble( struct svga_shader_emitter
*emit
)
2426 /* PS oDepth is incredibly fragile and it's very hard to catch the
2427 * types of usage that break it during shader emit. Easier just to
2428 * redirect the main program to a temporary and then only touch
2429 * oDepth with a hand-crafted MOV below.
2431 if (SVGA3dShaderGetRegType(emit
->true_pos
.value
) != 0) {
2433 if (!submit_op1( emit
,
2434 inst_token(SVGA3DOP_MOV
),
2436 scalar(src(emit
->temp_pos
), TGSI_SWIZZLE_Z
) ))
2440 /* Similarly for SM20 color outputs... Luckily SM30 isn't so
2443 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
2444 if (SVGA3dShaderGetRegType(emit
->true_col
[i
].value
) != 0) {
2446 /* Potentially override output colors with white for XOR
2447 * logicop workaround.
2449 if (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
2450 emit
->key
.fkey
.white_fragments
) {
2452 struct src_register one
= scalar( get_zero_immediate( emit
),
2455 if (!submit_op1( emit
,
2456 inst_token(SVGA3DOP_MOV
),
2462 if (!submit_op1( emit
,
2463 inst_token(SVGA3DOP_MOV
),
2465 src(emit
->temp_col
[i
]) ))
2474 static boolean
emit_vs_postamble( struct svga_shader_emitter
*emit
)
2476 /* PSIZ output is incredibly fragile and it's very hard to catch
2477 * the types of usage that break it during shader emit. Easier
2478 * just to redirect the main program to a temporary and then only
2479 * touch PSIZ with a hand-crafted MOV below.
2481 if (SVGA3dShaderGetRegType(emit
->true_psiz
.value
) != 0) {
2483 if (!submit_op1( emit
,
2484 inst_token(SVGA3DOP_MOV
),
2486 scalar(src(emit
->temp_psiz
), TGSI_SWIZZLE_X
) ))
2490 /* Need to perform various manipulations on vertex position to cope
2491 * with the different GL and D3D clip spaces.
2493 if (emit
->key
.vkey
.need_prescale
) {
2494 SVGA3dShaderDestToken temp_pos
= emit
->temp_pos
;
2495 SVGA3dShaderDestToken pos
= emit
->true_pos
;
2496 unsigned offset
= emit
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
2497 struct src_register prescale_scale
= src_register( SVGA3DREG_CONST
,
2499 struct src_register prescale_trans
= src_register( SVGA3DREG_CONST
,
2502 /* MUL temp_pos.xyz, temp_pos, prescale.scale
2503 * MAD result.position, temp_pos.wwww, prescale.trans, temp_pos
2504 * --> Note that prescale.trans.w == 0
2506 if (!submit_op2( emit
,
2507 inst_token(SVGA3DOP_MUL
),
2508 writemask(temp_pos
, TGSI_WRITEMASK_XYZ
),
2513 if (!submit_op3( emit
,
2514 inst_token(SVGA3DOP_MAD
),
2516 swizzle(src(temp_pos
), 3, 3, 3, 3),
2522 SVGA3dShaderDestToken temp_pos
= emit
->temp_pos
;
2523 SVGA3dShaderDestToken pos
= emit
->true_pos
;
2524 struct src_register imm_0055
= emit
->imm_0055
;
2526 /* Adjust GL clipping coordinate space to hardware (D3D-style):
2528 * DP4 temp_pos.z, {0,0,.5,.5}, temp_pos
2529 * MOV result.position, temp_pos
2531 if (!submit_op2( emit
,
2532 inst_token(SVGA3DOP_DP4
),
2533 writemask(temp_pos
, TGSI_WRITEMASK_Z
),
2538 if (!submit_op1( emit
,
2539 inst_token(SVGA3DOP_MOV
),
2550 1: COLOR = FrontColor;
2552 3: COLOR = BackColor;
2555 static boolean
emit_light_twoside( struct svga_shader_emitter
*emit
)
2557 struct src_register vface
, zero
;
2558 struct src_register front
[2];
2559 struct src_register back
[2];
2560 SVGA3dShaderDestToken color
[2];
2561 int count
= emit
->internal_color_count
;
2563 SVGA3dShaderInstToken if_token
;
2568 vface
= get_vface( emit
);
2569 zero
= get_zero_immediate( emit
);
2571 /* Can't use get_temp() to allocate the color reg as such
2572 * temporaries will be reclaimed after each instruction by the call
2573 * to reset_temp_regs().
2575 for (i
= 0; i
< count
; i
++) {
2576 color
[i
] = dst_register( SVGA3DREG_TEMP
,
2577 emit
->nr_hw_temp
++ );
2579 front
[i
] = emit
->input_map
[emit
->internal_color_idx
[i
]];
2581 /* Back is always the next input:
2584 back
[i
].base
.num
= front
[i
].base
.num
+ 1;
2586 /* Reassign the input_map to the actual front-face color:
2588 emit
->input_map
[emit
->internal_color_idx
[i
]] = src(color
[i
]);
2591 if_token
= inst_token( SVGA3DOP_IFC
);
2593 if (emit
->key
.fkey
.front_cw
)
2594 if_token
.control
= SVGA3DOPCOMP_GT
;
2596 if_token
.control
= SVGA3DOPCOMP_LT
;
2598 zero
= scalar(zero
, TGSI_SWIZZLE_X
);
2600 if (!(emit_instruction( emit
, if_token
) &&
2601 emit_src( emit
, vface
) &&
2602 emit_src( emit
, zero
) ))
2605 for (i
= 0; i
< count
; i
++) {
2606 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), color
[i
], front
[i
] ))
2610 if (!(emit_instruction( emit
, inst_token( SVGA3DOP_ELSE
))))
2613 for (i
= 0; i
< count
; i
++) {
2614 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), color
[i
], back
[i
] ))
2618 if (!emit_instruction( emit
, inst_token( SVGA3DOP_ENDIF
) ))
2625 0: SETP_GT TEMP, VFACE, 0
2626 where TEMP is a fake frontface register
2628 static boolean
emit_frontface( struct svga_shader_emitter
*emit
)
2630 struct src_register vface
, zero
;
2631 SVGA3dShaderDestToken temp
;
2632 struct src_register pass
, fail
;
2634 vface
= get_vface( emit
);
2635 zero
= get_zero_immediate( emit
);
2637 /* Can't use get_temp() to allocate the fake frontface reg as such
2638 * temporaries will be reclaimed after each instruction by the call
2639 * to reset_temp_regs().
2641 temp
= dst_register( SVGA3DREG_TEMP
,
2642 emit
->nr_hw_temp
++ );
2644 if (emit
->key
.fkey
.front_cw
) {
2645 pass
= scalar( zero
, TGSI_SWIZZLE_W
);
2646 fail
= scalar( zero
, TGSI_SWIZZLE_X
);
2648 pass
= scalar( zero
, TGSI_SWIZZLE_X
);
2649 fail
= scalar( zero
, TGSI_SWIZZLE_W
);
2652 if (!emit_conditional(emit
, PIPE_FUNC_GREATER
,
2653 temp
, vface
, scalar( zero
, TGSI_SWIZZLE_X
),
2657 /* Reassign the input_map to the actual front-face color:
2659 emit
->input_map
[emit
->internal_frontface_idx
] = src(temp
);
2664 static INLINE boolean
2665 needs_to_create_zero( struct svga_shader_emitter
*emit
)
2669 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
2670 if (!emit
->use_sm30
)
2673 if (emit
->key
.fkey
.light_twoside
)
2676 if (emit
->key
.fkey
.white_fragments
)
2679 if (emit
->emit_frontface
)
2682 if (emit
->info
.opcode_count
[TGSI_OPCODE_DST
] >= 1 ||
2683 emit
->info
.opcode_count
[TGSI_OPCODE_LIT
] >= 1)
2687 if (emit
->info
.opcode_count
[TGSI_OPCODE_IF
] >= 1 ||
2688 emit
->info
.opcode_count
[TGSI_OPCODE_BGNLOOP
] >= 1 ||
2689 emit
->info
.opcode_count
[TGSI_OPCODE_BGNFOR
] >= 1 ||
2690 emit
->info
.opcode_count
[TGSI_OPCODE_DDX
] >= 1 ||
2691 emit
->info
.opcode_count
[TGSI_OPCODE_DDY
] >= 1 ||
2692 emit
->info
.opcode_count
[TGSI_OPCODE_SGE
] >= 1 ||
2693 emit
->info
.opcode_count
[TGSI_OPCODE_SGT
] >= 1 ||
2694 emit
->info
.opcode_count
[TGSI_OPCODE_SLE
] >= 1 ||
2695 emit
->info
.opcode_count
[TGSI_OPCODE_SLT
] >= 1 ||
2696 emit
->info
.opcode_count
[TGSI_OPCODE_SNE
] >= 1 ||
2697 emit
->info
.opcode_count
[TGSI_OPCODE_SEQ
] >= 1 ||
2698 emit
->info
.opcode_count
[TGSI_OPCODE_EXP
] >= 1 ||
2699 emit
->info
.opcode_count
[TGSI_OPCODE_LOG
] >= 1 ||
2700 emit
->info
.opcode_count
[TGSI_OPCODE_XPD
] >= 1 ||
2701 emit
->info
.opcode_count
[TGSI_OPCODE_KILP
] >= 1)
2704 for (i
= 0; i
< emit
->key
.fkey
.num_textures
; i
++) {
2705 if (emit
->key
.fkey
.tex
[i
].compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
)
2712 static INLINE boolean
2713 needs_to_create_loop_const( struct svga_shader_emitter
*emit
)
2715 return (emit
->info
.opcode_count
[TGSI_OPCODE_BGNLOOP
] >= 1);
2718 static INLINE boolean
2719 needs_to_create_sincos_consts( struct svga_shader_emitter
*emit
)
2721 return !emit
->use_sm30
&& (emit
->info
.opcode_count
[TGSI_OPCODE_SIN
] >= 1 ||
2722 emit
->info
.opcode_count
[TGSI_OPCODE_COS
] >= 1 ||
2723 emit
->info
.opcode_count
[TGSI_OPCODE_SCS
] >= 1);
2726 static INLINE boolean
2727 needs_to_create_arl_consts( struct svga_shader_emitter
*emit
)
2729 return (emit
->num_arl_consts
> 0);
2732 static INLINE boolean
2733 pre_parse_add_indirect( struct svga_shader_emitter
*emit
,
2734 int num
, int current_arl
)
2739 for (i
= 0; i
< emit
->num_arl_consts
; ++i
) {
2740 if (emit
->arl_consts
[i
].arl_num
== current_arl
)
2744 if (emit
->num_arl_consts
== i
) {
2745 ++emit
->num_arl_consts
;
2747 emit
->arl_consts
[i
].number
= (emit
->arl_consts
[i
].number
> num
) ?
2749 emit
->arl_consts
[i
].number
;
2750 emit
->arl_consts
[i
].arl_num
= current_arl
;
2755 pre_parse_instruction( struct svga_shader_emitter
*emit
,
2756 const struct tgsi_full_instruction
*insn
,
2759 if (insn
->Src
[0].Register
.Indirect
&&
2760 insn
->Src
[0].Indirect
.File
== TGSI_FILE_ADDRESS
) {
2761 const struct tgsi_full_src_register
*reg
= &insn
->Src
[0];
2762 if (reg
->Register
.Index
< 0) {
2763 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
2767 if (insn
->Src
[1].Register
.Indirect
&&
2768 insn
->Src
[1].Indirect
.File
== TGSI_FILE_ADDRESS
) {
2769 const struct tgsi_full_src_register
*reg
= &insn
->Src
[1];
2770 if (reg
->Register
.Index
< 0) {
2771 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
2775 if (insn
->Src
[2].Register
.Indirect
&&
2776 insn
->Src
[2].Indirect
.File
== TGSI_FILE_ADDRESS
) {
2777 const struct tgsi_full_src_register
*reg
= &insn
->Src
[2];
2778 if (reg
->Register
.Index
< 0) {
2779 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
2787 pre_parse_tokens( struct svga_shader_emitter
*emit
,
2788 const struct tgsi_token
*tokens
)
2790 struct tgsi_parse_context parse
;
2791 int current_arl
= 0;
2793 tgsi_parse_init( &parse
, tokens
);
2795 while (!tgsi_parse_end_of_tokens( &parse
)) {
2796 tgsi_parse_token( &parse
);
2797 switch (parse
.FullToken
.Token
.Type
) {
2798 case TGSI_TOKEN_TYPE_IMMEDIATE
:
2799 case TGSI_TOKEN_TYPE_DECLARATION
:
2801 case TGSI_TOKEN_TYPE_INSTRUCTION
:
2802 if (parse
.FullToken
.FullInstruction
.Instruction
.Opcode
==
2806 if (!pre_parse_instruction( emit
, &parse
.FullToken
.FullInstruction
,
2818 static boolean
svga_shader_emit_helpers( struct svga_shader_emitter
*emit
)
2821 if (needs_to_create_zero( emit
)) {
2822 create_zero_immediate( emit
);
2824 if (needs_to_create_loop_const( emit
)) {
2825 create_loop_const( emit
);
2827 if (needs_to_create_sincos_consts( emit
)) {
2828 create_sincos_consts( emit
);
2830 if (needs_to_create_arl_consts( emit
)) {
2831 create_arl_consts( emit
);
2834 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
2835 if (!emit_ps_preamble( emit
))
2838 if (emit
->key
.fkey
.light_twoside
) {
2839 if (!emit_light_twoside( emit
))
2842 if (emit
->emit_frontface
) {
2843 if (!emit_frontface( emit
))
2851 boolean
svga_shader_emit_instructions( struct svga_shader_emitter
*emit
,
2852 const struct tgsi_token
*tokens
)
2854 struct tgsi_parse_context parse
;
2856 boolean helpers_emitted
= FALSE
;
2857 unsigned line_nr
= 0;
2859 tgsi_parse_init( &parse
, tokens
);
2860 emit
->internal_imm_count
= 0;
2862 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2863 ret
= emit_vs_preamble( emit
);
2868 pre_parse_tokens(emit
, tokens
);
2870 while (!tgsi_parse_end_of_tokens( &parse
)) {
2871 tgsi_parse_token( &parse
);
2873 switch (parse
.FullToken
.Token
.Type
) {
2874 case TGSI_TOKEN_TYPE_IMMEDIATE
:
2875 ret
= svga_emit_immediate( emit
, &parse
.FullToken
.FullImmediate
);
2880 case TGSI_TOKEN_TYPE_DECLARATION
:
2882 ret
= svga_translate_decl_sm30( emit
, &parse
.FullToken
.FullDeclaration
);
2884 ret
= svga_translate_decl_sm20( emit
, &parse
.FullToken
.FullDeclaration
);
2889 case TGSI_TOKEN_TYPE_INSTRUCTION
:
2890 if (!helpers_emitted
) {
2891 if (!svga_shader_emit_helpers( emit
))
2893 helpers_emitted
= TRUE
;
2895 ret
= svga_emit_instruction( emit
,
2897 &parse
.FullToken
.FullInstruction
);
2905 reset_temp_regs( emit
);
2908 /* Need to terminate the current subroutine. Note that the
2909 * hardware doesn't tolerate shaders without sub-routines
2910 * terminating with RET+END.
2912 if (!emit
->in_main_func
) {
2913 ret
= emit_instruction( emit
, inst_token( SVGA3DOP_RET
) );
2918 assert(emit
->dynamic_branching_level
== 0);
2920 /* Need to terminate the whole shader:
2922 ret
= emit_instruction( emit
, inst_token( SVGA3DOP_END
) );
2928 tgsi_parse_free( &parse
);