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_dump.h"
29 #include "tgsi/tgsi_parse.h"
30 #include "util/u_memory.h"
31 #include "util/u_math.h"
33 #include "svga_tgsi_emit.h"
34 #include "svga_context.h"
37 static boolean
emit_vs_postamble( struct svga_shader_emitter
*emit
);
38 static boolean
emit_ps_postamble( struct svga_shader_emitter
*emit
);
42 translate_opcode(uint opcode
)
45 case TGSI_OPCODE_ABS
: return SVGA3DOP_ABS
;
46 case TGSI_OPCODE_ADD
: return SVGA3DOP_ADD
;
47 case TGSI_OPCODE_DP2A
: return SVGA3DOP_DP2ADD
;
48 case TGSI_OPCODE_DP3
: return SVGA3DOP_DP3
;
49 case TGSI_OPCODE_DP4
: return SVGA3DOP_DP4
;
50 case TGSI_OPCODE_FRC
: return SVGA3DOP_FRC
;
51 case TGSI_OPCODE_MAD
: return SVGA3DOP_MAD
;
52 case TGSI_OPCODE_MAX
: return SVGA3DOP_MAX
;
53 case TGSI_OPCODE_MIN
: return SVGA3DOP_MIN
;
54 case TGSI_OPCODE_MOV
: return SVGA3DOP_MOV
;
55 case TGSI_OPCODE_MUL
: return SVGA3DOP_MUL
;
56 case TGSI_OPCODE_NOP
: return SVGA3DOP_NOP
;
57 case TGSI_OPCODE_NRM4
: return SVGA3DOP_NRM
;
59 debug_printf("Unkown opcode %u\n", opcode
);
61 return SVGA3DOP_LAST_INST
;
67 translate_file(unsigned file
)
70 case TGSI_FILE_TEMPORARY
: return SVGA3DREG_TEMP
;
71 case TGSI_FILE_INPUT
: return SVGA3DREG_INPUT
;
72 case TGSI_FILE_OUTPUT
: return SVGA3DREG_OUTPUT
; /* VS3.0+ only */
73 case TGSI_FILE_IMMEDIATE
: return SVGA3DREG_CONST
;
74 case TGSI_FILE_CONSTANT
: return SVGA3DREG_CONST
;
75 case TGSI_FILE_SAMPLER
: return SVGA3DREG_SAMPLER
;
76 case TGSI_FILE_ADDRESS
: return SVGA3DREG_ADDR
;
79 return SVGA3DREG_TEMP
;
84 static SVGA3dShaderDestToken
85 translate_dst_register( struct svga_shader_emitter
*emit
,
86 const struct tgsi_full_instruction
*insn
,
89 const struct tgsi_full_dst_register
*reg
= &insn
->Dst
[idx
];
90 SVGA3dShaderDestToken dest
;
92 switch (reg
->Register
.File
) {
93 case TGSI_FILE_OUTPUT
:
94 /* Output registers encode semantic information in their name.
95 * Need to lookup a table built at decl time:
97 dest
= emit
->output_map
[reg
->Register
.Index
];
102 unsigned index
= reg
->Register
.Index
;
103 assert(index
< SVGA3D_TEMPREG_MAX
);
104 index
= MIN2(index
, SVGA3D_TEMPREG_MAX
- 1);
105 dest
= dst_register(translate_file(reg
->Register
.File
), index
);
110 dest
.mask
= reg
->Register
.WriteMask
;
113 if (insn
->Instruction
.Saturate
)
114 dest
.dstMod
= SVGA3DDSTMOD_SATURATE
;
120 static struct src_register
121 swizzle( struct src_register src
,
127 x
= (src
.base
.swizzle
>> (x
* 2)) & 0x3;
128 y
= (src
.base
.swizzle
>> (y
* 2)) & 0x3;
129 z
= (src
.base
.swizzle
>> (z
* 2)) & 0x3;
130 w
= (src
.base
.swizzle
>> (w
* 2)) & 0x3;
132 src
.base
.swizzle
= TRANSLATE_SWIZZLE(x
,y
,z
,w
);
138 static struct src_register
139 scalar( struct src_register src
,
142 return swizzle( src
, comp
, comp
, comp
, comp
);
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
)
160 svga_arl_adjustment( const struct svga_shader_emitter
*emit
)
164 for (i
= 0; i
< emit
->num_arl_consts
; ++i
) {
165 if (emit
->arl_consts
[i
].arl_num
== emit
->current_arl
)
166 return emit
->arl_consts
[i
].number
;
172 static struct src_register
173 translate_src_register( const struct svga_shader_emitter
*emit
,
174 const struct tgsi_full_src_register
*reg
)
176 struct src_register src
;
178 switch (reg
->Register
.File
) {
179 case TGSI_FILE_INPUT
:
180 /* Input registers are referred to by their semantic name rather
181 * than by index. Use the mapping build up from the decls:
183 src
= emit
->input_map
[reg
->Register
.Index
];
186 case TGSI_FILE_IMMEDIATE
:
187 /* Immediates are appended after TGSI constants in the D3D
190 src
= src_register( translate_file( reg
->Register
.File
),
191 reg
->Register
.Index
+ emit
->imm_start
);
195 src
= src_register( translate_file( reg
->Register
.File
),
196 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.
261 * Note: if we exceed the temporary register limit we just use
262 * register SVGA3D_TEMPREG_MAX - 1.
264 static SVGA3dShaderDestToken
265 get_temp( struct svga_shader_emitter
*emit
)
267 int i
= emit
->nr_hw_temp
+ emit
->internal_temp_count
++;
268 assert(i
< SVGA3D_TEMPREG_MAX
);
269 i
= MIN2(i
, SVGA3D_TEMPREG_MAX
- 1);
270 return dst_register( SVGA3DREG_TEMP
, i
);
275 * Release a single temp. Currently only effective if it was the last
276 * allocated temp, otherwise release will be delayed until the next
277 * call to reset_temp_regs().
280 release_temp( struct svga_shader_emitter
*emit
,
281 SVGA3dShaderDestToken temp
)
283 if (temp
.num
== emit
->internal_temp_count
- 1)
284 emit
->internal_temp_count
--;
289 reset_temp_regs(struct svga_shader_emitter
*emit
)
291 emit
->internal_temp_count
= 0;
295 /* Replace the src with the temporary specified in the dst, but copying
296 * only the necessary channels, and preserving the original swizzle (which is
297 * important given that several opcodes have constraints in the allowed
301 emit_repl(struct svga_shader_emitter
*emit
,
302 SVGA3dShaderDestToken dst
,
303 struct src_register
*src0
)
305 unsigned src0_swizzle
;
308 assert(SVGA3dShaderGetRegType(dst
.value
) == SVGA3DREG_TEMP
);
310 src0_swizzle
= src0
->base
.swizzle
;
313 for (chan
= 0; chan
< 4; ++chan
) {
314 unsigned swizzle
= (src0_swizzle
>> (chan
*2)) & 0x3;
315 dst
.mask
|= 1 << swizzle
;
319 src0
->base
.swizzle
= SVGA3DSWIZZLE_NONE
;
321 if (!emit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, *src0
))
325 src0
->base
.swizzle
= src0_swizzle
;
332 submit_op0(struct svga_shader_emitter
*emit
,
333 SVGA3dShaderInstToken inst
,
334 SVGA3dShaderDestToken dest
)
336 return (emit_instruction( emit
, inst
) &&
337 emit_dst( emit
, dest
));
342 submit_op1(struct svga_shader_emitter
*emit
,
343 SVGA3dShaderInstToken inst
,
344 SVGA3dShaderDestToken dest
,
345 struct src_register src0
)
347 return emit_op1( emit
, inst
, dest
, src0
);
352 * SVGA shaders may not refer to >1 constant register in a single
353 * instruction. This function checks for that usage and inserts a
354 * move to temporary if detected.
356 * The same applies to input registers -- at most a single input
357 * register may be read by any instruction.
360 submit_op2(struct svga_shader_emitter
*emit
,
361 SVGA3dShaderInstToken inst
,
362 SVGA3dShaderDestToken dest
,
363 struct src_register src0
,
364 struct src_register src1
)
366 SVGA3dShaderDestToken temp
;
367 SVGA3dShaderRegType type0
, type1
;
368 boolean need_temp
= FALSE
;
371 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
372 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
374 if (type0
== SVGA3DREG_CONST
&&
375 type1
== SVGA3DREG_CONST
&&
376 src0
.base
.num
!= src1
.base
.num
)
379 if (type0
== SVGA3DREG_INPUT
&&
380 type1
== SVGA3DREG_INPUT
&&
381 src0
.base
.num
!= src1
.base
.num
)
385 temp
= get_temp( emit
);
387 if (!emit_repl( emit
, temp
, &src0
))
391 if (!emit_op2( emit
, inst
, dest
, src0
, src1
))
395 release_temp( emit
, temp
);
402 * SVGA shaders may not refer to >1 constant register in a single
403 * instruction. This function checks for that usage and inserts a
404 * move to temporary if detected.
407 submit_op3(struct svga_shader_emitter
*emit
,
408 SVGA3dShaderInstToken inst
,
409 SVGA3dShaderDestToken dest
,
410 struct src_register src0
,
411 struct src_register src1
,
412 struct src_register src2
)
414 SVGA3dShaderDestToken temp0
;
415 SVGA3dShaderDestToken temp1
;
416 boolean need_temp0
= FALSE
;
417 boolean need_temp1
= FALSE
;
418 SVGA3dShaderRegType type0
, type1
, type2
;
422 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
423 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
424 type2
= SVGA3dShaderGetRegType( src2
.base
.value
);
426 if (inst
.op
!= SVGA3DOP_SINCOS
) {
427 if (type0
== SVGA3DREG_CONST
&&
428 ((type1
== SVGA3DREG_CONST
&& src0
.base
.num
!= src1
.base
.num
) ||
429 (type2
== SVGA3DREG_CONST
&& src0
.base
.num
!= src2
.base
.num
)))
432 if (type1
== SVGA3DREG_CONST
&&
433 (type2
== SVGA3DREG_CONST
&& src1
.base
.num
!= src2
.base
.num
))
437 if (type0
== SVGA3DREG_INPUT
&&
438 ((type1
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src1
.base
.num
) ||
439 (type2
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src2
.base
.num
)))
442 if (type1
== SVGA3DREG_INPUT
&&
443 (type2
== SVGA3DREG_INPUT
&& src1
.base
.num
!= src2
.base
.num
))
447 temp0
= get_temp( emit
);
449 if (!emit_repl( emit
, temp0
, &src0
))
454 temp1
= get_temp( emit
);
456 if (!emit_repl( emit
, temp1
, &src1
))
460 if (!emit_op3( emit
, inst
, dest
, src0
, src1
, src2
))
464 release_temp( emit
, temp1
);
466 release_temp( emit
, temp0
);
472 * SVGA shaders may not refer to >1 constant register in a single
473 * instruction. This function checks for that usage and inserts a
474 * move to temporary if detected.
477 submit_op4(struct svga_shader_emitter
*emit
,
478 SVGA3dShaderInstToken inst
,
479 SVGA3dShaderDestToken dest
,
480 struct src_register src0
,
481 struct src_register src1
,
482 struct src_register src2
,
483 struct src_register src3
)
485 SVGA3dShaderDestToken temp0
;
486 SVGA3dShaderDestToken temp3
;
487 boolean need_temp0
= FALSE
;
488 boolean need_temp3
= FALSE
;
489 SVGA3dShaderRegType type0
, type1
, type2
, type3
;
493 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
494 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
495 type2
= SVGA3dShaderGetRegType( src2
.base
.value
);
496 type3
= SVGA3dShaderGetRegType( src2
.base
.value
);
498 /* Make life a little easier - this is only used by the TXD
499 * instruction which is guaranteed not to have a constant/input reg
500 * in one slot at least:
502 assert(type1
== SVGA3DREG_SAMPLER
);
504 if (type0
== SVGA3DREG_CONST
&&
505 ((type3
== SVGA3DREG_CONST
&& src0
.base
.num
!= src3
.base
.num
) ||
506 (type2
== SVGA3DREG_CONST
&& src0
.base
.num
!= src2
.base
.num
)))
509 if (type3
== SVGA3DREG_CONST
&&
510 (type2
== SVGA3DREG_CONST
&& src3
.base
.num
!= src2
.base
.num
))
513 if (type0
== SVGA3DREG_INPUT
&&
514 ((type3
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src3
.base
.num
) ||
515 (type2
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src2
.base
.num
)))
518 if (type3
== SVGA3DREG_INPUT
&&
519 (type2
== SVGA3DREG_INPUT
&& src3
.base
.num
!= src2
.base
.num
))
523 temp0
= get_temp( emit
);
525 if (!emit_repl( emit
, temp0
, &src0
))
530 temp3
= get_temp( emit
);
532 if (!emit_repl( emit
, temp3
, &src3
))
536 if (!emit_op4( emit
, inst
, dest
, src0
, src1
, src2
, src3
))
540 release_temp( emit
, temp3
);
542 release_temp( emit
, temp0
);
548 * Do the src and dest registers refer to the same register?
551 alias_src_dst(struct src_register src
,
552 SVGA3dShaderDestToken dst
)
554 if (src
.base
.num
!= dst
.num
)
557 if (SVGA3dShaderGetRegType(dst
.value
) !=
558 SVGA3dShaderGetRegType(src
.base
.value
))
566 submit_lrp(struct svga_shader_emitter
*emit
,
567 SVGA3dShaderDestToken dst
,
568 struct src_register src0
,
569 struct src_register src1
,
570 struct src_register src2
)
572 SVGA3dShaderDestToken tmp
;
573 boolean need_dst_tmp
= FALSE
;
575 /* The dst reg must be a temporary, and not be the same as src0 or src2 */
576 if (SVGA3dShaderGetRegType(dst
.value
) != SVGA3DREG_TEMP
||
577 alias_src_dst(src0
, dst
) ||
578 alias_src_dst(src2
, dst
))
582 tmp
= get_temp( emit
);
589 if (!submit_op3(emit
, inst_token( SVGA3DOP_LRP
), tmp
, src0
, src1
, src2
))
593 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), dst
, src( tmp
)))
602 emit_def_const(struct svga_shader_emitter
*emit
,
603 SVGA3dShaderConstType type
,
604 unsigned idx
, float a
, float b
, float c
, float d
)
607 SVGA3dShaderInstToken opcode
;
610 case SVGA3D_CONST_TYPE_FLOAT
:
611 opcode
= inst_token( SVGA3DOP_DEF
);
612 def
.dst
= dst_register( SVGA3DREG_CONST
, idx
);
613 def
.constValues
[0] = a
;
614 def
.constValues
[1] = b
;
615 def
.constValues
[2] = c
;
616 def
.constValues
[3] = d
;
618 case SVGA3D_CONST_TYPE_INT
:
619 opcode
= inst_token( SVGA3DOP_DEFI
);
620 def
.dst
= dst_register( SVGA3DREG_CONSTINT
, idx
);
621 def
.constIValues
[0] = (int)a
;
622 def
.constIValues
[1] = (int)b
;
623 def
.constIValues
[2] = (int)c
;
624 def
.constIValues
[3] = (int)d
;
628 opcode
= inst_token( SVGA3DOP_NOP
);
632 if (!emit_instruction(emit
, opcode
) ||
633 !svga_shader_emit_dwords( emit
, def
.values
, Elements(def
.values
)))
641 create_zero_immediate( struct svga_shader_emitter
*emit
)
643 unsigned idx
= emit
->nr_hw_float_const
++;
645 /* Emit the constant (0, 0.5, -1, 1) and use swizzling to generate
646 * other useful vectors.
648 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
649 idx
, 0, 0.5, -1, 1 ))
652 emit
->zero_immediate_idx
= idx
;
653 emit
->created_zero_immediate
= TRUE
;
660 create_loop_const( struct svga_shader_emitter
*emit
)
662 unsigned idx
= emit
->nr_hw_int_const
++;
664 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_INT
, idx
,
665 255, /* iteration count */
666 0, /* initial value */
668 0 /* not used, must be 0 */))
671 emit
->loop_const_idx
= idx
;
672 emit
->created_loop_const
= TRUE
;
678 create_arl_consts( struct svga_shader_emitter
*emit
)
682 for (i
= 0; i
< emit
->num_arl_consts
; i
+= 4) {
684 unsigned idx
= emit
->nr_hw_float_const
++;
686 for (j
= 0; j
< 4 && (j
+ i
) < emit
->num_arl_consts
; ++j
) {
687 vals
[j
] = (float) emit
->arl_consts
[i
+ j
].number
;
688 emit
->arl_consts
[i
+ j
].idx
= idx
;
691 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_X
;
694 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_Y
;
697 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_Z
;
700 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_W
;
707 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
, idx
,
718 * Return the register which holds the pixel shaders front/back-
721 static struct src_register
722 get_vface( struct svga_shader_emitter
*emit
)
724 assert(emit
->emitted_vface
);
725 return src_register(SVGA3DREG_MISCTYPE
, SVGA3DMISCREG_FACE
);
730 * returns {0, 0, 0, 1} immediate
732 static struct src_register
733 get_zero_immediate( struct svga_shader_emitter
*emit
)
735 assert(emit
->created_zero_immediate
);
736 assert(emit
->zero_immediate_idx
>= 0);
737 return swizzle(src_register( SVGA3DREG_CONST
,
738 emit
->zero_immediate_idx
),
744 * returns {1, 1, 1, -1} immediate
746 static struct src_register
747 get_pos_neg_one_immediate( struct svga_shader_emitter
*emit
)
749 assert(emit
->created_zero_immediate
);
750 assert(emit
->zero_immediate_idx
>= 0);
751 return swizzle(src_register( SVGA3DREG_CONST
,
752 emit
->zero_immediate_idx
),
758 * returns {0.5, 0.5, 0.5, 0.5} immediate
760 static struct src_register
761 get_half_immediate( struct svga_shader_emitter
*emit
)
763 assert(emit
->created_zero_immediate
);
764 assert(emit
->zero_immediate_idx
>= 0);
765 return swizzle(src_register(SVGA3DREG_CONST
, emit
->zero_immediate_idx
),
771 * returns the loop const
773 static struct src_register
774 get_loop_const( struct svga_shader_emitter
*emit
)
776 assert(emit
->created_loop_const
);
777 assert(emit
->loop_const_idx
>= 0);
778 return src_register( SVGA3DREG_CONSTINT
,
779 emit
->loop_const_idx
);
783 static struct src_register
784 get_fake_arl_const( struct svga_shader_emitter
*emit
)
786 struct src_register reg
;
787 int idx
= 0, swizzle
= 0, i
;
789 for (i
= 0; i
< emit
->num_arl_consts
; ++ i
) {
790 if (emit
->arl_consts
[i
].arl_num
== emit
->current_arl
) {
791 idx
= emit
->arl_consts
[i
].idx
;
792 swizzle
= emit
->arl_consts
[i
].swizzle
;
796 reg
= src_register( SVGA3DREG_CONST
, idx
);
797 return scalar(reg
, swizzle
);
802 * Return the register which holds the current dimenions of the
803 * texture bound to the given sampler
805 static struct src_register
806 get_tex_dimensions( struct svga_shader_emitter
*emit
, int sampler_num
)
809 struct src_register reg
;
811 /* the width/height indexes start right after constants */
812 idx
= emit
->key
.fkey
.tex
[sampler_num
].width_height_idx
+
813 emit
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
815 reg
= src_register( SVGA3DREG_CONST
, idx
);
821 emit_fake_arl(struct svga_shader_emitter
*emit
,
822 const struct tgsi_full_instruction
*insn
)
824 const struct src_register src0
=
825 translate_src_register(emit
, &insn
->Src
[0] );
826 struct src_register src1
= get_fake_arl_const( emit
);
827 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
828 SVGA3dShaderDestToken tmp
= get_temp( emit
);
830 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), tmp
, src0
))
833 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), tmp
, src( tmp
),
837 /* replicate the original swizzle */
839 src1
.base
.swizzle
= src0
.base
.swizzle
;
841 return submit_op1( emit
, inst_token( SVGA3DOP_MOVA
),
847 emit_if(struct svga_shader_emitter
*emit
,
848 const struct tgsi_full_instruction
*insn
)
850 struct src_register src0
=
851 translate_src_register(emit
, &insn
->Src
[0]);
852 struct src_register zero
= get_zero_immediate( emit
);
853 SVGA3dShaderInstToken if_token
= inst_token( SVGA3DOP_IFC
);
855 if_token
.control
= SVGA3DOPCOMPC_NE
;
856 zero
= scalar(zero
, TGSI_SWIZZLE_X
);
858 if (SVGA3dShaderGetRegType(src0
.base
.value
) == SVGA3DREG_CONST
) {
860 * Max different constant registers readable per IFC instruction is 1.
862 SVGA3dShaderDestToken tmp
= get_temp( emit
);
864 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), tmp
, src0
))
867 src0
= scalar(src( tmp
), TGSI_SWIZZLE_X
);
870 emit
->dynamic_branching_level
++;
872 return (emit_instruction( emit
, if_token
) &&
873 emit_src( emit
, src0
) &&
874 emit_src( emit
, zero
) );
879 emit_endif(struct svga_shader_emitter
*emit
,
880 const struct tgsi_full_instruction
*insn
)
882 emit
->dynamic_branching_level
--;
884 return emit_instruction(emit
, inst_token(SVGA3DOP_ENDIF
));
889 emit_else(struct svga_shader_emitter
*emit
,
890 const struct tgsi_full_instruction
*insn
)
892 return emit_instruction(emit
, inst_token(SVGA3DOP_ELSE
));
897 * Translate the following TGSI FLR instruction.
899 * To the following SVGA3D instruction sequence.
904 emit_floor(struct svga_shader_emitter
*emit
,
905 const struct tgsi_full_instruction
*insn
)
907 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
908 const struct src_register src0
=
909 translate_src_register(emit
, &insn
->Src
[0] );
910 SVGA3dShaderDestToken temp
= get_temp( emit
);
913 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
), temp
, src0
))
916 /* SUB DST, SRC, TMP */
917 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
, src0
,
918 negate( src( temp
) ) ))
926 * Translate the following TGSI CEIL instruction.
928 * To the following SVGA3D instruction sequence.
933 emit_ceil(struct svga_shader_emitter
*emit
,
934 const struct tgsi_full_instruction
*insn
)
936 SVGA3dShaderDestToken dst
= translate_dst_register(emit
, insn
, 0);
937 const struct src_register src0
=
938 translate_src_register(emit
, &insn
->Src
[0]);
939 SVGA3dShaderDestToken temp
= get_temp(emit
);
942 if (!submit_op1(emit
, inst_token(SVGA3DOP_FRC
), temp
, negate(src0
)))
945 /* ADD DST, SRC, TMP */
946 if (!submit_op2(emit
, inst_token(SVGA3DOP_ADD
), dst
, src0
, src(temp
)))
954 * Translate the following TGSI DIV instruction.
955 * DIV DST.xy, SRC0, SRC1
956 * To the following SVGA3D instruction sequence.
957 * RCP TMP.x, SRC1.xxxx
958 * RCP TMP.y, SRC1.yyyy
959 * MUL DST.xy, SRC0, TMP
962 emit_div(struct svga_shader_emitter
*emit
,
963 const struct tgsi_full_instruction
*insn
)
965 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
966 const struct src_register src0
=
967 translate_src_register(emit
, &insn
->Src
[0] );
968 const struct src_register src1
=
969 translate_src_register(emit
, &insn
->Src
[1] );
970 SVGA3dShaderDestToken temp
= get_temp( emit
);
973 /* For each enabled element, perform a RCP instruction. Note that
974 * RCP is scalar in SVGA3D:
976 for (i
= 0; i
< 4; i
++) {
977 unsigned channel
= 1 << i
;
978 if (dst
.mask
& channel
) {
979 /* RCP TMP.?, SRC1.???? */
980 if (!submit_op1( emit
, inst_token( SVGA3DOP_RCP
),
981 writemask(temp
, channel
),
990 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
), dst
, src0
,
999 * Translate the following TGSI DP2 instruction.
1000 * DP2 DST, SRC1, SRC2
1001 * To the following SVGA3D instruction sequence.
1002 * MUL TMP, SRC1, SRC2
1003 * ADD DST, TMP.xxxx, TMP.yyyy
1006 emit_dp2(struct svga_shader_emitter
*emit
,
1007 const struct tgsi_full_instruction
*insn
)
1009 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1010 const struct src_register src0
=
1011 translate_src_register(emit
, &insn
->Src
[0]);
1012 const struct src_register src1
=
1013 translate_src_register(emit
, &insn
->Src
[1]);
1014 SVGA3dShaderDestToken temp
= get_temp( emit
);
1015 struct src_register temp_src0
, temp_src1
;
1017 /* MUL TMP, SRC1, SRC2 */
1018 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
), temp
, src0
, src1
))
1021 temp_src0
= scalar(src( temp
), TGSI_SWIZZLE_X
);
1022 temp_src1
= scalar(src( temp
), TGSI_SWIZZLE_Y
);
1024 /* ADD DST, TMP.xxxx, TMP.yyyy */
1025 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
,
1026 temp_src0
, temp_src1
))
1034 * Translate the following TGSI DPH instruction.
1035 * DPH DST, SRC1, SRC2
1036 * To the following SVGA3D instruction sequence.
1037 * DP3 TMP, SRC1, SRC2
1038 * ADD DST, TMP, SRC2.wwww
1041 emit_dph(struct svga_shader_emitter
*emit
,
1042 const struct tgsi_full_instruction
*insn
)
1044 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1045 const struct src_register src0
= translate_src_register(
1046 emit
, &insn
->Src
[0] );
1047 struct src_register src1
=
1048 translate_src_register(emit
, &insn
->Src
[1]);
1049 SVGA3dShaderDestToken temp
= get_temp( emit
);
1051 /* DP3 TMP, SRC1, SRC2 */
1052 if (!submit_op2( emit
, inst_token( SVGA3DOP_DP3
), temp
, src0
, src1
))
1055 src1
= scalar(src1
, TGSI_SWIZZLE_W
);
1057 /* ADD DST, TMP, SRC2.wwww */
1058 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
,
1059 src( temp
), src1
))
1067 * Translate the following TGSI DST instruction.
1069 * To the following SVGA3D instruction sequence.
1075 emit_nrm(struct svga_shader_emitter
*emit
,
1076 const struct tgsi_full_instruction
*insn
)
1078 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1079 const struct src_register src0
=
1080 translate_src_register(emit
, &insn
->Src
[0]);
1081 SVGA3dShaderDestToken temp
= get_temp( emit
);
1083 /* DP3 TMP, SRC, SRC */
1084 if (!submit_op2( emit
, inst_token( SVGA3DOP_DP3
), temp
, src0
, src0
))
1088 if (!submit_op1( emit
, inst_token( SVGA3DOP_RSQ
), temp
, src( temp
)))
1091 /* MUL DST, SRC, TMP */
1092 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
), dst
,
1101 do_emit_sincos(struct svga_shader_emitter
*emit
,
1102 SVGA3dShaderDestToken dst
,
1103 struct src_register src0
)
1105 src0
= scalar(src0
, TGSI_SWIZZLE_X
);
1106 return submit_op1(emit
, inst_token(SVGA3DOP_SINCOS
), dst
, src0
);
1111 emit_sincos(struct svga_shader_emitter
*emit
,
1112 const struct tgsi_full_instruction
*insn
)
1114 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1115 struct src_register src0
= translate_src_register(emit
, &insn
->Src
[0]);
1116 SVGA3dShaderDestToken temp
= get_temp( emit
);
1119 if (!do_emit_sincos(emit
, writemask(temp
, TGSI_WRITEMASK_XY
), src0
))
1123 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src( temp
) ))
1135 emit_sin(struct svga_shader_emitter
*emit
,
1136 const struct tgsi_full_instruction
*insn
)
1138 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1139 struct src_register src0
=
1140 translate_src_register(emit
, &insn
->Src
[0] );
1141 SVGA3dShaderDestToken temp
= get_temp( emit
);
1144 if (!do_emit_sincos(emit
, writemask(temp
, TGSI_WRITEMASK_Y
), src0
))
1147 src0
= scalar(src( temp
), TGSI_SWIZZLE_Y
);
1149 /* MOV DST TMP.yyyy */
1150 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src0
))
1161 emit_cos(struct svga_shader_emitter
*emit
,
1162 const struct tgsi_full_instruction
*insn
)
1164 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1165 struct src_register src0
=
1166 translate_src_register(emit
, &insn
->Src
[0] );
1167 SVGA3dShaderDestToken temp
= get_temp( emit
);
1170 if (!do_emit_sincos( emit
, writemask(temp
, TGSI_WRITEMASK_X
), src0
))
1173 src0
= scalar(src( temp
), TGSI_SWIZZLE_X
);
1175 /* MOV DST TMP.xxxx */
1176 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src0
))
1184 emit_ssg(struct svga_shader_emitter
*emit
,
1185 const struct tgsi_full_instruction
*insn
)
1187 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1188 struct src_register src0
=
1189 translate_src_register(emit
, &insn
->Src
[0] );
1190 SVGA3dShaderDestToken temp0
= get_temp( emit
);
1191 SVGA3dShaderDestToken temp1
= get_temp( emit
);
1192 struct src_register zero
, one
;
1194 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1195 /* SGN DST, SRC0, TMP0, TMP1 */
1196 return submit_op3( emit
, inst_token( SVGA3DOP_SGN
), dst
, src0
,
1197 src( temp0
), src( temp1
) );
1200 zero
= get_zero_immediate( emit
);
1201 one
= scalar( zero
, TGSI_SWIZZLE_W
);
1202 zero
= scalar( zero
, TGSI_SWIZZLE_X
);
1204 /* CMP TMP0, SRC0, one, zero */
1205 if (!submit_op3( emit
, inst_token( SVGA3DOP_CMP
),
1206 writemask( temp0
, dst
.mask
), src0
, one
, zero
))
1209 /* CMP TMP1, negate(SRC0), negate(one), zero */
1210 if (!submit_op3( emit
, inst_token( SVGA3DOP_CMP
),
1211 writemask( temp1
, dst
.mask
), negate( src0
), negate( one
),
1215 /* ADD DST, TMP0, TMP1 */
1216 return submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
, src( temp0
),
1222 * ADD DST SRC0, negate(SRC0)
1225 emit_sub(struct svga_shader_emitter
*emit
,
1226 const struct tgsi_full_instruction
*insn
)
1228 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1229 struct src_register src0
= translate_src_register(
1230 emit
, &insn
->Src
[0] );
1231 struct src_register src1
= translate_src_register(
1232 emit
, &insn
->Src
[1] );
1234 src1
= negate(src1
);
1236 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
,
1245 emit_kil(struct svga_shader_emitter
*emit
,
1246 const struct tgsi_full_instruction
*insn
)
1248 const struct tgsi_full_src_register
*reg
= &insn
->Src
[0];
1249 struct src_register src0
, srcIn
;
1250 /* is the W component tested in another position? */
1251 const boolean w_tested
= (reg
->Register
.SwizzleW
== reg
->Register
.SwizzleX
||
1252 reg
->Register
.SwizzleW
== reg
->Register
.SwizzleY
||
1253 reg
->Register
.SwizzleW
== reg
->Register
.SwizzleZ
);
1254 const boolean special
= (reg
->Register
.Absolute
||
1255 reg
->Register
.Negate
||
1256 reg
->Register
.Indirect
||
1257 reg
->Register
.SwizzleX
!= 0 ||
1258 reg
->Register
.SwizzleY
!= 1 ||
1259 reg
->Register
.SwizzleZ
!= 2 ||
1260 reg
->Register
.File
!= TGSI_FILE_TEMPORARY
);
1261 SVGA3dShaderDestToken temp
;
1263 src0
= srcIn
= translate_src_register( emit
, reg
);
1265 if (special
|| !w_tested
) {
1266 /* need a temp reg */
1267 temp
= get_temp( emit
);
1271 /* move the source into a temp register */
1272 submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1273 writemask( temp
, TGSI_WRITEMASK_XYZ
),
1279 /* do the texkill (on the xyz components) */
1280 if (!submit_op0( emit
, inst_token( SVGA3DOP_TEXKILL
), dst(src0
) ))
1284 /* need to emit a second texkill to test the W component */
1285 /* put src.wwww into temp register */
1286 if (!submit_op1(emit
,
1287 inst_token( SVGA3DOP_MOV
),
1288 writemask( temp
, TGSI_WRITEMASK_XYZ
),
1289 scalar(srcIn
, TGSI_SWIZZLE_W
)))
1292 /* second texkill */
1293 if (!submit_op0( emit
, inst_token( SVGA3DOP_TEXKILL
), temp
))
1302 * mesa state tracker always emits kilp as an unconditional kil
1305 emit_kilp(struct svga_shader_emitter
*emit
,
1306 const struct tgsi_full_instruction
*insn
)
1308 SVGA3dShaderDestToken temp
;
1309 struct src_register one
= scalar( get_zero_immediate( emit
),
1311 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_TEXKILL
);
1313 /* texkill doesn't allow negation on the operand so lets move
1314 * negation of {1} to a temp register */
1315 temp
= get_temp( emit
);
1316 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp
,
1320 return submit_op0( emit
, inst
, temp
);
1325 * Test if r1 and r2 are the same register.
1328 same_register(struct src_register r1
, struct src_register r2
)
1330 return (r1
.base
.num
== r2
.base
.num
&&
1331 r1
.base
.type_upper
== r2
.base
.type_upper
&&
1332 r1
.base
.type_lower
== r2
.base
.type_lower
);
1337 /* Implement conditionals by initializing destination reg to 'fail',
1338 * then set predicate reg with UFOP_SETP, then move 'pass' to dest
1339 * based on predicate reg.
1341 * SETP src0, cmp, src1 -- do this first to avoid aliasing problems.
1346 emit_conditional(struct svga_shader_emitter
*emit
,
1347 unsigned compare_func
,
1348 SVGA3dShaderDestToken dst
,
1349 struct src_register src0
,
1350 struct src_register src1
,
1351 struct src_register pass
,
1352 struct src_register fail
)
1354 SVGA3dShaderDestToken pred_reg
= dst_register( SVGA3DREG_PREDICATE
, 0 );
1355 SVGA3dShaderInstToken setp_token
, mov_token
;
1356 setp_token
= inst_token( SVGA3DOP_SETP
);
1358 switch (compare_func
) {
1359 case PIPE_FUNC_NEVER
:
1360 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1363 case PIPE_FUNC_LESS
:
1364 setp_token
.control
= SVGA3DOPCOMP_LT
;
1366 case PIPE_FUNC_EQUAL
:
1367 setp_token
.control
= SVGA3DOPCOMP_EQ
;
1369 case PIPE_FUNC_LEQUAL
:
1370 setp_token
.control
= SVGA3DOPCOMP_LE
;
1372 case PIPE_FUNC_GREATER
:
1373 setp_token
.control
= SVGA3DOPCOMP_GT
;
1375 case PIPE_FUNC_NOTEQUAL
:
1376 setp_token
.control
= SVGA3DOPCOMPC_NE
;
1378 case PIPE_FUNC_GEQUAL
:
1379 setp_token
.control
= SVGA3DOPCOMP_GE
;
1381 case PIPE_FUNC_ALWAYS
:
1382 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1387 if (same_register(src(dst
), pass
)) {
1388 /* We'll get bad results if the dst and pass registers are the same
1389 * so use a temp register containing pass.
1391 SVGA3dShaderDestToken temp
= get_temp(emit
);
1392 if (!submit_op1(emit
, inst_token(SVGA3DOP_MOV
), temp
, pass
))
1397 /* SETP src0, COMPOP, src1 */
1398 if (!submit_op2( emit
, setp_token
, pred_reg
,
1402 mov_token
= inst_token( SVGA3DOP_MOV
);
1405 if (!submit_op1( emit
, mov_token
, dst
,
1409 /* MOV dst, pass (predicated)
1411 * Note that the predicate reg (and possible modifiers) is passed
1412 * as the first source argument.
1414 mov_token
.predicated
= 1;
1415 if (!submit_op2( emit
, mov_token
, dst
,
1416 src( pred_reg
), pass
))
1424 emit_select(struct svga_shader_emitter
*emit
,
1425 unsigned compare_func
,
1426 SVGA3dShaderDestToken dst
,
1427 struct src_register src0
,
1428 struct src_register src1
)
1430 /* There are some SVGA instructions which implement some selects
1431 * directly, but they are only available in the vertex shader.
1433 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1434 switch (compare_func
) {
1435 case PIPE_FUNC_GEQUAL
:
1436 return submit_op2( emit
, inst_token( SVGA3DOP_SGE
), dst
, src0
, src1
);
1437 case PIPE_FUNC_LEQUAL
:
1438 return submit_op2( emit
, inst_token( SVGA3DOP_SGE
), dst
, src1
, src0
);
1439 case PIPE_FUNC_GREATER
:
1440 return submit_op2( emit
, inst_token( SVGA3DOP_SLT
), dst
, src1
, src0
);
1441 case PIPE_FUNC_LESS
:
1442 return submit_op2( emit
, inst_token( SVGA3DOP_SLT
), dst
, src0
, src1
);
1448 /* Otherwise, need to use the setp approach:
1451 struct src_register one
, zero
;
1452 /* zero immediate is 0,0,0,1 */
1453 zero
= get_zero_immediate( emit
);
1454 one
= scalar( zero
, TGSI_SWIZZLE_W
);
1455 zero
= scalar( zero
, TGSI_SWIZZLE_X
);
1457 return emit_conditional(
1469 emit_select_op(struct svga_shader_emitter
*emit
,
1471 const struct tgsi_full_instruction
*insn
)
1473 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1474 struct src_register src0
= translate_src_register(
1475 emit
, &insn
->Src
[0] );
1476 struct src_register src1
= translate_src_register(
1477 emit
, &insn
->Src
[1] );
1479 return emit_select( emit
, compare
, dst
, src0
, src1
);
1484 * Translate TGSI CMP instruction.
1487 emit_cmp(struct svga_shader_emitter
*emit
,
1488 const struct tgsi_full_instruction
*insn
)
1490 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1491 const struct src_register src0
=
1492 translate_src_register(emit
, &insn
->Src
[0] );
1493 const struct src_register src1
=
1494 translate_src_register(emit
, &insn
->Src
[1] );
1495 const struct src_register src2
=
1496 translate_src_register(emit
, &insn
->Src
[2] );
1498 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1499 struct src_register zero
=
1500 scalar(get_zero_immediate(emit
), TGSI_SWIZZLE_X
);
1501 /* We used to simulate CMP with SLT+LRP. But that didn't work when
1502 * src1 or src2 was Inf/NaN. In particular, GLSL sqrt(0) failed
1503 * because it involves a CMP to handle the 0 case.
1504 * Use a conditional expression instead.
1506 return emit_conditional(emit
, PIPE_FUNC_LESS
, dst
,
1507 src0
, zero
, src1
, src2
);
1510 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
1512 /* CMP DST, SRC0, SRC2, SRC1 */
1513 return submit_op3( emit
, inst_token( SVGA3DOP_CMP
), dst
,
1520 * Translate texture instructions to SVGA3D representation.
1523 emit_tex2(struct svga_shader_emitter
*emit
,
1524 const struct tgsi_full_instruction
*insn
,
1525 SVGA3dShaderDestToken dst
)
1527 SVGA3dShaderInstToken inst
;
1528 struct src_register texcoord
;
1529 struct src_register sampler
;
1530 SVGA3dShaderDestToken tmp
;
1534 switch (insn
->Instruction
.Opcode
) {
1535 case TGSI_OPCODE_TEX
:
1536 inst
.op
= SVGA3DOP_TEX
;
1538 case TGSI_OPCODE_TXP
:
1539 inst
.op
= SVGA3DOP_TEX
;
1540 inst
.control
= SVGA3DOPCONT_PROJECT
;
1542 case TGSI_OPCODE_TXB
:
1543 inst
.op
= SVGA3DOP_TEX
;
1544 inst
.control
= SVGA3DOPCONT_BIAS
;
1546 case TGSI_OPCODE_TXL
:
1547 inst
.op
= SVGA3DOP_TEXLDL
;
1554 texcoord
= translate_src_register( emit
, &insn
->Src
[0] );
1555 sampler
= translate_src_register( emit
, &insn
->Src
[1] );
1557 if (emit
->key
.fkey
.tex
[sampler
.base
.num
].unnormalized
||
1558 emit
->dynamic_branching_level
> 0)
1559 tmp
= get_temp( emit
);
1561 /* Can't do mipmapping inside dynamic branch constructs. Force LOD
1562 * zero in that case.
1564 if (emit
->dynamic_branching_level
> 0 &&
1565 inst
.op
== SVGA3DOP_TEX
&&
1566 SVGA3dShaderGetRegType(texcoord
.base
.value
) == SVGA3DREG_TEMP
) {
1567 struct src_register zero
= get_zero_immediate( emit
);
1569 /* MOV tmp, texcoord */
1570 if (!submit_op1( emit
,
1571 inst_token( SVGA3DOP_MOV
),
1576 /* MOV tmp.w, zero */
1577 if (!submit_op1( emit
,
1578 inst_token( SVGA3DOP_MOV
),
1579 writemask( tmp
, TGSI_WRITEMASK_W
),
1580 scalar( zero
, TGSI_SWIZZLE_X
)))
1583 texcoord
= src( tmp
);
1584 inst
.op
= SVGA3DOP_TEXLDL
;
1587 /* Explicit normalization of texcoords:
1589 if (emit
->key
.fkey
.tex
[sampler
.base
.num
].unnormalized
) {
1590 struct src_register wh
= get_tex_dimensions( emit
, sampler
.base
.num
);
1592 /* MUL tmp, SRC0, WH */
1593 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
1594 tmp
, texcoord
, wh
))
1597 texcoord
= src( tmp
);
1600 return submit_op2( emit
, inst
, dst
, texcoord
, sampler
);
1605 * Translate texture instructions to SVGA3D representation.
1608 emit_tex4(struct svga_shader_emitter
*emit
,
1609 const struct tgsi_full_instruction
*insn
,
1610 SVGA3dShaderDestToken dst
)
1612 SVGA3dShaderInstToken inst
;
1613 struct src_register texcoord
;
1614 struct src_register ddx
;
1615 struct src_register ddy
;
1616 struct src_register sampler
;
1618 texcoord
= translate_src_register( emit
, &insn
->Src
[0] );
1619 ddx
= translate_src_register( emit
, &insn
->Src
[1] );
1620 ddy
= translate_src_register( emit
, &insn
->Src
[2] );
1621 sampler
= translate_src_register( emit
, &insn
->Src
[3] );
1625 switch (insn
->Instruction
.Opcode
) {
1626 case TGSI_OPCODE_TXD
:
1627 inst
.op
= SVGA3DOP_TEXLDD
; /* 4 args! */
1634 return submit_op4( emit
, inst
, dst
, texcoord
, sampler
, ddx
, ddy
);
1639 * Emit texture swizzle code.
1642 emit_tex_swizzle(struct svga_shader_emitter
*emit
,
1643 SVGA3dShaderDestToken dst
,
1644 struct src_register src
,
1650 const unsigned swizzleIn
[4] = {swizzle_x
, swizzle_y
, swizzle_z
, swizzle_w
};
1651 unsigned srcSwizzle
[4];
1652 unsigned srcWritemask
= 0x0, zeroWritemask
= 0x0, oneWritemask
= 0x0;
1655 /* build writemasks and srcSwizzle terms */
1656 for (i
= 0; i
< 4; i
++) {
1657 if (swizzleIn
[i
] == PIPE_SWIZZLE_ZERO
) {
1658 srcSwizzle
[i
] = TGSI_SWIZZLE_X
+ i
;
1659 zeroWritemask
|= (1 << i
);
1661 else if (swizzleIn
[i
] == PIPE_SWIZZLE_ONE
) {
1662 srcSwizzle
[i
] = TGSI_SWIZZLE_X
+ i
;
1663 oneWritemask
|= (1 << i
);
1666 srcSwizzle
[i
] = swizzleIn
[i
];
1667 srcWritemask
|= (1 << i
);
1671 /* write x/y/z/w comps */
1672 if (dst
.mask
& srcWritemask
) {
1673 if (!submit_op1(emit
,
1674 inst_token(SVGA3DOP_MOV
),
1675 writemask(dst
, srcWritemask
),
1685 if (dst
.mask
& zeroWritemask
) {
1686 if (!submit_op1(emit
,
1687 inst_token(SVGA3DOP_MOV
),
1688 writemask(dst
, zeroWritemask
),
1689 scalar(get_zero_immediate(emit
), TGSI_SWIZZLE_X
)))
1694 if (dst
.mask
& oneWritemask
) {
1695 if (!submit_op1(emit
,
1696 inst_token(SVGA3DOP_MOV
),
1697 writemask(dst
, oneWritemask
),
1698 scalar(get_zero_immediate(emit
), TGSI_SWIZZLE_W
)))
1707 emit_tex(struct svga_shader_emitter
*emit
,
1708 const struct tgsi_full_instruction
*insn
)
1710 SVGA3dShaderDestToken dst
=
1711 translate_dst_register( emit
, insn
, 0 );
1712 struct src_register src0
=
1713 translate_src_register( emit
, &insn
->Src
[0] );
1714 struct src_register src1
=
1715 translate_src_register( emit
, &insn
->Src
[1] );
1717 SVGA3dShaderDestToken tex_result
;
1718 const unsigned unit
= src1
.base
.num
;
1720 /* check for shadow samplers */
1721 boolean compare
= (emit
->key
.fkey
.tex
[unit
].compare_mode
==
1722 PIPE_TEX_COMPARE_R_TO_TEXTURE
);
1724 /* texture swizzle */
1725 boolean swizzle
= (emit
->key
.fkey
.tex
[unit
].swizzle_r
!= PIPE_SWIZZLE_RED
||
1726 emit
->key
.fkey
.tex
[unit
].swizzle_g
!= PIPE_SWIZZLE_GREEN
||
1727 emit
->key
.fkey
.tex
[unit
].swizzle_b
!= PIPE_SWIZZLE_BLUE
||
1728 emit
->key
.fkey
.tex
[unit
].swizzle_a
!= PIPE_SWIZZLE_ALPHA
);
1730 boolean saturate
= insn
->Instruction
.Saturate
!= TGSI_SAT_NONE
;
1732 /* If doing compare processing or tex swizzle or saturation, we need to put
1733 * the fetched color into a temporary so it can be used as a source later on.
1735 if (compare
|| swizzle
|| saturate
) {
1736 tex_result
= get_temp( emit
);
1742 switch(insn
->Instruction
.Opcode
) {
1743 case TGSI_OPCODE_TEX
:
1744 case TGSI_OPCODE_TXB
:
1745 case TGSI_OPCODE_TXP
:
1746 case TGSI_OPCODE_TXL
:
1747 if (!emit_tex2( emit
, insn
, tex_result
))
1750 case TGSI_OPCODE_TXD
:
1751 if (!emit_tex4( emit
, insn
, tex_result
))
1759 SVGA3dShaderDestToken dst2
;
1761 if (swizzle
|| saturate
)
1766 if (dst
.mask
& TGSI_WRITEMASK_XYZ
) {
1767 SVGA3dShaderDestToken src0_zdivw
= get_temp( emit
);
1768 /* When sampling a depth texture, the result of the comparison is in
1771 struct src_register tex_src_x
= scalar(src(tex_result
), TGSI_SWIZZLE_Y
);
1772 struct src_register r_coord
;
1774 if (insn
->Instruction
.Opcode
== TGSI_OPCODE_TXP
) {
1775 /* Divide texcoord R by Q */
1776 if (!submit_op1( emit
, inst_token( SVGA3DOP_RCP
),
1777 writemask(src0_zdivw
, TGSI_WRITEMASK_X
),
1778 scalar(src0
, TGSI_SWIZZLE_W
) ))
1781 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
1782 writemask(src0_zdivw
, TGSI_WRITEMASK_X
),
1783 scalar(src0
, TGSI_SWIZZLE_Z
),
1784 scalar(src(src0_zdivw
), TGSI_SWIZZLE_X
) ))
1787 r_coord
= scalar(src(src0_zdivw
), TGSI_SWIZZLE_X
);
1790 r_coord
= scalar(src0
, TGSI_SWIZZLE_Z
);
1793 /* Compare texture sample value against R component of texcoord */
1794 if (!emit_select(emit
,
1795 emit
->key
.fkey
.tex
[unit
].compare_func
,
1796 writemask( dst2
, TGSI_WRITEMASK_XYZ
),
1802 if (dst
.mask
& TGSI_WRITEMASK_W
) {
1803 struct src_register one
=
1804 scalar( get_zero_immediate( emit
), TGSI_SWIZZLE_W
);
1806 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1807 writemask( dst2
, TGSI_WRITEMASK_W
),
1813 if (saturate
&& !swizzle
) {
1814 /* MOV_SAT real_dst, dst */
1815 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src(tex_result
) ))
1819 /* swizzle from tex_result to dst (handles saturation too, if any) */
1820 emit_tex_swizzle(emit
,
1821 dst
, src(tex_result
),
1822 emit
->key
.fkey
.tex
[unit
].swizzle_r
,
1823 emit
->key
.fkey
.tex
[unit
].swizzle_g
,
1824 emit
->key
.fkey
.tex
[unit
].swizzle_b
,
1825 emit
->key
.fkey
.tex
[unit
].swizzle_a
);
1833 emit_bgnloop2(struct svga_shader_emitter
*emit
,
1834 const struct tgsi_full_instruction
*insn
)
1836 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_LOOP
);
1837 struct src_register loop_reg
= src_register( SVGA3DREG_LOOP
, 0 );
1838 struct src_register const_int
= get_loop_const( emit
);
1840 emit
->dynamic_branching_level
++;
1842 return (emit_instruction( emit
, inst
) &&
1843 emit_src( emit
, loop_reg
) &&
1844 emit_src( emit
, const_int
) );
1849 emit_endloop2(struct svga_shader_emitter
*emit
,
1850 const struct tgsi_full_instruction
*insn
)
1852 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_ENDLOOP
);
1854 emit
->dynamic_branching_level
--;
1856 return emit_instruction( emit
, inst
);
1861 emit_brk(struct svga_shader_emitter
*emit
,
1862 const struct tgsi_full_instruction
*insn
)
1864 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_BREAK
);
1865 return emit_instruction( emit
, inst
);
1870 emit_scalar_op1(struct svga_shader_emitter
*emit
,
1872 const struct tgsi_full_instruction
*insn
)
1874 SVGA3dShaderInstToken inst
;
1875 SVGA3dShaderDestToken dst
;
1876 struct src_register src
;
1878 inst
= inst_token( opcode
);
1879 dst
= translate_dst_register( emit
, insn
, 0 );
1880 src
= translate_src_register( emit
, &insn
->Src
[0] );
1881 src
= scalar( src
, TGSI_SWIZZLE_X
);
1883 return submit_op1( emit
, inst
, dst
, src
);
1888 emit_simple_instruction(struct svga_shader_emitter
*emit
,
1890 const struct tgsi_full_instruction
*insn
)
1892 const struct tgsi_full_src_register
*src
= insn
->Src
;
1893 SVGA3dShaderInstToken inst
;
1894 SVGA3dShaderDestToken dst
;
1896 inst
= inst_token( opcode
);
1897 dst
= translate_dst_register( emit
, insn
, 0 );
1899 switch (insn
->Instruction
.NumSrcRegs
) {
1901 return submit_op0( emit
, inst
, dst
);
1903 return submit_op1( emit
, inst
, dst
,
1904 translate_src_register( emit
, &src
[0] ));
1906 return submit_op2( emit
, inst
, dst
,
1907 translate_src_register( emit
, &src
[0] ),
1908 translate_src_register( emit
, &src
[1] ) );
1910 return submit_op3( emit
, inst
, dst
,
1911 translate_src_register( emit
, &src
[0] ),
1912 translate_src_register( emit
, &src
[1] ),
1913 translate_src_register( emit
, &src
[2] ) );
1922 emit_deriv(struct svga_shader_emitter
*emit
,
1923 const struct tgsi_full_instruction
*insn
)
1925 if (emit
->dynamic_branching_level
> 0 &&
1926 insn
->Src
[0].Register
.File
== TGSI_FILE_TEMPORARY
)
1928 struct src_register zero
= get_zero_immediate( emit
);
1929 SVGA3dShaderDestToken dst
=
1930 translate_dst_register( emit
, insn
, 0 );
1932 /* Deriv opcodes not valid inside dynamic branching, workaround
1933 * by zeroing out the destination.
1935 if (!submit_op1(emit
,
1936 inst_token( SVGA3DOP_MOV
),
1938 scalar(zero
, TGSI_SWIZZLE_X
)))
1945 const struct tgsi_full_src_register
*reg
= &insn
->Src
[0];
1946 SVGA3dShaderInstToken inst
;
1947 SVGA3dShaderDestToken dst
;
1948 struct src_register src0
;
1950 switch (insn
->Instruction
.Opcode
) {
1951 case TGSI_OPCODE_DDX
:
1952 opcode
= SVGA3DOP_DSX
;
1954 case TGSI_OPCODE_DDY
:
1955 opcode
= SVGA3DOP_DSY
;
1961 inst
= inst_token( opcode
);
1962 dst
= translate_dst_register( emit
, insn
, 0 );
1963 src0
= translate_src_register( emit
, reg
);
1965 /* We cannot use negate or abs on source to dsx/dsy instruction.
1967 if (reg
->Register
.Absolute
||
1968 reg
->Register
.Negate
) {
1969 SVGA3dShaderDestToken temp
= get_temp( emit
);
1971 if (!emit_repl( emit
, temp
, &src0
))
1975 return submit_op1( emit
, inst
, dst
, src0
);
1981 emit_arl(struct svga_shader_emitter
*emit
,
1982 const struct tgsi_full_instruction
*insn
)
1984 ++emit
->current_arl
;
1985 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
1986 /* MOVA not present in pixel shader instruction set.
1987 * Ignore this instruction altogether since it is
1988 * only used for loop counters -- and for that
1989 * we reference aL directly.
1993 if (svga_arl_needs_adjustment( emit
)) {
1994 return emit_fake_arl( emit
, insn
);
1996 /* no need to adjust, just emit straight arl */
1997 return emit_simple_instruction(emit
, SVGA3DOP_MOVA
, insn
);
2003 emit_pow(struct svga_shader_emitter
*emit
,
2004 const struct tgsi_full_instruction
*insn
)
2006 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2007 struct src_register src0
= translate_src_register(
2008 emit
, &insn
->Src
[0] );
2009 struct src_register src1
= translate_src_register(
2010 emit
, &insn
->Src
[1] );
2011 boolean need_tmp
= FALSE
;
2013 /* POW can only output to a temporary */
2014 if (insn
->Dst
[0].Register
.File
!= TGSI_FILE_TEMPORARY
)
2017 /* POW src1 must not be the same register as dst */
2018 if (alias_src_dst( src1
, dst
))
2021 /* it's a scalar op */
2022 src0
= scalar( src0
, TGSI_SWIZZLE_X
);
2023 src1
= scalar( src1
, TGSI_SWIZZLE_X
);
2026 SVGA3dShaderDestToken tmp
=
2027 writemask(get_temp( emit
), TGSI_WRITEMASK_X
);
2029 if (!submit_op2(emit
, inst_token( SVGA3DOP_POW
), tmp
, src0
, src1
))
2032 return submit_op1(emit
, inst_token( SVGA3DOP_MOV
),
2033 dst
, scalar(src(tmp
), 0) );
2036 return submit_op2(emit
, inst_token( SVGA3DOP_POW
), dst
, src0
, src1
);
2042 emit_xpd(struct svga_shader_emitter
*emit
,
2043 const struct tgsi_full_instruction
*insn
)
2045 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2046 const struct src_register src0
= translate_src_register(
2047 emit
, &insn
->Src
[0] );
2048 const struct src_register src1
= translate_src_register(
2049 emit
, &insn
->Src
[1] );
2050 boolean need_dst_tmp
= FALSE
;
2052 /* XPD can only output to a temporary */
2053 if (SVGA3dShaderGetRegType(dst
.value
) != SVGA3DREG_TEMP
)
2054 need_dst_tmp
= TRUE
;
2056 /* The dst reg must not be the same as src0 or src1*/
2057 if (alias_src_dst(src0
, dst
) ||
2058 alias_src_dst(src1
, dst
))
2059 need_dst_tmp
= TRUE
;
2062 SVGA3dShaderDestToken tmp
= get_temp( emit
);
2064 /* Obey DX9 restrictions on mask:
2066 tmp
.mask
= dst
.mask
& TGSI_WRITEMASK_XYZ
;
2068 if (!submit_op2(emit
, inst_token( SVGA3DOP_CRS
), tmp
, src0
, src1
))
2071 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), dst
, src( tmp
)))
2075 if (!submit_op2(emit
, inst_token( SVGA3DOP_CRS
), dst
, src0
, src1
))
2079 /* Need to emit 1.0 to dst.w?
2081 if (dst
.mask
& TGSI_WRITEMASK_W
) {
2082 struct src_register zero
= get_zero_immediate( emit
);
2084 if (!submit_op1(emit
,
2085 inst_token( SVGA3DOP_MOV
),
2086 writemask(dst
, TGSI_WRITEMASK_W
),
2096 emit_lrp(struct svga_shader_emitter
*emit
,
2097 const struct tgsi_full_instruction
*insn
)
2099 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2100 const struct src_register src0
= translate_src_register(
2101 emit
, &insn
->Src
[0] );
2102 const struct src_register src1
= translate_src_register(
2103 emit
, &insn
->Src
[1] );
2104 const struct src_register src2
= translate_src_register(
2105 emit
, &insn
->Src
[2] );
2107 return submit_lrp(emit
, dst
, src0
, src1
, src2
);
2112 emit_dst_insn(struct svga_shader_emitter
*emit
,
2113 const struct tgsi_full_instruction
*insn
)
2115 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2116 /* SVGA/DX9 has a DST instruction, but only for vertex shaders:
2118 return emit_simple_instruction(emit
, SVGA3DOP_DST
, insn
);
2121 /* result[0] = 1 * 1;
2122 * result[1] = a[1] * b[1];
2123 * result[2] = a[2] * 1;
2124 * result[3] = 1 * b[3];
2126 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2127 SVGA3dShaderDestToken tmp
;
2128 const struct src_register src0
= translate_src_register(
2129 emit
, &insn
->Src
[0] );
2130 const struct src_register src1
= translate_src_register(
2131 emit
, &insn
->Src
[1] );
2132 struct src_register zero
= get_zero_immediate( emit
);
2133 boolean need_tmp
= FALSE
;
2135 if (SVGA3dShaderGetRegType(dst
.value
) != SVGA3DREG_TEMP
||
2136 alias_src_dst(src0
, dst
) ||
2137 alias_src_dst(src1
, dst
))
2141 tmp
= get_temp( emit
);
2149 if (tmp
.mask
& TGSI_WRITEMASK_XW
) {
2150 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2151 writemask(tmp
, TGSI_WRITEMASK_XW
),
2158 if (tmp
.mask
& TGSI_WRITEMASK_YZ
) {
2159 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2160 writemask(tmp
, TGSI_WRITEMASK_YZ
),
2165 /* tmp.yw = tmp * src1
2167 if (tmp
.mask
& TGSI_WRITEMASK_YW
) {
2168 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
2169 writemask(tmp
, TGSI_WRITEMASK_YW
),
2178 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2190 emit_exp(struct svga_shader_emitter
*emit
,
2191 const struct tgsi_full_instruction
*insn
)
2193 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2194 struct src_register src0
=
2195 translate_src_register( emit
, &insn
->Src
[0] );
2196 struct src_register zero
= get_zero_immediate( emit
);
2197 SVGA3dShaderDestToken fraction
;
2199 if (dst
.mask
& TGSI_WRITEMASK_Y
)
2201 else if (dst
.mask
& TGSI_WRITEMASK_X
)
2202 fraction
= get_temp( emit
);
2206 /* If y is being written, fill it with src0 - floor(src0).
2208 if (dst
.mask
& TGSI_WRITEMASK_XY
) {
2209 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
),
2210 writemask( fraction
, TGSI_WRITEMASK_Y
),
2215 /* If x is being written, fill it with 2 ^ floor(src0).
2217 if (dst
.mask
& TGSI_WRITEMASK_X
) {
2218 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
),
2219 writemask( dst
, TGSI_WRITEMASK_X
),
2221 scalar( negate( src( fraction
) ), TGSI_SWIZZLE_Y
) ) )
2224 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXP
),
2225 writemask( dst
, TGSI_WRITEMASK_X
),
2226 scalar( src( dst
), TGSI_SWIZZLE_X
) ) )
2229 if (!(dst
.mask
& TGSI_WRITEMASK_Y
))
2230 release_temp( emit
, fraction
);
2233 /* If z is being written, fill it with 2 ^ src0 (partial precision).
2235 if (dst
.mask
& TGSI_WRITEMASK_Z
) {
2236 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXPP
),
2237 writemask( dst
, TGSI_WRITEMASK_Z
),
2242 /* If w is being written, fill it with one.
2244 if (dst
.mask
& TGSI_WRITEMASK_W
) {
2245 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2246 writemask(dst
, TGSI_WRITEMASK_W
),
2247 scalar( zero
, TGSI_SWIZZLE_W
) ))
2256 emit_lit(struct svga_shader_emitter
*emit
,
2257 const struct tgsi_full_instruction
*insn
)
2259 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2260 /* SVGA/DX9 has a LIT instruction, but only for vertex shaders:
2262 return emit_simple_instruction(emit
, SVGA3DOP_LIT
, insn
);
2265 /* D3D vs. GL semantics can be fairly easily accomodated by
2266 * variations on this sequence.
2270 * tmp.z = pow(src.y,src.w)
2271 * p0 = src0.xxxx > 0
2272 * result = zero.wxxw
2273 * (p0) result.yz = tmp
2277 * tmp.z = pow(src.y,src.w)
2278 * p0 = src0.xxyy > 0
2279 * result = zero.wxxw
2280 * (p0) result.yz = tmp
2282 * Will implement the GL version for now.
2284 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2285 SVGA3dShaderDestToken tmp
= get_temp( emit
);
2286 const struct src_register src0
= translate_src_register(
2287 emit
, &insn
->Src
[0] );
2288 struct src_register zero
= get_zero_immediate( emit
);
2290 /* tmp = pow(src.y, src.w)
2292 if (dst
.mask
& TGSI_WRITEMASK_Z
) {
2293 if (!submit_op2(emit
, inst_token( SVGA3DOP_POW
),
2302 if (dst
.mask
& TGSI_WRITEMASK_Y
) {
2303 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2304 writemask(tmp
, TGSI_WRITEMASK_Y
),
2309 /* Can't quite do this with emit conditional due to the extra
2310 * writemask on the predicated mov:
2313 SVGA3dShaderDestToken pred_reg
= dst_register( SVGA3DREG_PREDICATE
, 0 );
2314 SVGA3dShaderInstToken setp_token
, mov_token
;
2315 struct src_register predsrc
;
2317 setp_token
= inst_token( SVGA3DOP_SETP
);
2318 mov_token
= inst_token( SVGA3DOP_MOV
);
2320 setp_token
.control
= SVGA3DOPCOMP_GT
;
2322 /* D3D vs GL semantics:
2325 predsrc
= swizzle(src0
, 0, 0, 1, 1); /* D3D */
2327 predsrc
= swizzle(src0
, 0, 0, 0, 0); /* GL */
2329 /* SETP src0.xxyy, GT, {0}.x */
2330 if (!submit_op2( emit
, setp_token
, pred_reg
,
2332 swizzle(zero
, 0, 0, 0, 0) ))
2336 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
,
2337 swizzle(zero
, 3, 0, 0, 3 )))
2340 /* MOV dst.yz, tmp (predicated)
2342 * Note that the predicate reg (and possible modifiers) is passed
2343 * as the first source argument.
2345 if (dst
.mask
& TGSI_WRITEMASK_YZ
) {
2346 mov_token
.predicated
= 1;
2347 if (!submit_op2( emit
, mov_token
,
2348 writemask(dst
, TGSI_WRITEMASK_YZ
),
2349 src( pred_reg
), src( tmp
) ))
2360 emit_ex2(struct svga_shader_emitter
*emit
,
2361 const struct tgsi_full_instruction
*insn
)
2363 SVGA3dShaderInstToken inst
;
2364 SVGA3dShaderDestToken dst
;
2365 struct src_register src0
;
2367 inst
= inst_token( SVGA3DOP_EXP
);
2368 dst
= translate_dst_register( emit
, insn
, 0 );
2369 src0
= translate_src_register( emit
, &insn
->Src
[0] );
2370 src0
= scalar( src0
, TGSI_SWIZZLE_X
);
2372 if (dst
.mask
!= TGSI_WRITEMASK_XYZW
) {
2373 SVGA3dShaderDestToken tmp
= get_temp( emit
);
2375 if (!submit_op1( emit
, inst
, tmp
, src0
))
2378 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2380 scalar( src( tmp
), TGSI_SWIZZLE_X
) );
2383 return submit_op1( emit
, inst
, dst
, src0
);
2388 emit_log(struct svga_shader_emitter
*emit
,
2389 const struct tgsi_full_instruction
*insn
)
2391 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2392 struct src_register src0
=
2393 translate_src_register( emit
, &insn
->Src
[0] );
2394 struct src_register zero
= get_zero_immediate( emit
);
2395 SVGA3dShaderDestToken abs_tmp
;
2396 struct src_register abs_src0
;
2397 SVGA3dShaderDestToken log2_abs
;
2401 if (dst
.mask
& TGSI_WRITEMASK_Z
)
2403 else if (dst
.mask
& TGSI_WRITEMASK_XY
)
2404 log2_abs
= get_temp( emit
);
2408 /* If z is being written, fill it with log2( abs( src0 ) ).
2410 if (dst
.mask
& TGSI_WRITEMASK_XYZ
) {
2411 if (!src0
.base
.srcMod
|| src0
.base
.srcMod
== SVGA3DSRCMOD_ABS
)
2414 abs_tmp
= get_temp( emit
);
2416 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2421 abs_src0
= src( abs_tmp
);
2424 abs_src0
= absolute( scalar( abs_src0
, TGSI_SWIZZLE_X
) );
2426 if (!submit_op1( emit
, inst_token( SVGA3DOP_LOG
),
2427 writemask( log2_abs
, TGSI_WRITEMASK_Z
),
2432 if (dst
.mask
& TGSI_WRITEMASK_XY
) {
2433 SVGA3dShaderDestToken floor_log2
;
2435 if (dst
.mask
& TGSI_WRITEMASK_X
)
2438 floor_log2
= get_temp( emit
);
2440 /* If x is being written, fill it with floor( log2( abs( src0 ) ) ).
2442 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
),
2443 writemask( floor_log2
, TGSI_WRITEMASK_X
),
2444 scalar( src( log2_abs
), TGSI_SWIZZLE_Z
) ) )
2447 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
),
2448 writemask( floor_log2
, TGSI_WRITEMASK_X
),
2449 scalar( src( log2_abs
), TGSI_SWIZZLE_Z
),
2450 negate( src( floor_log2
) ) ) )
2453 /* If y is being written, fill it with
2454 * abs ( src0 ) / ( 2 ^ floor( log2( abs( src0 ) ) ) ).
2456 if (dst
.mask
& TGSI_WRITEMASK_Y
) {
2457 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXP
),
2458 writemask( dst
, TGSI_WRITEMASK_Y
),
2459 negate( scalar( src( floor_log2
),
2460 TGSI_SWIZZLE_X
) ) ) )
2463 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
2464 writemask( dst
, TGSI_WRITEMASK_Y
),
2470 if (!(dst
.mask
& TGSI_WRITEMASK_X
))
2471 release_temp( emit
, floor_log2
);
2473 if (!(dst
.mask
& TGSI_WRITEMASK_Z
))
2474 release_temp( emit
, log2_abs
);
2477 if (dst
.mask
& TGSI_WRITEMASK_XYZ
&& src0
.base
.srcMod
&&
2478 src0
.base
.srcMod
!= SVGA3DSRCMOD_ABS
)
2479 release_temp( emit
, abs_tmp
);
2481 /* If w is being written, fill it with one.
2483 if (dst
.mask
& TGSI_WRITEMASK_W
) {
2484 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2485 writemask(dst
, TGSI_WRITEMASK_W
),
2486 scalar( zero
, TGSI_SWIZZLE_W
) ))
2495 * Translate TGSI TRUNC or ROUND instruction.
2496 * We need to truncate toward zero. Ex: trunc(-1.9) = -1
2497 * Different approaches are needed for VS versus PS.
2500 emit_trunc_round(struct svga_shader_emitter
*emit
,
2501 const struct tgsi_full_instruction
*insn
,
2504 SVGA3dShaderDestToken dst
= translate_dst_register(emit
, insn
, 0);
2505 const struct src_register src0
=
2506 translate_src_register(emit
, &insn
->Src
[0] );
2507 SVGA3dShaderDestToken t1
= get_temp(emit
);
2510 SVGA3dShaderDestToken t0
= get_temp(emit
);
2511 struct src_register half
= get_half_immediate(emit
);
2513 /* t0 = abs(src0) + 0.5 */
2514 if (!submit_op2(emit
, inst_token(SVGA3DOP_ADD
), t0
,
2515 absolute(src0
), half
))
2518 /* t1 = fract(t0) */
2519 if (!submit_op1(emit
, inst_token(SVGA3DOP_FRC
), t1
, src(t0
)))
2523 if (!submit_op2(emit
, inst_token(SVGA3DOP_ADD
), t1
, src(t0
),
2530 /* t1 = fract(abs(src0)) */
2531 if (!submit_op1(emit
, inst_token(SVGA3DOP_FRC
), t1
, absolute(src0
)))
2534 /* t1 = abs(src0) - t1 */
2535 if (!submit_op2(emit
, inst_token(SVGA3DOP_ADD
), t1
, absolute(src0
),
2541 * Now we need to multiply t1 by the sign of the original value.
2543 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2544 /* For VS: use SGN instruction */
2545 /* Need two extra/dummy registers: */
2546 SVGA3dShaderDestToken t2
= get_temp(emit
), t3
= get_temp(emit
),
2547 t4
= get_temp(emit
);
2549 /* t2 = sign(src0) */
2550 if (!submit_op3(emit
, inst_token(SVGA3DOP_SGN
), t2
, src0
,
2555 if (!submit_op2(emit
, inst_token(SVGA3DOP_MUL
), dst
, src(t1
), src(t2
)))
2559 /* For FS: Use CMP instruction */
2560 return submit_op3(emit
, inst_token( SVGA3DOP_CMP
), dst
,
2561 src0
, src(t1
), negate(src(t1
)));
2569 emit_bgnsub(struct svga_shader_emitter
*emit
,
2571 const struct tgsi_full_instruction
*insn
)
2575 /* Note that we've finished the main function and are now emitting
2576 * subroutines. This affects how we terminate the generated
2579 emit
->in_main_func
= FALSE
;
2581 for (i
= 0; i
< emit
->nr_labels
; i
++) {
2582 if (emit
->label
[i
] == position
) {
2583 return (emit_instruction( emit
, inst_token( SVGA3DOP_RET
) ) &&
2584 emit_instruction( emit
, inst_token( SVGA3DOP_LABEL
) ) &&
2585 emit_src( emit
, src_register( SVGA3DREG_LABEL
, i
)));
2595 emit_call(struct svga_shader_emitter
*emit
,
2596 const struct tgsi_full_instruction
*insn
)
2598 unsigned position
= insn
->Label
.Label
;
2601 for (i
= 0; i
< emit
->nr_labels
; i
++) {
2602 if (emit
->label
[i
] == position
)
2606 if (emit
->nr_labels
== Elements(emit
->label
))
2609 if (i
== emit
->nr_labels
) {
2610 emit
->label
[i
] = position
;
2614 return (emit_instruction( emit
, inst_token( SVGA3DOP_CALL
) ) &&
2615 emit_src( emit
, src_register( SVGA3DREG_LABEL
, i
)));
2620 * Called at the end of the shader. Actually, emit special "fix-up"
2621 * code for the vertex/fragment shader.
2624 emit_end(struct svga_shader_emitter
*emit
)
2626 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2627 return emit_vs_postamble( emit
);
2630 return emit_ps_postamble( emit
);
2637 svga_emit_instruction(struct svga_shader_emitter
*emit
,
2639 const struct tgsi_full_instruction
*insn
)
2641 switch (insn
->Instruction
.Opcode
) {
2643 case TGSI_OPCODE_ARL
:
2644 return emit_arl( emit
, insn
);
2646 case TGSI_OPCODE_TEX
:
2647 case TGSI_OPCODE_TXB
:
2648 case TGSI_OPCODE_TXP
:
2649 case TGSI_OPCODE_TXL
:
2650 case TGSI_OPCODE_TXD
:
2651 return emit_tex( emit
, insn
);
2653 case TGSI_OPCODE_DDX
:
2654 case TGSI_OPCODE_DDY
:
2655 return emit_deriv( emit
, insn
);
2657 case TGSI_OPCODE_BGNSUB
:
2658 return emit_bgnsub( emit
, position
, insn
);
2660 case TGSI_OPCODE_ENDSUB
:
2663 case TGSI_OPCODE_CAL
:
2664 return emit_call( emit
, insn
);
2666 case TGSI_OPCODE_FLR
:
2667 return emit_floor( emit
, insn
);
2669 case TGSI_OPCODE_TRUNC
:
2670 return emit_trunc_round( emit
, insn
, FALSE
);
2672 case TGSI_OPCODE_ROUND
:
2673 return emit_trunc_round( emit
, insn
, TRUE
);
2675 case TGSI_OPCODE_CEIL
:
2676 return emit_ceil( emit
, insn
);
2678 case TGSI_OPCODE_CMP
:
2679 return emit_cmp( emit
, insn
);
2681 case TGSI_OPCODE_DIV
:
2682 return emit_div( emit
, insn
);
2684 case TGSI_OPCODE_DP2
:
2685 return emit_dp2( emit
, insn
);
2687 case TGSI_OPCODE_DPH
:
2688 return emit_dph( emit
, insn
);
2690 case TGSI_OPCODE_NRM
:
2691 return emit_nrm( emit
, insn
);
2693 case TGSI_OPCODE_COS
:
2694 return emit_cos( emit
, insn
);
2696 case TGSI_OPCODE_SIN
:
2697 return emit_sin( emit
, insn
);
2699 case TGSI_OPCODE_SCS
:
2700 return emit_sincos( emit
, insn
);
2702 case TGSI_OPCODE_END
:
2703 /* TGSI always finishes the main func with an END */
2704 return emit_end( emit
);
2706 case TGSI_OPCODE_KIL
:
2707 return emit_kil( emit
, insn
);
2709 /* Selection opcodes. The underlying language is fairly
2710 * non-orthogonal about these.
2712 case TGSI_OPCODE_SEQ
:
2713 return emit_select_op( emit
, PIPE_FUNC_EQUAL
, insn
);
2715 case TGSI_OPCODE_SNE
:
2716 return emit_select_op( emit
, PIPE_FUNC_NOTEQUAL
, insn
);
2718 case TGSI_OPCODE_SGT
:
2719 return emit_select_op( emit
, PIPE_FUNC_GREATER
, insn
);
2721 case TGSI_OPCODE_SGE
:
2722 return emit_select_op( emit
, PIPE_FUNC_GEQUAL
, insn
);
2724 case TGSI_OPCODE_SLT
:
2725 return emit_select_op( emit
, PIPE_FUNC_LESS
, insn
);
2727 case TGSI_OPCODE_SLE
:
2728 return emit_select_op( emit
, PIPE_FUNC_LEQUAL
, insn
);
2730 case TGSI_OPCODE_SUB
:
2731 return emit_sub( emit
, insn
);
2733 case TGSI_OPCODE_POW
:
2734 return emit_pow( emit
, insn
);
2736 case TGSI_OPCODE_EX2
:
2737 return emit_ex2( emit
, insn
);
2739 case TGSI_OPCODE_EXP
:
2740 return emit_exp( emit
, insn
);
2742 case TGSI_OPCODE_LOG
:
2743 return emit_log( emit
, insn
);
2745 case TGSI_OPCODE_LG2
:
2746 return emit_scalar_op1( emit
, SVGA3DOP_LOG
, insn
);
2748 case TGSI_OPCODE_RSQ
:
2749 return emit_scalar_op1( emit
, SVGA3DOP_RSQ
, insn
);
2751 case TGSI_OPCODE_RCP
:
2752 return emit_scalar_op1( emit
, SVGA3DOP_RCP
, insn
);
2754 case TGSI_OPCODE_CONT
:
2755 case TGSI_OPCODE_RET
:
2756 /* This is a noop -- we tell mesa that we can't support RET
2757 * within a function (early return), so this will always be
2758 * followed by an ENDSUB.
2762 /* These aren't actually used by any of the frontends we care
2765 case TGSI_OPCODE_CLAMP
:
2766 case TGSI_OPCODE_AND
:
2767 case TGSI_OPCODE_OR
:
2768 case TGSI_OPCODE_I2F
:
2769 case TGSI_OPCODE_NOT
:
2770 case TGSI_OPCODE_SHL
:
2771 case TGSI_OPCODE_ISHR
:
2772 case TGSI_OPCODE_XOR
:
2775 case TGSI_OPCODE_IF
:
2776 return emit_if( emit
, insn
);
2777 case TGSI_OPCODE_ELSE
:
2778 return emit_else( emit
, insn
);
2779 case TGSI_OPCODE_ENDIF
:
2780 return emit_endif( emit
, insn
);
2782 case TGSI_OPCODE_BGNLOOP
:
2783 return emit_bgnloop2( emit
, insn
);
2784 case TGSI_OPCODE_ENDLOOP
:
2785 return emit_endloop2( emit
, insn
);
2786 case TGSI_OPCODE_BRK
:
2787 return emit_brk( emit
, insn
);
2789 case TGSI_OPCODE_XPD
:
2790 return emit_xpd( emit
, insn
);
2792 case TGSI_OPCODE_KILP
:
2793 return emit_kilp( emit
, insn
);
2795 case TGSI_OPCODE_DST
:
2796 return emit_dst_insn( emit
, insn
);
2798 case TGSI_OPCODE_LIT
:
2799 return emit_lit( emit
, insn
);
2801 case TGSI_OPCODE_LRP
:
2802 return emit_lrp( emit
, insn
);
2804 case TGSI_OPCODE_SSG
:
2805 return emit_ssg( emit
, insn
);
2809 unsigned opcode
= translate_opcode(insn
->Instruction
.Opcode
);
2811 if (opcode
== SVGA3DOP_LAST_INST
)
2814 if (!emit_simple_instruction( emit
, opcode
, insn
))
2824 svga_emit_immediate(struct svga_shader_emitter
*emit
,
2825 struct tgsi_full_immediate
*imm
)
2827 static const float id
[4] = {0,0,0,1};
2831 assert(1 <= imm
->Immediate
.NrTokens
&& imm
->Immediate
.NrTokens
<= 5);
2832 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++) {
2833 float f
= imm
->u
[i
].Float
;
2834 value
[i
] = util_is_inf_or_nan(f
) ? 0.0f
: f
;
2837 for ( ; i
< 4; i
++ )
2840 return emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
2841 emit
->imm_start
+ emit
->internal_imm_count
++,
2842 value
[0], value
[1], value
[2], value
[3]);
2847 make_immediate(struct svga_shader_emitter
*emit
,
2848 float a
, float b
, float c
, float d
,
2849 struct src_register
*out
)
2851 unsigned idx
= emit
->nr_hw_float_const
++;
2853 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
2857 *out
= src_register( SVGA3DREG_CONST
, idx
);
2864 emit_vs_preamble(struct svga_shader_emitter
*emit
)
2866 if (!emit
->key
.vkey
.need_prescale
) {
2867 if (!make_immediate( emit
, 0, 0, .5, .5,
2877 emit_ps_preamble(struct svga_shader_emitter
*emit
)
2879 if (emit
->ps_reads_pos
&& emit
->info
.reads_z
) {
2881 * Assemble the position from various bits of inputs. Depth and W are
2882 * passed in a texcoord this is due to D3D's vPos not hold Z or W.
2883 * Also fixup the perspective interpolation.
2885 * temp_pos.xy = vPos.xy
2886 * temp_pos.w = rcp(texcoord1.w);
2887 * temp_pos.z = texcoord1.z * temp_pos.w;
2889 if (!submit_op1( emit
,
2890 inst_token(SVGA3DOP_MOV
),
2891 writemask( emit
->ps_temp_pos
, TGSI_WRITEMASK_XY
),
2892 emit
->ps_true_pos
))
2895 if (!submit_op1( emit
,
2896 inst_token(SVGA3DOP_RCP
),
2897 writemask( emit
->ps_temp_pos
, TGSI_WRITEMASK_W
),
2898 scalar( emit
->ps_depth_pos
, TGSI_SWIZZLE_W
) ))
2901 if (!submit_op2( emit
,
2902 inst_token(SVGA3DOP_MUL
),
2903 writemask( emit
->ps_temp_pos
, TGSI_WRITEMASK_Z
),
2904 scalar( emit
->ps_depth_pos
, TGSI_SWIZZLE_Z
),
2905 scalar( src(emit
->ps_temp_pos
), TGSI_SWIZZLE_W
) ))
2914 emit_ps_postamble(struct svga_shader_emitter
*emit
)
2918 /* PS oDepth is incredibly fragile and it's very hard to catch the
2919 * types of usage that break it during shader emit. Easier just to
2920 * redirect the main program to a temporary and then only touch
2921 * oDepth with a hand-crafted MOV below.
2923 if (SVGA3dShaderGetRegType(emit
->true_pos
.value
) != 0) {
2924 if (!submit_op1( emit
,
2925 inst_token(SVGA3DOP_MOV
),
2927 scalar(src(emit
->temp_pos
), TGSI_SWIZZLE_Z
) ))
2931 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
2932 if (SVGA3dShaderGetRegType(emit
->true_col
[i
].value
) != 0) {
2933 /* Potentially override output colors with white for XOR
2934 * logicop workaround.
2936 if (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
2937 emit
->key
.fkey
.white_fragments
) {
2938 struct src_register one
= scalar( get_zero_immediate( emit
),
2941 if (!submit_op1( emit
,
2942 inst_token(SVGA3DOP_MOV
),
2948 if (!submit_op1( emit
,
2949 inst_token(SVGA3DOP_MOV
),
2951 src(emit
->temp_col
[i
]) ))
2962 emit_vs_postamble(struct svga_shader_emitter
*emit
)
2964 /* PSIZ output is incredibly fragile and it's very hard to catch
2965 * the types of usage that break it during shader emit. Easier
2966 * just to redirect the main program to a temporary and then only
2967 * touch PSIZ with a hand-crafted MOV below.
2969 if (SVGA3dShaderGetRegType(emit
->true_psiz
.value
) != 0) {
2970 if (!submit_op1( emit
,
2971 inst_token(SVGA3DOP_MOV
),
2973 scalar(src(emit
->temp_psiz
), TGSI_SWIZZLE_X
) ))
2977 /* Need to perform various manipulations on vertex position to cope
2978 * with the different GL and D3D clip spaces.
2980 if (emit
->key
.vkey
.need_prescale
) {
2981 SVGA3dShaderDestToken temp_pos
= emit
->temp_pos
;
2982 SVGA3dShaderDestToken depth
= emit
->depth_pos
;
2983 SVGA3dShaderDestToken pos
= emit
->true_pos
;
2984 unsigned offset
= emit
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
2985 struct src_register prescale_scale
= src_register( SVGA3DREG_CONST
,
2987 struct src_register prescale_trans
= src_register( SVGA3DREG_CONST
,
2990 if (!submit_op1( emit
,
2991 inst_token(SVGA3DOP_MOV
),
2992 writemask(depth
, TGSI_WRITEMASK_W
),
2993 scalar(src(temp_pos
), TGSI_SWIZZLE_W
) ))
2996 /* MUL temp_pos.xyz, temp_pos, prescale.scale
2997 * MAD result.position, temp_pos.wwww, prescale.trans, temp_pos
2998 * --> Note that prescale.trans.w == 0
3000 if (!submit_op2( emit
,
3001 inst_token(SVGA3DOP_MUL
),
3002 writemask(temp_pos
, TGSI_WRITEMASK_XYZ
),
3007 if (!submit_op3( emit
,
3008 inst_token(SVGA3DOP_MAD
),
3010 swizzle(src(temp_pos
), 3, 3, 3, 3),
3015 /* Also write to depth value */
3016 if (!submit_op3( emit
,
3017 inst_token(SVGA3DOP_MAD
),
3018 writemask(depth
, TGSI_WRITEMASK_Z
),
3019 swizzle(src(temp_pos
), 3, 3, 3, 3),
3025 SVGA3dShaderDestToken temp_pos
= emit
->temp_pos
;
3026 SVGA3dShaderDestToken depth
= emit
->depth_pos
;
3027 SVGA3dShaderDestToken pos
= emit
->true_pos
;
3028 struct src_register imm_0055
= emit
->imm_0055
;
3030 /* Adjust GL clipping coordinate space to hardware (D3D-style):
3032 * DP4 temp_pos.z, {0,0,.5,.5}, temp_pos
3033 * MOV result.position, temp_pos
3035 if (!submit_op2( emit
,
3036 inst_token(SVGA3DOP_DP4
),
3037 writemask(temp_pos
, TGSI_WRITEMASK_Z
),
3042 if (!submit_op1( emit
,
3043 inst_token(SVGA3DOP_MOV
),
3048 /* Move the manipulated depth into the extra texcoord reg */
3049 if (!submit_op1( emit
,
3050 inst_token(SVGA3DOP_MOV
),
3051 writemask(depth
, TGSI_WRITEMASK_ZW
),
3061 * For the pixel shader: emit the code which chooses the front
3062 * or back face color depending on triangle orientation.
3065 * 1: COLOR = FrontColor;
3067 * 3: COLOR = BackColor;
3071 emit_light_twoside(struct svga_shader_emitter
*emit
)
3073 struct src_register vface
, zero
;
3074 struct src_register front
[2];
3075 struct src_register back
[2];
3076 SVGA3dShaderDestToken color
[2];
3077 int count
= emit
->internal_color_count
;
3079 SVGA3dShaderInstToken if_token
;
3084 vface
= get_vface( emit
);
3085 zero
= get_zero_immediate( emit
);
3087 /* Can't use get_temp() to allocate the color reg as such
3088 * temporaries will be reclaimed after each instruction by the call
3089 * to reset_temp_regs().
3091 for (i
= 0; i
< count
; i
++) {
3092 color
[i
] = dst_register( SVGA3DREG_TEMP
, emit
->nr_hw_temp
++ );
3093 front
[i
] = emit
->input_map
[emit
->internal_color_idx
[i
]];
3095 /* Back is always the next input:
3098 back
[i
].base
.num
= front
[i
].base
.num
+ 1;
3100 /* Reassign the input_map to the actual front-face color:
3102 emit
->input_map
[emit
->internal_color_idx
[i
]] = src(color
[i
]);
3105 if_token
= inst_token( SVGA3DOP_IFC
);
3107 if (emit
->key
.fkey
.front_ccw
)
3108 if_token
.control
= SVGA3DOPCOMP_LT
;
3110 if_token
.control
= SVGA3DOPCOMP_GT
;
3112 zero
= scalar(zero
, TGSI_SWIZZLE_X
);
3114 if (!(emit_instruction( emit
, if_token
) &&
3115 emit_src( emit
, vface
) &&
3116 emit_src( emit
, zero
) ))
3119 for (i
= 0; i
< count
; i
++) {
3120 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), color
[i
], front
[i
] ))
3124 if (!(emit_instruction( emit
, inst_token( SVGA3DOP_ELSE
))))
3127 for (i
= 0; i
< count
; i
++) {
3128 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), color
[i
], back
[i
] ))
3132 if (!emit_instruction( emit
, inst_token( SVGA3DOP_ENDIF
) ))
3140 * 0: SETP_GT TEMP, VFACE, 0
3141 * where TEMP is a fake frontface register
3144 emit_frontface(struct svga_shader_emitter
*emit
)
3146 struct src_register vface
, zero
;
3147 SVGA3dShaderDestToken temp
;
3148 struct src_register pass
, fail
;
3150 vface
= get_vface( emit
);
3151 zero
= get_zero_immediate( emit
);
3153 /* Can't use get_temp() to allocate the fake frontface reg as such
3154 * temporaries will be reclaimed after each instruction by the call
3155 * to reset_temp_regs().
3157 temp
= dst_register( SVGA3DREG_TEMP
,
3158 emit
->nr_hw_temp
++ );
3160 if (emit
->key
.fkey
.front_ccw
) {
3161 pass
= scalar( zero
, TGSI_SWIZZLE_X
);
3162 fail
= scalar( zero
, TGSI_SWIZZLE_W
);
3164 pass
= scalar( zero
, TGSI_SWIZZLE_W
);
3165 fail
= scalar( zero
, TGSI_SWIZZLE_X
);
3168 if (!emit_conditional(emit
, PIPE_FUNC_GREATER
,
3169 temp
, vface
, scalar( zero
, TGSI_SWIZZLE_X
),
3173 /* Reassign the input_map to the actual front-face color:
3175 emit
->input_map
[emit
->internal_frontface_idx
] = src(temp
);
3182 * Emit code to invert the T component of the incoming texture coordinate.
3183 * This is used for drawing point sprites when
3184 * pipe_rasterizer_state::sprite_coord_mode == PIPE_SPRITE_COORD_LOWER_LEFT.
3187 emit_inverted_texcoords(struct svga_shader_emitter
*emit
)
3189 struct src_register zero
= get_zero_immediate(emit
);
3190 struct src_register pos_neg_one
= get_pos_neg_one_immediate( emit
);
3191 unsigned inverted_texcoords
= emit
->inverted_texcoords
;
3193 while (inverted_texcoords
) {
3194 const unsigned unit
= ffs(inverted_texcoords
) - 1;
3196 assert(emit
->inverted_texcoords
& (1 << unit
));
3198 assert(unit
< Elements(emit
->ps_true_texcoord
));
3200 assert(unit
< Elements(emit
->ps_inverted_texcoord_input
));
3202 assert(emit
->ps_inverted_texcoord_input
[unit
]
3203 < Elements(emit
->input_map
));
3205 /* inverted = coord * (1, -1, 1, 1) + (0, 1, 0, 0) */
3206 if (!submit_op3(emit
,
3207 inst_token(SVGA3DOP_MAD
),
3208 dst(emit
->ps_inverted_texcoord
[unit
]),
3209 emit
->ps_true_texcoord
[unit
],
3210 swizzle(pos_neg_one
, 0, 3, 0, 0), /* (1, -1, 1, 1) */
3211 swizzle(zero
, 0, 3, 0, 0))) /* (0, 1, 0, 0) */
3214 /* Reassign the input_map entry to the new texcoord register */
3215 emit
->input_map
[emit
->ps_inverted_texcoord_input
[unit
]] =
3216 emit
->ps_inverted_texcoord
[unit
];
3218 inverted_texcoords
&= ~(1 << unit
);
3226 needs_to_create_zero( struct svga_shader_emitter
*emit
)
3230 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
3231 if (emit
->key
.fkey
.light_twoside
)
3234 if (emit
->key
.fkey
.white_fragments
)
3237 if (emit
->emit_frontface
)
3240 if (emit
->info
.opcode_count
[TGSI_OPCODE_DST
] >= 1 ||
3241 emit
->info
.opcode_count
[TGSI_OPCODE_SSG
] >= 1 ||
3242 emit
->info
.opcode_count
[TGSI_OPCODE_LIT
] >= 1)
3245 if (emit
->inverted_texcoords
)
3248 /* look for any PIPE_SWIZZLE_ZERO/ONE terms */
3249 for (i
= 0; i
< emit
->key
.fkey
.num_textures
; i
++) {
3250 if (emit
->key
.fkey
.tex
[i
].swizzle_r
> PIPE_SWIZZLE_ALPHA
||
3251 emit
->key
.fkey
.tex
[i
].swizzle_g
> PIPE_SWIZZLE_ALPHA
||
3252 emit
->key
.fkey
.tex
[i
].swizzle_b
> PIPE_SWIZZLE_ALPHA
||
3253 emit
->key
.fkey
.tex
[i
].swizzle_a
> PIPE_SWIZZLE_ALPHA
)
3257 for (i
= 0; i
< emit
->key
.fkey
.num_textures
; i
++) {
3258 if (emit
->key
.fkey
.tex
[i
].compare_mode
3259 == PIPE_TEX_COMPARE_R_TO_TEXTURE
)
3264 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
3265 if (emit
->info
.opcode_count
[TGSI_OPCODE_CMP
] >= 1)
3269 if (emit
->info
.opcode_count
[TGSI_OPCODE_IF
] >= 1 ||
3270 emit
->info
.opcode_count
[TGSI_OPCODE_BGNLOOP
] >= 1 ||
3271 emit
->info
.opcode_count
[TGSI_OPCODE_DDX
] >= 1 ||
3272 emit
->info
.opcode_count
[TGSI_OPCODE_DDY
] >= 1 ||
3273 emit
->info
.opcode_count
[TGSI_OPCODE_ROUND
] >= 1 ||
3274 emit
->info
.opcode_count
[TGSI_OPCODE_SGE
] >= 1 ||
3275 emit
->info
.opcode_count
[TGSI_OPCODE_SGT
] >= 1 ||
3276 emit
->info
.opcode_count
[TGSI_OPCODE_SLE
] >= 1 ||
3277 emit
->info
.opcode_count
[TGSI_OPCODE_SLT
] >= 1 ||
3278 emit
->info
.opcode_count
[TGSI_OPCODE_SNE
] >= 1 ||
3279 emit
->info
.opcode_count
[TGSI_OPCODE_SEQ
] >= 1 ||
3280 emit
->info
.opcode_count
[TGSI_OPCODE_EXP
] >= 1 ||
3281 emit
->info
.opcode_count
[TGSI_OPCODE_LOG
] >= 1 ||
3282 emit
->info
.opcode_count
[TGSI_OPCODE_XPD
] >= 1 ||
3283 emit
->info
.opcode_count
[TGSI_OPCODE_KILP
] >= 1)
3291 needs_to_create_loop_const( struct svga_shader_emitter
*emit
)
3293 return (emit
->info
.opcode_count
[TGSI_OPCODE_BGNLOOP
] >= 1);
3298 needs_to_create_arl_consts( struct svga_shader_emitter
*emit
)
3300 return (emit
->num_arl_consts
> 0);
3305 pre_parse_add_indirect( struct svga_shader_emitter
*emit
,
3306 int num
, int current_arl
)
3311 for (i
= 0; i
< emit
->num_arl_consts
; ++i
) {
3312 if (emit
->arl_consts
[i
].arl_num
== current_arl
)
3316 if (emit
->num_arl_consts
== i
) {
3317 ++emit
->num_arl_consts
;
3319 emit
->arl_consts
[i
].number
= (emit
->arl_consts
[i
].number
> num
) ?
3321 emit
->arl_consts
[i
].number
;
3322 emit
->arl_consts
[i
].arl_num
= current_arl
;
3328 pre_parse_instruction( struct svga_shader_emitter
*emit
,
3329 const struct tgsi_full_instruction
*insn
,
3332 if (insn
->Src
[0].Register
.Indirect
&&
3333 insn
->Src
[0].Indirect
.File
== TGSI_FILE_ADDRESS
) {
3334 const struct tgsi_full_src_register
*reg
= &insn
->Src
[0];
3335 if (reg
->Register
.Index
< 0) {
3336 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
3340 if (insn
->Src
[1].Register
.Indirect
&&
3341 insn
->Src
[1].Indirect
.File
== TGSI_FILE_ADDRESS
) {
3342 const struct tgsi_full_src_register
*reg
= &insn
->Src
[1];
3343 if (reg
->Register
.Index
< 0) {
3344 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
3348 if (insn
->Src
[2].Register
.Indirect
&&
3349 insn
->Src
[2].Indirect
.File
== TGSI_FILE_ADDRESS
) {
3350 const struct tgsi_full_src_register
*reg
= &insn
->Src
[2];
3351 if (reg
->Register
.Index
< 0) {
3352 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
3361 pre_parse_tokens( struct svga_shader_emitter
*emit
,
3362 const struct tgsi_token
*tokens
)
3364 struct tgsi_parse_context parse
;
3365 int current_arl
= 0;
3367 tgsi_parse_init( &parse
, tokens
);
3369 while (!tgsi_parse_end_of_tokens( &parse
)) {
3370 tgsi_parse_token( &parse
);
3371 switch (parse
.FullToken
.Token
.Type
) {
3372 case TGSI_TOKEN_TYPE_IMMEDIATE
:
3373 case TGSI_TOKEN_TYPE_DECLARATION
:
3375 case TGSI_TOKEN_TYPE_INSTRUCTION
:
3376 if (parse
.FullToken
.FullInstruction
.Instruction
.Opcode
==
3380 if (!pre_parse_instruction( emit
, &parse
.FullToken
.FullInstruction
,
3394 svga_shader_emit_helpers(struct svga_shader_emitter
*emit
)
3396 if (needs_to_create_zero( emit
)) {
3397 create_zero_immediate( emit
);
3399 if (needs_to_create_loop_const( emit
)) {
3400 create_loop_const( emit
);
3402 if (needs_to_create_arl_consts( emit
)) {
3403 create_arl_consts( emit
);
3406 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
3407 if (!emit_ps_preamble( emit
))
3410 if (emit
->key
.fkey
.light_twoside
) {
3411 if (!emit_light_twoside( emit
))
3414 if (emit
->emit_frontface
) {
3415 if (!emit_frontface( emit
))
3418 if (emit
->inverted_texcoords
) {
3419 if (!emit_inverted_texcoords( emit
))
3429 svga_shader_emit_instructions(struct svga_shader_emitter
*emit
,
3430 const struct tgsi_token
*tokens
)
3432 struct tgsi_parse_context parse
;
3434 boolean helpers_emitted
= FALSE
;
3435 unsigned line_nr
= 0;
3437 tgsi_parse_init( &parse
, tokens
);
3438 emit
->internal_imm_count
= 0;
3440 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
3441 ret
= emit_vs_preamble( emit
);
3446 pre_parse_tokens(emit
, tokens
);
3448 while (!tgsi_parse_end_of_tokens( &parse
)) {
3449 tgsi_parse_token( &parse
);
3451 switch (parse
.FullToken
.Token
.Type
) {
3452 case TGSI_TOKEN_TYPE_IMMEDIATE
:
3453 ret
= svga_emit_immediate( emit
, &parse
.FullToken
.FullImmediate
);
3458 case TGSI_TOKEN_TYPE_DECLARATION
:
3459 ret
= svga_translate_decl_sm30( emit
, &parse
.FullToken
.FullDeclaration
);
3464 case TGSI_TOKEN_TYPE_INSTRUCTION
:
3465 if (!helpers_emitted
) {
3466 if (!svga_shader_emit_helpers( emit
))
3468 helpers_emitted
= TRUE
;
3470 ret
= svga_emit_instruction( emit
,
3472 &parse
.FullToken
.FullInstruction
);
3480 reset_temp_regs( emit
);
3483 /* Need to terminate the current subroutine. Note that the
3484 * hardware doesn't tolerate shaders without sub-routines
3485 * terminating with RET+END.
3487 if (!emit
->in_main_func
) {
3488 ret
= emit_instruction( emit
, inst_token( SVGA3DOP_RET
) );
3493 assert(emit
->dynamic_branching_level
== 0);
3495 /* Need to terminate the whole shader:
3497 ret
= emit_instruction( emit
, inst_token( SVGA3DOP_END
) );
3502 tgsi_parse_free( &parse
);