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"
30 #include "util/u_math.h"
32 #include "svga_tgsi_emit.h"
33 #include "svga_context.h"
36 static boolean
emit_vs_postamble( struct svga_shader_emitter
*emit
);
37 static boolean
emit_ps_postamble( struct svga_shader_emitter
*emit
);
47 case TGSI_OPCODE_ABS
: return SVGA3DOP_ABS
;
48 case TGSI_OPCODE_ADD
: return SVGA3DOP_ADD
;
49 case TGSI_OPCODE_BREAKC
: return SVGA3DOP_BREAKC
;
50 case TGSI_OPCODE_DP2A
: return SVGA3DOP_DP2ADD
;
51 case TGSI_OPCODE_DP3
: return SVGA3DOP_DP3
;
52 case TGSI_OPCODE_DP4
: return SVGA3DOP_DP4
;
53 case TGSI_OPCODE_FRC
: return SVGA3DOP_FRC
;
54 case TGSI_OPCODE_MAD
: return SVGA3DOP_MAD
;
55 case TGSI_OPCODE_MAX
: return SVGA3DOP_MAX
;
56 case TGSI_OPCODE_MIN
: return SVGA3DOP_MIN
;
57 case TGSI_OPCODE_MOV
: return SVGA3DOP_MOV
;
58 case TGSI_OPCODE_MUL
: return SVGA3DOP_MUL
;
59 case TGSI_OPCODE_NOP
: return SVGA3DOP_NOP
;
60 case TGSI_OPCODE_NRM4
: return SVGA3DOP_NRM
;
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.
202 if (reg
->Register
.Indirect
) {
203 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
204 /* Pixel shaders have only loop registers for relative
205 * addressing into inputs. Ignore the redundant address
206 * register, the contents of aL should be in sync with it.
208 if (reg
->Register
.File
== TGSI_FILE_INPUT
) {
209 src
.base
.relAddr
= 1;
210 src
.indirect
= src_token(SVGA3DREG_LOOP
, 0);
214 /* Constant buffers only.
216 if (reg
->Register
.File
== TGSI_FILE_CONSTANT
) {
217 /* we shift the offset towards the minimum */
218 if (svga_arl_needs_adjustment( emit
)) {
219 src
.base
.num
-= svga_arl_adjustment( emit
);
221 src
.base
.relAddr
= 1;
223 /* Not really sure what should go in the second token:
225 src
.indirect
= src_token( SVGA3DREG_ADDR
,
226 reg
->Indirect
.Index
);
228 src
.indirect
.swizzle
= SWIZZLE_XXXX
;
234 reg
->Register
.SwizzleX
,
235 reg
->Register
.SwizzleY
,
236 reg
->Register
.SwizzleZ
,
237 reg
->Register
.SwizzleW
);
239 /* src.mod isn't a bitfield, unfortunately:
240 * See tgsi_util_get_full_src_register_sign_mode for implementation details.
242 if (reg
->Register
.Absolute
) {
243 if (reg
->Register
.Negate
)
244 src
.base
.srcMod
= SVGA3DSRCMOD_ABSNEG
;
246 src
.base
.srcMod
= SVGA3DSRCMOD_ABS
;
249 if (reg
->Register
.Negate
)
250 src
.base
.srcMod
= SVGA3DSRCMOD_NEG
;
252 src
.base
.srcMod
= SVGA3DSRCMOD_NONE
;
260 * Get a temporary register, return -1 if none available
262 static INLINE SVGA3dShaderDestToken
263 get_temp( struct svga_shader_emitter
*emit
)
265 int i
= emit
->nr_hw_temp
+ emit
->internal_temp_count
++;
267 return dst_register( SVGA3DREG_TEMP
, i
);
270 /* Release a single temp. Currently only effective if it was the last
271 * allocated temp, otherwise release will be delayed until the next
272 * call to reset_temp_regs().
275 release_temp( struct svga_shader_emitter
*emit
,
276 SVGA3dShaderDestToken temp
)
278 if (temp
.num
== emit
->internal_temp_count
- 1)
279 emit
->internal_temp_count
--;
282 static void reset_temp_regs( struct svga_shader_emitter
*emit
)
284 emit
->internal_temp_count
= 0;
288 /* Replace the src with the temporary specified in the dst, but copying
289 * only the necessary channels, and preserving the original swizzle (which is
290 * important given that several opcodes have constraints in the allowed
293 static boolean
emit_repl( struct svga_shader_emitter
*emit
,
294 SVGA3dShaderDestToken dst
,
295 struct src_register
*src0
)
297 unsigned src0_swizzle
;
300 assert(SVGA3dShaderGetRegType(dst
.value
) == SVGA3DREG_TEMP
);
302 src0_swizzle
= src0
->base
.swizzle
;
305 for (chan
= 0; chan
< 4; ++chan
) {
306 unsigned swizzle
= (src0_swizzle
>> (chan
*2)) & 0x3;
307 dst
.mask
|= 1 << swizzle
;
311 src0
->base
.swizzle
= SVGA3DSWIZZLE_NONE
;
313 if (!emit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, *src0
))
317 src0
->base
.swizzle
= src0_swizzle
;
323 static boolean
submit_op0( struct svga_shader_emitter
*emit
,
324 SVGA3dShaderInstToken inst
,
325 SVGA3dShaderDestToken dest
)
327 return (emit_instruction( emit
, inst
) &&
328 emit_dst( emit
, dest
));
331 static boolean
submit_op1( struct svga_shader_emitter
*emit
,
332 SVGA3dShaderInstToken inst
,
333 SVGA3dShaderDestToken dest
,
334 struct src_register src0
)
336 return emit_op1( emit
, inst
, dest
, src0
);
340 /* SVGA shaders may not refer to >1 constant register in a single
341 * instruction. This function checks for that usage and inserts a
342 * move to temporary if detected.
344 * The same applies to input registers -- at most a single input
345 * register may be read by any instruction.
347 static boolean
submit_op2( struct svga_shader_emitter
*emit
,
348 SVGA3dShaderInstToken inst
,
349 SVGA3dShaderDestToken dest
,
350 struct src_register src0
,
351 struct src_register src1
)
353 SVGA3dShaderDestToken temp
;
354 SVGA3dShaderRegType type0
, type1
;
355 boolean need_temp
= FALSE
;
358 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
359 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
361 if (type0
== SVGA3DREG_CONST
&&
362 type1
== SVGA3DREG_CONST
&&
363 src0
.base
.num
!= src1
.base
.num
)
366 if (type0
== SVGA3DREG_INPUT
&&
367 type1
== SVGA3DREG_INPUT
&&
368 src0
.base
.num
!= src1
.base
.num
)
372 temp
= get_temp( emit
);
374 if (!emit_repl( emit
, temp
, &src0
))
378 if (!emit_op2( emit
, inst
, dest
, src0
, src1
))
382 release_temp( emit
, temp
);
388 /* SVGA shaders may not refer to >1 constant register in a single
389 * instruction. This function checks for that usage and inserts a
390 * move to temporary if detected.
392 static boolean
submit_op3( struct svga_shader_emitter
*emit
,
393 SVGA3dShaderInstToken inst
,
394 SVGA3dShaderDestToken dest
,
395 struct src_register src0
,
396 struct src_register src1
,
397 struct src_register src2
)
399 SVGA3dShaderDestToken temp0
;
400 SVGA3dShaderDestToken temp1
;
401 boolean need_temp0
= FALSE
;
402 boolean need_temp1
= FALSE
;
403 SVGA3dShaderRegType type0
, type1
, type2
;
407 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
408 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
409 type2
= SVGA3dShaderGetRegType( src2
.base
.value
);
411 if (inst
.op
!= SVGA3DOP_SINCOS
) {
412 if (type0
== SVGA3DREG_CONST
&&
413 ((type1
== SVGA3DREG_CONST
&& src0
.base
.num
!= src1
.base
.num
) ||
414 (type2
== SVGA3DREG_CONST
&& src0
.base
.num
!= src2
.base
.num
)))
417 if (type1
== SVGA3DREG_CONST
&&
418 (type2
== SVGA3DREG_CONST
&& src1
.base
.num
!= src2
.base
.num
))
422 if (type0
== SVGA3DREG_INPUT
&&
423 ((type1
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src1
.base
.num
) ||
424 (type2
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src2
.base
.num
)))
427 if (type1
== SVGA3DREG_INPUT
&&
428 (type2
== SVGA3DREG_INPUT
&& src1
.base
.num
!= src2
.base
.num
))
432 temp0
= get_temp( emit
);
434 if (!emit_repl( emit
, temp0
, &src0
))
439 temp1
= get_temp( emit
);
441 if (!emit_repl( emit
, temp1
, &src1
))
445 if (!emit_op3( emit
, inst
, dest
, src0
, src1
, src2
))
449 release_temp( emit
, temp1
);
451 release_temp( emit
, temp0
);
458 /* SVGA shaders may not refer to >1 constant register in a single
459 * instruction. This function checks for that usage and inserts a
460 * move to temporary if detected.
462 static boolean
submit_op4( struct svga_shader_emitter
*emit
,
463 SVGA3dShaderInstToken inst
,
464 SVGA3dShaderDestToken dest
,
465 struct src_register src0
,
466 struct src_register src1
,
467 struct src_register src2
,
468 struct src_register src3
)
470 SVGA3dShaderDestToken temp0
;
471 SVGA3dShaderDestToken temp3
;
472 boolean need_temp0
= FALSE
;
473 boolean need_temp3
= FALSE
;
474 SVGA3dShaderRegType type0
, type1
, type2
, type3
;
478 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
479 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
480 type2
= SVGA3dShaderGetRegType( src2
.base
.value
);
481 type3
= SVGA3dShaderGetRegType( src2
.base
.value
);
483 /* Make life a little easier - this is only used by the TXD
484 * instruction which is guaranteed not to have a constant/input reg
485 * in one slot at least:
487 assert(type1
== SVGA3DREG_SAMPLER
);
489 if (type0
== SVGA3DREG_CONST
&&
490 ((type3
== SVGA3DREG_CONST
&& src0
.base
.num
!= src3
.base
.num
) ||
491 (type2
== SVGA3DREG_CONST
&& src0
.base
.num
!= src2
.base
.num
)))
494 if (type3
== SVGA3DREG_CONST
&&
495 (type2
== SVGA3DREG_CONST
&& src3
.base
.num
!= src2
.base
.num
))
498 if (type0
== SVGA3DREG_INPUT
&&
499 ((type3
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src3
.base
.num
) ||
500 (type2
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src2
.base
.num
)))
503 if (type3
== SVGA3DREG_INPUT
&&
504 (type2
== SVGA3DREG_INPUT
&& src3
.base
.num
!= src2
.base
.num
))
508 temp0
= get_temp( emit
);
510 if (!emit_repl( emit
, temp0
, &src0
))
515 temp3
= get_temp( emit
);
517 if (!emit_repl( emit
, temp3
, &src3
))
521 if (!emit_op4( emit
, inst
, dest
, src0
, src1
, src2
, src3
))
525 release_temp( emit
, temp3
);
527 release_temp( emit
, temp0
);
532 static boolean
alias_src_dst( struct src_register src
,
533 SVGA3dShaderDestToken dst
)
535 if (src
.base
.num
!= dst
.num
)
538 if (SVGA3dShaderGetRegType(dst
.value
) !=
539 SVGA3dShaderGetRegType(src
.base
.value
))
546 static boolean
submit_lrp(struct svga_shader_emitter
*emit
,
547 SVGA3dShaderDestToken dst
,
548 struct src_register src0
,
549 struct src_register src1
,
550 struct src_register src2
)
552 SVGA3dShaderDestToken tmp
;
553 boolean need_dst_tmp
= FALSE
;
555 /* The dst reg must be a temporary, and not be the same as src0 or src2 */
556 if (SVGA3dShaderGetRegType(dst
.value
) != SVGA3DREG_TEMP
||
557 alias_src_dst(src0
, dst
) ||
558 alias_src_dst(src2
, dst
))
562 tmp
= get_temp( emit
);
569 if (!submit_op3(emit
, inst_token( SVGA3DOP_LRP
), tmp
, src0
, src1
, src2
))
573 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), dst
, src( tmp
)))
581 static boolean
emit_def_const( struct svga_shader_emitter
*emit
,
582 SVGA3dShaderConstType type
,
590 SVGA3dShaderInstToken opcode
;
593 case SVGA3D_CONST_TYPE_FLOAT
:
594 opcode
= inst_token( SVGA3DOP_DEF
);
595 def
.dst
= dst_register( SVGA3DREG_CONST
, idx
);
596 def
.constValues
[0] = a
;
597 def
.constValues
[1] = b
;
598 def
.constValues
[2] = c
;
599 def
.constValues
[3] = d
;
601 case SVGA3D_CONST_TYPE_INT
:
602 opcode
= inst_token( SVGA3DOP_DEFI
);
603 def
.dst
= dst_register( SVGA3DREG_CONSTINT
, idx
);
604 def
.constIValues
[0] = (int)a
;
605 def
.constIValues
[1] = (int)b
;
606 def
.constIValues
[2] = (int)c
;
607 def
.constIValues
[3] = (int)d
;
611 opcode
= inst_token( SVGA3DOP_NOP
);
615 if (!emit_instruction(emit
, opcode
) ||
616 !svga_shader_emit_dwords( emit
, def
.values
, Elements(def
.values
)))
622 static INLINE boolean
623 create_zero_immediate( struct svga_shader_emitter
*emit
)
625 unsigned idx
= emit
->nr_hw_float_const
++;
627 /* Emit the constant (0, 0, -1, 1) and use swizzling to generate
628 * other useful vectors.
630 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
634 emit
->zero_immediate_idx
= idx
;
635 emit
->created_zero_immediate
= TRUE
;
640 static INLINE boolean
641 create_loop_const( struct svga_shader_emitter
*emit
)
643 unsigned idx
= emit
->nr_hw_int_const
++;
645 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_INT
, idx
,
646 255, /* iteration count */
647 0, /* initial value */
649 0 /* not used, must be 0 */))
652 emit
->loop_const_idx
= idx
;
653 emit
->created_loop_const
= TRUE
;
658 static INLINE boolean
659 create_sincos_consts( struct svga_shader_emitter
*emit
)
661 unsigned idx
= emit
->nr_hw_float_const
++;
663 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
, idx
,
670 emit
->sincos_consts_idx
= idx
;
671 idx
= emit
->nr_hw_float_const
++;
673 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
, idx
,
680 emit
->created_sincos_consts
= TRUE
;
685 static INLINE boolean
686 create_arl_consts( struct svga_shader_emitter
*emit
)
690 for (i
= 0; i
< emit
->num_arl_consts
; i
+= 4) {
692 unsigned idx
= emit
->nr_hw_float_const
++;
694 for (j
= 0; j
< 4 && (j
+ i
) < emit
->num_arl_consts
; ++j
) {
695 vals
[j
] = emit
->arl_consts
[i
+ j
].number
;
696 emit
->arl_consts
[i
+ j
].idx
= idx
;
699 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_X
;
702 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_Y
;
705 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_Z
;
708 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_W
;
715 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
, idx
,
724 static INLINE
struct src_register
725 get_vface( struct svga_shader_emitter
*emit
)
727 assert(emit
->emitted_vface
);
728 return src_register(SVGA3DREG_MISCTYPE
,
732 /* returns {0, 0, 0, 1} immediate */
733 static INLINE
struct src_register
734 get_zero_immediate( struct svga_shader_emitter
*emit
)
736 assert(emit
->created_zero_immediate
);
737 assert(emit
->zero_immediate_idx
>= 0);
738 return swizzle(src_register( SVGA3DREG_CONST
,
739 emit
->zero_immediate_idx
),
743 /* returns {1, 1, 1, -1} immediate */
744 static INLINE
struct src_register
745 get_pos_neg_one_immediate( struct svga_shader_emitter
*emit
)
747 assert(emit
->created_zero_immediate
);
748 assert(emit
->zero_immediate_idx
>= 0);
749 return swizzle(src_register( SVGA3DREG_CONST
,
750 emit
->zero_immediate_idx
),
754 /* returns the loop const */
755 static INLINE
struct src_register
756 get_loop_const( struct svga_shader_emitter
*emit
)
758 assert(emit
->created_loop_const
);
759 assert(emit
->loop_const_idx
>= 0);
760 return src_register( SVGA3DREG_CONSTINT
,
761 emit
->loop_const_idx
);
764 /* returns a sincos const */
765 static INLINE
struct src_register
766 get_sincos_const( struct svga_shader_emitter
*emit
,
769 assert(emit
->created_sincos_consts
);
770 assert(emit
->sincos_consts_idx
>= 0);
771 assert(index
== 0 || index
== 1);
772 return src_register( SVGA3DREG_CONST
,
773 emit
->sincos_consts_idx
+ index
);
776 static INLINE
struct src_register
777 get_fake_arl_const( struct svga_shader_emitter
*emit
)
779 struct src_register reg
;
780 int idx
= 0, swizzle
= 0, i
;
782 for (i
= 0; i
< emit
->num_arl_consts
; ++ i
) {
783 if (emit
->arl_consts
[i
].arl_num
== emit
->current_arl
) {
784 idx
= emit
->arl_consts
[i
].idx
;
785 swizzle
= emit
->arl_consts
[i
].swizzle
;
789 reg
= src_register( SVGA3DREG_CONST
, idx
);
790 return scalar(reg
, swizzle
);
793 static INLINE
struct src_register
794 get_tex_dimensions( struct svga_shader_emitter
*emit
, int sampler_num
)
797 struct src_register reg
;
799 /* the width/height indexes start right after constants */
800 idx
= emit
->key
.fkey
.tex
[sampler_num
].width_height_idx
+
801 emit
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
803 reg
= src_register( SVGA3DREG_CONST
, idx
);
807 static boolean
emit_fake_arl(struct svga_shader_emitter
*emit
,
808 const struct tgsi_full_instruction
*insn
)
810 const struct src_register src0
= translate_src_register(
811 emit
, &insn
->Src
[0] );
812 struct src_register src1
= get_fake_arl_const( emit
);
813 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
814 SVGA3dShaderDestToken tmp
= get_temp( emit
);
816 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), tmp
, src0
))
819 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), tmp
, src( tmp
),
823 /* replicate the original swizzle */
825 src1
.base
.swizzle
= src0
.base
.swizzle
;
827 return submit_op1( emit
, inst_token( SVGA3DOP_MOVA
),
831 static boolean
emit_if(struct svga_shader_emitter
*emit
,
832 const struct tgsi_full_instruction
*insn
)
834 struct src_register src0
= translate_src_register(
835 emit
, &insn
->Src
[0] );
836 struct src_register zero
= get_zero_immediate( emit
);
837 SVGA3dShaderInstToken if_token
= inst_token( SVGA3DOP_IFC
);
839 if_token
.control
= SVGA3DOPCOMPC_NE
;
840 zero
= scalar(zero
, TGSI_SWIZZLE_X
);
842 if (SVGA3dShaderGetRegType(src0
.base
.value
) == SVGA3DREG_CONST
) {
844 * Max different constant registers readable per IFC instruction is 1.
847 SVGA3dShaderDestToken tmp
= get_temp( emit
);
849 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), tmp
, src0
))
852 src0
= scalar(src( tmp
), TGSI_SWIZZLE_X
);
855 emit
->dynamic_branching_level
++;
857 return (emit_instruction( emit
, if_token
) &&
858 emit_src( emit
, src0
) &&
859 emit_src( emit
, zero
) );
862 static boolean
emit_endif(struct svga_shader_emitter
*emit
,
863 const struct tgsi_full_instruction
*insn
)
865 emit
->dynamic_branching_level
--;
867 return (emit_instruction( emit
,
868 inst_token( SVGA3DOP_ENDIF
)));
871 static boolean
emit_else(struct svga_shader_emitter
*emit
,
872 const struct tgsi_full_instruction
*insn
)
874 return (emit_instruction( emit
,
875 inst_token( SVGA3DOP_ELSE
)));
878 /* Translate the following TGSI FLR instruction.
880 * To the following SVGA3D instruction sequence.
884 static boolean
emit_floor(struct svga_shader_emitter
*emit
,
885 const struct tgsi_full_instruction
*insn
)
887 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
888 const struct src_register src0
= translate_src_register(
889 emit
, &insn
->Src
[0] );
890 SVGA3dShaderDestToken temp
= get_temp( emit
);
893 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
), temp
, src0
))
896 /* SUB DST, SRC, TMP */
897 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
, src0
,
898 negate( src( temp
) ) ))
905 /* Translate the following TGSI CMP instruction.
906 * CMP DST, SRC0, SRC1, SRC2
907 * To the following SVGA3D instruction sequence.
908 * CMP DST, SRC0, SRC2, SRC1
910 static boolean
emit_cmp(struct svga_shader_emitter
*emit
,
911 const struct tgsi_full_instruction
*insn
)
913 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
914 const struct src_register src0
= translate_src_register(
915 emit
, &insn
->Src
[0] );
916 const struct src_register src1
= translate_src_register(
917 emit
, &insn
->Src
[1] );
918 const struct src_register src2
= translate_src_register(
919 emit
, &insn
->Src
[2] );
921 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
922 SVGA3dShaderDestToken temp
= get_temp(emit
);
923 struct src_register zero
= scalar(get_zero_immediate(emit
), TGSI_SWIZZLE_X
);
925 /* Since vertex shaders don't support the CMP instruction,
926 * simulate it with SLT and LRP instructions.
928 * LRP DST, TMP, SRC1, SRC2
930 if (!submit_op2(emit
, inst_token(SVGA3DOP_SLT
), temp
, src0
, zero
))
932 return submit_lrp(emit
, dst
, src(temp
), src1
, src2
);
935 /* CMP DST, SRC0, SRC2, SRC1 */
936 return submit_op3( emit
, inst_token( SVGA3DOP_CMP
), dst
, src0
, src2
, src1
);
941 /* Translate the following TGSI DIV instruction.
942 * DIV DST.xy, SRC0, SRC1
943 * To the following SVGA3D instruction sequence.
944 * RCP TMP.x, SRC1.xxxx
945 * RCP TMP.y, SRC1.yyyy
946 * MUL DST.xy, SRC0, TMP
948 static boolean
emit_div(struct svga_shader_emitter
*emit
,
949 const struct tgsi_full_instruction
*insn
)
951 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
952 const struct src_register src0
= translate_src_register(
953 emit
, &insn
->Src
[0] );
954 const struct src_register src1
= translate_src_register(
955 emit
, &insn
->Src
[1] );
956 SVGA3dShaderDestToken temp
= get_temp( emit
);
959 /* For each enabled element, perform a RCP instruction. Note that
960 * RCP is scalar in SVGA3D:
962 for (i
= 0; i
< 4; i
++) {
963 unsigned channel
= 1 << i
;
964 if (dst
.mask
& channel
) {
965 /* RCP TMP.?, SRC1.???? */
966 if (!submit_op1( emit
, inst_token( SVGA3DOP_RCP
),
967 writemask(temp
, channel
),
973 /* Then multiply them out with a single mul:
977 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
), dst
, src0
,
984 /* Translate the following TGSI DP2 instruction.
985 * DP2 DST, SRC1, SRC2
986 * To the following SVGA3D instruction sequence.
987 * MUL TMP, SRC1, SRC2
988 * ADD DST, TMP.xxxx, TMP.yyyy
990 static boolean
emit_dp2(struct svga_shader_emitter
*emit
,
991 const struct tgsi_full_instruction
*insn
)
993 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
994 const struct src_register src0
= translate_src_register(
995 emit
, &insn
->Src
[0] );
996 const struct src_register src1
= translate_src_register(
997 emit
, &insn
->Src
[1] );
998 SVGA3dShaderDestToken temp
= get_temp( emit
);
999 struct src_register temp_src0
, temp_src1
;
1001 /* MUL TMP, SRC1, SRC2 */
1002 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
), temp
, src0
, src1
))
1005 temp_src0
= scalar(src( temp
), TGSI_SWIZZLE_X
);
1006 temp_src1
= scalar(src( temp
), TGSI_SWIZZLE_Y
);
1008 /* ADD DST, TMP.xxxx, TMP.yyyy */
1009 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
,
1010 temp_src0
, temp_src1
))
1017 /* Translate the following TGSI DPH instruction.
1018 * DPH DST, SRC1, SRC2
1019 * To the following SVGA3D instruction sequence.
1020 * DP3 TMP, SRC1, SRC2
1021 * ADD DST, TMP, SRC2.wwww
1023 static boolean
emit_dph(struct svga_shader_emitter
*emit
,
1024 const struct tgsi_full_instruction
*insn
)
1026 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1027 const struct src_register src0
= translate_src_register(
1028 emit
, &insn
->Src
[0] );
1029 struct src_register src1
= translate_src_register(
1030 emit
, &insn
->Src
[1] );
1031 SVGA3dShaderDestToken temp
= get_temp( emit
);
1033 /* DP3 TMP, SRC1, SRC2 */
1034 if (!submit_op2( emit
, inst_token( SVGA3DOP_DP3
), temp
, src0
, src1
))
1037 src1
= scalar(src1
, TGSI_SWIZZLE_W
);
1039 /* ADD DST, TMP, SRC2.wwww */
1040 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
,
1041 src( temp
), src1
))
1047 /* Translate the following TGSI DST instruction.
1049 * To the following SVGA3D instruction sequence.
1054 static boolean
emit_nrm(struct svga_shader_emitter
*emit
,
1055 const struct tgsi_full_instruction
*insn
)
1057 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1058 const struct src_register src0
= translate_src_register(
1059 emit
, &insn
->Src
[0] );
1060 SVGA3dShaderDestToken temp
= get_temp( emit
);
1062 /* DP3 TMP, SRC, SRC */
1063 if (!submit_op2( emit
, inst_token( SVGA3DOP_DP3
), temp
, src0
, src0
))
1067 if (!submit_op1( emit
, inst_token( SVGA3DOP_RSQ
), temp
, src( temp
)))
1070 /* MUL DST, SRC, TMP */
1071 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
), dst
,
1079 static boolean
do_emit_sincos(struct svga_shader_emitter
*emit
,
1080 SVGA3dShaderDestToken dst
,
1081 struct src_register src0
)
1083 src0
= scalar(src0
, TGSI_SWIZZLE_X
);
1085 if (emit
->use_sm30
) {
1086 return submit_op1( emit
, inst_token( SVGA3DOP_SINCOS
),
1089 struct src_register const1
= get_sincos_const( emit
, 0 );
1090 struct src_register const2
= get_sincos_const( emit
, 1 );
1092 return submit_op3( emit
, inst_token( SVGA3DOP_SINCOS
),
1093 dst
, src0
, const1
, const2
);
1097 static boolean
emit_sincos(struct svga_shader_emitter
*emit
,
1098 const struct tgsi_full_instruction
*insn
)
1100 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1101 struct src_register src0
= translate_src_register(
1102 emit
, &insn
->Src
[0] );
1103 SVGA3dShaderDestToken temp
= get_temp( emit
);
1106 if (!do_emit_sincos(emit
, writemask(temp
, TGSI_WRITEMASK_XY
), src0
))
1110 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src( temp
) ))
1120 static boolean
emit_sin(struct svga_shader_emitter
*emit
,
1121 const struct tgsi_full_instruction
*insn
)
1123 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1124 struct src_register src0
= translate_src_register(
1125 emit
, &insn
->Src
[0] );
1126 SVGA3dShaderDestToken temp
= get_temp( emit
);
1129 if (!do_emit_sincos(emit
, writemask(temp
, TGSI_WRITEMASK_Y
), src0
))
1132 src0
= scalar(src( temp
), TGSI_SWIZZLE_Y
);
1134 /* MOV DST TMP.yyyy */
1135 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src0
))
1145 static boolean
emit_cos(struct svga_shader_emitter
*emit
,
1146 const struct tgsi_full_instruction
*insn
)
1148 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1149 struct src_register src0
= translate_src_register(
1150 emit
, &insn
->Src
[0] );
1151 SVGA3dShaderDestToken temp
= get_temp( emit
);
1154 if (!do_emit_sincos( emit
, writemask(temp
, TGSI_WRITEMASK_X
), src0
))
1157 src0
= scalar(src( temp
), TGSI_SWIZZLE_X
);
1159 /* MOV DST TMP.xxxx */
1160 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src0
))
1166 static boolean
emit_ssg(struct svga_shader_emitter
*emit
,
1167 const struct tgsi_full_instruction
*insn
)
1169 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1170 struct src_register src0
= translate_src_register(
1171 emit
, &insn
->Src
[0] );
1172 SVGA3dShaderDestToken temp0
= get_temp( emit
);
1173 SVGA3dShaderDestToken temp1
= get_temp( emit
);
1174 struct src_register zero
, one
;
1176 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1177 /* SGN DST, SRC0, TMP0, TMP1 */
1178 return submit_op3( emit
, inst_token( SVGA3DOP_SGN
), dst
, src0
,
1179 src( temp0
), src( temp1
) );
1182 zero
= get_zero_immediate( emit
);
1183 one
= scalar( zero
, TGSI_SWIZZLE_W
);
1184 zero
= scalar( zero
, TGSI_SWIZZLE_X
);
1186 /* CMP TMP0, SRC0, one, zero */
1187 if (!submit_op3( emit
, inst_token( SVGA3DOP_CMP
),
1188 writemask( temp0
, dst
.mask
), src0
, one
, zero
))
1191 /* CMP TMP1, negate(SRC0), negate(one), zero */
1192 if (!submit_op3( emit
, inst_token( SVGA3DOP_CMP
),
1193 writemask( temp1
, dst
.mask
), negate( src0
), negate( one
),
1197 /* ADD DST, TMP0, TMP1 */
1198 return submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
, src( temp0
),
1203 * ADD DST SRC0, negate(SRC0)
1205 static boolean
emit_sub(struct svga_shader_emitter
*emit
,
1206 const struct tgsi_full_instruction
*insn
)
1208 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1209 struct src_register src0
= translate_src_register(
1210 emit
, &insn
->Src
[0] );
1211 struct src_register src1
= translate_src_register(
1212 emit
, &insn
->Src
[1] );
1214 src1
= negate(src1
);
1216 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
,
1224 static boolean
emit_kil(struct svga_shader_emitter
*emit
,
1225 const struct tgsi_full_instruction
*insn
)
1227 SVGA3dShaderInstToken inst
;
1228 const struct tgsi_full_src_register
*reg
= &insn
->Src
[0];
1229 struct src_register src0
;
1231 inst
= inst_token( SVGA3DOP_TEXKILL
);
1232 src0
= translate_src_register( emit
, reg
);
1234 if (reg
->Register
.Absolute
||
1235 reg
->Register
.Negate
||
1236 reg
->Register
.Indirect
||
1237 reg
->Register
.SwizzleX
!= 0 ||
1238 reg
->Register
.SwizzleY
!= 1 ||
1239 reg
->Register
.SwizzleZ
!= 2 ||
1240 reg
->Register
.File
!= TGSI_FILE_TEMPORARY
)
1242 SVGA3dShaderDestToken temp
= get_temp( emit
);
1244 submit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp
, src0
);
1248 return submit_op0( emit
, inst
, dst(src0
) );
1252 /* mesa state tracker always emits kilp as an unconditional
1254 static boolean
emit_kilp(struct svga_shader_emitter
*emit
,
1255 const struct tgsi_full_instruction
*insn
)
1257 SVGA3dShaderInstToken inst
;
1258 SVGA3dShaderDestToken temp
;
1259 struct src_register one
= scalar( get_zero_immediate( emit
),
1262 inst
= inst_token( SVGA3DOP_TEXKILL
);
1264 /* texkill doesn't allow negation on the operand so lets move
1265 * negation of {1} to a temp register */
1266 temp
= get_temp( emit
);
1267 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp
,
1271 return submit_op0( emit
, inst
, temp
);
1274 /* Implement conditionals by initializing destination reg to 'fail',
1275 * then set predicate reg with UFOP_SETP, then move 'pass' to dest
1276 * based on predicate reg.
1278 * SETP src0, cmp, src1 -- do this first to avoid aliasing problems.
1283 emit_conditional(struct svga_shader_emitter
*emit
,
1284 unsigned compare_func
,
1285 SVGA3dShaderDestToken dst
,
1286 struct src_register src0
,
1287 struct src_register src1
,
1288 struct src_register pass
,
1289 struct src_register fail
)
1291 SVGA3dShaderDestToken pred_reg
= dst_register( SVGA3DREG_PREDICATE
, 0 );
1292 SVGA3dShaderInstToken setp_token
, mov_token
;
1293 setp_token
= inst_token( SVGA3DOP_SETP
);
1295 switch (compare_func
) {
1296 case PIPE_FUNC_NEVER
:
1297 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1300 case PIPE_FUNC_LESS
:
1301 setp_token
.control
= SVGA3DOPCOMP_LT
;
1303 case PIPE_FUNC_EQUAL
:
1304 setp_token
.control
= SVGA3DOPCOMP_EQ
;
1306 case PIPE_FUNC_LEQUAL
:
1307 setp_token
.control
= SVGA3DOPCOMP_LE
;
1309 case PIPE_FUNC_GREATER
:
1310 setp_token
.control
= SVGA3DOPCOMP_GT
;
1312 case PIPE_FUNC_NOTEQUAL
:
1313 setp_token
.control
= SVGA3DOPCOMPC_NE
;
1315 case PIPE_FUNC_GEQUAL
:
1316 setp_token
.control
= SVGA3DOPCOMP_GE
;
1318 case PIPE_FUNC_ALWAYS
:
1319 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1324 /* SETP src0, COMPOP, src1 */
1325 if (!submit_op2( emit
, setp_token
, pred_reg
,
1329 mov_token
= inst_token( SVGA3DOP_MOV
);
1332 if (!submit_op1( emit
, mov_token
, dst
,
1336 /* MOV dst, pass (predicated)
1338 * Note that the predicate reg (and possible modifiers) is passed
1339 * as the first source argument.
1341 mov_token
.predicated
= 1;
1342 if (!submit_op2( emit
, mov_token
, dst
,
1343 src( pred_reg
), pass
))
1351 emit_select(struct svga_shader_emitter
*emit
,
1352 unsigned compare_func
,
1353 SVGA3dShaderDestToken dst
,
1354 struct src_register src0
,
1355 struct src_register src1
)
1357 /* There are some SVGA instructions which implement some selects
1358 * directly, but they are only available in the vertex shader.
1360 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1361 switch (compare_func
) {
1362 case PIPE_FUNC_GEQUAL
:
1363 return submit_op2( emit
, inst_token( SVGA3DOP_SGE
), dst
, src0
, src1
);
1364 case PIPE_FUNC_LEQUAL
:
1365 return submit_op2( emit
, inst_token( SVGA3DOP_SGE
), dst
, src1
, src0
);
1366 case PIPE_FUNC_GREATER
:
1367 return submit_op2( emit
, inst_token( SVGA3DOP_SLT
), dst
, src1
, src0
);
1368 case PIPE_FUNC_LESS
:
1369 return submit_op2( emit
, inst_token( SVGA3DOP_SLT
), dst
, src0
, src1
);
1376 /* Otherwise, need to use the setp approach:
1379 struct src_register one
, zero
;
1380 /* zero immediate is 0,0,0,1 */
1381 zero
= get_zero_immediate( emit
);
1382 one
= scalar( zero
, TGSI_SWIZZLE_W
);
1383 zero
= scalar( zero
, TGSI_SWIZZLE_X
);
1385 return emit_conditional(
1396 static boolean
emit_select_op(struct svga_shader_emitter
*emit
,
1398 const struct tgsi_full_instruction
*insn
)
1400 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1401 struct src_register src0
= translate_src_register(
1402 emit
, &insn
->Src
[0] );
1403 struct src_register src1
= translate_src_register(
1404 emit
, &insn
->Src
[1] );
1406 return emit_select( emit
, compare
, dst
, src0
, src1
);
1410 /* Translate texture instructions to SVGA3D representation.
1412 static boolean
emit_tex2(struct svga_shader_emitter
*emit
,
1413 const struct tgsi_full_instruction
*insn
,
1414 SVGA3dShaderDestToken dst
)
1416 SVGA3dShaderInstToken inst
;
1417 struct src_register texcoord
;
1418 struct src_register sampler
;
1419 SVGA3dShaderDestToken tmp
;
1423 switch (insn
->Instruction
.Opcode
) {
1424 case TGSI_OPCODE_TEX
:
1425 inst
.op
= SVGA3DOP_TEX
;
1427 case TGSI_OPCODE_TXP
:
1428 inst
.op
= SVGA3DOP_TEX
;
1429 inst
.control
= SVGA3DOPCONT_PROJECT
;
1431 case TGSI_OPCODE_TXB
:
1432 inst
.op
= SVGA3DOP_TEX
;
1433 inst
.control
= SVGA3DOPCONT_BIAS
;
1435 case TGSI_OPCODE_TXL
:
1436 inst
.op
= SVGA3DOP_TEXLDL
;
1443 texcoord
= translate_src_register( emit
, &insn
->Src
[0] );
1444 sampler
= translate_src_register( emit
, &insn
->Src
[1] );
1446 if (emit
->key
.fkey
.tex
[sampler
.base
.num
].unnormalized
||
1447 emit
->dynamic_branching_level
> 0)
1448 tmp
= get_temp( emit
);
1450 /* Can't do mipmapping inside dynamic branch constructs. Force LOD
1451 * zero in that case.
1453 if (emit
->dynamic_branching_level
> 0 &&
1454 inst
.op
== SVGA3DOP_TEX
&&
1455 SVGA3dShaderGetRegType(texcoord
.base
.value
) == SVGA3DREG_TEMP
) {
1456 struct src_register zero
= get_zero_immediate( emit
);
1458 /* MOV tmp, texcoord */
1459 if (!submit_op1( emit
,
1460 inst_token( SVGA3DOP_MOV
),
1465 /* MOV tmp.w, zero */
1466 if (!submit_op1( emit
,
1467 inst_token( SVGA3DOP_MOV
),
1468 writemask( tmp
, TGSI_WRITEMASK_W
),
1469 scalar( zero
, TGSI_SWIZZLE_X
)))
1472 texcoord
= src( tmp
);
1473 inst
.op
= SVGA3DOP_TEXLDL
;
1476 /* Explicit normalization of texcoords:
1478 if (emit
->key
.fkey
.tex
[sampler
.base
.num
].unnormalized
) {
1479 struct src_register wh
= get_tex_dimensions( emit
, sampler
.base
.num
);
1481 /* MUL tmp, SRC0, WH */
1482 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
1483 tmp
, texcoord
, wh
))
1486 texcoord
= src( tmp
);
1489 return submit_op2( emit
, inst
, dst
, texcoord
, sampler
);
1495 /* Translate texture instructions to SVGA3D representation.
1497 static boolean
emit_tex4(struct svga_shader_emitter
*emit
,
1498 const struct tgsi_full_instruction
*insn
,
1499 SVGA3dShaderDestToken dst
)
1501 SVGA3dShaderInstToken inst
;
1502 struct src_register texcoord
;
1503 struct src_register ddx
;
1504 struct src_register ddy
;
1505 struct src_register sampler
;
1507 texcoord
= translate_src_register( emit
, &insn
->Src
[0] );
1508 ddx
= translate_src_register( emit
, &insn
->Src
[1] );
1509 ddy
= translate_src_register( emit
, &insn
->Src
[2] );
1510 sampler
= translate_src_register( emit
, &insn
->Src
[3] );
1514 switch (insn
->Instruction
.Opcode
) {
1515 case TGSI_OPCODE_TXD
:
1516 inst
.op
= SVGA3DOP_TEXLDD
; /* 4 args! */
1523 return submit_op4( emit
, inst
, dst
, texcoord
, sampler
, ddx
, ddy
);
1527 static boolean
emit_tex(struct svga_shader_emitter
*emit
,
1528 const struct tgsi_full_instruction
*insn
)
1530 SVGA3dShaderDestToken dst
=
1531 translate_dst_register( emit
, insn
, 0 );
1532 struct src_register src0
=
1533 translate_src_register( emit
, &insn
->Src
[0] );
1534 struct src_register src1
=
1535 translate_src_register( emit
, &insn
->Src
[1] );
1537 SVGA3dShaderDestToken tex_result
;
1539 /* check for shadow samplers */
1540 boolean compare
= (emit
->key
.fkey
.tex
[src1
.base
.num
].compare_mode
==
1541 PIPE_TEX_COMPARE_R_TO_TEXTURE
);
1544 /* If doing compare processing, need to put this value into a
1545 * temporary so it can be used as a source later on.
1548 (!emit
->use_sm30
&& dst
.mask
!= TGSI_WRITEMASK_XYZW
) ) {
1549 tex_result
= get_temp( emit
);
1555 switch(insn
->Instruction
.Opcode
) {
1556 case TGSI_OPCODE_TEX
:
1557 case TGSI_OPCODE_TXB
:
1558 case TGSI_OPCODE_TXP
:
1559 case TGSI_OPCODE_TXL
:
1560 if (!emit_tex2( emit
, insn
, tex_result
))
1563 case TGSI_OPCODE_TXD
:
1564 if (!emit_tex4( emit
, insn
, tex_result
))
1573 if (dst
.mask
& TGSI_WRITEMASK_XYZ
) {
1574 SVGA3dShaderDestToken src0_zdivw
= get_temp( emit
);
1575 struct src_register tex_src_x
= scalar(src(tex_result
), TGSI_SWIZZLE_Y
);
1577 /* Divide texcoord R by Q */
1578 if (!submit_op1( emit
, inst_token( SVGA3DOP_RCP
),
1579 writemask(src0_zdivw
, TGSI_WRITEMASK_X
),
1580 scalar(src0
, TGSI_SWIZZLE_W
) ))
1583 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
1584 writemask(src0_zdivw
, TGSI_WRITEMASK_X
),
1585 scalar(src0
, TGSI_SWIZZLE_Z
),
1586 scalar(src(src0_zdivw
), TGSI_SWIZZLE_X
) ))
1591 emit
->key
.fkey
.tex
[src1
.base
.num
].compare_func
,
1592 writemask( dst
, TGSI_WRITEMASK_XYZ
),
1593 scalar(src(src0_zdivw
), TGSI_SWIZZLE_X
),
1598 if (dst
.mask
& TGSI_WRITEMASK_W
) {
1599 struct src_register one
=
1600 scalar( get_zero_immediate( emit
), TGSI_SWIZZLE_W
);
1602 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1603 writemask( dst
, TGSI_WRITEMASK_W
),
1610 else if (!emit
->use_sm30
&& dst
.mask
!= TGSI_WRITEMASK_XYZW
)
1612 if (!emit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src(tex_result
) ))
1619 static boolean
emit_bgnloop2( struct svga_shader_emitter
*emit
,
1620 const struct tgsi_full_instruction
*insn
)
1622 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_LOOP
);
1623 struct src_register loop_reg
= src_register( SVGA3DREG_LOOP
, 0 );
1624 struct src_register const_int
= get_loop_const( emit
);
1626 emit
->dynamic_branching_level
++;
1628 return (emit_instruction( emit
, inst
) &&
1629 emit_src( emit
, loop_reg
) &&
1630 emit_src( emit
, const_int
) );
1633 static boolean
emit_endloop2( struct svga_shader_emitter
*emit
,
1634 const struct tgsi_full_instruction
*insn
)
1636 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_ENDLOOP
);
1638 emit
->dynamic_branching_level
--;
1640 return emit_instruction( emit
, inst
);
1643 static boolean
emit_brk( struct svga_shader_emitter
*emit
,
1644 const struct tgsi_full_instruction
*insn
)
1646 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_BREAK
);
1647 return emit_instruction( emit
, inst
);
1650 static boolean
emit_scalar_op1( struct svga_shader_emitter
*emit
,
1652 const struct tgsi_full_instruction
*insn
)
1654 SVGA3dShaderInstToken inst
;
1655 SVGA3dShaderDestToken dst
;
1656 struct src_register src
;
1658 inst
= inst_token( opcode
);
1659 dst
= translate_dst_register( emit
, insn
, 0 );
1660 src
= translate_src_register( emit
, &insn
->Src
[0] );
1661 src
= scalar( src
, TGSI_SWIZZLE_X
);
1663 return submit_op1( emit
, inst
, dst
, src
);
1667 static boolean
emit_simple_instruction(struct svga_shader_emitter
*emit
,
1669 const struct tgsi_full_instruction
*insn
)
1671 const struct tgsi_full_src_register
*src
= insn
->Src
;
1672 SVGA3dShaderInstToken inst
;
1673 SVGA3dShaderDestToken dst
;
1675 inst
= inst_token( opcode
);
1676 dst
= translate_dst_register( emit
, insn
, 0 );
1678 switch (insn
->Instruction
.NumSrcRegs
) {
1680 return submit_op0( emit
, inst
, dst
);
1682 return submit_op1( emit
, inst
, dst
,
1683 translate_src_register( emit
, &src
[0] ));
1685 return submit_op2( emit
, inst
, dst
,
1686 translate_src_register( emit
, &src
[0] ),
1687 translate_src_register( emit
, &src
[1] ) );
1689 return submit_op3( emit
, inst
, dst
,
1690 translate_src_register( emit
, &src
[0] ),
1691 translate_src_register( emit
, &src
[1] ),
1692 translate_src_register( emit
, &src
[2] ) );
1700 static boolean
emit_deriv(struct svga_shader_emitter
*emit
,
1701 const struct tgsi_full_instruction
*insn
)
1703 if (emit
->dynamic_branching_level
> 0 &&
1704 insn
->Src
[0].Register
.File
== TGSI_FILE_TEMPORARY
)
1706 struct src_register zero
= get_zero_immediate( emit
);
1707 SVGA3dShaderDestToken dst
=
1708 translate_dst_register( emit
, insn
, 0 );
1710 /* Deriv opcodes not valid inside dynamic branching, workaround
1711 * by zeroing out the destination.
1713 if (!submit_op1(emit
,
1714 inst_token( SVGA3DOP_MOV
),
1716 scalar(zero
, TGSI_SWIZZLE_X
)))
1723 const struct tgsi_full_src_register
*reg
= &insn
->Src
[0];
1724 SVGA3dShaderInstToken inst
;
1725 SVGA3dShaderDestToken dst
;
1726 struct src_register src0
;
1728 switch (insn
->Instruction
.Opcode
) {
1729 case TGSI_OPCODE_DDX
:
1730 opcode
= SVGA3DOP_DSX
;
1732 case TGSI_OPCODE_DDY
:
1733 opcode
= SVGA3DOP_DSY
;
1739 inst
= inst_token( opcode
);
1740 dst
= translate_dst_register( emit
, insn
, 0 );
1741 src0
= translate_src_register( emit
, reg
);
1743 /* We cannot use negate or abs on source to dsx/dsy instruction.
1745 if (reg
->Register
.Absolute
||
1746 reg
->Register
.Negate
) {
1747 SVGA3dShaderDestToken temp
= get_temp( emit
);
1749 if (!emit_repl( emit
, temp
, &src0
))
1753 return submit_op1( emit
, inst
, dst
, src0
);
1757 static boolean
emit_arl(struct svga_shader_emitter
*emit
,
1758 const struct tgsi_full_instruction
*insn
)
1760 ++emit
->current_arl
;
1761 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
1762 /* MOVA not present in pixel shader instruction set.
1763 * Ignore this instruction altogether since it is
1764 * only used for loop counters -- and for that
1765 * we reference aL directly.
1769 if (svga_arl_needs_adjustment( emit
)) {
1770 return emit_fake_arl( emit
, insn
);
1772 /* no need to adjust, just emit straight arl */
1773 return emit_simple_instruction(emit
, SVGA3DOP_MOVA
, insn
);
1777 static boolean
emit_pow(struct svga_shader_emitter
*emit
,
1778 const struct tgsi_full_instruction
*insn
)
1780 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1781 struct src_register src0
= translate_src_register(
1782 emit
, &insn
->Src
[0] );
1783 struct src_register src1
= translate_src_register(
1784 emit
, &insn
->Src
[1] );
1785 boolean need_tmp
= FALSE
;
1787 /* POW can only output to a temporary */
1788 if (insn
->Dst
[0].Register
.File
!= TGSI_FILE_TEMPORARY
)
1791 /* POW src1 must not be the same register as dst */
1792 if (alias_src_dst( src1
, dst
))
1795 /* it's a scalar op */
1796 src0
= scalar( src0
, TGSI_SWIZZLE_X
);
1797 src1
= scalar( src1
, TGSI_SWIZZLE_X
);
1800 SVGA3dShaderDestToken tmp
= writemask(get_temp( emit
), TGSI_WRITEMASK_X
);
1802 if (!submit_op2(emit
, inst_token( SVGA3DOP_POW
), tmp
, src0
, src1
))
1805 return submit_op1(emit
, inst_token( SVGA3DOP_MOV
), dst
, scalar(src(tmp
), 0) );
1808 return submit_op2(emit
, inst_token( SVGA3DOP_POW
), dst
, src0
, src1
);
1812 static boolean
emit_xpd(struct svga_shader_emitter
*emit
,
1813 const struct tgsi_full_instruction
*insn
)
1815 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1816 const struct src_register src0
= translate_src_register(
1817 emit
, &insn
->Src
[0] );
1818 const struct src_register src1
= translate_src_register(
1819 emit
, &insn
->Src
[1] );
1820 boolean need_dst_tmp
= FALSE
;
1822 /* XPD can only output to a temporary */
1823 if (SVGA3dShaderGetRegType(dst
.value
) != SVGA3DREG_TEMP
)
1824 need_dst_tmp
= TRUE
;
1826 /* The dst reg must not be the same as src0 or src1*/
1827 if (alias_src_dst(src0
, dst
) ||
1828 alias_src_dst(src1
, dst
))
1829 need_dst_tmp
= TRUE
;
1832 SVGA3dShaderDestToken tmp
= get_temp( emit
);
1834 /* Obey DX9 restrictions on mask:
1836 tmp
.mask
= dst
.mask
& TGSI_WRITEMASK_XYZ
;
1838 if (!submit_op2(emit
, inst_token( SVGA3DOP_CRS
), tmp
, src0
, src1
))
1841 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), dst
, src( tmp
)))
1845 if (!submit_op2(emit
, inst_token( SVGA3DOP_CRS
), dst
, src0
, src1
))
1849 /* Need to emit 1.0 to dst.w?
1851 if (dst
.mask
& TGSI_WRITEMASK_W
) {
1852 struct src_register zero
= get_zero_immediate( emit
);
1854 if (!submit_op1(emit
,
1855 inst_token( SVGA3DOP_MOV
),
1856 writemask(dst
, TGSI_WRITEMASK_W
),
1865 static boolean
emit_lrp(struct svga_shader_emitter
*emit
,
1866 const struct tgsi_full_instruction
*insn
)
1868 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1869 const struct src_register src0
= translate_src_register(
1870 emit
, &insn
->Src
[0] );
1871 const struct src_register src1
= translate_src_register(
1872 emit
, &insn
->Src
[1] );
1873 const struct src_register src2
= translate_src_register(
1874 emit
, &insn
->Src
[2] );
1876 return submit_lrp(emit
, dst
, src0
, src1
, src2
);
1880 static boolean
emit_dst_insn(struct svga_shader_emitter
*emit
,
1881 const struct tgsi_full_instruction
*insn
)
1883 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1884 /* SVGA/DX9 has a DST instruction, but only for vertex shaders:
1886 return emit_simple_instruction(emit
, SVGA3DOP_DST
, insn
);
1890 /* result[0] = 1 * 1;
1891 * result[1] = a[1] * b[1];
1892 * result[2] = a[2] * 1;
1893 * result[3] = 1 * b[3];
1896 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1897 SVGA3dShaderDestToken tmp
;
1898 const struct src_register src0
= translate_src_register(
1899 emit
, &insn
->Src
[0] );
1900 const struct src_register src1
= translate_src_register(
1901 emit
, &insn
->Src
[1] );
1902 struct src_register zero
= get_zero_immediate( emit
);
1903 boolean need_tmp
= FALSE
;
1905 if (SVGA3dShaderGetRegType(dst
.value
) != SVGA3DREG_TEMP
||
1906 alias_src_dst(src0
, dst
) ||
1907 alias_src_dst(src1
, dst
))
1911 tmp
= get_temp( emit
);
1919 if (tmp
.mask
& TGSI_WRITEMASK_XW
) {
1920 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1921 writemask(tmp
, TGSI_WRITEMASK_XW
),
1928 if (tmp
.mask
& TGSI_WRITEMASK_YZ
) {
1929 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1930 writemask(tmp
, TGSI_WRITEMASK_YZ
),
1935 /* tmp.yw = tmp * src1
1937 if (tmp
.mask
& TGSI_WRITEMASK_YW
) {
1938 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
1939 writemask(tmp
, TGSI_WRITEMASK_YW
),
1948 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1959 static boolean
emit_exp(struct svga_shader_emitter
*emit
,
1960 const struct tgsi_full_instruction
*insn
)
1962 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1963 struct src_register src0
=
1964 translate_src_register( emit
, &insn
->Src
[0] );
1965 struct src_register zero
= get_zero_immediate( emit
);
1966 SVGA3dShaderDestToken fraction
;
1968 if (dst
.mask
& TGSI_WRITEMASK_Y
)
1970 else if (dst
.mask
& TGSI_WRITEMASK_X
)
1971 fraction
= get_temp( emit
);
1975 /* If y is being written, fill it with src0 - floor(src0).
1977 if (dst
.mask
& TGSI_WRITEMASK_XY
) {
1978 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
),
1979 writemask( fraction
, TGSI_WRITEMASK_Y
),
1984 /* If x is being written, fill it with 2 ^ floor(src0).
1986 if (dst
.mask
& TGSI_WRITEMASK_X
) {
1987 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
),
1988 writemask( dst
, TGSI_WRITEMASK_X
),
1990 scalar( negate( src( fraction
) ), TGSI_SWIZZLE_Y
) ) )
1993 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXP
),
1994 writemask( dst
, TGSI_WRITEMASK_X
),
1995 scalar( src( dst
), TGSI_SWIZZLE_X
) ) )
1998 if (!(dst
.mask
& TGSI_WRITEMASK_Y
))
1999 release_temp( emit
, fraction
);
2002 /* If z is being written, fill it with 2 ^ src0 (partial precision).
2004 if (dst
.mask
& TGSI_WRITEMASK_Z
) {
2005 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXPP
),
2006 writemask( dst
, TGSI_WRITEMASK_Z
),
2011 /* If w is being written, fill it with one.
2013 if (dst
.mask
& TGSI_WRITEMASK_W
) {
2014 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2015 writemask(dst
, TGSI_WRITEMASK_W
),
2016 scalar( zero
, TGSI_SWIZZLE_W
) ))
2023 static boolean
emit_lit(struct svga_shader_emitter
*emit
,
2024 const struct tgsi_full_instruction
*insn
)
2026 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2027 /* SVGA/DX9 has a LIT instruction, but only for vertex shaders:
2029 return emit_simple_instruction(emit
, SVGA3DOP_LIT
, insn
);
2033 /* D3D vs. GL semantics can be fairly easily accomodated by
2034 * variations on this sequence.
2038 * tmp.z = pow(src.y,src.w)
2039 * p0 = src0.xxxx > 0
2040 * result = zero.wxxw
2041 * (p0) result.yz = tmp
2045 * tmp.z = pow(src.y,src.w)
2046 * p0 = src0.xxyy > 0
2047 * result = zero.wxxw
2048 * (p0) result.yz = tmp
2050 * Will implement the GL version for now.
2053 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2054 SVGA3dShaderDestToken tmp
= get_temp( emit
);
2055 const struct src_register src0
= translate_src_register(
2056 emit
, &insn
->Src
[0] );
2057 struct src_register zero
= get_zero_immediate( emit
);
2059 /* tmp = pow(src.y, src.w)
2061 if (dst
.mask
& TGSI_WRITEMASK_Z
) {
2062 if (!submit_op2(emit
, inst_token( SVGA3DOP_POW
),
2071 if (dst
.mask
& TGSI_WRITEMASK_Y
) {
2072 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2073 writemask(tmp
, TGSI_WRITEMASK_Y
),
2078 /* Can't quite do this with emit conditional due to the extra
2079 * writemask on the predicated mov:
2082 SVGA3dShaderDestToken pred_reg
= dst_register( SVGA3DREG_PREDICATE
, 0 );
2083 SVGA3dShaderInstToken setp_token
, mov_token
;
2084 struct src_register predsrc
;
2086 setp_token
= inst_token( SVGA3DOP_SETP
);
2087 mov_token
= inst_token( SVGA3DOP_MOV
);
2089 setp_token
.control
= SVGA3DOPCOMP_GT
;
2091 /* D3D vs GL semantics:
2094 predsrc
= swizzle(src0
, 0, 0, 1, 1); /* D3D */
2096 predsrc
= swizzle(src0
, 0, 0, 0, 0); /* GL */
2098 /* SETP src0.xxyy, GT, {0}.x */
2099 if (!submit_op2( emit
, setp_token
, pred_reg
,
2101 swizzle(zero
, 0, 0, 0, 0) ))
2105 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
,
2106 swizzle(zero
, 3, 0, 0, 3 )))
2109 /* MOV dst.yz, tmp (predicated)
2111 * Note that the predicate reg (and possible modifiers) is passed
2112 * as the first source argument.
2114 if (dst
.mask
& TGSI_WRITEMASK_YZ
) {
2115 mov_token
.predicated
= 1;
2116 if (!submit_op2( emit
, mov_token
,
2117 writemask(dst
, TGSI_WRITEMASK_YZ
),
2118 src( pred_reg
), src( tmp
) ))
2130 static boolean
emit_ex2( struct svga_shader_emitter
*emit
,
2131 const struct tgsi_full_instruction
*insn
)
2133 SVGA3dShaderInstToken inst
;
2134 SVGA3dShaderDestToken dst
;
2135 struct src_register src0
;
2137 inst
= inst_token( SVGA3DOP_EXP
);
2138 dst
= translate_dst_register( emit
, insn
, 0 );
2139 src0
= translate_src_register( emit
, &insn
->Src
[0] );
2140 src0
= scalar( src0
, TGSI_SWIZZLE_X
);
2142 if (dst
.mask
!= TGSI_WRITEMASK_XYZW
) {
2143 SVGA3dShaderDestToken tmp
= get_temp( emit
);
2145 if (!submit_op1( emit
, inst
, tmp
, src0
))
2148 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2150 scalar( src( tmp
), TGSI_SWIZZLE_X
) );
2153 return submit_op1( emit
, inst
, dst
, src0
);
2157 static boolean
emit_log(struct svga_shader_emitter
*emit
,
2158 const struct tgsi_full_instruction
*insn
)
2160 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2161 struct src_register src0
=
2162 translate_src_register( emit
, &insn
->Src
[0] );
2163 struct src_register zero
= get_zero_immediate( emit
);
2164 SVGA3dShaderDestToken abs_tmp
;
2165 struct src_register abs_src0
;
2166 SVGA3dShaderDestToken log2_abs
;
2170 if (dst
.mask
& TGSI_WRITEMASK_Z
)
2172 else if (dst
.mask
& TGSI_WRITEMASK_XY
)
2173 log2_abs
= get_temp( emit
);
2177 /* If z is being written, fill it with log2( abs( src0 ) ).
2179 if (dst
.mask
& TGSI_WRITEMASK_XYZ
) {
2180 if (!src0
.base
.srcMod
|| src0
.base
.srcMod
== SVGA3DSRCMOD_ABS
)
2183 abs_tmp
= get_temp( emit
);
2185 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2190 abs_src0
= src( abs_tmp
);
2193 abs_src0
= absolute( scalar( abs_src0
, TGSI_SWIZZLE_X
) );
2195 if (!submit_op1( emit
, inst_token( SVGA3DOP_LOG
),
2196 writemask( log2_abs
, TGSI_WRITEMASK_Z
),
2201 if (dst
.mask
& TGSI_WRITEMASK_XY
) {
2202 SVGA3dShaderDestToken floor_log2
;
2204 if (dst
.mask
& TGSI_WRITEMASK_X
)
2207 floor_log2
= get_temp( emit
);
2209 /* If x is being written, fill it with floor( log2( abs( src0 ) ) ).
2211 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
),
2212 writemask( floor_log2
, TGSI_WRITEMASK_X
),
2213 scalar( src( log2_abs
), TGSI_SWIZZLE_Z
) ) )
2216 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
),
2217 writemask( floor_log2
, TGSI_WRITEMASK_X
),
2218 scalar( src( log2_abs
), TGSI_SWIZZLE_Z
),
2219 negate( src( floor_log2
) ) ) )
2222 /* If y is being written, fill it with
2223 * abs ( src0 ) / ( 2 ^ floor( log2( abs( src0 ) ) ) ).
2225 if (dst
.mask
& TGSI_WRITEMASK_Y
) {
2226 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXP
),
2227 writemask( dst
, TGSI_WRITEMASK_Y
),
2228 negate( scalar( src( floor_log2
),
2229 TGSI_SWIZZLE_X
) ) ) )
2232 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
2233 writemask( dst
, TGSI_WRITEMASK_Y
),
2239 if (!(dst
.mask
& TGSI_WRITEMASK_X
))
2240 release_temp( emit
, floor_log2
);
2242 if (!(dst
.mask
& TGSI_WRITEMASK_Z
))
2243 release_temp( emit
, log2_abs
);
2246 if (dst
.mask
& TGSI_WRITEMASK_XYZ
&& src0
.base
.srcMod
&&
2247 src0
.base
.srcMod
!= SVGA3DSRCMOD_ABS
)
2248 release_temp( emit
, abs_tmp
);
2250 /* If w is being written, fill it with one.
2252 if (dst
.mask
& TGSI_WRITEMASK_W
) {
2253 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2254 writemask(dst
, TGSI_WRITEMASK_W
),
2255 scalar( zero
, TGSI_SWIZZLE_W
) ))
2263 static boolean
emit_bgnsub( struct svga_shader_emitter
*emit
,
2265 const struct tgsi_full_instruction
*insn
)
2269 /* Note that we've finished the main function and are now emitting
2270 * subroutines. This affects how we terminate the generated
2273 emit
->in_main_func
= FALSE
;
2275 for (i
= 0; i
< emit
->nr_labels
; i
++) {
2276 if (emit
->label
[i
] == position
) {
2277 return (emit_instruction( emit
, inst_token( SVGA3DOP_RET
) ) &&
2278 emit_instruction( emit
, inst_token( SVGA3DOP_LABEL
) ) &&
2279 emit_src( emit
, src_register( SVGA3DREG_LABEL
, i
)));
2287 static boolean
emit_call( struct svga_shader_emitter
*emit
,
2288 const struct tgsi_full_instruction
*insn
)
2290 unsigned position
= insn
->Label
.Label
;
2293 for (i
= 0; i
< emit
->nr_labels
; i
++) {
2294 if (emit
->label
[i
] == position
)
2298 if (emit
->nr_labels
== Elements(emit
->label
))
2301 if (i
== emit
->nr_labels
) {
2302 emit
->label
[i
] = position
;
2306 return (emit_instruction( emit
, inst_token( SVGA3DOP_CALL
) ) &&
2307 emit_src( emit
, src_register( SVGA3DREG_LABEL
, i
)));
2311 static boolean
emit_end( struct svga_shader_emitter
*emit
)
2313 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2314 return emit_vs_postamble( emit
);
2317 return emit_ps_postamble( emit
);
2323 static boolean
svga_emit_instruction( struct svga_shader_emitter
*emit
,
2325 const struct tgsi_full_instruction
*insn
)
2327 switch (insn
->Instruction
.Opcode
) {
2329 case TGSI_OPCODE_ARL
:
2330 return emit_arl( emit
, insn
);
2332 case TGSI_OPCODE_TEX
:
2333 case TGSI_OPCODE_TXB
:
2334 case TGSI_OPCODE_TXP
:
2335 case TGSI_OPCODE_TXL
:
2336 case TGSI_OPCODE_TXD
:
2337 return emit_tex( emit
, insn
);
2339 case TGSI_OPCODE_DDX
:
2340 case TGSI_OPCODE_DDY
:
2341 return emit_deriv( emit
, insn
);
2343 case TGSI_OPCODE_BGNSUB
:
2344 return emit_bgnsub( emit
, position
, insn
);
2346 case TGSI_OPCODE_ENDSUB
:
2349 case TGSI_OPCODE_CAL
:
2350 return emit_call( emit
, insn
);
2352 case TGSI_OPCODE_FLR
:
2353 case TGSI_OPCODE_TRUNC
: /* should be TRUNC, not FLR */
2354 return emit_floor( emit
, insn
);
2356 case TGSI_OPCODE_CMP
:
2357 return emit_cmp( emit
, insn
);
2359 case TGSI_OPCODE_DIV
:
2360 return emit_div( emit
, insn
);
2362 case TGSI_OPCODE_DP2
:
2363 return emit_dp2( emit
, insn
);
2365 case TGSI_OPCODE_DPH
:
2366 return emit_dph( emit
, insn
);
2368 case TGSI_OPCODE_NRM
:
2369 return emit_nrm( emit
, insn
);
2371 case TGSI_OPCODE_COS
:
2372 return emit_cos( emit
, insn
);
2374 case TGSI_OPCODE_SIN
:
2375 return emit_sin( emit
, insn
);
2377 case TGSI_OPCODE_SCS
:
2378 return emit_sincos( emit
, insn
);
2380 case TGSI_OPCODE_END
:
2381 /* TGSI always finishes the main func with an END */
2382 return emit_end( emit
);
2384 case TGSI_OPCODE_KIL
:
2385 return emit_kil( emit
, insn
);
2387 /* Selection opcodes. The underlying language is fairly
2388 * non-orthogonal about these.
2390 case TGSI_OPCODE_SEQ
:
2391 return emit_select_op( emit
, PIPE_FUNC_EQUAL
, insn
);
2393 case TGSI_OPCODE_SNE
:
2394 return emit_select_op( emit
, PIPE_FUNC_NOTEQUAL
, insn
);
2396 case TGSI_OPCODE_SGT
:
2397 return emit_select_op( emit
, PIPE_FUNC_GREATER
, insn
);
2399 case TGSI_OPCODE_SGE
:
2400 return emit_select_op( emit
, PIPE_FUNC_GEQUAL
, insn
);
2402 case TGSI_OPCODE_SLT
:
2403 return emit_select_op( emit
, PIPE_FUNC_LESS
, insn
);
2405 case TGSI_OPCODE_SLE
:
2406 return emit_select_op( emit
, PIPE_FUNC_LEQUAL
, insn
);
2408 case TGSI_OPCODE_SUB
:
2409 return emit_sub( emit
, insn
);
2411 case TGSI_OPCODE_POW
:
2412 return emit_pow( emit
, insn
);
2414 case TGSI_OPCODE_EX2
:
2415 return emit_ex2( emit
, insn
);
2417 case TGSI_OPCODE_EXP
:
2418 return emit_exp( emit
, insn
);
2420 case TGSI_OPCODE_LOG
:
2421 return emit_log( emit
, insn
);
2423 case TGSI_OPCODE_LG2
:
2424 return emit_scalar_op1( emit
, SVGA3DOP_LOG
, insn
);
2426 case TGSI_OPCODE_RSQ
:
2427 return emit_scalar_op1( emit
, SVGA3DOP_RSQ
, insn
);
2429 case TGSI_OPCODE_RCP
:
2430 return emit_scalar_op1( emit
, SVGA3DOP_RCP
, insn
);
2432 case TGSI_OPCODE_CONT
:
2433 case TGSI_OPCODE_RET
:
2434 /* This is a noop -- we tell mesa that we can't support RET
2435 * within a function (early return), so this will always be
2436 * followed by an ENDSUB.
2440 /* These aren't actually used by any of the frontends we care
2443 case TGSI_OPCODE_CLAMP
:
2444 case TGSI_OPCODE_ROUND
:
2445 case TGSI_OPCODE_AND
:
2446 case TGSI_OPCODE_OR
:
2447 case TGSI_OPCODE_I2F
:
2448 case TGSI_OPCODE_NOT
:
2449 case TGSI_OPCODE_SHL
:
2450 case TGSI_OPCODE_ISHR
:
2451 case TGSI_OPCODE_XOR
:
2454 case TGSI_OPCODE_IF
:
2455 return emit_if( emit
, insn
);
2456 case TGSI_OPCODE_ELSE
:
2457 return emit_else( emit
, insn
);
2458 case TGSI_OPCODE_ENDIF
:
2459 return emit_endif( emit
, insn
);
2461 case TGSI_OPCODE_BGNLOOP
:
2462 return emit_bgnloop2( emit
, insn
);
2463 case TGSI_OPCODE_ENDLOOP
:
2464 return emit_endloop2( emit
, insn
);
2465 case TGSI_OPCODE_BRK
:
2466 return emit_brk( emit
, insn
);
2468 case TGSI_OPCODE_XPD
:
2469 return emit_xpd( emit
, insn
);
2471 case TGSI_OPCODE_KILP
:
2472 return emit_kilp( emit
, insn
);
2474 case TGSI_OPCODE_DST
:
2475 return emit_dst_insn( emit
, insn
);
2477 case TGSI_OPCODE_LIT
:
2478 return emit_lit( emit
, insn
);
2480 case TGSI_OPCODE_LRP
:
2481 return emit_lrp( emit
, insn
);
2483 case TGSI_OPCODE_SSG
:
2484 return emit_ssg( emit
, insn
);
2487 unsigned opcode
= translate_opcode(insn
->Instruction
.Opcode
);
2489 if (opcode
== SVGA3DOP_LAST_INST
)
2492 if (!emit_simple_instruction( emit
, opcode
, insn
))
2501 static boolean
svga_emit_immediate( struct svga_shader_emitter
*emit
,
2502 struct tgsi_full_immediate
*imm
)
2504 static const float id
[4] = {0,0,0,1};
2508 assert(1 <= imm
->Immediate
.NrTokens
&& imm
->Immediate
.NrTokens
<= 5);
2509 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++)
2510 value
[i
] = imm
->u
[i
].Float
;
2512 for ( ; i
< 4; i
++ )
2515 return emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
2516 emit
->imm_start
+ emit
->internal_imm_count
++,
2517 value
[0], value
[1], value
[2], value
[3]);
2520 static boolean
make_immediate( struct svga_shader_emitter
*emit
,
2525 struct src_register
*out
)
2527 unsigned idx
= emit
->nr_hw_float_const
++;
2529 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
2533 *out
= src_register( SVGA3DREG_CONST
, idx
);
2538 static boolean
emit_vs_preamble( struct svga_shader_emitter
*emit
)
2540 if (!emit
->key
.vkey
.need_prescale
) {
2541 if (!make_immediate( emit
, 0, 0, .5, .5,
2549 static boolean
emit_ps_preamble( struct svga_shader_emitter
*emit
)
2553 /* For SM20, need to initialize the temporaries we're using to hold
2554 * color outputs to some value. Shaders which don't set all of
2555 * these values are likely to be rejected by the DX9 runtime.
2557 if (!emit
->use_sm30
) {
2558 struct src_register zero
= get_zero_immediate( emit
);
2559 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
2560 if (SVGA3dShaderGetRegType(emit
->true_col
[i
].value
) != 0) {
2562 if (!submit_op1( emit
,
2563 inst_token(SVGA3DOP_MOV
),
2569 } else if (emit
->ps_reads_pos
&& emit
->info
.reads_z
) {
2571 * Assemble the position from various bits of inputs. Depth and W are
2572 * passed in a texcoord this is due to D3D's vPos not hold Z or W.
2573 * Also fixup the perspective interpolation.
2575 * temp_pos.xy = vPos.xy
2576 * temp_pos.w = rcp(texcoord1.w);
2577 * temp_pos.z = texcoord1.z * temp_pos.w;
2579 if (!submit_op1( emit
,
2580 inst_token(SVGA3DOP_MOV
),
2581 writemask( emit
->ps_temp_pos
, TGSI_WRITEMASK_XY
),
2582 emit
->ps_true_pos
))
2585 if (!submit_op1( emit
,
2586 inst_token(SVGA3DOP_RCP
),
2587 writemask( emit
->ps_temp_pos
, TGSI_WRITEMASK_W
),
2588 scalar( emit
->ps_depth_pos
, TGSI_SWIZZLE_W
) ))
2591 if (!submit_op2( emit
,
2592 inst_token(SVGA3DOP_MUL
),
2593 writemask( emit
->ps_temp_pos
, TGSI_WRITEMASK_Z
),
2594 scalar( emit
->ps_depth_pos
, TGSI_SWIZZLE_Z
),
2595 scalar( src(emit
->ps_temp_pos
), TGSI_SWIZZLE_W
) ))
2602 static boolean
emit_ps_postamble( struct svga_shader_emitter
*emit
)
2606 /* PS oDepth is incredibly fragile and it's very hard to catch the
2607 * types of usage that break it during shader emit. Easier just to
2608 * redirect the main program to a temporary and then only touch
2609 * oDepth with a hand-crafted MOV below.
2611 if (SVGA3dShaderGetRegType(emit
->true_pos
.value
) != 0) {
2613 if (!submit_op1( emit
,
2614 inst_token(SVGA3DOP_MOV
),
2616 scalar(src(emit
->temp_pos
), TGSI_SWIZZLE_Z
) ))
2620 /* Similarly for SM20 color outputs... Luckily SM30 isn't so
2623 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
2624 if (SVGA3dShaderGetRegType(emit
->true_col
[i
].value
) != 0) {
2626 /* Potentially override output colors with white for XOR
2627 * logicop workaround.
2629 if (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
2630 emit
->key
.fkey
.white_fragments
) {
2632 struct src_register one
= scalar( get_zero_immediate( emit
),
2635 if (!submit_op1( emit
,
2636 inst_token(SVGA3DOP_MOV
),
2642 if (!submit_op1( emit
,
2643 inst_token(SVGA3DOP_MOV
),
2645 src(emit
->temp_col
[i
]) ))
2654 static boolean
emit_vs_postamble( struct svga_shader_emitter
*emit
)
2656 /* PSIZ output is incredibly fragile and it's very hard to catch
2657 * the types of usage that break it during shader emit. Easier
2658 * just to redirect the main program to a temporary and then only
2659 * touch PSIZ with a hand-crafted MOV below.
2661 if (SVGA3dShaderGetRegType(emit
->true_psiz
.value
) != 0) {
2663 if (!submit_op1( emit
,
2664 inst_token(SVGA3DOP_MOV
),
2666 scalar(src(emit
->temp_psiz
), TGSI_SWIZZLE_X
) ))
2670 /* Need to perform various manipulations on vertex position to cope
2671 * with the different GL and D3D clip spaces.
2673 if (emit
->key
.vkey
.need_prescale
) {
2674 SVGA3dShaderDestToken temp_pos
= emit
->temp_pos
;
2675 SVGA3dShaderDestToken depth
= emit
->depth_pos
;
2676 SVGA3dShaderDestToken pos
= emit
->true_pos
;
2677 unsigned offset
= emit
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
2678 struct src_register prescale_scale
= src_register( SVGA3DREG_CONST
,
2680 struct src_register prescale_trans
= src_register( SVGA3DREG_CONST
,
2683 if (!submit_op1( emit
,
2684 inst_token(SVGA3DOP_MOV
),
2685 writemask(depth
, TGSI_WRITEMASK_W
),
2686 scalar(src(temp_pos
), TGSI_SWIZZLE_W
) ))
2689 /* MUL temp_pos.xyz, temp_pos, prescale.scale
2690 * MAD result.position, temp_pos.wwww, prescale.trans, temp_pos
2691 * --> Note that prescale.trans.w == 0
2693 if (!submit_op2( emit
,
2694 inst_token(SVGA3DOP_MUL
),
2695 writemask(temp_pos
, TGSI_WRITEMASK_XYZ
),
2700 if (!submit_op3( emit
,
2701 inst_token(SVGA3DOP_MAD
),
2703 swizzle(src(temp_pos
), 3, 3, 3, 3),
2708 /* Also write to depth value */
2709 if (!submit_op3( emit
,
2710 inst_token(SVGA3DOP_MAD
),
2711 writemask(depth
, TGSI_WRITEMASK_Z
),
2712 swizzle(src(temp_pos
), 3, 3, 3, 3),
2718 SVGA3dShaderDestToken temp_pos
= emit
->temp_pos
;
2719 SVGA3dShaderDestToken depth
= emit
->depth_pos
;
2720 SVGA3dShaderDestToken pos
= emit
->true_pos
;
2721 struct src_register imm_0055
= emit
->imm_0055
;
2723 /* Adjust GL clipping coordinate space to hardware (D3D-style):
2725 * DP4 temp_pos.z, {0,0,.5,.5}, temp_pos
2726 * MOV result.position, temp_pos
2728 if (!submit_op2( emit
,
2729 inst_token(SVGA3DOP_DP4
),
2730 writemask(temp_pos
, TGSI_WRITEMASK_Z
),
2735 if (!submit_op1( emit
,
2736 inst_token(SVGA3DOP_MOV
),
2741 /* Move the manipulated depth into the extra texcoord reg */
2742 if (!submit_op1( emit
,
2743 inst_token(SVGA3DOP_MOV
),
2744 writemask(depth
, TGSI_WRITEMASK_ZW
),
2754 1: COLOR = FrontColor;
2756 3: COLOR = BackColor;
2759 static boolean
emit_light_twoside( struct svga_shader_emitter
*emit
)
2761 struct src_register vface
, zero
;
2762 struct src_register front
[2];
2763 struct src_register back
[2];
2764 SVGA3dShaderDestToken color
[2];
2765 int count
= emit
->internal_color_count
;
2767 SVGA3dShaderInstToken if_token
;
2772 vface
= get_vface( emit
);
2773 zero
= get_zero_immediate( emit
);
2775 /* Can't use get_temp() to allocate the color reg as such
2776 * temporaries will be reclaimed after each instruction by the call
2777 * to reset_temp_regs().
2779 for (i
= 0; i
< count
; i
++) {
2780 color
[i
] = dst_register( SVGA3DREG_TEMP
,
2781 emit
->nr_hw_temp
++ );
2783 front
[i
] = emit
->input_map
[emit
->internal_color_idx
[i
]];
2785 /* Back is always the next input:
2788 back
[i
].base
.num
= front
[i
].base
.num
+ 1;
2790 /* Reassign the input_map to the actual front-face color:
2792 emit
->input_map
[emit
->internal_color_idx
[i
]] = src(color
[i
]);
2795 if_token
= inst_token( SVGA3DOP_IFC
);
2797 if (emit
->key
.fkey
.front_ccw
)
2798 if_token
.control
= SVGA3DOPCOMP_LT
;
2800 if_token
.control
= SVGA3DOPCOMP_GT
;
2802 zero
= scalar(zero
, TGSI_SWIZZLE_X
);
2804 if (!(emit_instruction( emit
, if_token
) &&
2805 emit_src( emit
, vface
) &&
2806 emit_src( emit
, zero
) ))
2809 for (i
= 0; i
< count
; i
++) {
2810 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), color
[i
], front
[i
] ))
2814 if (!(emit_instruction( emit
, inst_token( SVGA3DOP_ELSE
))))
2817 for (i
= 0; i
< count
; i
++) {
2818 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), color
[i
], back
[i
] ))
2822 if (!emit_instruction( emit
, inst_token( SVGA3DOP_ENDIF
) ))
2829 0: SETP_GT TEMP, VFACE, 0
2830 where TEMP is a fake frontface register
2832 static boolean
emit_frontface( struct svga_shader_emitter
*emit
)
2834 struct src_register vface
, zero
;
2835 SVGA3dShaderDestToken temp
;
2836 struct src_register pass
, fail
;
2838 vface
= get_vface( emit
);
2839 zero
= get_zero_immediate( emit
);
2841 /* Can't use get_temp() to allocate the fake frontface reg as such
2842 * temporaries will be reclaimed after each instruction by the call
2843 * to reset_temp_regs().
2845 temp
= dst_register( SVGA3DREG_TEMP
,
2846 emit
->nr_hw_temp
++ );
2848 if (emit
->key
.fkey
.front_ccw
) {
2849 pass
= scalar( zero
, TGSI_SWIZZLE_X
);
2850 fail
= scalar( zero
, TGSI_SWIZZLE_W
);
2852 pass
= scalar( zero
, TGSI_SWIZZLE_W
);
2853 fail
= scalar( zero
, TGSI_SWIZZLE_X
);
2856 if (!emit_conditional(emit
, PIPE_FUNC_GREATER
,
2857 temp
, vface
, scalar( zero
, TGSI_SWIZZLE_X
),
2861 /* Reassign the input_map to the actual front-face color:
2863 emit
->input_map
[emit
->internal_frontface_idx
] = src(temp
);
2870 * Emit code to invert the T component of the incoming texture coordinate.
2871 * This is used for drawing point sprites when
2872 * pipe_rasterizer_state::sprite_coord_mode == PIPE_SPRITE_COORD_LOWER_LEFT.
2874 static boolean
emit_inverted_texcoords( struct svga_shader_emitter
*emit
)
2876 struct src_register zero
= get_zero_immediate(emit
);
2877 struct src_register pos_neg_one
= get_pos_neg_one_immediate( emit
);
2878 unsigned inverted_texcoords
= emit
->inverted_texcoords
;
2880 while (inverted_texcoords
) {
2881 const unsigned unit
= ffs(inverted_texcoords
) - 1;
2883 assert(emit
->inverted_texcoords
& (1 << unit
));
2885 assert(unit
< Elements(emit
->ps_true_texcoord
));
2887 assert(unit
< Elements(emit
->ps_inverted_texcoord_input
));
2889 assert(emit
->ps_inverted_texcoord_input
[unit
]
2890 < Elements(emit
->input_map
));
2892 /* inverted = coord * (1, -1, 1, 1) + (0, 1, 0, 0) */
2893 if (!submit_op3(emit
,
2894 inst_token(SVGA3DOP_MAD
),
2895 dst(emit
->ps_inverted_texcoord
[unit
]),
2896 emit
->ps_true_texcoord
[unit
],
2897 swizzle(pos_neg_one
, 0, 3, 0, 0), /* (1, -1, 1, 1) */
2898 swizzle(zero
, 0, 3, 0, 0))) /* (0, 1, 0, 0) */
2901 /* Reassign the input_map entry to the new texcoord register */
2902 emit
->input_map
[emit
->ps_inverted_texcoord_input
[unit
]] =
2903 emit
->ps_inverted_texcoord
[unit
];
2905 inverted_texcoords
&= ~(1 << unit
);
2912 static INLINE boolean
2913 needs_to_create_zero( struct svga_shader_emitter
*emit
)
2917 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
2918 if (!emit
->use_sm30
)
2921 if (emit
->key
.fkey
.light_twoside
)
2924 if (emit
->key
.fkey
.white_fragments
)
2927 if (emit
->emit_frontface
)
2930 if (emit
->info
.opcode_count
[TGSI_OPCODE_DST
] >= 1 ||
2931 emit
->info
.opcode_count
[TGSI_OPCODE_SSG
] >= 1 ||
2932 emit
->info
.opcode_count
[TGSI_OPCODE_LIT
] >= 1)
2935 if (emit
->inverted_texcoords
)
2939 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2940 if (emit
->info
.opcode_count
[TGSI_OPCODE_CMP
] >= 1)
2944 if (emit
->info
.opcode_count
[TGSI_OPCODE_IF
] >= 1 ||
2945 emit
->info
.opcode_count
[TGSI_OPCODE_BGNLOOP
] >= 1 ||
2946 emit
->info
.opcode_count
[TGSI_OPCODE_DDX
] >= 1 ||
2947 emit
->info
.opcode_count
[TGSI_OPCODE_DDY
] >= 1 ||
2948 emit
->info
.opcode_count
[TGSI_OPCODE_SGE
] >= 1 ||
2949 emit
->info
.opcode_count
[TGSI_OPCODE_SGT
] >= 1 ||
2950 emit
->info
.opcode_count
[TGSI_OPCODE_SLE
] >= 1 ||
2951 emit
->info
.opcode_count
[TGSI_OPCODE_SLT
] >= 1 ||
2952 emit
->info
.opcode_count
[TGSI_OPCODE_SNE
] >= 1 ||
2953 emit
->info
.opcode_count
[TGSI_OPCODE_SEQ
] >= 1 ||
2954 emit
->info
.opcode_count
[TGSI_OPCODE_EXP
] >= 1 ||
2955 emit
->info
.opcode_count
[TGSI_OPCODE_LOG
] >= 1 ||
2956 emit
->info
.opcode_count
[TGSI_OPCODE_XPD
] >= 1 ||
2957 emit
->info
.opcode_count
[TGSI_OPCODE_KILP
] >= 1)
2960 for (i
= 0; i
< emit
->key
.fkey
.num_textures
; i
++) {
2961 if (emit
->key
.fkey
.tex
[i
].compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
)
2968 static INLINE boolean
2969 needs_to_create_loop_const( struct svga_shader_emitter
*emit
)
2971 return (emit
->info
.opcode_count
[TGSI_OPCODE_BGNLOOP
] >= 1);
2974 static INLINE boolean
2975 needs_to_create_sincos_consts( struct svga_shader_emitter
*emit
)
2977 return !emit
->use_sm30
&& (emit
->info
.opcode_count
[TGSI_OPCODE_SIN
] >= 1 ||
2978 emit
->info
.opcode_count
[TGSI_OPCODE_COS
] >= 1 ||
2979 emit
->info
.opcode_count
[TGSI_OPCODE_SCS
] >= 1);
2982 static INLINE boolean
2983 needs_to_create_arl_consts( struct svga_shader_emitter
*emit
)
2985 return (emit
->num_arl_consts
> 0);
2988 static INLINE boolean
2989 pre_parse_add_indirect( struct svga_shader_emitter
*emit
,
2990 int num
, int current_arl
)
2995 for (i
= 0; i
< emit
->num_arl_consts
; ++i
) {
2996 if (emit
->arl_consts
[i
].arl_num
== current_arl
)
3000 if (emit
->num_arl_consts
== i
) {
3001 ++emit
->num_arl_consts
;
3003 emit
->arl_consts
[i
].number
= (emit
->arl_consts
[i
].number
> num
) ?
3005 emit
->arl_consts
[i
].number
;
3006 emit
->arl_consts
[i
].arl_num
= current_arl
;
3011 pre_parse_instruction( struct svga_shader_emitter
*emit
,
3012 const struct tgsi_full_instruction
*insn
,
3015 if (insn
->Src
[0].Register
.Indirect
&&
3016 insn
->Src
[0].Indirect
.File
== TGSI_FILE_ADDRESS
) {
3017 const struct tgsi_full_src_register
*reg
= &insn
->Src
[0];
3018 if (reg
->Register
.Index
< 0) {
3019 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
3023 if (insn
->Src
[1].Register
.Indirect
&&
3024 insn
->Src
[1].Indirect
.File
== TGSI_FILE_ADDRESS
) {
3025 const struct tgsi_full_src_register
*reg
= &insn
->Src
[1];
3026 if (reg
->Register
.Index
< 0) {
3027 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
3031 if (insn
->Src
[2].Register
.Indirect
&&
3032 insn
->Src
[2].Indirect
.File
== TGSI_FILE_ADDRESS
) {
3033 const struct tgsi_full_src_register
*reg
= &insn
->Src
[2];
3034 if (reg
->Register
.Index
< 0) {
3035 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
3043 pre_parse_tokens( struct svga_shader_emitter
*emit
,
3044 const struct tgsi_token
*tokens
)
3046 struct tgsi_parse_context parse
;
3047 int current_arl
= 0;
3049 tgsi_parse_init( &parse
, tokens
);
3051 while (!tgsi_parse_end_of_tokens( &parse
)) {
3052 tgsi_parse_token( &parse
);
3053 switch (parse
.FullToken
.Token
.Type
) {
3054 case TGSI_TOKEN_TYPE_IMMEDIATE
:
3055 case TGSI_TOKEN_TYPE_DECLARATION
:
3057 case TGSI_TOKEN_TYPE_INSTRUCTION
:
3058 if (parse
.FullToken
.FullInstruction
.Instruction
.Opcode
==
3062 if (!pre_parse_instruction( emit
, &parse
.FullToken
.FullInstruction
,
3074 static boolean
svga_shader_emit_helpers( struct svga_shader_emitter
*emit
)
3077 if (needs_to_create_zero( emit
)) {
3078 create_zero_immediate( emit
);
3080 if (needs_to_create_loop_const( emit
)) {
3081 create_loop_const( emit
);
3083 if (needs_to_create_sincos_consts( emit
)) {
3084 create_sincos_consts( emit
);
3086 if (needs_to_create_arl_consts( emit
)) {
3087 create_arl_consts( emit
);
3090 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
3091 if (!emit_ps_preamble( emit
))
3094 if (emit
->key
.fkey
.light_twoside
) {
3095 if (!emit_light_twoside( emit
))
3098 if (emit
->emit_frontface
) {
3099 if (!emit_frontface( emit
))
3102 if (emit
->inverted_texcoords
) {
3103 if (!emit_inverted_texcoords( emit
))
3111 boolean
svga_shader_emit_instructions( struct svga_shader_emitter
*emit
,
3112 const struct tgsi_token
*tokens
)
3114 struct tgsi_parse_context parse
;
3116 boolean helpers_emitted
= FALSE
;
3117 unsigned line_nr
= 0;
3119 tgsi_parse_init( &parse
, tokens
);
3120 emit
->internal_imm_count
= 0;
3122 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
3123 ret
= emit_vs_preamble( emit
);
3128 pre_parse_tokens(emit
, tokens
);
3130 while (!tgsi_parse_end_of_tokens( &parse
)) {
3131 tgsi_parse_token( &parse
);
3133 switch (parse
.FullToken
.Token
.Type
) {
3134 case TGSI_TOKEN_TYPE_IMMEDIATE
:
3135 ret
= svga_emit_immediate( emit
, &parse
.FullToken
.FullImmediate
);
3140 case TGSI_TOKEN_TYPE_DECLARATION
:
3142 ret
= svga_translate_decl_sm30( emit
, &parse
.FullToken
.FullDeclaration
);
3144 ret
= svga_translate_decl_sm20( emit
, &parse
.FullToken
.FullDeclaration
);
3149 case TGSI_TOKEN_TYPE_INSTRUCTION
:
3150 if (!helpers_emitted
) {
3151 if (!svga_shader_emit_helpers( emit
))
3153 helpers_emitted
= TRUE
;
3155 ret
= svga_emit_instruction( emit
,
3157 &parse
.FullToken
.FullInstruction
);
3165 reset_temp_regs( emit
);
3168 /* Need to terminate the current subroutine. Note that the
3169 * hardware doesn't tolerate shaders without sub-routines
3170 * terminating with RET+END.
3172 if (!emit
->in_main_func
) {
3173 ret
= emit_instruction( emit
, inst_token( SVGA3DOP_RET
) );
3178 assert(emit
->dynamic_branching_level
== 0);
3180 /* Need to terminate the whole shader:
3182 ret
= emit_instruction( emit
, inst_token( SVGA3DOP_END
) );
3187 tgsi_parse_free( &parse
);