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_FRC
: return SVGA3DOP_FRC
;
53 case TGSI_OPCODE_MAD
: return SVGA3DOP_MAD
;
54 case TGSI_OPCODE_MAX
: return SVGA3DOP_MAX
;
55 case TGSI_OPCODE_MIN
: return SVGA3DOP_MIN
;
56 case TGSI_OPCODE_MOV
: return SVGA3DOP_MOV
;
57 case TGSI_OPCODE_MUL
: return SVGA3DOP_MUL
;
58 case TGSI_OPCODE_NOP
: return SVGA3DOP_NOP
;
59 case TGSI_OPCODE_NRM4
: return SVGA3DOP_NRM
;
60 case TGSI_OPCODE_SSG
: return SVGA3DOP_SGN
;
62 debug_printf("Unkown opcode %u\n", opcode
);
64 return SVGA3DOP_LAST_INST
;
69 static unsigned translate_file( unsigned file
)
72 case TGSI_FILE_TEMPORARY
: return SVGA3DREG_TEMP
;
73 case TGSI_FILE_INPUT
: return SVGA3DREG_INPUT
;
74 case TGSI_FILE_OUTPUT
: return SVGA3DREG_OUTPUT
; /* VS3.0+ only */
75 case TGSI_FILE_IMMEDIATE
: return SVGA3DREG_CONST
;
76 case TGSI_FILE_CONSTANT
: return SVGA3DREG_CONST
;
77 case TGSI_FILE_SAMPLER
: return SVGA3DREG_SAMPLER
;
78 case TGSI_FILE_ADDRESS
: return SVGA3DREG_ADDR
;
81 return SVGA3DREG_TEMP
;
90 static SVGA3dShaderDestToken
91 translate_dst_register( struct svga_shader_emitter
*emit
,
92 const struct tgsi_full_instruction
*insn
,
95 const struct tgsi_full_dst_register
*reg
= &insn
->Dst
[idx
];
96 SVGA3dShaderDestToken dest
;
98 switch (reg
->Register
.File
) {
99 case TGSI_FILE_OUTPUT
:
100 /* Output registers encode semantic information in their name.
101 * Need to lookup a table built at decl time:
103 dest
= emit
->output_map
[reg
->Register
.Index
];
107 dest
= dst_register( translate_file( reg
->Register
.File
),
108 reg
->Register
.Index
);
112 dest
.mask
= reg
->Register
.WriteMask
;
115 if (insn
->Instruction
.Saturate
)
116 dest
.dstMod
= SVGA3DDSTMOD_SATURATE
;
122 static struct src_register
123 swizzle( struct src_register src
,
129 x
= (src
.base
.swizzle
>> (x
* 2)) & 0x3;
130 y
= (src
.base
.swizzle
>> (y
* 2)) & 0x3;
131 z
= (src
.base
.swizzle
>> (z
* 2)) & 0x3;
132 w
= (src
.base
.swizzle
>> (w
* 2)) & 0x3;
134 src
.base
.swizzle
= TRANSLATE_SWIZZLE(x
,y
,z
,w
);
139 static struct src_register
140 scalar( struct src_register src
,
143 return swizzle( src
, comp
, comp
, comp
, comp
);
146 static INLINE boolean
147 svga_arl_needs_adjustment( const struct svga_shader_emitter
*emit
)
151 for (i
= 0; i
< emit
->num_arl_consts
; ++i
) {
152 if (emit
->arl_consts
[i
].arl_num
== emit
->current_arl
)
159 svga_arl_adjustment( const struct svga_shader_emitter
*emit
)
163 for (i
= 0; i
< emit
->num_arl_consts
; ++i
) {
164 if (emit
->arl_consts
[i
].arl_num
== emit
->current_arl
)
165 return emit
->arl_consts
[i
].number
;
170 static struct src_register
171 translate_src_register( const struct svga_shader_emitter
*emit
,
172 const struct tgsi_full_src_register
*reg
)
174 struct src_register src
;
176 switch (reg
->Register
.File
) {
177 case TGSI_FILE_INPUT
:
178 /* Input registers are referred to by their semantic name rather
179 * than by index. Use the mapping build up from the decls:
181 src
= emit
->input_map
[reg
->Register
.Index
];
184 case TGSI_FILE_IMMEDIATE
:
185 /* Immediates are appended after TGSI constants in the D3D
188 src
= src_register( translate_file( reg
->Register
.File
),
189 reg
->Register
.Index
+
194 src
= src_register( translate_file( reg
->Register
.File
),
195 reg
->Register
.Index
);
200 /* Indirect addressing (for coninstant buffer lookups only)
202 if (reg
->Register
.Indirect
)
204 /* we shift the offset towards the minimum */
205 if (svga_arl_needs_adjustment( emit
)) {
206 src
.base
.num
-= svga_arl_adjustment( emit
);
208 src
.base
.relAddr
= 1;
210 /* Not really sure what should go in the second token:
212 src
.indirect
= src_token( SVGA3DREG_ADDR
,
213 reg
->Indirect
.Index
);
215 src
.indirect
.swizzle
= SWIZZLE_XXXX
;
219 reg
->Register
.SwizzleX
,
220 reg
->Register
.SwizzleY
,
221 reg
->Register
.SwizzleZ
,
222 reg
->Register
.SwizzleW
);
224 /* src.mod isn't a bitfield, unfortunately:
225 * See tgsi_util_get_full_src_register_sign_mode for implementation details.
227 if (reg
->Register
.Absolute
) {
228 if (reg
->Register
.Negate
)
229 src
.base
.srcMod
= SVGA3DSRCMOD_ABSNEG
;
231 src
.base
.srcMod
= SVGA3DSRCMOD_ABS
;
234 if (reg
->Register
.Negate
)
235 src
.base
.srcMod
= SVGA3DSRCMOD_NEG
;
237 src
.base
.srcMod
= SVGA3DSRCMOD_NONE
;
245 * Get a temporary register, return -1 if none available
247 static INLINE SVGA3dShaderDestToken
248 get_temp( struct svga_shader_emitter
*emit
)
250 int i
= emit
->nr_hw_temp
+ emit
->internal_temp_count
++;
252 return dst_register( SVGA3DREG_TEMP
, i
);
255 /* Release a single temp. Currently only effective if it was the last
256 * allocated temp, otherwise release will be delayed until the next
257 * call to reset_temp_regs().
260 release_temp( struct svga_shader_emitter
*emit
,
261 SVGA3dShaderDestToken temp
)
263 if (temp
.num
== emit
->internal_temp_count
- 1)
264 emit
->internal_temp_count
--;
267 static void reset_temp_regs( struct svga_shader_emitter
*emit
)
269 emit
->internal_temp_count
= 0;
273 static boolean
submit_op0( struct svga_shader_emitter
*emit
,
274 SVGA3dShaderInstToken inst
,
275 SVGA3dShaderDestToken dest
)
277 return (emit_instruction( emit
, inst
) &&
278 emit_dst( emit
, dest
));
281 static boolean
submit_op1( struct svga_shader_emitter
*emit
,
282 SVGA3dShaderInstToken inst
,
283 SVGA3dShaderDestToken dest
,
284 struct src_register src0
)
286 return emit_op1( emit
, inst
, dest
, src0
);
290 /* SVGA shaders may not refer to >1 constant register in a single
291 * instruction. This function checks for that usage and inserts a
292 * move to temporary if detected.
294 * The same applies to input registers -- at most a single input
295 * register may be read by any instruction.
297 static boolean
submit_op2( struct svga_shader_emitter
*emit
,
298 SVGA3dShaderInstToken inst
,
299 SVGA3dShaderDestToken dest
,
300 struct src_register src0
,
301 struct src_register src1
)
303 SVGA3dShaderDestToken temp
;
304 SVGA3dShaderRegType type0
, type1
;
305 boolean need_temp
= FALSE
;
308 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
309 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
311 if (type0
== SVGA3DREG_CONST
&&
312 type1
== SVGA3DREG_CONST
&&
313 src0
.base
.num
!= src1
.base
.num
)
316 if (type0
== SVGA3DREG_INPUT
&&
317 type1
== SVGA3DREG_INPUT
&&
318 src0
.base
.num
!= src1
.base
.num
)
323 temp
= get_temp( emit
);
325 if (!emit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp
, src0
))
331 if (!emit_op2( emit
, inst
, dest
, src0
, src1
))
335 release_temp( emit
, temp
);
341 /* SVGA shaders may not refer to >1 constant register in a single
342 * instruction. This function checks for that usage and inserts a
343 * move to temporary if detected.
345 static boolean
submit_op3( struct svga_shader_emitter
*emit
,
346 SVGA3dShaderInstToken inst
,
347 SVGA3dShaderDestToken dest
,
348 struct src_register src0
,
349 struct src_register src1
,
350 struct src_register src2
)
352 SVGA3dShaderDestToken temp0
;
353 SVGA3dShaderDestToken temp1
;
354 boolean need_temp0
= FALSE
;
355 boolean need_temp1
= FALSE
;
356 SVGA3dShaderRegType type0
, type1
, type2
;
360 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
361 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
362 type2
= SVGA3dShaderGetRegType( src2
.base
.value
);
364 if (inst
.op
!= SVGA3DOP_SINCOS
) {
365 if (type0
== SVGA3DREG_CONST
&&
366 ((type1
== SVGA3DREG_CONST
&& src0
.base
.num
!= src1
.base
.num
) ||
367 (type2
== SVGA3DREG_CONST
&& src0
.base
.num
!= src2
.base
.num
)))
370 if (type1
== SVGA3DREG_CONST
&&
371 (type2
== SVGA3DREG_CONST
&& src1
.base
.num
!= src2
.base
.num
))
375 if (type0
== SVGA3DREG_INPUT
&&
376 ((type1
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src1
.base
.num
) ||
377 (type2
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src2
.base
.num
)))
380 if (type1
== SVGA3DREG_INPUT
&&
381 (type2
== SVGA3DREG_INPUT
&& src1
.base
.num
!= src2
.base
.num
))
386 temp0
= get_temp( emit
);
388 if (!emit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp0
, src0
))
396 temp1
= get_temp( emit
);
398 if (!emit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp1
, src1
))
404 if (!emit_op3( emit
, inst
, dest
, src0
, src1
, src2
))
408 release_temp( emit
, temp1
);
410 release_temp( emit
, temp0
);
417 /* SVGA shaders may not refer to >1 constant register in a single
418 * instruction. This function checks for that usage and inserts a
419 * move to temporary if detected.
421 static boolean
submit_op4( struct svga_shader_emitter
*emit
,
422 SVGA3dShaderInstToken inst
,
423 SVGA3dShaderDestToken dest
,
424 struct src_register src0
,
425 struct src_register src1
,
426 struct src_register src2
,
427 struct src_register src3
)
429 SVGA3dShaderDestToken temp0
;
430 SVGA3dShaderDestToken temp3
;
431 boolean need_temp0
= FALSE
;
432 boolean need_temp3
= FALSE
;
433 SVGA3dShaderRegType type0
, type1
, type2
, type3
;
437 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
438 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
439 type2
= SVGA3dShaderGetRegType( src2
.base
.value
);
440 type3
= SVGA3dShaderGetRegType( src2
.base
.value
);
442 /* Make life a little easier - this is only used by the TXD
443 * instruction which is guaranteed not to have a constant/input reg
444 * in one slot at least:
446 assert(type1
== SVGA3DREG_SAMPLER
);
448 if (type0
== SVGA3DREG_CONST
&&
449 ((type3
== SVGA3DREG_CONST
&& src0
.base
.num
!= src3
.base
.num
) ||
450 (type2
== SVGA3DREG_CONST
&& src0
.base
.num
!= src2
.base
.num
)))
453 if (type3
== SVGA3DREG_CONST
&&
454 (type2
== SVGA3DREG_CONST
&& src3
.base
.num
!= src2
.base
.num
))
457 if (type0
== SVGA3DREG_INPUT
&&
458 ((type3
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src3
.base
.num
) ||
459 (type2
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src2
.base
.num
)))
462 if (type3
== SVGA3DREG_INPUT
&&
463 (type2
== SVGA3DREG_INPUT
&& src3
.base
.num
!= src2
.base
.num
))
468 temp0
= get_temp( emit
);
470 if (!emit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp0
, src0
))
478 temp3
= get_temp( emit
);
480 if (!emit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp3
, src3
))
486 if (!emit_op4( emit
, inst
, dest
, src0
, src1
, src2
, src3
))
490 release_temp( emit
, temp3
);
492 release_temp( emit
, temp0
);
497 static boolean
emit_def_const( struct svga_shader_emitter
*emit
,
498 SVGA3dShaderConstType type
,
506 SVGA3dShaderInstToken opcode
;
509 case SVGA3D_CONST_TYPE_FLOAT
:
510 opcode
= inst_token( SVGA3DOP_DEF
);
511 def
.dst
= dst_register( SVGA3DREG_CONST
, idx
);
512 def
.constValues
[0] = a
;
513 def
.constValues
[1] = b
;
514 def
.constValues
[2] = c
;
515 def
.constValues
[3] = d
;
517 case SVGA3D_CONST_TYPE_INT
:
518 opcode
= inst_token( SVGA3DOP_DEFI
);
519 def
.dst
= dst_register( SVGA3DREG_CONSTINT
, idx
);
520 def
.constIValues
[0] = (int)a
;
521 def
.constIValues
[1] = (int)b
;
522 def
.constIValues
[2] = (int)c
;
523 def
.constIValues
[3] = (int)d
;
527 opcode
= inst_token( SVGA3DOP_NOP
);
531 if (!emit_instruction(emit
, opcode
) ||
532 !svga_shader_emit_dwords( emit
, def
.values
, Elements(def
.values
)))
538 static INLINE boolean
539 create_zero_immediate( struct svga_shader_emitter
*emit
)
541 unsigned idx
= emit
->nr_hw_const
++;
543 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
547 emit
->zero_immediate_idx
= idx
;
548 emit
->created_zero_immediate
= TRUE
;
553 static INLINE boolean
554 create_loop_const( struct svga_shader_emitter
*emit
)
556 unsigned idx
= emit
->nr_hw_const
++;
558 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_INT
, idx
,
559 255, /* iteration count */
560 0, /* initial value */
562 0 /* not used, must be 0 */))
565 emit
->loop_const_idx
= idx
;
566 emit
->created_loop_const
= TRUE
;
571 static INLINE boolean
572 create_sincos_consts( struct svga_shader_emitter
*emit
)
574 unsigned idx
= emit
->nr_hw_const
++;
576 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
, idx
,
583 emit
->sincos_consts_idx
= idx
;
584 idx
= emit
->nr_hw_const
++;
586 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
, idx
,
593 emit
->created_sincos_consts
= TRUE
;
598 static INLINE boolean
599 create_arl_consts( struct svga_shader_emitter
*emit
)
603 for (i
= 0; i
< emit
->num_arl_consts
; i
+= 4) {
605 unsigned idx
= emit
->nr_hw_const
++;
607 for (j
= 0; j
< 4 && (j
+ i
) < emit
->num_arl_consts
; ++j
) {
608 vals
[j
] = emit
->arl_consts
[i
+ j
].number
;
609 emit
->arl_consts
[i
+ j
].idx
= idx
;
612 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_X
;
615 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_Y
;
618 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_Z
;
621 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_W
;
628 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
, idx
,
637 static INLINE
struct src_register
638 get_vface( struct svga_shader_emitter
*emit
)
640 assert(emit
->emitted_vface
);
641 return src_register(SVGA3DREG_MISCTYPE
,
645 /* returns {0, 0, 0, 1} immediate */
646 static INLINE
struct src_register
647 get_zero_immediate( struct svga_shader_emitter
*emit
)
649 assert(emit
->created_zero_immediate
);
650 assert(emit
->zero_immediate_idx
>= 0);
651 return src_register( SVGA3DREG_CONST
,
652 emit
->zero_immediate_idx
);
655 /* returns the loop const */
656 static INLINE
struct src_register
657 get_loop_const( struct svga_shader_emitter
*emit
)
659 assert(emit
->created_loop_const
);
660 assert(emit
->loop_const_idx
>= 0);
661 return src_register( SVGA3DREG_CONSTINT
,
662 emit
->loop_const_idx
);
665 /* returns a sincos const */
666 static INLINE
struct src_register
667 get_sincos_const( struct svga_shader_emitter
*emit
,
670 assert(emit
->created_sincos_consts
);
671 assert(emit
->sincos_consts_idx
>= 0);
672 assert(index
== 0 || index
== 1);
673 return src_register( SVGA3DREG_CONST
,
674 emit
->sincos_consts_idx
+ index
);
677 static INLINE
struct src_register
678 get_fake_arl_const( struct svga_shader_emitter
*emit
)
680 struct src_register reg
;
681 int idx
= 0, swizzle
= 0, i
;
683 for (i
= 0; i
< emit
->num_arl_consts
; ++ i
) {
684 if (emit
->arl_consts
[i
].arl_num
== emit
->current_arl
) {
685 idx
= emit
->arl_consts
[i
].idx
;
686 swizzle
= emit
->arl_consts
[i
].swizzle
;
690 reg
= src_register( SVGA3DREG_CONST
, idx
);
691 return scalar(reg
, swizzle
);
694 static INLINE
struct src_register
695 get_tex_dimensions( struct svga_shader_emitter
*emit
, int sampler_num
)
698 struct src_register reg
;
700 /* the width/height indexes start right after constants */
701 idx
= emit
->key
.fkey
.tex
[sampler_num
].width_height_idx
+
702 emit
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
704 reg
= src_register( SVGA3DREG_CONST
, idx
);
708 static boolean
emit_fake_arl(struct svga_shader_emitter
*emit
,
709 const struct tgsi_full_instruction
*insn
)
711 const struct src_register src0
= translate_src_register(
712 emit
, &insn
->Src
[0] );
713 struct src_register src1
= get_fake_arl_const( emit
);
714 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
715 SVGA3dShaderDestToken tmp
= get_temp( emit
);
717 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), tmp
, src0
))
720 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), tmp
, src( tmp
),
724 /* replicate the original swizzle */
726 src1
.base
.swizzle
= src0
.base
.swizzle
;
728 return submit_op1( emit
, inst_token( SVGA3DOP_MOVA
),
732 static boolean
emit_if(struct svga_shader_emitter
*emit
,
733 const struct tgsi_full_instruction
*insn
)
735 const struct src_register src
= translate_src_register(
736 emit
, &insn
->Src
[0] );
737 struct src_register zero
= get_zero_immediate( emit
);
738 SVGA3dShaderInstToken if_token
= inst_token( SVGA3DOP_IFC
);
740 if_token
.control
= SVGA3DOPCOMPC_NE
;
741 zero
= scalar(zero
, TGSI_SWIZZLE_X
);
743 emit
->dynamic_branching_level
++;
745 return (emit_instruction( emit
, if_token
) &&
746 emit_src( emit
, src
) &&
747 emit_src( emit
, zero
) );
750 static boolean
emit_endif(struct svga_shader_emitter
*emit
,
751 const struct tgsi_full_instruction
*insn
)
753 emit
->dynamic_branching_level
--;
755 return (emit_instruction( emit
,
756 inst_token( SVGA3DOP_ENDIF
)));
759 static boolean
emit_else(struct svga_shader_emitter
*emit
,
760 const struct tgsi_full_instruction
*insn
)
762 return (emit_instruction( emit
,
763 inst_token( SVGA3DOP_ELSE
)));
766 /* Translate the following TGSI FLR instruction.
768 * To the following SVGA3D instruction sequence.
772 static boolean
emit_floor(struct svga_shader_emitter
*emit
,
773 const struct tgsi_full_instruction
*insn
)
775 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
776 const struct src_register src0
= translate_src_register(
777 emit
, &insn
->Src
[0] );
778 SVGA3dShaderDestToken temp
= get_temp( emit
);
781 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
), temp
, src0
))
784 /* SUB DST, SRC, TMP */
785 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
, src0
,
786 negate( src( temp
) ) ))
793 /* Translate the following TGSI CMP instruction.
794 * CMP DST, SRC0, SRC1, SRC2
795 * To the following SVGA3D instruction sequence.
796 * CMP DST, SRC0, SRC2, SRC1
798 static boolean
emit_cmp(struct svga_shader_emitter
*emit
,
799 const struct tgsi_full_instruction
*insn
)
801 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
802 const struct src_register src0
= translate_src_register(
803 emit
, &insn
->Src
[0] );
804 const struct src_register src1
= translate_src_register(
805 emit
, &insn
->Src
[1] );
806 const struct src_register src2
= translate_src_register(
807 emit
, &insn
->Src
[2] );
809 /* CMP DST, SRC0, SRC2, SRC1 */
810 return submit_op3( emit
, inst_token( SVGA3DOP_CMP
), dst
, src0
, src2
, src1
);
815 /* Translate the following TGSI DIV instruction.
816 * DIV DST.xy, SRC0, SRC1
817 * To the following SVGA3D instruction sequence.
818 * RCP TMP.x, SRC1.xxxx
819 * RCP TMP.y, SRC1.yyyy
820 * MUL DST.xy, SRC0, TMP
822 static boolean
emit_div(struct svga_shader_emitter
*emit
,
823 const struct tgsi_full_instruction
*insn
)
825 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
826 const struct src_register src0
= translate_src_register(
827 emit
, &insn
->Src
[0] );
828 const struct src_register src1
= translate_src_register(
829 emit
, &insn
->Src
[1] );
830 SVGA3dShaderDestToken temp
= get_temp( emit
);
833 /* For each enabled element, perform a RCP instruction. Note that
834 * RCP is scalar in SVGA3D:
836 for (i
= 0; i
< 4; i
++) {
837 unsigned channel
= 1 << i
;
838 if (dst
.mask
& channel
) {
839 /* RCP TMP.?, SRC1.???? */
840 if (!submit_op1( emit
, inst_token( SVGA3DOP_RCP
),
841 writemask(temp
, channel
),
847 /* Then multiply them out with a single mul:
851 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
), dst
, src0
,
858 /* Translate the following TGSI DP2 instruction.
859 * DP2 DST, SRC1, SRC2
860 * To the following SVGA3D instruction sequence.
861 * MUL TMP, SRC1, SRC2
862 * ADD DST, TMP.xxxx, TMP.yyyy
864 static boolean
emit_dp2(struct svga_shader_emitter
*emit
,
865 const struct tgsi_full_instruction
*insn
)
867 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
868 const struct src_register src0
= translate_src_register(
869 emit
, &insn
->Src
[0] );
870 const struct src_register src1
= translate_src_register(
871 emit
, &insn
->Src
[1] );
872 SVGA3dShaderDestToken temp
= get_temp( emit
);
873 struct src_register temp_src0
, temp_src1
;
875 /* MUL TMP, SRC1, SRC2 */
876 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
), temp
, src0
, src1
))
879 temp_src0
= scalar(src( temp
), TGSI_SWIZZLE_X
);
880 temp_src1
= scalar(src( temp
), TGSI_SWIZZLE_Y
);
882 /* ADD DST, TMP.xxxx, TMP.yyyy */
883 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
,
884 temp_src0
, temp_src1
))
891 /* Translate the following TGSI DPH instruction.
892 * DPH DST, SRC1, SRC2
893 * To the following SVGA3D instruction sequence.
894 * DP3 TMP, SRC1, SRC2
895 * ADD DST, TMP, SRC2.wwww
897 static boolean
emit_dph(struct svga_shader_emitter
*emit
,
898 const struct tgsi_full_instruction
*insn
)
900 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
901 const struct src_register src0
= translate_src_register(
902 emit
, &insn
->Src
[0] );
903 struct src_register src1
= translate_src_register(
904 emit
, &insn
->Src
[1] );
905 SVGA3dShaderDestToken temp
= get_temp( emit
);
907 /* DP3 TMP, SRC1, SRC2 */
908 if (!submit_op2( emit
, inst_token( SVGA3DOP_DP3
), temp
, src0
, src1
))
911 src1
= scalar(src1
, TGSI_SWIZZLE_W
);
913 /* ADD DST, TMP, SRC2.wwww */
914 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
,
921 /* Translate the following TGSI DST instruction.
923 * To the following SVGA3D instruction sequence.
928 static boolean
emit_nrm(struct svga_shader_emitter
*emit
,
929 const struct tgsi_full_instruction
*insn
)
931 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
932 const struct src_register src0
= translate_src_register(
933 emit
, &insn
->Src
[0] );
934 SVGA3dShaderDestToken temp
= get_temp( emit
);
936 /* DP3 TMP, SRC, SRC */
937 if (!submit_op2( emit
, inst_token( SVGA3DOP_DP3
), temp
, src0
, src0
))
941 if (!submit_op1( emit
, inst_token( SVGA3DOP_RSQ
), temp
, src( temp
)))
944 /* MUL DST, SRC, TMP */
945 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
), dst
,
953 static boolean
do_emit_sincos(struct svga_shader_emitter
*emit
,
954 SVGA3dShaderDestToken dst
,
955 struct src_register src0
)
957 src0
= scalar(src0
, TGSI_SWIZZLE_X
);
959 if (emit
->use_sm30
) {
960 return submit_op1( emit
, inst_token( SVGA3DOP_SINCOS
),
963 struct src_register const1
= get_sincos_const( emit
, 0 );
964 struct src_register const2
= get_sincos_const( emit
, 1 );
966 return submit_op3( emit
, inst_token( SVGA3DOP_SINCOS
),
967 dst
, src0
, const1
, const2
);
971 static boolean
emit_sincos(struct svga_shader_emitter
*emit
,
972 const struct tgsi_full_instruction
*insn
)
974 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
975 struct src_register src0
= translate_src_register(
976 emit
, &insn
->Src
[0] );
977 SVGA3dShaderDestToken temp
= get_temp( emit
);
980 if (!do_emit_sincos(emit
, writemask(temp
, TGSI_WRITEMASK_XY
), src0
))
984 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src( temp
) ))
994 static boolean
emit_sin(struct svga_shader_emitter
*emit
,
995 const struct tgsi_full_instruction
*insn
)
997 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
998 struct src_register src0
= translate_src_register(
999 emit
, &insn
->Src
[0] );
1000 SVGA3dShaderDestToken temp
= get_temp( emit
);
1003 if (!do_emit_sincos(emit
, writemask(temp
, TGSI_WRITEMASK_Y
), src0
))
1006 src0
= scalar(src( temp
), TGSI_SWIZZLE_Y
);
1008 /* MOV DST TMP.yyyy */
1009 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src0
))
1019 static boolean
emit_cos(struct svga_shader_emitter
*emit
,
1020 const struct tgsi_full_instruction
*insn
)
1022 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1023 struct src_register src0
= translate_src_register(
1024 emit
, &insn
->Src
[0] );
1025 SVGA3dShaderDestToken temp
= get_temp( emit
);
1028 if (!do_emit_sincos( emit
, writemask(temp
, TGSI_WRITEMASK_X
), src0
))
1031 src0
= scalar(src( temp
), TGSI_SWIZZLE_X
);
1033 /* MOV DST TMP.xxxx */
1034 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src0
))
1042 * ADD DST SRC0, negate(SRC0)
1044 static boolean
emit_sub(struct svga_shader_emitter
*emit
,
1045 const struct tgsi_full_instruction
*insn
)
1047 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1048 struct src_register src0
= translate_src_register(
1049 emit
, &insn
->Src
[0] );
1050 struct src_register src1
= translate_src_register(
1051 emit
, &insn
->Src
[1] );
1053 src1
= negate(src1
);
1055 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
,
1063 static boolean
emit_kil(struct svga_shader_emitter
*emit
,
1064 const struct tgsi_full_instruction
*insn
)
1066 SVGA3dShaderInstToken inst
;
1067 const struct tgsi_full_src_register
*reg
= &insn
->Src
[0];
1068 struct src_register src0
;
1070 inst
= inst_token( SVGA3DOP_TEXKILL
);
1071 src0
= translate_src_register( emit
, reg
);
1073 if (reg
->Register
.Absolute
||
1074 reg
->Register
.Negate
||
1075 reg
->Register
.Indirect
||
1076 reg
->Register
.SwizzleX
!= 0 ||
1077 reg
->Register
.SwizzleY
!= 1 ||
1078 reg
->Register
.SwizzleZ
!= 2 ||
1079 reg
->Register
.File
!= TGSI_FILE_TEMPORARY
)
1081 SVGA3dShaderDestToken temp
= get_temp( emit
);
1083 submit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp
, src0
);
1087 return submit_op0( emit
, inst
, dst(src0
) );
1091 /* mesa state tracker always emits kilp as an unconditional
1093 static boolean
emit_kilp(struct svga_shader_emitter
*emit
,
1094 const struct tgsi_full_instruction
*insn
)
1096 SVGA3dShaderInstToken inst
;
1097 SVGA3dShaderDestToken temp
;
1098 struct src_register one
= scalar( get_zero_immediate( emit
),
1101 inst
= inst_token( SVGA3DOP_TEXKILL
);
1103 /* texkill doesn't allow negation on the operand so lets move
1104 * negation of {1} to a temp register */
1105 temp
= get_temp( emit
);
1106 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp
,
1110 return submit_op0( emit
, inst
, temp
);
1113 /* Implement conditionals by initializing destination reg to 'fail',
1114 * then set predicate reg with UFOP_SETP, then move 'pass' to dest
1115 * based on predicate reg.
1117 * SETP src0, cmp, src1 -- do this first to avoid aliasing problems.
1122 emit_conditional(struct svga_shader_emitter
*emit
,
1123 unsigned compare_func
,
1124 SVGA3dShaderDestToken dst
,
1125 struct src_register src0
,
1126 struct src_register src1
,
1127 struct src_register pass
,
1128 struct src_register fail
)
1130 SVGA3dShaderDestToken pred_reg
= dst_register( SVGA3DREG_PREDICATE
, 0 );
1131 SVGA3dShaderInstToken setp_token
, mov_token
;
1132 setp_token
= inst_token( SVGA3DOP_SETP
);
1134 switch (compare_func
) {
1135 case PIPE_FUNC_NEVER
:
1136 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1139 case PIPE_FUNC_LESS
:
1140 setp_token
.control
= SVGA3DOPCOMP_LT
;
1142 case PIPE_FUNC_EQUAL
:
1143 setp_token
.control
= SVGA3DOPCOMP_EQ
;
1145 case PIPE_FUNC_LEQUAL
:
1146 setp_token
.control
= SVGA3DOPCOMP_LE
;
1148 case PIPE_FUNC_GREATER
:
1149 setp_token
.control
= SVGA3DOPCOMP_GT
;
1151 case PIPE_FUNC_NOTEQUAL
:
1152 setp_token
.control
= SVGA3DOPCOMPC_NE
;
1154 case PIPE_FUNC_GEQUAL
:
1155 setp_token
.control
= SVGA3DOPCOMP_GE
;
1157 case PIPE_FUNC_ALWAYS
:
1158 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1163 /* SETP src0, COMPOP, src1 */
1164 if (!submit_op2( emit
, setp_token
, pred_reg
,
1168 mov_token
= inst_token( SVGA3DOP_MOV
);
1171 if (!submit_op1( emit
, mov_token
, dst
,
1175 /* MOV dst, pass (predicated)
1177 * Note that the predicate reg (and possible modifiers) is passed
1178 * as the first source argument.
1180 mov_token
.predicated
= 1;
1181 if (!submit_op2( emit
, mov_token
, dst
,
1182 src( pred_reg
), pass
))
1190 emit_select(struct svga_shader_emitter
*emit
,
1191 unsigned compare_func
,
1192 SVGA3dShaderDestToken dst
,
1193 struct src_register src0
,
1194 struct src_register src1
)
1196 /* There are some SVGA instructions which implement some selects
1197 * directly, but they are only available in the vertex shader.
1199 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1200 switch (compare_func
) {
1201 case PIPE_FUNC_GEQUAL
:
1202 return submit_op2( emit
, inst_token( SVGA3DOP_SGE
), dst
, src0
, src1
);
1203 case PIPE_FUNC_LEQUAL
:
1204 return submit_op2( emit
, inst_token( SVGA3DOP_SGE
), dst
, src1
, src0
);
1205 case PIPE_FUNC_GREATER
:
1206 return submit_op2( emit
, inst_token( SVGA3DOP_SLT
), dst
, src1
, src0
);
1207 case PIPE_FUNC_LESS
:
1208 return submit_op2( emit
, inst_token( SVGA3DOP_SLT
), dst
, src0
, src1
);
1215 /* Otherwise, need to use the setp approach:
1218 struct src_register one
, zero
;
1219 /* zero immediate is 0,0,0,1 */
1220 zero
= get_zero_immediate( emit
);
1221 one
= scalar( zero
, TGSI_SWIZZLE_W
);
1222 zero
= scalar( zero
, TGSI_SWIZZLE_X
);
1224 return emit_conditional(
1235 static boolean
emit_select_op(struct svga_shader_emitter
*emit
,
1237 const struct tgsi_full_instruction
*insn
)
1239 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1240 struct src_register src0
= translate_src_register(
1241 emit
, &insn
->Src
[0] );
1242 struct src_register src1
= translate_src_register(
1243 emit
, &insn
->Src
[1] );
1245 return emit_select( emit
, compare
, dst
, src0
, src1
);
1249 /* Translate texture instructions to SVGA3D representation.
1251 static boolean
emit_tex2(struct svga_shader_emitter
*emit
,
1252 const struct tgsi_full_instruction
*insn
,
1253 SVGA3dShaderDestToken dst
)
1255 SVGA3dShaderInstToken inst
;
1256 struct src_register texcoord
;
1257 struct src_register sampler
;
1258 SVGA3dShaderDestToken tmp
;
1262 switch (insn
->Instruction
.Opcode
) {
1263 case TGSI_OPCODE_TEX
:
1264 inst
.op
= SVGA3DOP_TEX
;
1266 case TGSI_OPCODE_TXP
:
1267 inst
.op
= SVGA3DOP_TEX
;
1268 inst
.control
= SVGA3DOPCONT_PROJECT
;
1270 case TGSI_OPCODE_TXB
:
1271 inst
.op
= SVGA3DOP_TEX
;
1272 inst
.control
= SVGA3DOPCONT_BIAS
;
1274 case TGSI_OPCODE_TXL
:
1275 inst
.op
= SVGA3DOP_TEXLDL
;
1282 texcoord
= translate_src_register( emit
, &insn
->Src
[0] );
1283 sampler
= translate_src_register( emit
, &insn
->Src
[1] );
1285 if (emit
->key
.fkey
.tex
[sampler
.base
.num
].unnormalized
||
1286 emit
->dynamic_branching_level
> 0)
1287 tmp
= get_temp( emit
);
1289 /* Can't do mipmapping inside dynamic branch constructs. Force LOD
1290 * zero in that case.
1292 if (emit
->dynamic_branching_level
> 0 &&
1293 inst
.op
== SVGA3DOP_TEX
&&
1294 SVGA3dShaderGetRegType(texcoord
.base
.value
) == SVGA3DREG_TEMP
) {
1295 struct src_register zero
= get_zero_immediate( emit
);
1297 /* MOV tmp, texcoord */
1298 if (!submit_op1( emit
,
1299 inst_token( SVGA3DOP_MOV
),
1304 /* MOV tmp.w, zero */
1305 if (!submit_op1( emit
,
1306 inst_token( SVGA3DOP_MOV
),
1307 writemask( tmp
, TGSI_WRITEMASK_W
),
1308 scalar( zero
, TGSI_SWIZZLE_X
)))
1311 texcoord
= src( tmp
);
1312 inst
.op
= SVGA3DOP_TEXLDL
;
1315 /* Explicit normalization of texcoords:
1317 if (emit
->key
.fkey
.tex
[sampler
.base
.num
].unnormalized
) {
1318 struct src_register wh
= get_tex_dimensions( emit
, sampler
.base
.num
);
1320 /* MUL tmp, SRC0, WH */
1321 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
1322 tmp
, texcoord
, wh
))
1325 texcoord
= src( tmp
);
1328 return submit_op2( emit
, inst
, dst
, texcoord
, sampler
);
1334 /* Translate texture instructions to SVGA3D representation.
1336 static boolean
emit_tex4(struct svga_shader_emitter
*emit
,
1337 const struct tgsi_full_instruction
*insn
,
1338 SVGA3dShaderDestToken dst
)
1340 SVGA3dShaderInstToken inst
;
1341 struct src_register texcoord
;
1342 struct src_register ddx
;
1343 struct src_register ddy
;
1344 struct src_register sampler
;
1346 texcoord
= translate_src_register( emit
, &insn
->Src
[0] );
1347 ddx
= translate_src_register( emit
, &insn
->Src
[1] );
1348 ddy
= translate_src_register( emit
, &insn
->Src
[2] );
1349 sampler
= translate_src_register( emit
, &insn
->Src
[3] );
1353 switch (insn
->Instruction
.Opcode
) {
1354 case TGSI_OPCODE_TXD
:
1355 inst
.op
= SVGA3DOP_TEXLDD
; /* 4 args! */
1362 return submit_op4( emit
, inst
, dst
, texcoord
, sampler
, ddx
, ddy
);
1366 static boolean
emit_tex(struct svga_shader_emitter
*emit
,
1367 const struct tgsi_full_instruction
*insn
)
1369 SVGA3dShaderDestToken dst
=
1370 translate_dst_register( emit
, insn
, 0 );
1371 struct src_register src0
=
1372 translate_src_register( emit
, &insn
->Src
[0] );
1373 struct src_register src1
=
1374 translate_src_register( emit
, &insn
->Src
[1] );
1376 SVGA3dShaderDestToken tex_result
;
1378 /* check for shadow samplers */
1379 boolean compare
= (emit
->key
.fkey
.tex
[src1
.base
.num
].compare_mode
==
1380 PIPE_TEX_COMPARE_R_TO_TEXTURE
);
1383 /* If doing compare processing, need to put this value into a
1384 * temporary so it can be used as a source later on.
1387 (!emit
->use_sm30
&& dst
.mask
!= TGSI_WRITEMASK_XYZW
) ) {
1388 tex_result
= get_temp( emit
);
1394 switch(insn
->Instruction
.Opcode
) {
1395 case TGSI_OPCODE_TEX
:
1396 case TGSI_OPCODE_TXB
:
1397 case TGSI_OPCODE_TXP
:
1398 case TGSI_OPCODE_TXL
:
1399 if (!emit_tex2( emit
, insn
, tex_result
))
1402 case TGSI_OPCODE_TXD
:
1403 if (!emit_tex4( emit
, insn
, tex_result
))
1412 if (dst
.mask
& TGSI_WRITEMASK_XYZ
) {
1413 SVGA3dShaderDestToken src0_zdivw
= get_temp( emit
);
1414 struct src_register tex_src_x
= scalar(src(tex_result
), TGSI_SWIZZLE_Y
);
1416 /* Divide texcoord R by Q */
1417 if (!submit_op1( emit
, inst_token( SVGA3DOP_RCP
),
1418 writemask(src0_zdivw
, TGSI_WRITEMASK_X
),
1419 scalar(src0
, TGSI_SWIZZLE_W
) ))
1422 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
1423 writemask(src0_zdivw
, TGSI_WRITEMASK_X
),
1424 scalar(src0
, TGSI_SWIZZLE_Z
),
1425 scalar(src(src0_zdivw
), TGSI_SWIZZLE_X
) ))
1430 emit
->key
.fkey
.tex
[src1
.base
.num
].compare_func
,
1431 writemask( dst
, TGSI_WRITEMASK_XYZ
),
1432 scalar(src(src0_zdivw
), TGSI_SWIZZLE_X
),
1437 if (dst
.mask
& TGSI_WRITEMASK_W
) {
1438 struct src_register one
=
1439 scalar( get_zero_immediate( emit
), TGSI_SWIZZLE_W
);
1441 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1442 writemask( dst
, TGSI_WRITEMASK_W
),
1449 else if (!emit
->use_sm30
&& dst
.mask
!= TGSI_WRITEMASK_XYZW
)
1451 if (!emit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src(tex_result
) ))
1458 static boolean
emit_bgnloop2( struct svga_shader_emitter
*emit
,
1459 const struct tgsi_full_instruction
*insn
)
1461 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_LOOP
);
1462 struct src_register loop_reg
= src_register( SVGA3DREG_LOOP
, 0 );
1463 struct src_register const_int
= get_loop_const( emit
);
1465 emit
->dynamic_branching_level
++;
1467 return (emit_instruction( emit
, inst
) &&
1468 emit_src( emit
, loop_reg
) &&
1469 emit_src( emit
, const_int
) );
1472 static boolean
emit_endloop2( struct svga_shader_emitter
*emit
,
1473 const struct tgsi_full_instruction
*insn
)
1475 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_ENDLOOP
);
1477 emit
->dynamic_branching_level
--;
1479 return emit_instruction( emit
, inst
);
1482 static boolean
emit_brk( struct svga_shader_emitter
*emit
,
1483 const struct tgsi_full_instruction
*insn
)
1485 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_BREAK
);
1486 return emit_instruction( emit
, inst
);
1489 static boolean
emit_scalar_op1( struct svga_shader_emitter
*emit
,
1491 const struct tgsi_full_instruction
*insn
)
1493 SVGA3dShaderInstToken inst
;
1494 SVGA3dShaderDestToken dst
;
1495 struct src_register src
;
1497 inst
= inst_token( opcode
);
1498 dst
= translate_dst_register( emit
, insn
, 0 );
1499 src
= translate_src_register( emit
, &insn
->Src
[0] );
1500 src
= scalar( src
, TGSI_SWIZZLE_X
);
1502 return submit_op1( emit
, inst
, dst
, src
);
1506 static boolean
emit_simple_instruction(struct svga_shader_emitter
*emit
,
1508 const struct tgsi_full_instruction
*insn
)
1510 const struct tgsi_full_src_register
*src
= insn
->Src
;
1511 SVGA3dShaderInstToken inst
;
1512 SVGA3dShaderDestToken dst
;
1514 inst
= inst_token( opcode
);
1515 dst
= translate_dst_register( emit
, insn
, 0 );
1517 switch (insn
->Instruction
.NumSrcRegs
) {
1519 return submit_op0( emit
, inst
, dst
);
1521 return submit_op1( emit
, inst
, dst
,
1522 translate_src_register( emit
, &src
[0] ));
1524 return submit_op2( emit
, inst
, dst
,
1525 translate_src_register( emit
, &src
[0] ),
1526 translate_src_register( emit
, &src
[1] ) );
1528 return submit_op3( emit
, inst
, dst
,
1529 translate_src_register( emit
, &src
[0] ),
1530 translate_src_register( emit
, &src
[1] ),
1531 translate_src_register( emit
, &src
[2] ) );
1539 static boolean
emit_deriv(struct svga_shader_emitter
*emit
,
1540 const struct tgsi_full_instruction
*insn
)
1542 if (emit
->dynamic_branching_level
> 0 &&
1543 insn
->Src
[0].Register
.File
== TGSI_FILE_TEMPORARY
)
1545 struct src_register zero
= get_zero_immediate( emit
);
1546 SVGA3dShaderDestToken dst
=
1547 translate_dst_register( emit
, insn
, 0 );
1549 /* Deriv opcodes not valid inside dynamic branching, workaround
1550 * by zeroing out the destination.
1552 if (!submit_op1(emit
,
1553 inst_token( SVGA3DOP_MOV
),
1555 scalar(zero
, TGSI_SWIZZLE_X
)))
1563 switch (insn
->Instruction
.Opcode
) {
1564 case TGSI_OPCODE_DDX
:
1565 opcode
= SVGA3DOP_DSX
;
1567 case TGSI_OPCODE_DDY
:
1568 opcode
= SVGA3DOP_DSY
;
1574 return emit_simple_instruction( emit
, opcode
, insn
);
1578 static boolean
emit_arl(struct svga_shader_emitter
*emit
,
1579 const struct tgsi_full_instruction
*insn
)
1581 ++emit
->current_arl
;
1582 if (svga_arl_needs_adjustment( emit
)) {
1583 return emit_fake_arl( emit
, insn
);
1585 /* no need to adjust, just emit straight arl */
1586 return emit_simple_instruction(emit
, SVGA3DOP_MOVA
, insn
);
1590 static boolean
alias_src_dst( struct src_register src
,
1591 SVGA3dShaderDestToken dst
)
1593 if (src
.base
.num
!= dst
.num
)
1596 if (SVGA3dShaderGetRegType(dst
.value
) !=
1597 SVGA3dShaderGetRegType(src
.base
.value
))
1603 static boolean
emit_pow(struct svga_shader_emitter
*emit
,
1604 const struct tgsi_full_instruction
*insn
)
1606 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1607 struct src_register src0
= translate_src_register(
1608 emit
, &insn
->Src
[0] );
1609 struct src_register src1
= translate_src_register(
1610 emit
, &insn
->Src
[1] );
1611 boolean need_tmp
= FALSE
;
1613 /* POW can only output to a temporary */
1614 if (insn
->Dst
[0].Register
.File
!= TGSI_FILE_TEMPORARY
)
1617 /* POW src1 must not be the same register as dst */
1618 if (alias_src_dst( src1
, dst
))
1621 /* it's a scalar op */
1622 src0
= scalar( src0
, TGSI_SWIZZLE_X
);
1623 src1
= scalar( src1
, TGSI_SWIZZLE_X
);
1626 SVGA3dShaderDestToken tmp
= writemask(get_temp( emit
), TGSI_WRITEMASK_X
);
1628 if (!submit_op2(emit
, inst_token( SVGA3DOP_POW
), tmp
, src0
, src1
))
1631 return submit_op1(emit
, inst_token( SVGA3DOP_MOV
), dst
, scalar(src(tmp
), 0) );
1634 return submit_op2(emit
, inst_token( SVGA3DOP_POW
), dst
, src0
, src1
);
1638 static boolean
emit_xpd(struct svga_shader_emitter
*emit
,
1639 const struct tgsi_full_instruction
*insn
)
1641 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1642 const struct src_register src0
= translate_src_register(
1643 emit
, &insn
->Src
[0] );
1644 const struct src_register src1
= translate_src_register(
1645 emit
, &insn
->Src
[1] );
1646 boolean need_dst_tmp
= FALSE
;
1648 /* XPD can only output to a temporary */
1649 if (SVGA3dShaderGetRegType(dst
.value
) != SVGA3DREG_TEMP
)
1650 need_dst_tmp
= TRUE
;
1652 /* The dst reg must not be the same as src0 or src1*/
1653 if (alias_src_dst(src0
, dst
) ||
1654 alias_src_dst(src1
, dst
))
1655 need_dst_tmp
= TRUE
;
1658 SVGA3dShaderDestToken tmp
= get_temp( emit
);
1660 /* Obey DX9 restrictions on mask:
1662 tmp
.mask
= dst
.mask
& TGSI_WRITEMASK_XYZ
;
1664 if (!submit_op2(emit
, inst_token( SVGA3DOP_CRS
), tmp
, src0
, src1
))
1667 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), dst
, src( tmp
)))
1671 if (!submit_op2(emit
, inst_token( SVGA3DOP_CRS
), dst
, src0
, src1
))
1675 /* Need to emit 1.0 to dst.w?
1677 if (dst
.mask
& TGSI_WRITEMASK_W
) {
1678 struct src_register zero
= get_zero_immediate( emit
);
1680 if (!submit_op1(emit
,
1681 inst_token( SVGA3DOP_MOV
),
1682 writemask(dst
, TGSI_WRITEMASK_W
),
1691 static boolean
emit_lrp(struct svga_shader_emitter
*emit
,
1692 const struct tgsi_full_instruction
*insn
)
1694 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1695 SVGA3dShaderDestToken tmp
;
1696 const struct src_register src0
= translate_src_register(
1697 emit
, &insn
->Src
[0] );
1698 const struct src_register src1
= translate_src_register(
1699 emit
, &insn
->Src
[1] );
1700 const struct src_register src2
= translate_src_register(
1701 emit
, &insn
->Src
[2] );
1702 boolean need_dst_tmp
= FALSE
;
1704 /* The dst reg must not be the same as src0 or src2 */
1705 if (alias_src_dst(src0
, dst
) ||
1706 alias_src_dst(src2
, dst
))
1707 need_dst_tmp
= TRUE
;
1710 tmp
= get_temp( emit
);
1711 tmp
.mask
= dst
.mask
;
1717 if (!submit_op3(emit
, inst_token( SVGA3DOP_LRP
), tmp
, src0
, src1
, src2
))
1721 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), dst
, src( tmp
)))
1729 static boolean
emit_dst_insn(struct svga_shader_emitter
*emit
,
1730 const struct tgsi_full_instruction
*insn
)
1732 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1733 /* SVGA/DX9 has a DST instruction, but only for vertex shaders:
1735 return emit_simple_instruction(emit
, SVGA3DOP_DST
, insn
);
1739 /* result[0] = 1 * 1;
1740 * result[1] = a[1] * b[1];
1741 * result[2] = a[2] * 1;
1742 * result[3] = 1 * b[3];
1745 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1746 SVGA3dShaderDestToken tmp
;
1747 const struct src_register src0
= translate_src_register(
1748 emit
, &insn
->Src
[0] );
1749 const struct src_register src1
= translate_src_register(
1750 emit
, &insn
->Src
[1] );
1751 struct src_register zero
= get_zero_immediate( emit
);
1752 boolean need_tmp
= FALSE
;
1754 if (SVGA3dShaderGetRegType(dst
.value
) != SVGA3DREG_TEMP
||
1755 alias_src_dst(src0
, dst
) ||
1756 alias_src_dst(src1
, dst
))
1760 tmp
= get_temp( emit
);
1768 if (tmp
.mask
& TGSI_WRITEMASK_XW
) {
1769 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1770 writemask(tmp
, TGSI_WRITEMASK_XW
),
1777 if (tmp
.mask
& TGSI_WRITEMASK_YZ
) {
1778 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1779 writemask(tmp
, TGSI_WRITEMASK_YZ
),
1784 /* tmp.yw = tmp * src1
1786 if (tmp
.mask
& TGSI_WRITEMASK_YW
) {
1787 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
1788 writemask(tmp
, TGSI_WRITEMASK_YW
),
1797 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1808 static boolean
emit_exp(struct svga_shader_emitter
*emit
,
1809 const struct tgsi_full_instruction
*insn
)
1811 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1812 struct src_register src0
=
1813 translate_src_register( emit
, &insn
->Src
[0] );
1814 struct src_register zero
= get_zero_immediate( emit
);
1815 SVGA3dShaderDestToken fraction
;
1817 if (dst
.mask
& TGSI_WRITEMASK_Y
)
1819 else if (dst
.mask
& TGSI_WRITEMASK_X
)
1820 fraction
= get_temp( emit
);
1824 /* If y is being written, fill it with src0 - floor(src0).
1826 if (dst
.mask
& TGSI_WRITEMASK_XY
) {
1827 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
),
1828 writemask( fraction
, TGSI_WRITEMASK_Y
),
1833 /* If x is being written, fill it with 2 ^ floor(src0).
1835 if (dst
.mask
& TGSI_WRITEMASK_X
) {
1836 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
),
1837 writemask( dst
, TGSI_WRITEMASK_X
),
1839 scalar( negate( src( fraction
) ), TGSI_SWIZZLE_Y
) ) )
1842 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXP
),
1843 writemask( dst
, TGSI_WRITEMASK_X
),
1844 scalar( src( dst
), TGSI_SWIZZLE_X
) ) )
1847 if (!(dst
.mask
& TGSI_WRITEMASK_Y
))
1848 release_temp( emit
, fraction
);
1851 /* If z is being written, fill it with 2 ^ src0 (partial precision).
1853 if (dst
.mask
& TGSI_WRITEMASK_Z
) {
1854 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXPP
),
1855 writemask( dst
, TGSI_WRITEMASK_Z
),
1860 /* If w is being written, fill it with one.
1862 if (dst
.mask
& TGSI_WRITEMASK_W
) {
1863 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1864 writemask(dst
, TGSI_WRITEMASK_W
),
1865 scalar( zero
, TGSI_SWIZZLE_W
) ))
1872 static boolean
emit_lit(struct svga_shader_emitter
*emit
,
1873 const struct tgsi_full_instruction
*insn
)
1875 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1876 /* SVGA/DX9 has a LIT instruction, but only for vertex shaders:
1878 return emit_simple_instruction(emit
, SVGA3DOP_LIT
, insn
);
1882 /* D3D vs. GL semantics can be fairly easily accomodated by
1883 * variations on this sequence.
1887 * tmp.z = pow(src.y,src.w)
1888 * p0 = src0.xxxx > 0
1889 * result = zero.wxxw
1890 * (p0) result.yz = tmp
1894 * tmp.z = pow(src.y,src.w)
1895 * p0 = src0.xxyy > 0
1896 * result = zero.wxxw
1897 * (p0) result.yz = tmp
1899 * Will implement the GL version for now.
1902 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1903 SVGA3dShaderDestToken tmp
= get_temp( emit
);
1904 const struct src_register src0
= translate_src_register(
1905 emit
, &insn
->Src
[0] );
1906 struct src_register zero
= get_zero_immediate( emit
);
1908 /* tmp = pow(src.y, src.w)
1910 if (dst
.mask
& TGSI_WRITEMASK_Z
) {
1911 if (!submit_op2(emit
, inst_token( SVGA3DOP_POW
),
1920 if (dst
.mask
& TGSI_WRITEMASK_Y
) {
1921 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1922 writemask(tmp
, TGSI_WRITEMASK_Y
),
1927 /* Can't quite do this with emit conditional due to the extra
1928 * writemask on the predicated mov:
1931 SVGA3dShaderDestToken pred_reg
= dst_register( SVGA3DREG_PREDICATE
, 0 );
1932 SVGA3dShaderInstToken setp_token
, mov_token
;
1933 struct src_register predsrc
;
1935 setp_token
= inst_token( SVGA3DOP_SETP
);
1936 mov_token
= inst_token( SVGA3DOP_MOV
);
1938 setp_token
.control
= SVGA3DOPCOMP_GT
;
1940 /* D3D vs GL semantics:
1943 predsrc
= swizzle(src0
, 0, 0, 1, 1); /* D3D */
1945 predsrc
= swizzle(src0
, 0, 0, 0, 0); /* GL */
1947 /* SETP src0.xxyy, GT, {0}.x */
1948 if (!submit_op2( emit
, setp_token
, pred_reg
,
1950 swizzle(zero
, 0, 0, 0, 0) ))
1954 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
,
1955 swizzle(zero
, 3, 0, 0, 3 )))
1958 /* MOV dst.yz, tmp (predicated)
1960 * Note that the predicate reg (and possible modifiers) is passed
1961 * as the first source argument.
1963 if (dst
.mask
& TGSI_WRITEMASK_YZ
) {
1964 mov_token
.predicated
= 1;
1965 if (!submit_op2( emit
, mov_token
,
1966 writemask(dst
, TGSI_WRITEMASK_YZ
),
1967 src( pred_reg
), src( tmp
) ))
1979 static boolean
emit_ex2( struct svga_shader_emitter
*emit
,
1980 const struct tgsi_full_instruction
*insn
)
1982 SVGA3dShaderInstToken inst
;
1983 SVGA3dShaderDestToken dst
;
1984 struct src_register src0
;
1986 inst
= inst_token( SVGA3DOP_EXP
);
1987 dst
= translate_dst_register( emit
, insn
, 0 );
1988 src0
= translate_src_register( emit
, &insn
->Src
[0] );
1989 src0
= scalar( src0
, TGSI_SWIZZLE_X
);
1991 if (dst
.mask
!= TGSI_WRITEMASK_XYZW
) {
1992 SVGA3dShaderDestToken tmp
= get_temp( emit
);
1994 if (!submit_op1( emit
, inst
, tmp
, src0
))
1997 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1999 scalar( src( tmp
), TGSI_SWIZZLE_X
) );
2002 return submit_op1( emit
, inst
, dst
, src0
);
2006 static boolean
emit_log(struct svga_shader_emitter
*emit
,
2007 const struct tgsi_full_instruction
*insn
)
2009 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2010 struct src_register src0
=
2011 translate_src_register( emit
, &insn
->Src
[0] );
2012 struct src_register zero
= get_zero_immediate( emit
);
2013 SVGA3dShaderDestToken abs_tmp
;
2014 struct src_register abs_src0
;
2015 SVGA3dShaderDestToken log2_abs
;
2019 if (dst
.mask
& TGSI_WRITEMASK_Z
)
2021 else if (dst
.mask
& TGSI_WRITEMASK_XY
)
2022 log2_abs
= get_temp( emit
);
2026 /* If z is being written, fill it with log2( abs( src0 ) ).
2028 if (dst
.mask
& TGSI_WRITEMASK_XYZ
) {
2029 if (!src0
.base
.srcMod
|| src0
.base
.srcMod
== SVGA3DSRCMOD_ABS
)
2032 abs_tmp
= get_temp( emit
);
2034 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2039 abs_src0
= src( abs_tmp
);
2042 abs_src0
= absolute( scalar( abs_src0
, TGSI_SWIZZLE_X
) );
2044 if (!submit_op1( emit
, inst_token( SVGA3DOP_LOG
),
2045 writemask( log2_abs
, TGSI_WRITEMASK_Z
),
2050 if (dst
.mask
& TGSI_WRITEMASK_XY
) {
2051 SVGA3dShaderDestToken floor_log2
;
2053 if (dst
.mask
& TGSI_WRITEMASK_X
)
2056 floor_log2
= get_temp( emit
);
2058 /* If x is being written, fill it with floor( log2( abs( src0 ) ) ).
2060 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
),
2061 writemask( floor_log2
, TGSI_WRITEMASK_X
),
2062 scalar( src( log2_abs
), TGSI_SWIZZLE_Z
) ) )
2065 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
),
2066 writemask( floor_log2
, TGSI_WRITEMASK_X
),
2067 scalar( src( log2_abs
), TGSI_SWIZZLE_Z
),
2068 negate( src( floor_log2
) ) ) )
2071 /* If y is being written, fill it with
2072 * abs ( src0 ) / ( 2 ^ floor( log2( abs( src0 ) ) ) ).
2074 if (dst
.mask
& TGSI_WRITEMASK_Y
) {
2075 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXP
),
2076 writemask( dst
, TGSI_WRITEMASK_Y
),
2077 negate( scalar( src( floor_log2
),
2078 TGSI_SWIZZLE_X
) ) ) )
2081 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
2082 writemask( dst
, TGSI_WRITEMASK_Y
),
2088 if (!(dst
.mask
& TGSI_WRITEMASK_X
))
2089 release_temp( emit
, floor_log2
);
2091 if (!(dst
.mask
& TGSI_WRITEMASK_Z
))
2092 release_temp( emit
, log2_abs
);
2095 if (dst
.mask
& TGSI_WRITEMASK_XYZ
&& src0
.base
.srcMod
&&
2096 src0
.base
.srcMod
!= SVGA3DSRCMOD_ABS
)
2097 release_temp( emit
, abs_tmp
);
2099 /* If w is being written, fill it with one.
2101 if (dst
.mask
& TGSI_WRITEMASK_W
) {
2102 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2103 writemask(dst
, TGSI_WRITEMASK_W
),
2104 scalar( zero
, TGSI_SWIZZLE_W
) ))
2112 static boolean
emit_bgnsub( struct svga_shader_emitter
*emit
,
2114 const struct tgsi_full_instruction
*insn
)
2118 /* Note that we've finished the main function and are now emitting
2119 * subroutines. This affects how we terminate the generated
2122 emit
->in_main_func
= FALSE
;
2124 for (i
= 0; i
< emit
->nr_labels
; i
++) {
2125 if (emit
->label
[i
] == position
) {
2126 return (emit_instruction( emit
, inst_token( SVGA3DOP_RET
) ) &&
2127 emit_instruction( emit
, inst_token( SVGA3DOP_LABEL
) ) &&
2128 emit_src( emit
, src_register( SVGA3DREG_LABEL
, i
)));
2136 static boolean
emit_call( struct svga_shader_emitter
*emit
,
2137 const struct tgsi_full_instruction
*insn
)
2139 unsigned position
= insn
->Label
.Label
;
2142 for (i
= 0; i
< emit
->nr_labels
; i
++) {
2143 if (emit
->label
[i
] == position
)
2147 if (emit
->nr_labels
== Elements(emit
->label
))
2150 if (i
== emit
->nr_labels
) {
2151 emit
->label
[i
] = position
;
2155 return (emit_instruction( emit
, inst_token( SVGA3DOP_CALL
) ) &&
2156 emit_src( emit
, src_register( SVGA3DREG_LABEL
, i
)));
2160 static boolean
emit_end( struct svga_shader_emitter
*emit
)
2162 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2163 return emit_vs_postamble( emit
);
2166 return emit_ps_postamble( emit
);
2172 static boolean
svga_emit_instruction( struct svga_shader_emitter
*emit
,
2174 const struct tgsi_full_instruction
*insn
)
2176 switch (insn
->Instruction
.Opcode
) {
2178 case TGSI_OPCODE_ARL
:
2179 return emit_arl( emit
, insn
);
2181 case TGSI_OPCODE_TEX
:
2182 case TGSI_OPCODE_TXB
:
2183 case TGSI_OPCODE_TXP
:
2184 case TGSI_OPCODE_TXL
:
2185 case TGSI_OPCODE_TXD
:
2186 return emit_tex( emit
, insn
);
2188 case TGSI_OPCODE_DDX
:
2189 case TGSI_OPCODE_DDY
:
2190 return emit_deriv( emit
, insn
);
2192 case TGSI_OPCODE_BGNSUB
:
2193 return emit_bgnsub( emit
, position
, insn
);
2195 case TGSI_OPCODE_ENDSUB
:
2198 case TGSI_OPCODE_CAL
:
2199 return emit_call( emit
, insn
);
2201 case TGSI_OPCODE_FLR
:
2202 case TGSI_OPCODE_TRUNC
: /* should be TRUNC, not FLR */
2203 return emit_floor( emit
, insn
);
2205 case TGSI_OPCODE_CMP
:
2206 return emit_cmp( emit
, insn
);
2208 case TGSI_OPCODE_DIV
:
2209 return emit_div( emit
, insn
);
2211 case TGSI_OPCODE_DP2
:
2212 return emit_dp2( emit
, insn
);
2214 case TGSI_OPCODE_DPH
:
2215 return emit_dph( emit
, insn
);
2217 case TGSI_OPCODE_NRM
:
2218 return emit_nrm( emit
, insn
);
2220 case TGSI_OPCODE_COS
:
2221 return emit_cos( emit
, insn
);
2223 case TGSI_OPCODE_SIN
:
2224 return emit_sin( emit
, insn
);
2226 case TGSI_OPCODE_SCS
:
2227 return emit_sincos( emit
, insn
);
2229 case TGSI_OPCODE_END
:
2230 /* TGSI always finishes the main func with an END */
2231 return emit_end( emit
);
2233 case TGSI_OPCODE_KIL
:
2234 return emit_kil( emit
, insn
);
2236 /* Selection opcodes. The underlying language is fairly
2237 * non-orthogonal about these.
2239 case TGSI_OPCODE_SEQ
:
2240 return emit_select_op( emit
, PIPE_FUNC_EQUAL
, insn
);
2242 case TGSI_OPCODE_SNE
:
2243 return emit_select_op( emit
, PIPE_FUNC_NOTEQUAL
, insn
);
2245 case TGSI_OPCODE_SGT
:
2246 return emit_select_op( emit
, PIPE_FUNC_GREATER
, insn
);
2248 case TGSI_OPCODE_SGE
:
2249 return emit_select_op( emit
, PIPE_FUNC_GEQUAL
, insn
);
2251 case TGSI_OPCODE_SLT
:
2252 return emit_select_op( emit
, PIPE_FUNC_LESS
, insn
);
2254 case TGSI_OPCODE_SLE
:
2255 return emit_select_op( emit
, PIPE_FUNC_LEQUAL
, insn
);
2257 case TGSI_OPCODE_SUB
:
2258 return emit_sub( emit
, insn
);
2260 case TGSI_OPCODE_POW
:
2261 return emit_pow( emit
, insn
);
2263 case TGSI_OPCODE_EX2
:
2264 return emit_ex2( emit
, insn
);
2266 case TGSI_OPCODE_EXP
:
2267 return emit_exp( emit
, insn
);
2269 case TGSI_OPCODE_LOG
:
2270 return emit_log( emit
, insn
);
2272 case TGSI_OPCODE_LG2
:
2273 return emit_scalar_op1( emit
, SVGA3DOP_LOG
, insn
);
2275 case TGSI_OPCODE_RSQ
:
2276 return emit_scalar_op1( emit
, SVGA3DOP_RSQ
, insn
);
2278 case TGSI_OPCODE_RCP
:
2279 return emit_scalar_op1( emit
, SVGA3DOP_RCP
, insn
);
2281 case TGSI_OPCODE_CONT
:
2282 case TGSI_OPCODE_RET
:
2283 /* This is a noop -- we tell mesa that we can't support RET
2284 * within a function (early return), so this will always be
2285 * followed by an ENDSUB.
2289 /* These aren't actually used by any of the frontends we care
2292 case TGSI_OPCODE_CLAMP
:
2293 case TGSI_OPCODE_ROUND
:
2294 case TGSI_OPCODE_AND
:
2295 case TGSI_OPCODE_OR
:
2296 case TGSI_OPCODE_I2F
:
2297 case TGSI_OPCODE_NOT
:
2298 case TGSI_OPCODE_SHL
:
2299 case TGSI_OPCODE_ISHR
:
2300 case TGSI_OPCODE_XOR
:
2303 case TGSI_OPCODE_IF
:
2304 return emit_if( emit
, insn
);
2305 case TGSI_OPCODE_ELSE
:
2306 return emit_else( emit
, insn
);
2307 case TGSI_OPCODE_ENDIF
:
2308 return emit_endif( emit
, insn
);
2310 case TGSI_OPCODE_BGNLOOP
:
2311 return emit_bgnloop2( emit
, insn
);
2312 case TGSI_OPCODE_ENDLOOP
:
2313 return emit_endloop2( emit
, insn
);
2314 case TGSI_OPCODE_BRK
:
2315 return emit_brk( emit
, insn
);
2317 case TGSI_OPCODE_XPD
:
2318 return emit_xpd( emit
, insn
);
2320 case TGSI_OPCODE_KILP
:
2321 return emit_kilp( emit
, insn
);
2323 case TGSI_OPCODE_DST
:
2324 return emit_dst_insn( emit
, insn
);
2326 case TGSI_OPCODE_LIT
:
2327 return emit_lit( emit
, insn
);
2329 case TGSI_OPCODE_LRP
:
2330 return emit_lrp( emit
, insn
);
2333 unsigned opcode
= translate_opcode(insn
->Instruction
.Opcode
);
2335 if (opcode
== SVGA3DOP_LAST_INST
)
2338 if (!emit_simple_instruction( emit
, opcode
, insn
))
2347 static boolean
svga_emit_immediate( struct svga_shader_emitter
*emit
,
2348 struct tgsi_full_immediate
*imm
)
2350 static const float id
[4] = {0,0,0,1};
2354 assert(1 <= imm
->Immediate
.NrTokens
&& imm
->Immediate
.NrTokens
<= 5);
2355 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++)
2356 value
[i
] = imm
->u
[i
].Float
;
2358 for ( ; i
< 4; i
++ )
2361 return emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
2362 emit
->imm_start
+ emit
->internal_imm_count
++,
2363 value
[0], value
[1], value
[2], value
[3]);
2366 static boolean
make_immediate( struct svga_shader_emitter
*emit
,
2371 struct src_register
*out
)
2373 unsigned idx
= emit
->nr_hw_const
++;
2375 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
2379 *out
= src_register( SVGA3DREG_CONST
, idx
);
2384 static boolean
emit_vs_preamble( struct svga_shader_emitter
*emit
)
2386 if (!emit
->key
.vkey
.need_prescale
) {
2387 if (!make_immediate( emit
, 0, 0, .5, .5,
2395 static boolean
emit_ps_preamble( struct svga_shader_emitter
*emit
)
2399 /* For SM20, need to initialize the temporaries we're using to hold
2400 * color outputs to some value. Shaders which don't set all of
2401 * these values are likely to be rejected by the DX9 runtime.
2403 if (!emit
->use_sm30
) {
2404 struct src_register zero
= get_zero_immediate( emit
);
2405 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
2406 if (SVGA3dShaderGetRegType(emit
->true_col
[i
].value
) != 0) {
2408 if (!submit_op1( emit
,
2409 inst_token(SVGA3DOP_MOV
),
2420 static boolean
emit_ps_postamble( struct svga_shader_emitter
*emit
)
2424 /* PS oDepth is incredibly fragile and it's very hard to catch the
2425 * types of usage that break it during shader emit. Easier just to
2426 * redirect the main program to a temporary and then only touch
2427 * oDepth with a hand-crafted MOV below.
2429 if (SVGA3dShaderGetRegType(emit
->true_pos
.value
) != 0) {
2431 if (!submit_op1( emit
,
2432 inst_token(SVGA3DOP_MOV
),
2434 scalar(src(emit
->temp_pos
), TGSI_SWIZZLE_Z
) ))
2438 /* Similarly for SM20 color outputs... Luckily SM30 isn't so
2441 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
2442 if (SVGA3dShaderGetRegType(emit
->true_col
[i
].value
) != 0) {
2444 /* Potentially override output colors with white for XOR
2445 * logicop workaround.
2447 if (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
2448 emit
->key
.fkey
.white_fragments
) {
2450 struct src_register one
= scalar( get_zero_immediate( emit
),
2453 if (!submit_op1( emit
,
2454 inst_token(SVGA3DOP_MOV
),
2460 if (!submit_op1( emit
,
2461 inst_token(SVGA3DOP_MOV
),
2463 src(emit
->temp_col
[i
]) ))
2472 static boolean
emit_vs_postamble( struct svga_shader_emitter
*emit
)
2474 /* PSIZ output is incredibly fragile and it's very hard to catch
2475 * the types of usage that break it during shader emit. Easier
2476 * just to redirect the main program to a temporary and then only
2477 * touch PSIZ with a hand-crafted MOV below.
2479 if (SVGA3dShaderGetRegType(emit
->true_psiz
.value
) != 0) {
2481 if (!submit_op1( emit
,
2482 inst_token(SVGA3DOP_MOV
),
2484 scalar(src(emit
->temp_psiz
), TGSI_SWIZZLE_X
) ))
2488 /* Need to perform various manipulations on vertex position to cope
2489 * with the different GL and D3D clip spaces.
2491 if (emit
->key
.vkey
.need_prescale
) {
2492 SVGA3dShaderDestToken temp_pos
= emit
->temp_pos
;
2493 SVGA3dShaderDestToken pos
= emit
->true_pos
;
2494 unsigned offset
= emit
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
2495 struct src_register prescale_scale
= src_register( SVGA3DREG_CONST
,
2497 struct src_register prescale_trans
= src_register( SVGA3DREG_CONST
,
2500 /* MUL temp_pos.xyz, temp_pos, prescale.scale
2501 * MAD result.position, temp_pos.wwww, prescale.trans, temp_pos
2502 * --> Note that prescale.trans.w == 0
2504 if (!submit_op2( emit
,
2505 inst_token(SVGA3DOP_MUL
),
2506 writemask(temp_pos
, TGSI_WRITEMASK_XYZ
),
2511 if (!submit_op3( emit
,
2512 inst_token(SVGA3DOP_MAD
),
2514 swizzle(src(temp_pos
), 3, 3, 3, 3),
2520 SVGA3dShaderDestToken temp_pos
= emit
->temp_pos
;
2521 SVGA3dShaderDestToken pos
= emit
->true_pos
;
2522 struct src_register imm_0055
= emit
->imm_0055
;
2524 /* Adjust GL clipping coordinate space to hardware (D3D-style):
2526 * DP4 temp_pos.z, {0,0,.5,.5}, temp_pos
2527 * MOV result.position, temp_pos
2529 if (!submit_op2( emit
,
2530 inst_token(SVGA3DOP_DP4
),
2531 writemask(temp_pos
, TGSI_WRITEMASK_Z
),
2536 if (!submit_op1( emit
,
2537 inst_token(SVGA3DOP_MOV
),
2548 1: COLOR = FrontColor;
2550 3: COLOR = BackColor;
2553 static boolean
emit_light_twoside( struct svga_shader_emitter
*emit
)
2555 struct src_register vface
, zero
;
2556 struct src_register front
[2];
2557 struct src_register back
[2];
2558 SVGA3dShaderDestToken color
[2];
2559 int count
= emit
->internal_color_count
;
2561 SVGA3dShaderInstToken if_token
;
2566 vface
= get_vface( emit
);
2567 zero
= get_zero_immediate( emit
);
2569 /* Can't use get_temp() to allocate the color reg as such
2570 * temporaries will be reclaimed after each instruction by the call
2571 * to reset_temp_regs().
2573 for (i
= 0; i
< count
; i
++) {
2574 color
[i
] = dst_register( SVGA3DREG_TEMP
,
2575 emit
->nr_hw_temp
++ );
2577 front
[i
] = emit
->input_map
[emit
->internal_color_idx
[i
]];
2579 /* Back is always the next input:
2582 back
[i
].base
.num
= front
[i
].base
.num
+ 1;
2584 /* Reassign the input_map to the actual front-face color:
2586 emit
->input_map
[emit
->internal_color_idx
[i
]] = src(color
[i
]);
2589 if_token
= inst_token( SVGA3DOP_IFC
);
2591 if (emit
->key
.fkey
.front_cw
)
2592 if_token
.control
= SVGA3DOPCOMP_GT
;
2594 if_token
.control
= SVGA3DOPCOMP_LT
;
2596 zero
= scalar(zero
, TGSI_SWIZZLE_X
);
2598 if (!(emit_instruction( emit
, if_token
) &&
2599 emit_src( emit
, vface
) &&
2600 emit_src( emit
, zero
) ))
2603 for (i
= 0; i
< count
; i
++) {
2604 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), color
[i
], front
[i
] ))
2608 if (!(emit_instruction( emit
, inst_token( SVGA3DOP_ELSE
))))
2611 for (i
= 0; i
< count
; i
++) {
2612 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), color
[i
], back
[i
] ))
2616 if (!emit_instruction( emit
, inst_token( SVGA3DOP_ENDIF
) ))
2623 0: SETP_GT TEMP, VFACE, 0
2624 where TEMP is a fake frontface register
2626 static boolean
emit_frontface( struct svga_shader_emitter
*emit
)
2628 struct src_register vface
, zero
;
2629 SVGA3dShaderDestToken temp
;
2630 struct src_register pass
, fail
;
2632 vface
= get_vface( emit
);
2633 zero
= get_zero_immediate( emit
);
2635 /* Can't use get_temp() to allocate the fake frontface reg as such
2636 * temporaries will be reclaimed after each instruction by the call
2637 * to reset_temp_regs().
2639 temp
= dst_register( SVGA3DREG_TEMP
,
2640 emit
->nr_hw_temp
++ );
2642 if (emit
->key
.fkey
.front_cw
) {
2643 pass
= scalar( zero
, TGSI_SWIZZLE_W
);
2644 fail
= scalar( zero
, TGSI_SWIZZLE_X
);
2646 pass
= scalar( zero
, TGSI_SWIZZLE_X
);
2647 fail
= scalar( zero
, TGSI_SWIZZLE_W
);
2650 if (!emit_conditional(emit
, PIPE_FUNC_GREATER
,
2651 temp
, vface
, scalar( zero
, TGSI_SWIZZLE_X
),
2655 /* Reassign the input_map to the actual front-face color:
2657 emit
->input_map
[emit
->internal_frontface_idx
] = src(temp
);
2662 static INLINE boolean
2663 needs_to_create_zero( struct svga_shader_emitter
*emit
)
2667 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
2668 if (!emit
->use_sm30
)
2671 if (emit
->key
.fkey
.light_twoside
)
2674 if (emit
->key
.fkey
.white_fragments
)
2677 if (emit
->emit_frontface
)
2680 if (emit
->info
.opcode_count
[TGSI_OPCODE_DST
] >= 1 ||
2681 emit
->info
.opcode_count
[TGSI_OPCODE_LIT
] >= 1)
2685 if (emit
->info
.opcode_count
[TGSI_OPCODE_IF
] >= 1 ||
2686 emit
->info
.opcode_count
[TGSI_OPCODE_BGNLOOP
] >= 1 ||
2687 emit
->info
.opcode_count
[TGSI_OPCODE_DDX
] >= 1 ||
2688 emit
->info
.opcode_count
[TGSI_OPCODE_DDY
] >= 1 ||
2689 emit
->info
.opcode_count
[TGSI_OPCODE_SGE
] >= 1 ||
2690 emit
->info
.opcode_count
[TGSI_OPCODE_SGT
] >= 1 ||
2691 emit
->info
.opcode_count
[TGSI_OPCODE_SLE
] >= 1 ||
2692 emit
->info
.opcode_count
[TGSI_OPCODE_SLT
] >= 1 ||
2693 emit
->info
.opcode_count
[TGSI_OPCODE_SNE
] >= 1 ||
2694 emit
->info
.opcode_count
[TGSI_OPCODE_SEQ
] >= 1 ||
2695 emit
->info
.opcode_count
[TGSI_OPCODE_EXP
] >= 1 ||
2696 emit
->info
.opcode_count
[TGSI_OPCODE_LOG
] >= 1 ||
2697 emit
->info
.opcode_count
[TGSI_OPCODE_XPD
] >= 1 ||
2698 emit
->info
.opcode_count
[TGSI_OPCODE_KILP
] >= 1)
2701 for (i
= 0; i
< emit
->key
.fkey
.num_textures
; i
++) {
2702 if (emit
->key
.fkey
.tex
[i
].compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
)
2709 static INLINE boolean
2710 needs_to_create_loop_const( struct svga_shader_emitter
*emit
)
2712 return (emit
->info
.opcode_count
[TGSI_OPCODE_BGNLOOP
] >= 1);
2715 static INLINE boolean
2716 needs_to_create_sincos_consts( struct svga_shader_emitter
*emit
)
2718 return !emit
->use_sm30
&& (emit
->info
.opcode_count
[TGSI_OPCODE_SIN
] >= 1 ||
2719 emit
->info
.opcode_count
[TGSI_OPCODE_COS
] >= 1 ||
2720 emit
->info
.opcode_count
[TGSI_OPCODE_SCS
] >= 1);
2723 static INLINE boolean
2724 needs_to_create_arl_consts( struct svga_shader_emitter
*emit
)
2726 return (emit
->num_arl_consts
> 0);
2729 static INLINE boolean
2730 pre_parse_add_indirect( struct svga_shader_emitter
*emit
,
2731 int num
, int current_arl
)
2736 for (i
= 0; i
< emit
->num_arl_consts
; ++i
) {
2737 if (emit
->arl_consts
[i
].arl_num
== current_arl
)
2741 if (emit
->num_arl_consts
== i
) {
2742 ++emit
->num_arl_consts
;
2744 emit
->arl_consts
[i
].number
= (emit
->arl_consts
[i
].number
> num
) ?
2746 emit
->arl_consts
[i
].number
;
2747 emit
->arl_consts
[i
].arl_num
= current_arl
;
2752 pre_parse_instruction( struct svga_shader_emitter
*emit
,
2753 const struct tgsi_full_instruction
*insn
,
2756 if (insn
->Src
[0].Register
.Indirect
&&
2757 insn
->Src
[0].Indirect
.File
== TGSI_FILE_ADDRESS
) {
2758 const struct tgsi_full_src_register
*reg
= &insn
->Src
[0];
2759 if (reg
->Register
.Index
< 0) {
2760 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
2764 if (insn
->Src
[1].Register
.Indirect
&&
2765 insn
->Src
[1].Indirect
.File
== TGSI_FILE_ADDRESS
) {
2766 const struct tgsi_full_src_register
*reg
= &insn
->Src
[1];
2767 if (reg
->Register
.Index
< 0) {
2768 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
2772 if (insn
->Src
[2].Register
.Indirect
&&
2773 insn
->Src
[2].Indirect
.File
== TGSI_FILE_ADDRESS
) {
2774 const struct tgsi_full_src_register
*reg
= &insn
->Src
[2];
2775 if (reg
->Register
.Index
< 0) {
2776 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
2784 pre_parse_tokens( struct svga_shader_emitter
*emit
,
2785 const struct tgsi_token
*tokens
)
2787 struct tgsi_parse_context parse
;
2788 int current_arl
= 0;
2790 tgsi_parse_init( &parse
, tokens
);
2792 while (!tgsi_parse_end_of_tokens( &parse
)) {
2793 tgsi_parse_token( &parse
);
2794 switch (parse
.FullToken
.Token
.Type
) {
2795 case TGSI_TOKEN_TYPE_IMMEDIATE
:
2796 case TGSI_TOKEN_TYPE_DECLARATION
:
2798 case TGSI_TOKEN_TYPE_INSTRUCTION
:
2799 if (parse
.FullToken
.FullInstruction
.Instruction
.Opcode
==
2803 if (!pre_parse_instruction( emit
, &parse
.FullToken
.FullInstruction
,
2815 static boolean
svga_shader_emit_helpers( struct svga_shader_emitter
*emit
)
2818 if (needs_to_create_zero( emit
)) {
2819 create_zero_immediate( emit
);
2821 if (needs_to_create_loop_const( emit
)) {
2822 create_loop_const( emit
);
2824 if (needs_to_create_sincos_consts( emit
)) {
2825 create_sincos_consts( emit
);
2827 if (needs_to_create_arl_consts( emit
)) {
2828 create_arl_consts( emit
);
2831 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
2832 if (!emit_ps_preamble( emit
))
2835 if (emit
->key
.fkey
.light_twoside
) {
2836 if (!emit_light_twoside( emit
))
2839 if (emit
->emit_frontface
) {
2840 if (!emit_frontface( emit
))
2848 boolean
svga_shader_emit_instructions( struct svga_shader_emitter
*emit
,
2849 const struct tgsi_token
*tokens
)
2851 struct tgsi_parse_context parse
;
2853 boolean helpers_emitted
= FALSE
;
2854 unsigned line_nr
= 0;
2856 tgsi_parse_init( &parse
, tokens
);
2857 emit
->internal_imm_count
= 0;
2859 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2860 ret
= emit_vs_preamble( emit
);
2865 pre_parse_tokens(emit
, tokens
);
2867 while (!tgsi_parse_end_of_tokens( &parse
)) {
2868 tgsi_parse_token( &parse
);
2870 switch (parse
.FullToken
.Token
.Type
) {
2871 case TGSI_TOKEN_TYPE_IMMEDIATE
:
2872 ret
= svga_emit_immediate( emit
, &parse
.FullToken
.FullImmediate
);
2877 case TGSI_TOKEN_TYPE_DECLARATION
:
2879 ret
= svga_translate_decl_sm30( emit
, &parse
.FullToken
.FullDeclaration
);
2881 ret
= svga_translate_decl_sm20( emit
, &parse
.FullToken
.FullDeclaration
);
2886 case TGSI_TOKEN_TYPE_INSTRUCTION
:
2887 if (!helpers_emitted
) {
2888 if (!svga_shader_emit_helpers( emit
))
2890 helpers_emitted
= TRUE
;
2892 ret
= svga_emit_instruction( emit
,
2894 &parse
.FullToken
.FullInstruction
);
2902 reset_temp_regs( emit
);
2905 /* Need to terminate the current subroutine. Note that the
2906 * hardware doesn't tolerate shaders without sub-routines
2907 * terminating with RET+END.
2909 if (!emit
->in_main_func
) {
2910 ret
= emit_instruction( emit
, inst_token( SVGA3DOP_RET
) );
2915 assert(emit
->dynamic_branching_level
== 0);
2917 /* Need to terminate the whole shader:
2919 ret
= emit_instruction( emit
, inst_token( SVGA3DOP_END
) );
2925 tgsi_parse_free( &parse
);