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"
32 #include "util/u_pstipple.h"
34 #include "svga_tgsi_emit.h"
35 #include "svga_context.h"
38 static boolean
emit_vs_postamble( struct svga_shader_emitter
*emit
);
39 static boolean
emit_ps_postamble( struct svga_shader_emitter
*emit
);
43 translate_opcode(uint opcode
)
46 case TGSI_OPCODE_ADD
: return SVGA3DOP_ADD
;
47 case TGSI_OPCODE_DP3
: return SVGA3DOP_DP3
;
48 case TGSI_OPCODE_DP4
: return SVGA3DOP_DP4
;
49 case TGSI_OPCODE_FRC
: return SVGA3DOP_FRC
;
50 case TGSI_OPCODE_MAD
: return SVGA3DOP_MAD
;
51 case TGSI_OPCODE_MAX
: return SVGA3DOP_MAX
;
52 case TGSI_OPCODE_MIN
: return SVGA3DOP_MIN
;
53 case TGSI_OPCODE_MOV
: return SVGA3DOP_MOV
;
54 case TGSI_OPCODE_MUL
: return SVGA3DOP_MUL
;
55 case TGSI_OPCODE_NOP
: return SVGA3DOP_NOP
;
57 assert(!"svga: unexpected opcode in translate_opcode()");
58 return SVGA3DOP_LAST_INST
;
64 translate_file(unsigned file
)
67 case TGSI_FILE_TEMPORARY
: return SVGA3DREG_TEMP
;
68 case TGSI_FILE_INPUT
: return SVGA3DREG_INPUT
;
69 case TGSI_FILE_OUTPUT
: return SVGA3DREG_OUTPUT
; /* VS3.0+ only */
70 case TGSI_FILE_IMMEDIATE
: return SVGA3DREG_CONST
;
71 case TGSI_FILE_CONSTANT
: return SVGA3DREG_CONST
;
72 case TGSI_FILE_SAMPLER
: return SVGA3DREG_SAMPLER
;
73 case TGSI_FILE_ADDRESS
: return SVGA3DREG_ADDR
;
75 assert(!"svga: unexpected register file in translate_file()");
76 return SVGA3DREG_TEMP
;
82 * Translate a TGSI destination register to an SVGA3DShaderDestToken.
83 * \param insn the TGSI instruction
84 * \param idx which TGSI dest register to translate (usually (always?) zero)
86 static SVGA3dShaderDestToken
87 translate_dst_register( struct svga_shader_emitter
*emit
,
88 const struct tgsi_full_instruction
*insn
,
91 const struct tgsi_full_dst_register
*reg
= &insn
->Dst
[idx
];
92 SVGA3dShaderDestToken dest
;
94 switch (reg
->Register
.File
) {
95 case TGSI_FILE_OUTPUT
:
96 /* Output registers encode semantic information in their name.
97 * Need to lookup a table built at decl time:
99 dest
= emit
->output_map
[reg
->Register
.Index
];
100 emit
->num_output_writes
++;
105 unsigned index
= reg
->Register
.Index
;
106 assert(index
< SVGA3D_TEMPREG_MAX
);
107 index
= MIN2(index
, SVGA3D_TEMPREG_MAX
- 1);
108 dest
= dst_register(translate_file(reg
->Register
.File
), index
);
113 if (reg
->Register
.Indirect
) {
114 debug_warning("Indirect indexing of dest registers is not supported!\n");
117 dest
.mask
= reg
->Register
.WriteMask
;
120 if (insn
->Instruction
.Saturate
)
121 dest
.dstMod
= SVGA3DDSTMOD_SATURATE
;
128 * Apply a swizzle to a src_register, returning a new src_register
129 * Ex: swizzle(SRC.ZZYY, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_X, SWIZZLE_Y)
130 * would return SRC.YYZZ
132 static struct src_register
133 swizzle(struct src_register src
,
134 unsigned x
, unsigned y
, unsigned z
, unsigned w
)
140 x
= (src
.base
.swizzle
>> (x
* 2)) & 0x3;
141 y
= (src
.base
.swizzle
>> (y
* 2)) & 0x3;
142 z
= (src
.base
.swizzle
>> (z
* 2)) & 0x3;
143 w
= (src
.base
.swizzle
>> (w
* 2)) & 0x3;
145 src
.base
.swizzle
= TRANSLATE_SWIZZLE(x
, y
, z
, w
);
152 * Apply a "scalar" swizzle to a src_register returning a new
153 * src_register where all the swizzle terms are the same.
154 * Ex: scalar(SRC.WZYX, SWIZZLE_Y) would return SRC.ZZZZ
156 static struct src_register
157 scalar(struct src_register src
, unsigned comp
)
160 return swizzle( src
, comp
, comp
, comp
, comp
);
165 svga_arl_needs_adjustment( const struct svga_shader_emitter
*emit
)
169 for (i
= 0; i
< emit
->num_arl_consts
; ++i
) {
170 if (emit
->arl_consts
[i
].arl_num
== emit
->current_arl
)
178 svga_arl_adjustment( const struct svga_shader_emitter
*emit
)
182 for (i
= 0; i
< emit
->num_arl_consts
; ++i
) {
183 if (emit
->arl_consts
[i
].arl_num
== emit
->current_arl
)
184 return emit
->arl_consts
[i
].number
;
191 * Translate a TGSI src register to a src_register.
193 static struct src_register
194 translate_src_register( const struct svga_shader_emitter
*emit
,
195 const struct tgsi_full_src_register
*reg
)
197 struct src_register src
;
199 switch (reg
->Register
.File
) {
200 case TGSI_FILE_INPUT
:
201 /* Input registers are referred to by their semantic name rather
202 * than by index. Use the mapping build up from the decls:
204 src
= emit
->input_map
[reg
->Register
.Index
];
207 case TGSI_FILE_IMMEDIATE
:
208 /* Immediates are appended after TGSI constants in the D3D
211 src
= src_register( translate_file( reg
->Register
.File
),
212 reg
->Register
.Index
+ emit
->imm_start
);
216 src
= src_register( translate_file( reg
->Register
.File
),
217 reg
->Register
.Index
);
221 /* Indirect addressing.
223 if (reg
->Register
.Indirect
) {
224 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
225 /* Pixel shaders have only loop registers for relative
226 * addressing into inputs. Ignore the redundant address
227 * register, the contents of aL should be in sync with it.
229 if (reg
->Register
.File
== TGSI_FILE_INPUT
) {
230 src
.base
.relAddr
= 1;
231 src
.indirect
= src_token(SVGA3DREG_LOOP
, 0);
235 /* Constant buffers only.
237 if (reg
->Register
.File
== TGSI_FILE_CONSTANT
) {
238 /* we shift the offset towards the minimum */
239 if (svga_arl_needs_adjustment( emit
)) {
240 src
.base
.num
-= svga_arl_adjustment( emit
);
242 src
.base
.relAddr
= 1;
244 /* Not really sure what should go in the second token:
246 src
.indirect
= src_token( SVGA3DREG_ADDR
,
247 reg
->Indirect
.Index
);
249 src
.indirect
.swizzle
= SWIZZLE_XXXX
;
255 reg
->Register
.SwizzleX
,
256 reg
->Register
.SwizzleY
,
257 reg
->Register
.SwizzleZ
,
258 reg
->Register
.SwizzleW
);
260 /* src.mod isn't a bitfield, unfortunately:
261 * See tgsi_util_get_full_src_register_sign_mode for implementation details.
263 if (reg
->Register
.Absolute
) {
264 if (reg
->Register
.Negate
)
265 src
.base
.srcMod
= SVGA3DSRCMOD_ABSNEG
;
267 src
.base
.srcMod
= SVGA3DSRCMOD_ABS
;
270 if (reg
->Register
.Negate
)
271 src
.base
.srcMod
= SVGA3DSRCMOD_NEG
;
273 src
.base
.srcMod
= SVGA3DSRCMOD_NONE
;
281 * Get a temporary register.
282 * Note: if we exceed the temporary register limit we just use
283 * register SVGA3D_TEMPREG_MAX - 1.
285 static SVGA3dShaderDestToken
286 get_temp( struct svga_shader_emitter
*emit
)
288 int i
= emit
->nr_hw_temp
+ emit
->internal_temp_count
++;
289 if (i
>= SVGA3D_TEMPREG_MAX
) {
290 debug_warn_once("svga: Too many temporary registers used in shader\n");
291 i
= SVGA3D_TEMPREG_MAX
- 1;
293 return dst_register( SVGA3DREG_TEMP
, i
);
298 * Release a single temp. Currently only effective if it was the last
299 * allocated temp, otherwise release will be delayed until the next
300 * call to reset_temp_regs().
303 release_temp( struct svga_shader_emitter
*emit
,
304 SVGA3dShaderDestToken temp
)
306 if (temp
.num
== emit
->internal_temp_count
- 1)
307 emit
->internal_temp_count
--;
315 reset_temp_regs(struct svga_shader_emitter
*emit
)
317 emit
->internal_temp_count
= 0;
321 /** Emit bytecode for a src_register */
323 emit_src(struct svga_shader_emitter
*emit
, const struct src_register src
)
325 if (src
.base
.relAddr
) {
326 assert(src
.base
.reserved0
);
327 assert(src
.indirect
.reserved0
);
328 return (svga_shader_emit_dword( emit
, src
.base
.value
) &&
329 svga_shader_emit_dword( emit
, src
.indirect
.value
));
332 assert(src
.base
.reserved0
);
333 return svga_shader_emit_dword( emit
, src
.base
.value
);
338 /** Emit bytecode for a dst_register */
340 emit_dst(struct svga_shader_emitter
*emit
, SVGA3dShaderDestToken dest
)
342 assert(dest
.reserved0
);
344 return svga_shader_emit_dword( emit
, dest
.value
);
348 /** Emit bytecode for a 1-operand instruction */
350 emit_op1(struct svga_shader_emitter
*emit
,
351 SVGA3dShaderInstToken inst
,
352 SVGA3dShaderDestToken dest
,
353 struct src_register src0
)
355 return (emit_instruction(emit
, inst
) &&
356 emit_dst(emit
, dest
) &&
357 emit_src(emit
, src0
));
361 /** Emit bytecode for a 2-operand instruction */
363 emit_op2(struct svga_shader_emitter
*emit
,
364 SVGA3dShaderInstToken inst
,
365 SVGA3dShaderDestToken dest
,
366 struct src_register src0
,
367 struct src_register src1
)
369 return (emit_instruction(emit
, inst
) &&
370 emit_dst(emit
, dest
) &&
371 emit_src(emit
, src0
) &&
372 emit_src(emit
, src1
));
376 /** Emit bytecode for a 3-operand instruction */
378 emit_op3(struct svga_shader_emitter
*emit
,
379 SVGA3dShaderInstToken inst
,
380 SVGA3dShaderDestToken dest
,
381 struct src_register src0
,
382 struct src_register src1
,
383 struct src_register src2
)
385 return (emit_instruction(emit
, inst
) &&
386 emit_dst(emit
, dest
) &&
387 emit_src(emit
, src0
) &&
388 emit_src(emit
, src1
) &&
389 emit_src(emit
, src2
));
393 /** Emit bytecode for a 4-operand instruction */
395 emit_op4(struct svga_shader_emitter
*emit
,
396 SVGA3dShaderInstToken inst
,
397 SVGA3dShaderDestToken dest
,
398 struct src_register src0
,
399 struct src_register src1
,
400 struct src_register src2
,
401 struct src_register src3
)
403 return (emit_instruction(emit
, inst
) &&
404 emit_dst(emit
, dest
) &&
405 emit_src(emit
, src0
) &&
406 emit_src(emit
, src1
) &&
407 emit_src(emit
, src2
) &&
408 emit_src(emit
, src3
));
413 * Apply the absolute value modifier to the given src_register, returning
414 * a new src_register.
416 static struct src_register
417 absolute(struct src_register src
)
419 src
.base
.srcMod
= SVGA3DSRCMOD_ABS
;
425 * Apply the negation modifier to the given src_register, returning
426 * a new src_register.
428 static struct src_register
429 negate(struct src_register src
)
431 switch (src
.base
.srcMod
) {
432 case SVGA3DSRCMOD_ABS
:
433 src
.base
.srcMod
= SVGA3DSRCMOD_ABSNEG
;
435 case SVGA3DSRCMOD_ABSNEG
:
436 src
.base
.srcMod
= SVGA3DSRCMOD_ABS
;
438 case SVGA3DSRCMOD_NEG
:
439 src
.base
.srcMod
= SVGA3DSRCMOD_NONE
;
441 case SVGA3DSRCMOD_NONE
:
442 src
.base
.srcMod
= SVGA3DSRCMOD_NEG
;
450 /* Replace the src with the temporary specified in the dst, but copying
451 * only the necessary channels, and preserving the original swizzle (which is
452 * important given that several opcodes have constraints in the allowed
456 emit_repl(struct svga_shader_emitter
*emit
,
457 SVGA3dShaderDestToken dst
,
458 struct src_register
*src0
)
460 unsigned src0_swizzle
;
463 assert(SVGA3dShaderGetRegType(dst
.value
) == SVGA3DREG_TEMP
);
465 src0_swizzle
= src0
->base
.swizzle
;
468 for (chan
= 0; chan
< 4; ++chan
) {
469 unsigned swizzle
= (src0_swizzle
>> (chan
*2)) & 0x3;
470 dst
.mask
|= 1 << swizzle
;
474 src0
->base
.swizzle
= SVGA3DSWIZZLE_NONE
;
476 if (!emit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, *src0
))
480 src0
->base
.swizzle
= src0_swizzle
;
487 * Submit/emit an instruction with zero operands.
490 submit_op0(struct svga_shader_emitter
*emit
,
491 SVGA3dShaderInstToken inst
,
492 SVGA3dShaderDestToken dest
)
494 return (emit_instruction( emit
, inst
) &&
495 emit_dst( emit
, dest
));
500 * Submit/emit an instruction with one operand.
503 submit_op1(struct svga_shader_emitter
*emit
,
504 SVGA3dShaderInstToken inst
,
505 SVGA3dShaderDestToken dest
,
506 struct src_register src0
)
508 return emit_op1( emit
, inst
, dest
, src0
);
513 * Submit/emit an instruction with two operands.
515 * SVGA shaders may not refer to >1 constant register in a single
516 * instruction. This function checks for that usage and inserts a
517 * move to temporary if detected.
519 * The same applies to input registers -- at most a single input
520 * register may be read by any instruction.
523 submit_op2(struct svga_shader_emitter
*emit
,
524 SVGA3dShaderInstToken inst
,
525 SVGA3dShaderDestToken dest
,
526 struct src_register src0
,
527 struct src_register src1
)
529 SVGA3dShaderDestToken temp
;
530 SVGA3dShaderRegType type0
, type1
;
531 boolean need_temp
= FALSE
;
534 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
535 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
537 if (type0
== SVGA3DREG_CONST
&&
538 type1
== SVGA3DREG_CONST
&&
539 src0
.base
.num
!= src1
.base
.num
)
542 if (type0
== SVGA3DREG_INPUT
&&
543 type1
== SVGA3DREG_INPUT
&&
544 src0
.base
.num
!= src1
.base
.num
)
548 temp
= get_temp( emit
);
550 if (!emit_repl( emit
, temp
, &src0
))
554 if (!emit_op2( emit
, inst
, dest
, src0
, src1
))
558 release_temp( emit
, temp
);
565 * Submit/emit an instruction with three operands.
567 * SVGA shaders may not refer to >1 constant register in a single
568 * instruction. This function checks for that usage and inserts a
569 * move to temporary if detected.
572 submit_op3(struct svga_shader_emitter
*emit
,
573 SVGA3dShaderInstToken inst
,
574 SVGA3dShaderDestToken dest
,
575 struct src_register src0
,
576 struct src_register src1
,
577 struct src_register src2
)
579 SVGA3dShaderDestToken temp0
;
580 SVGA3dShaderDestToken temp1
;
581 boolean need_temp0
= FALSE
;
582 boolean need_temp1
= FALSE
;
583 SVGA3dShaderRegType type0
, type1
, type2
;
587 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
588 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
589 type2
= SVGA3dShaderGetRegType( src2
.base
.value
);
591 if (inst
.op
!= SVGA3DOP_SINCOS
) {
592 if (type0
== SVGA3DREG_CONST
&&
593 ((type1
== SVGA3DREG_CONST
&& src0
.base
.num
!= src1
.base
.num
) ||
594 (type2
== SVGA3DREG_CONST
&& src0
.base
.num
!= src2
.base
.num
)))
597 if (type1
== SVGA3DREG_CONST
&&
598 (type2
== SVGA3DREG_CONST
&& src1
.base
.num
!= src2
.base
.num
))
602 if (type0
== SVGA3DREG_INPUT
&&
603 ((type1
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src1
.base
.num
) ||
604 (type2
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src2
.base
.num
)))
607 if (type1
== SVGA3DREG_INPUT
&&
608 (type2
== SVGA3DREG_INPUT
&& src1
.base
.num
!= src2
.base
.num
))
612 temp0
= get_temp( emit
);
614 if (!emit_repl( emit
, temp0
, &src0
))
619 temp1
= get_temp( emit
);
621 if (!emit_repl( emit
, temp1
, &src1
))
625 if (!emit_op3( emit
, inst
, dest
, src0
, src1
, src2
))
629 release_temp( emit
, temp1
);
631 release_temp( emit
, temp0
);
637 * Submit/emit an instruction with four operands.
639 * SVGA shaders may not refer to >1 constant register in a single
640 * instruction. This function checks for that usage and inserts a
641 * move to temporary if detected.
644 submit_op4(struct svga_shader_emitter
*emit
,
645 SVGA3dShaderInstToken inst
,
646 SVGA3dShaderDestToken dest
,
647 struct src_register src0
,
648 struct src_register src1
,
649 struct src_register src2
,
650 struct src_register src3
)
652 SVGA3dShaderDestToken temp0
;
653 SVGA3dShaderDestToken temp3
;
654 boolean need_temp0
= FALSE
;
655 boolean need_temp3
= FALSE
;
656 SVGA3dShaderRegType type0
, type1
, type2
, type3
;
660 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
661 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
662 type2
= SVGA3dShaderGetRegType( src2
.base
.value
);
663 type3
= SVGA3dShaderGetRegType( src2
.base
.value
);
665 /* Make life a little easier - this is only used by the TXD
666 * instruction which is guaranteed not to have a constant/input reg
667 * in one slot at least:
669 assert(type1
== SVGA3DREG_SAMPLER
);
672 if (type0
== SVGA3DREG_CONST
&&
673 ((type3
== SVGA3DREG_CONST
&& src0
.base
.num
!= src3
.base
.num
) ||
674 (type2
== SVGA3DREG_CONST
&& src0
.base
.num
!= src2
.base
.num
)))
677 if (type3
== SVGA3DREG_CONST
&&
678 (type2
== SVGA3DREG_CONST
&& src3
.base
.num
!= src2
.base
.num
))
681 if (type0
== SVGA3DREG_INPUT
&&
682 ((type3
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src3
.base
.num
) ||
683 (type2
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src2
.base
.num
)))
686 if (type3
== SVGA3DREG_INPUT
&&
687 (type2
== SVGA3DREG_INPUT
&& src3
.base
.num
!= src2
.base
.num
))
691 temp0
= get_temp( emit
);
693 if (!emit_repl( emit
, temp0
, &src0
))
698 temp3
= get_temp( emit
);
700 if (!emit_repl( emit
, temp3
, &src3
))
704 if (!emit_op4( emit
, inst
, dest
, src0
, src1
, src2
, src3
))
708 release_temp( emit
, temp3
);
710 release_temp( emit
, temp0
);
716 * Do the src and dest registers refer to the same register?
719 alias_src_dst(struct src_register src
,
720 SVGA3dShaderDestToken dst
)
722 if (src
.base
.num
!= dst
.num
)
725 if (SVGA3dShaderGetRegType(dst
.value
) !=
726 SVGA3dShaderGetRegType(src
.base
.value
))
734 * Helper for emitting SVGA immediate values using the SVGA3DOP_DEF[I]
738 emit_def_const(struct svga_shader_emitter
*emit
,
739 SVGA3dShaderConstType type
,
740 unsigned idx
, float a
, float b
, float c
, float d
)
743 SVGA3dShaderInstToken opcode
;
746 case SVGA3D_CONST_TYPE_FLOAT
:
747 opcode
= inst_token( SVGA3DOP_DEF
);
748 def
.dst
= dst_register( SVGA3DREG_CONST
, idx
);
749 def
.constValues
[0] = a
;
750 def
.constValues
[1] = b
;
751 def
.constValues
[2] = c
;
752 def
.constValues
[3] = d
;
754 case SVGA3D_CONST_TYPE_INT
:
755 opcode
= inst_token( SVGA3DOP_DEFI
);
756 def
.dst
= dst_register( SVGA3DREG_CONSTINT
, idx
);
757 def
.constIValues
[0] = (int)a
;
758 def
.constIValues
[1] = (int)b
;
759 def
.constIValues
[2] = (int)c
;
760 def
.constIValues
[3] = (int)d
;
764 opcode
= inst_token( SVGA3DOP_NOP
);
768 if (!emit_instruction(emit
, opcode
) ||
769 !svga_shader_emit_dwords( emit
, def
.values
, ARRAY_SIZE(def
.values
)))
777 create_loop_const( struct svga_shader_emitter
*emit
)
779 unsigned idx
= emit
->nr_hw_int_const
++;
781 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_INT
, idx
,
782 255, /* iteration count */
783 0, /* initial value */
785 0 /* not used, must be 0 */))
788 emit
->loop_const_idx
= idx
;
789 emit
->created_loop_const
= TRUE
;
795 create_arl_consts( struct svga_shader_emitter
*emit
)
799 for (i
= 0; i
< emit
->num_arl_consts
; i
+= 4) {
801 unsigned idx
= emit
->nr_hw_float_const
++;
803 for (j
= 0; j
< 4 && (j
+ i
) < emit
->num_arl_consts
; ++j
) {
804 vals
[j
] = (float) emit
->arl_consts
[i
+ j
].number
;
805 emit
->arl_consts
[i
+ j
].idx
= idx
;
808 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_X
;
811 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_Y
;
814 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_Z
;
817 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_W
;
824 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
, idx
,
835 * Return the register which holds the pixel shaders front/back-
838 static struct src_register
839 get_vface( struct svga_shader_emitter
*emit
)
841 assert(emit
->emitted_vface
);
842 return src_register(SVGA3DREG_MISCTYPE
, SVGA3DMISCREG_FACE
);
847 * Create/emit a "common" constant with values {0, 0.5, -1, 1}.
848 * We can swizzle this to produce other useful constants such as
849 * {0, 0, 0, 0}, {1, 1, 1, 1}, etc.
852 create_common_immediate( struct svga_shader_emitter
*emit
)
854 unsigned idx
= emit
->nr_hw_float_const
++;
856 /* Emit the constant (0, 0.5, -1, 1) and use swizzling to generate
857 * other useful vectors.
859 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
860 idx
, 0.0f
, 0.5f
, -1.0f
, 1.0f
))
862 emit
->common_immediate_idx
[0] = idx
;
865 /* Emit constant {2, 0, 0, 0} (only the 2 is used for now) */
866 if (emit
->key
.vs
.adjust_attrib_range
) {
867 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
868 idx
, 2.0f
, 0.0f
, 0.0f
, 0.0f
))
870 emit
->common_immediate_idx
[1] = idx
;
873 emit
->common_immediate_idx
[1] = -1;
876 emit
->created_common_immediate
= TRUE
;
883 * Return swizzle/position for the given value in the "common" immediate.
885 static inline unsigned
886 common_immediate_swizzle(float value
)
889 return TGSI_SWIZZLE_X
;
890 else if (value
== 0.5f
)
891 return TGSI_SWIZZLE_Y
;
892 else if (value
== -1.0f
)
893 return TGSI_SWIZZLE_Z
;
894 else if (value
== 1.0f
)
895 return TGSI_SWIZZLE_W
;
897 assert(!"illegal value in common_immediate_swizzle");
898 return TGSI_SWIZZLE_X
;
904 * Returns an immediate reg where all the terms are either 0, 1, 2 or 0.5
906 static struct src_register
907 get_immediate(struct svga_shader_emitter
*emit
,
908 float x
, float y
, float z
, float w
)
910 unsigned sx
= common_immediate_swizzle(x
);
911 unsigned sy
= common_immediate_swizzle(y
);
912 unsigned sz
= common_immediate_swizzle(z
);
913 unsigned sw
= common_immediate_swizzle(w
);
914 assert(emit
->created_common_immediate
);
915 assert(emit
->common_immediate_idx
[0] >= 0);
916 return swizzle(src_register(SVGA3DREG_CONST
, emit
->common_immediate_idx
[0]),
922 * returns {0, 0, 0, 0} immediate
924 static struct src_register
925 get_zero_immediate( struct svga_shader_emitter
*emit
)
927 assert(emit
->created_common_immediate
);
928 assert(emit
->common_immediate_idx
[0] >= 0);
929 return swizzle(src_register( SVGA3DREG_CONST
,
930 emit
->common_immediate_idx
[0]),
936 * returns {1, 1, 1, 1} immediate
938 static struct src_register
939 get_one_immediate( struct svga_shader_emitter
*emit
)
941 assert(emit
->created_common_immediate
);
942 assert(emit
->common_immediate_idx
[0] >= 0);
943 return swizzle(src_register( SVGA3DREG_CONST
,
944 emit
->common_immediate_idx
[0]),
950 * returns {0.5, 0.5, 0.5, 0.5} immediate
952 static struct src_register
953 get_half_immediate( struct svga_shader_emitter
*emit
)
955 assert(emit
->created_common_immediate
);
956 assert(emit
->common_immediate_idx
[0] >= 0);
957 return swizzle(src_register(SVGA3DREG_CONST
, emit
->common_immediate_idx
[0]),
963 * returns {2, 2, 2, 2} immediate
965 static struct src_register
966 get_two_immediate( struct svga_shader_emitter
*emit
)
968 /* Note we use the second common immediate here */
969 assert(emit
->created_common_immediate
);
970 assert(emit
->common_immediate_idx
[1] >= 0);
971 return swizzle(src_register( SVGA3DREG_CONST
,
972 emit
->common_immediate_idx
[1]),
978 * returns the loop const
980 static struct src_register
981 get_loop_const( struct svga_shader_emitter
*emit
)
983 assert(emit
->created_loop_const
);
984 assert(emit
->loop_const_idx
>= 0);
985 return src_register( SVGA3DREG_CONSTINT
,
986 emit
->loop_const_idx
);
990 static struct src_register
991 get_fake_arl_const( struct svga_shader_emitter
*emit
)
993 struct src_register reg
;
994 int idx
= 0, swizzle
= 0, i
;
996 for (i
= 0; i
< emit
->num_arl_consts
; ++ i
) {
997 if (emit
->arl_consts
[i
].arl_num
== emit
->current_arl
) {
998 idx
= emit
->arl_consts
[i
].idx
;
999 swizzle
= emit
->arl_consts
[i
].swizzle
;
1003 reg
= src_register( SVGA3DREG_CONST
, idx
);
1004 return scalar(reg
, swizzle
);
1009 * Return a register which holds the width and height of the texture
1010 * currently bound to the given sampler.
1012 static struct src_register
1013 get_tex_dimensions( struct svga_shader_emitter
*emit
, int sampler_num
)
1016 struct src_register reg
;
1018 /* the width/height indexes start right after constants */
1019 idx
= emit
->key
.tex
[sampler_num
].width_height_idx
+
1020 emit
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
1022 reg
= src_register( SVGA3DREG_CONST
, idx
);
1028 emit_fake_arl(struct svga_shader_emitter
*emit
,
1029 const struct tgsi_full_instruction
*insn
)
1031 const struct src_register src0
=
1032 translate_src_register(emit
, &insn
->Src
[0] );
1033 struct src_register src1
= get_fake_arl_const( emit
);
1034 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1035 SVGA3dShaderDestToken tmp
= get_temp( emit
);
1037 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), tmp
, src0
))
1040 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), tmp
, src( tmp
),
1044 /* replicate the original swizzle */
1046 src1
.base
.swizzle
= src0
.base
.swizzle
;
1048 return submit_op1( emit
, inst_token( SVGA3DOP_MOVA
),
1054 emit_if(struct svga_shader_emitter
*emit
,
1055 const struct tgsi_full_instruction
*insn
)
1057 struct src_register src0
=
1058 translate_src_register(emit
, &insn
->Src
[0]);
1059 struct src_register zero
= get_zero_immediate(emit
);
1060 SVGA3dShaderInstToken if_token
= inst_token( SVGA3DOP_IFC
);
1062 if_token
.control
= SVGA3DOPCOMPC_NE
;
1064 if (SVGA3dShaderGetRegType(src0
.base
.value
) == SVGA3DREG_CONST
) {
1066 * Max different constant registers readable per IFC instruction is 1.
1068 SVGA3dShaderDestToken tmp
= get_temp( emit
);
1070 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), tmp
, src0
))
1073 src0
= scalar(src( tmp
), TGSI_SWIZZLE_X
);
1076 emit
->dynamic_branching_level
++;
1078 return (emit_instruction( emit
, if_token
) &&
1079 emit_src( emit
, src0
) &&
1080 emit_src( emit
, zero
) );
1085 emit_else(struct svga_shader_emitter
*emit
,
1086 const struct tgsi_full_instruction
*insn
)
1088 return emit_instruction(emit
, inst_token(SVGA3DOP_ELSE
));
1093 emit_endif(struct svga_shader_emitter
*emit
,
1094 const struct tgsi_full_instruction
*insn
)
1096 emit
->dynamic_branching_level
--;
1098 return emit_instruction(emit
, inst_token(SVGA3DOP_ENDIF
));
1103 * Translate the following TGSI FLR instruction.
1105 * To the following SVGA3D instruction sequence.
1110 emit_floor(struct svga_shader_emitter
*emit
,
1111 const struct tgsi_full_instruction
*insn
)
1113 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1114 const struct src_register src0
=
1115 translate_src_register(emit
, &insn
->Src
[0] );
1116 SVGA3dShaderDestToken temp
= get_temp( emit
);
1119 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
), temp
, src0
))
1122 /* SUB DST, SRC, TMP */
1123 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
, src0
,
1124 negate( src( temp
) ) ))
1132 * Translate the following TGSI CEIL instruction.
1134 * To the following SVGA3D instruction sequence.
1139 emit_ceil(struct svga_shader_emitter
*emit
,
1140 const struct tgsi_full_instruction
*insn
)
1142 SVGA3dShaderDestToken dst
= translate_dst_register(emit
, insn
, 0);
1143 const struct src_register src0
=
1144 translate_src_register(emit
, &insn
->Src
[0]);
1145 SVGA3dShaderDestToken temp
= get_temp(emit
);
1148 if (!submit_op1(emit
, inst_token(SVGA3DOP_FRC
), temp
, negate(src0
)))
1151 /* ADD DST, SRC, TMP */
1152 if (!submit_op2(emit
, inst_token(SVGA3DOP_ADD
), dst
, src0
, src(temp
)))
1160 * Translate the following TGSI DIV instruction.
1161 * DIV DST.xy, SRC0, SRC1
1162 * To the following SVGA3D instruction sequence.
1163 * RCP TMP.x, SRC1.xxxx
1164 * RCP TMP.y, SRC1.yyyy
1165 * MUL DST.xy, SRC0, TMP
1168 emit_div(struct svga_shader_emitter
*emit
,
1169 const struct tgsi_full_instruction
*insn
)
1171 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1172 const struct src_register src0
=
1173 translate_src_register(emit
, &insn
->Src
[0] );
1174 const struct src_register src1
=
1175 translate_src_register(emit
, &insn
->Src
[1] );
1176 SVGA3dShaderDestToken temp
= get_temp( emit
);
1179 /* For each enabled element, perform a RCP instruction. Note that
1180 * RCP is scalar in SVGA3D:
1182 for (i
= 0; i
< 4; i
++) {
1183 unsigned channel
= 1 << i
;
1184 if (dst
.mask
& channel
) {
1185 /* RCP TMP.?, SRC1.???? */
1186 if (!submit_op1( emit
, inst_token( SVGA3DOP_RCP
),
1187 writemask(temp
, channel
),
1194 * MUL DST, SRC0, TMP
1196 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
), dst
, src0
,
1205 * Translate the following TGSI DP2 instruction.
1206 * DP2 DST, SRC1, SRC2
1207 * To the following SVGA3D instruction sequence.
1208 * MUL TMP, SRC1, SRC2
1209 * ADD DST, TMP.xxxx, TMP.yyyy
1212 emit_dp2(struct svga_shader_emitter
*emit
,
1213 const struct tgsi_full_instruction
*insn
)
1215 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1216 const struct src_register src0
=
1217 translate_src_register(emit
, &insn
->Src
[0]);
1218 const struct src_register src1
=
1219 translate_src_register(emit
, &insn
->Src
[1]);
1220 SVGA3dShaderDestToken temp
= get_temp( emit
);
1221 struct src_register temp_src0
, temp_src1
;
1223 /* MUL TMP, SRC1, SRC2 */
1224 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
), temp
, src0
, src1
))
1227 temp_src0
= scalar(src( temp
), TGSI_SWIZZLE_X
);
1228 temp_src1
= scalar(src( temp
), TGSI_SWIZZLE_Y
);
1230 /* ADD DST, TMP.xxxx, TMP.yyyy */
1231 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
,
1232 temp_src0
, temp_src1
))
1240 * Sine / Cosine helper function.
1243 do_emit_sincos(struct svga_shader_emitter
*emit
,
1244 SVGA3dShaderDestToken dst
,
1245 struct src_register src0
)
1247 src0
= scalar(src0
, TGSI_SWIZZLE_X
);
1248 return submit_op1(emit
, inst_token(SVGA3DOP_SINCOS
), dst
, src0
);
1253 * Translate/emit a TGSI SIN, COS or CSC instruction.
1256 emit_sincos(struct svga_shader_emitter
*emit
,
1257 const struct tgsi_full_instruction
*insn
)
1259 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1260 struct src_register src0
= translate_src_register(emit
, &insn
->Src
[0]);
1261 SVGA3dShaderDestToken temp
= get_temp( emit
);
1264 if (!do_emit_sincos(emit
, writemask(temp
, TGSI_WRITEMASK_XY
), src0
))
1268 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src( temp
) ))
1276 * Translate TGSI SIN instruction into:
1281 emit_sin(struct svga_shader_emitter
*emit
,
1282 const struct tgsi_full_instruction
*insn
)
1284 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1285 struct src_register src0
=
1286 translate_src_register(emit
, &insn
->Src
[0] );
1287 SVGA3dShaderDestToken temp
= get_temp( emit
);
1290 if (!do_emit_sincos(emit
, writemask(temp
, TGSI_WRITEMASK_Y
), src0
))
1293 src0
= scalar(src( temp
), TGSI_SWIZZLE_Y
);
1295 /* MOV DST TMP.yyyy */
1296 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src0
))
1304 * Translate TGSI COS instruction into:
1309 emit_cos(struct svga_shader_emitter
*emit
,
1310 const struct tgsi_full_instruction
*insn
)
1312 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1313 struct src_register src0
=
1314 translate_src_register(emit
, &insn
->Src
[0] );
1315 SVGA3dShaderDestToken temp
= get_temp( emit
);
1318 if (!do_emit_sincos( emit
, writemask(temp
, TGSI_WRITEMASK_X
), src0
))
1321 src0
= scalar(src( temp
), TGSI_SWIZZLE_X
);
1323 /* MOV DST TMP.xxxx */
1324 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src0
))
1332 * Translate/emit TGSI SSG (Set Sign: -1, 0, +1) instruction.
1335 emit_ssg(struct svga_shader_emitter
*emit
,
1336 const struct tgsi_full_instruction
*insn
)
1338 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1339 struct src_register src0
=
1340 translate_src_register(emit
, &insn
->Src
[0] );
1341 SVGA3dShaderDestToken temp0
= get_temp( emit
);
1342 SVGA3dShaderDestToken temp1
= get_temp( emit
);
1343 struct src_register zero
, one
;
1345 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1346 /* SGN DST, SRC0, TMP0, TMP1 */
1347 return submit_op3( emit
, inst_token( SVGA3DOP_SGN
), dst
, src0
,
1348 src( temp0
), src( temp1
) );
1351 one
= get_one_immediate(emit
);
1352 zero
= get_zero_immediate(emit
);
1354 /* CMP TMP0, SRC0, one, zero */
1355 if (!submit_op3( emit
, inst_token( SVGA3DOP_CMP
),
1356 writemask( temp0
, dst
.mask
), src0
, one
, zero
))
1359 /* CMP TMP1, negate(SRC0), negate(one), zero */
1360 if (!submit_op3( emit
, inst_token( SVGA3DOP_CMP
),
1361 writemask( temp1
, dst
.mask
), negate( src0
), negate( one
),
1365 /* ADD DST, TMP0, TMP1 */
1366 return submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
, src( temp0
),
1372 * Translate/emit KILL_IF instruction (kill if any of X,Y,Z,W are negative).
1375 emit_kill_if(struct svga_shader_emitter
*emit
,
1376 const struct tgsi_full_instruction
*insn
)
1378 const struct tgsi_full_src_register
*reg
= &insn
->Src
[0];
1379 struct src_register src0
, srcIn
;
1380 const boolean special
= (reg
->Register
.Absolute
||
1381 reg
->Register
.Negate
||
1382 reg
->Register
.Indirect
||
1383 reg
->Register
.SwizzleX
!= 0 ||
1384 reg
->Register
.SwizzleY
!= 1 ||
1385 reg
->Register
.SwizzleZ
!= 2 ||
1386 reg
->Register
.File
!= TGSI_FILE_TEMPORARY
);
1387 SVGA3dShaderDestToken temp
;
1389 src0
= srcIn
= translate_src_register( emit
, reg
);
1392 /* need a temp reg */
1393 temp
= get_temp( emit
);
1397 /* move the source into a temp register */
1398 submit_op1(emit
, inst_token(SVGA3DOP_MOV
), temp
, src0
);
1403 /* Do the texkill by checking if any of the XYZW components are < 0.
1404 * Note that ps_2_0 and later take XYZW in consideration, while ps_1_x
1405 * only used XYZ. The MSDN documentation about this is incorrect.
1407 if (!submit_op0( emit
, inst_token( SVGA3DOP_TEXKILL
), dst(src0
) ))
1415 * Translate/emit unconditional kill instruction (usually found inside
1416 * an IF/ELSE/ENDIF block).
1419 emit_kill(struct svga_shader_emitter
*emit
,
1420 const struct tgsi_full_instruction
*insn
)
1422 SVGA3dShaderDestToken temp
;
1423 struct src_register one
= get_one_immediate(emit
);
1424 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_TEXKILL
);
1426 /* texkill doesn't allow negation on the operand so lets move
1427 * negation of {1} to a temp register */
1428 temp
= get_temp( emit
);
1429 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp
,
1433 return submit_op0( emit
, inst
, temp
);
1438 * Test if r1 and r2 are the same register.
1441 same_register(struct src_register r1
, struct src_register r2
)
1443 return (r1
.base
.num
== r2
.base
.num
&&
1444 r1
.base
.type_upper
== r2
.base
.type_upper
&&
1445 r1
.base
.type_lower
== r2
.base
.type_lower
);
1451 * Implement conditionals by initializing destination reg to 'fail',
1452 * then set predicate reg with UFOP_SETP, then move 'pass' to dest
1453 * based on predicate reg.
1455 * SETP src0, cmp, src1 -- do this first to avoid aliasing problems.
1460 emit_conditional(struct svga_shader_emitter
*emit
,
1461 unsigned compare_func
,
1462 SVGA3dShaderDestToken dst
,
1463 struct src_register src0
,
1464 struct src_register src1
,
1465 struct src_register pass
,
1466 struct src_register fail
)
1468 SVGA3dShaderDestToken pred_reg
= dst_register( SVGA3DREG_PREDICATE
, 0 );
1469 SVGA3dShaderInstToken setp_token
;
1471 switch (compare_func
) {
1472 case PIPE_FUNC_NEVER
:
1473 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1476 case PIPE_FUNC_LESS
:
1477 setp_token
= inst_token_setp(SVGA3DOPCOMP_LT
);
1479 case PIPE_FUNC_EQUAL
:
1480 setp_token
= inst_token_setp(SVGA3DOPCOMP_EQ
);
1482 case PIPE_FUNC_LEQUAL
:
1483 setp_token
= inst_token_setp(SVGA3DOPCOMP_LE
);
1485 case PIPE_FUNC_GREATER
:
1486 setp_token
= inst_token_setp(SVGA3DOPCOMP_GT
);
1488 case PIPE_FUNC_NOTEQUAL
:
1489 setp_token
= inst_token_setp(SVGA3DOPCOMPC_NE
);
1491 case PIPE_FUNC_GEQUAL
:
1492 setp_token
= inst_token_setp(SVGA3DOPCOMP_GE
);
1494 case PIPE_FUNC_ALWAYS
:
1495 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1500 if (same_register(src(dst
), pass
)) {
1501 /* We'll get bad results if the dst and pass registers are the same
1502 * so use a temp register containing pass.
1504 SVGA3dShaderDestToken temp
= get_temp(emit
);
1505 if (!submit_op1(emit
, inst_token(SVGA3DOP_MOV
), temp
, pass
))
1510 /* SETP src0, COMPOP, src1 */
1511 if (!submit_op2( emit
, setp_token
, pred_reg
,
1516 if (!submit_op1(emit
, inst_token(SVGA3DOP_MOV
), dst
, fail
))
1519 /* MOV dst, pass (predicated)
1521 * Note that the predicate reg (and possible modifiers) is passed
1522 * as the first source argument.
1524 if (!submit_op2(emit
,
1525 inst_token_predicated(SVGA3DOP_MOV
), dst
,
1526 src(pred_reg
), pass
))
1534 * Helper for emiting 'selection' commands. Basically:
1541 emit_select(struct svga_shader_emitter
*emit
,
1542 unsigned compare_func
,
1543 SVGA3dShaderDestToken dst
,
1544 struct src_register src0
,
1545 struct src_register src1
)
1547 /* There are some SVGA instructions which implement some selects
1548 * directly, but they are only available in the vertex shader.
1550 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1551 switch (compare_func
) {
1552 case PIPE_FUNC_GEQUAL
:
1553 return submit_op2( emit
, inst_token( SVGA3DOP_SGE
), dst
, src0
, src1
);
1554 case PIPE_FUNC_LEQUAL
:
1555 return submit_op2( emit
, inst_token( SVGA3DOP_SGE
), dst
, src1
, src0
);
1556 case PIPE_FUNC_GREATER
:
1557 return submit_op2( emit
, inst_token( SVGA3DOP_SLT
), dst
, src1
, src0
);
1558 case PIPE_FUNC_LESS
:
1559 return submit_op2( emit
, inst_token( SVGA3DOP_SLT
), dst
, src0
, src1
);
1565 /* Otherwise, need to use the setp approach:
1568 struct src_register one
, zero
;
1569 /* zero immediate is 0,0,0,1 */
1570 zero
= get_zero_immediate(emit
);
1571 one
= get_one_immediate(emit
);
1573 return emit_conditional(emit
, compare_func
, dst
, src0
, src1
, one
, zero
);
1579 * Translate/emit a TGSI SEQ, SNE, SLT, SGE, etc. instruction.
1582 emit_select_op(struct svga_shader_emitter
*emit
,
1584 const struct tgsi_full_instruction
*insn
)
1586 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1587 struct src_register src0
= translate_src_register(
1588 emit
, &insn
->Src
[0] );
1589 struct src_register src1
= translate_src_register(
1590 emit
, &insn
->Src
[1] );
1592 return emit_select( emit
, compare
, dst
, src0
, src1
);
1597 * Translate TGSI CMP instruction. Component-wise:
1598 * dst = (src0 < 0.0) ? src1 : src2
1601 emit_cmp(struct svga_shader_emitter
*emit
,
1602 const struct tgsi_full_instruction
*insn
)
1604 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1605 const struct src_register src0
=
1606 translate_src_register(emit
, &insn
->Src
[0] );
1607 const struct src_register src1
=
1608 translate_src_register(emit
, &insn
->Src
[1] );
1609 const struct src_register src2
=
1610 translate_src_register(emit
, &insn
->Src
[2] );
1612 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1613 struct src_register zero
= get_zero_immediate(emit
);
1614 /* We used to simulate CMP with SLT+LRP. But that didn't work when
1615 * src1 or src2 was Inf/NaN. In particular, GLSL sqrt(0) failed
1616 * because it involves a CMP to handle the 0 case.
1617 * Use a conditional expression instead.
1619 return emit_conditional(emit
, PIPE_FUNC_LESS
, dst
,
1620 src0
, zero
, src1
, src2
);
1623 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
1625 /* CMP DST, SRC0, SRC2, SRC1 */
1626 return submit_op3( emit
, inst_token( SVGA3DOP_CMP
), dst
,
1633 * Translate/emit 2-operand (coord, sampler) texture instructions.
1636 emit_tex2(struct svga_shader_emitter
*emit
,
1637 const struct tgsi_full_instruction
*insn
,
1638 SVGA3dShaderDestToken dst
)
1640 SVGA3dShaderInstToken inst
;
1641 struct src_register texcoord
;
1642 struct src_register sampler
;
1643 SVGA3dShaderDestToken tmp
;
1647 switch (insn
->Instruction
.Opcode
) {
1648 case TGSI_OPCODE_TEX
:
1649 inst
.op
= SVGA3DOP_TEX
;
1651 case TGSI_OPCODE_TXP
:
1652 inst
.op
= SVGA3DOP_TEX
;
1653 inst
.control
= SVGA3DOPCONT_PROJECT
;
1655 case TGSI_OPCODE_TXB
:
1656 inst
.op
= SVGA3DOP_TEX
;
1657 inst
.control
= SVGA3DOPCONT_BIAS
;
1659 case TGSI_OPCODE_TXL
:
1660 inst
.op
= SVGA3DOP_TEXLDL
;
1667 texcoord
= translate_src_register( emit
, &insn
->Src
[0] );
1668 sampler
= translate_src_register( emit
, &insn
->Src
[1] );
1670 if (emit
->key
.tex
[sampler
.base
.num
].unnormalized
||
1671 emit
->dynamic_branching_level
> 0)
1672 tmp
= get_temp( emit
);
1674 /* Can't do mipmapping inside dynamic branch constructs. Force LOD
1675 * zero in that case.
1677 if (emit
->dynamic_branching_level
> 0 &&
1678 inst
.op
== SVGA3DOP_TEX
&&
1679 SVGA3dShaderGetRegType(texcoord
.base
.value
) == SVGA3DREG_TEMP
) {
1680 struct src_register zero
= get_zero_immediate(emit
);
1682 /* MOV tmp, texcoord */
1683 if (!submit_op1( emit
,
1684 inst_token( SVGA3DOP_MOV
),
1689 /* MOV tmp.w, zero */
1690 if (!submit_op1( emit
,
1691 inst_token( SVGA3DOP_MOV
),
1692 writemask( tmp
, TGSI_WRITEMASK_W
),
1696 texcoord
= src( tmp
);
1697 inst
.op
= SVGA3DOP_TEXLDL
;
1700 /* Explicit normalization of texcoords:
1702 if (emit
->key
.tex
[sampler
.base
.num
].unnormalized
) {
1703 struct src_register wh
= get_tex_dimensions( emit
, sampler
.base
.num
);
1705 /* MUL tmp, SRC0, WH */
1706 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
1707 tmp
, texcoord
, wh
))
1710 texcoord
= src( tmp
);
1713 return submit_op2( emit
, inst
, dst
, texcoord
, sampler
);
1718 * Translate/emit 4-operand (coord, ddx, ddy, sampler) texture instructions.
1721 emit_tex4(struct svga_shader_emitter
*emit
,
1722 const struct tgsi_full_instruction
*insn
,
1723 SVGA3dShaderDestToken dst
)
1725 SVGA3dShaderInstToken inst
;
1726 struct src_register texcoord
;
1727 struct src_register ddx
;
1728 struct src_register ddy
;
1729 struct src_register sampler
;
1731 texcoord
= translate_src_register( emit
, &insn
->Src
[0] );
1732 ddx
= translate_src_register( emit
, &insn
->Src
[1] );
1733 ddy
= translate_src_register( emit
, &insn
->Src
[2] );
1734 sampler
= translate_src_register( emit
, &insn
->Src
[3] );
1738 switch (insn
->Instruction
.Opcode
) {
1739 case TGSI_OPCODE_TXD
:
1740 inst
.op
= SVGA3DOP_TEXLDD
; /* 4 args! */
1747 return submit_op4( emit
, inst
, dst
, texcoord
, sampler
, ddx
, ddy
);
1752 * Emit texture swizzle code. We do this here since SVGA samplers don't
1753 * directly support swizzles.
1756 emit_tex_swizzle(struct svga_shader_emitter
*emit
,
1757 SVGA3dShaderDestToken dst
,
1758 struct src_register src
,
1764 const unsigned swizzleIn
[4] = {swizzle_x
, swizzle_y
, swizzle_z
, swizzle_w
};
1765 unsigned srcSwizzle
[4];
1766 unsigned srcWritemask
= 0x0, zeroWritemask
= 0x0, oneWritemask
= 0x0;
1769 /* build writemasks and srcSwizzle terms */
1770 for (i
= 0; i
< 4; i
++) {
1771 if (swizzleIn
[i
] == PIPE_SWIZZLE_0
) {
1772 srcSwizzle
[i
] = TGSI_SWIZZLE_X
+ i
;
1773 zeroWritemask
|= (1 << i
);
1775 else if (swizzleIn
[i
] == PIPE_SWIZZLE_1
) {
1776 srcSwizzle
[i
] = TGSI_SWIZZLE_X
+ i
;
1777 oneWritemask
|= (1 << i
);
1780 srcSwizzle
[i
] = swizzleIn
[i
];
1781 srcWritemask
|= (1 << i
);
1785 /* write x/y/z/w comps */
1786 if (dst
.mask
& srcWritemask
) {
1787 if (!submit_op1(emit
,
1788 inst_token(SVGA3DOP_MOV
),
1789 writemask(dst
, srcWritemask
),
1799 if (dst
.mask
& zeroWritemask
) {
1800 if (!submit_op1(emit
,
1801 inst_token(SVGA3DOP_MOV
),
1802 writemask(dst
, zeroWritemask
),
1803 get_zero_immediate(emit
)))
1808 if (dst
.mask
& oneWritemask
) {
1809 if (!submit_op1(emit
,
1810 inst_token(SVGA3DOP_MOV
),
1811 writemask(dst
, oneWritemask
),
1812 get_one_immediate(emit
)))
1821 * Translate/emit a TGSI texture sample instruction.
1824 emit_tex(struct svga_shader_emitter
*emit
,
1825 const struct tgsi_full_instruction
*insn
)
1827 SVGA3dShaderDestToken dst
=
1828 translate_dst_register( emit
, insn
, 0 );
1829 struct src_register src0
=
1830 translate_src_register( emit
, &insn
->Src
[0] );
1831 struct src_register src1
=
1832 translate_src_register( emit
, &insn
->Src
[1] );
1834 SVGA3dShaderDestToken tex_result
;
1835 const unsigned unit
= src1
.base
.num
;
1837 /* check for shadow samplers */
1838 boolean compare
= (emit
->key
.tex
[unit
].compare_mode
==
1839 PIPE_TEX_COMPARE_R_TO_TEXTURE
);
1841 /* texture swizzle */
1842 boolean swizzle
= (emit
->key
.tex
[unit
].swizzle_r
!= PIPE_SWIZZLE_X
||
1843 emit
->key
.tex
[unit
].swizzle_g
!= PIPE_SWIZZLE_Y
||
1844 emit
->key
.tex
[unit
].swizzle_b
!= PIPE_SWIZZLE_Z
||
1845 emit
->key
.tex
[unit
].swizzle_a
!= PIPE_SWIZZLE_W
);
1847 boolean saturate
= insn
->Instruction
.Saturate
;
1849 /* If doing compare processing or tex swizzle or saturation, we need to put
1850 * the fetched color into a temporary so it can be used as a source later on.
1852 if (compare
|| swizzle
|| saturate
) {
1853 tex_result
= get_temp( emit
);
1859 switch(insn
->Instruction
.Opcode
) {
1860 case TGSI_OPCODE_TEX
:
1861 case TGSI_OPCODE_TXB
:
1862 case TGSI_OPCODE_TXP
:
1863 case TGSI_OPCODE_TXL
:
1864 if (!emit_tex2( emit
, insn
, tex_result
))
1867 case TGSI_OPCODE_TXD
:
1868 if (!emit_tex4( emit
, insn
, tex_result
))
1876 SVGA3dShaderDestToken dst2
;
1878 if (swizzle
|| saturate
)
1883 if (dst
.mask
& TGSI_WRITEMASK_XYZ
) {
1884 SVGA3dShaderDestToken src0_zdivw
= get_temp( emit
);
1885 /* When sampling a depth texture, the result of the comparison is in
1888 struct src_register tex_src_x
= scalar(src(tex_result
), TGSI_SWIZZLE_Y
);
1889 struct src_register r_coord
;
1891 if (insn
->Instruction
.Opcode
== TGSI_OPCODE_TXP
) {
1892 /* Divide texcoord R by Q */
1893 if (!submit_op1( emit
, inst_token( SVGA3DOP_RCP
),
1894 writemask(src0_zdivw
, TGSI_WRITEMASK_X
),
1895 scalar(src0
, TGSI_SWIZZLE_W
) ))
1898 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
1899 writemask(src0_zdivw
, TGSI_WRITEMASK_X
),
1900 scalar(src0
, TGSI_SWIZZLE_Z
),
1901 scalar(src(src0_zdivw
), TGSI_SWIZZLE_X
) ))
1904 r_coord
= scalar(src(src0_zdivw
), TGSI_SWIZZLE_X
);
1907 r_coord
= scalar(src0
, TGSI_SWIZZLE_Z
);
1910 /* Compare texture sample value against R component of texcoord */
1911 if (!emit_select(emit
,
1912 emit
->key
.tex
[unit
].compare_func
,
1913 writemask( dst2
, TGSI_WRITEMASK_XYZ
),
1919 if (dst
.mask
& TGSI_WRITEMASK_W
) {
1920 struct src_register one
= get_one_immediate(emit
);
1922 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1923 writemask( dst2
, TGSI_WRITEMASK_W
),
1929 if (saturate
&& !swizzle
) {
1930 /* MOV_SAT real_dst, dst */
1931 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src(tex_result
) ))
1935 /* swizzle from tex_result to dst (handles saturation too, if any) */
1936 emit_tex_swizzle(emit
,
1937 dst
, src(tex_result
),
1938 emit
->key
.tex
[unit
].swizzle_r
,
1939 emit
->key
.tex
[unit
].swizzle_g
,
1940 emit
->key
.tex
[unit
].swizzle_b
,
1941 emit
->key
.tex
[unit
].swizzle_a
);
1949 emit_bgnloop(struct svga_shader_emitter
*emit
,
1950 const struct tgsi_full_instruction
*insn
)
1952 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_LOOP
);
1953 struct src_register loop_reg
= src_register( SVGA3DREG_LOOP
, 0 );
1954 struct src_register const_int
= get_loop_const( emit
);
1956 emit
->dynamic_branching_level
++;
1958 return (emit_instruction( emit
, inst
) &&
1959 emit_src( emit
, loop_reg
) &&
1960 emit_src( emit
, const_int
) );
1965 emit_endloop(struct svga_shader_emitter
*emit
,
1966 const struct tgsi_full_instruction
*insn
)
1968 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_ENDLOOP
);
1970 emit
->dynamic_branching_level
--;
1972 return emit_instruction( emit
, inst
);
1977 * Translate/emit TGSI BREAK (out of loop) instruction.
1980 emit_brk(struct svga_shader_emitter
*emit
,
1981 const struct tgsi_full_instruction
*insn
)
1983 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_BREAK
);
1984 return emit_instruction( emit
, inst
);
1989 * Emit simple instruction which operates on one scalar value (not
1990 * a vector). Ex: LG2, RCP, RSQ.
1993 emit_scalar_op1(struct svga_shader_emitter
*emit
,
1995 const struct tgsi_full_instruction
*insn
)
1997 SVGA3dShaderInstToken inst
;
1998 SVGA3dShaderDestToken dst
;
1999 struct src_register src
;
2001 inst
= inst_token( opcode
);
2002 dst
= translate_dst_register( emit
, insn
, 0 );
2003 src
= translate_src_register( emit
, &insn
->Src
[0] );
2004 src
= scalar( src
, TGSI_SWIZZLE_X
);
2006 return submit_op1( emit
, inst
, dst
, src
);
2011 * Translate/emit a simple instruction (one which has no special-case
2012 * code) such as ADD, MUL, MIN, MAX.
2015 emit_simple_instruction(struct svga_shader_emitter
*emit
,
2017 const struct tgsi_full_instruction
*insn
)
2019 const struct tgsi_full_src_register
*src
= insn
->Src
;
2020 SVGA3dShaderInstToken inst
;
2021 SVGA3dShaderDestToken dst
;
2023 inst
= inst_token( opcode
);
2024 dst
= translate_dst_register( emit
, insn
, 0 );
2026 switch (insn
->Instruction
.NumSrcRegs
) {
2028 return submit_op0( emit
, inst
, dst
);
2030 return submit_op1( emit
, inst
, dst
,
2031 translate_src_register( emit
, &src
[0] ));
2033 return submit_op2( emit
, inst
, dst
,
2034 translate_src_register( emit
, &src
[0] ),
2035 translate_src_register( emit
, &src
[1] ) );
2037 return submit_op3( emit
, inst
, dst
,
2038 translate_src_register( emit
, &src
[0] ),
2039 translate_src_register( emit
, &src
[1] ),
2040 translate_src_register( emit
, &src
[2] ) );
2049 * TGSI_OPCODE_MOVE is only special-cased here to detect the
2050 * svga_fragment_shader::constant_color_output case.
2053 emit_mov(struct svga_shader_emitter
*emit
,
2054 const struct tgsi_full_instruction
*insn
)
2056 const struct tgsi_full_src_register
*src
= &insn
->Src
[0];
2057 const struct tgsi_full_dst_register
*dst
= &insn
->Dst
[0];
2059 if (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
2060 dst
->Register
.File
== TGSI_FILE_OUTPUT
&&
2061 dst
->Register
.Index
== 0 &&
2062 src
->Register
.File
== TGSI_FILE_CONSTANT
&&
2063 !src
->Register
.Indirect
) {
2064 emit
->constant_color_output
= TRUE
;
2067 return emit_simple_instruction(emit
, SVGA3DOP_MOV
, insn
);
2072 * Translate/emit TGSI DDX, DDY instructions.
2075 emit_deriv(struct svga_shader_emitter
*emit
,
2076 const struct tgsi_full_instruction
*insn
)
2078 if (emit
->dynamic_branching_level
> 0 &&
2079 insn
->Src
[0].Register
.File
== TGSI_FILE_TEMPORARY
)
2081 SVGA3dShaderDestToken dst
=
2082 translate_dst_register( emit
, insn
, 0 );
2084 /* Deriv opcodes not valid inside dynamic branching, workaround
2085 * by zeroing out the destination.
2087 if (!submit_op1(emit
,
2088 inst_token( SVGA3DOP_MOV
),
2090 get_zero_immediate(emit
)))
2097 const struct tgsi_full_src_register
*reg
= &insn
->Src
[0];
2098 SVGA3dShaderInstToken inst
;
2099 SVGA3dShaderDestToken dst
;
2100 struct src_register src0
;
2102 switch (insn
->Instruction
.Opcode
) {
2103 case TGSI_OPCODE_DDX
:
2104 opcode
= SVGA3DOP_DSX
;
2106 case TGSI_OPCODE_DDY
:
2107 opcode
= SVGA3DOP_DSY
;
2113 inst
= inst_token( opcode
);
2114 dst
= translate_dst_register( emit
, insn
, 0 );
2115 src0
= translate_src_register( emit
, reg
);
2117 /* We cannot use negate or abs on source to dsx/dsy instruction.
2119 if (reg
->Register
.Absolute
||
2120 reg
->Register
.Negate
) {
2121 SVGA3dShaderDestToken temp
= get_temp( emit
);
2123 if (!emit_repl( emit
, temp
, &src0
))
2127 return submit_op1( emit
, inst
, dst
, src0
);
2133 * Translate/emit ARL (Address Register Load) instruction. Used to
2134 * move a value into the special 'address' register. Used to implement
2135 * indirect/variable indexing into arrays.
2138 emit_arl(struct svga_shader_emitter
*emit
,
2139 const struct tgsi_full_instruction
*insn
)
2141 ++emit
->current_arl
;
2142 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
2143 /* MOVA not present in pixel shader instruction set.
2144 * Ignore this instruction altogether since it is
2145 * only used for loop counters -- and for that
2146 * we reference aL directly.
2150 if (svga_arl_needs_adjustment( emit
)) {
2151 return emit_fake_arl( emit
, insn
);
2153 /* no need to adjust, just emit straight arl */
2154 return emit_simple_instruction(emit
, SVGA3DOP_MOVA
, insn
);
2160 emit_pow(struct svga_shader_emitter
*emit
,
2161 const struct tgsi_full_instruction
*insn
)
2163 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2164 struct src_register src0
= translate_src_register(
2165 emit
, &insn
->Src
[0] );
2166 struct src_register src1
= translate_src_register(
2167 emit
, &insn
->Src
[1] );
2168 boolean need_tmp
= FALSE
;
2170 /* POW can only output to a temporary */
2171 if (insn
->Dst
[0].Register
.File
!= TGSI_FILE_TEMPORARY
)
2174 /* POW src1 must not be the same register as dst */
2175 if (alias_src_dst( src1
, dst
))
2178 /* it's a scalar op */
2179 src0
= scalar( src0
, TGSI_SWIZZLE_X
);
2180 src1
= scalar( src1
, TGSI_SWIZZLE_X
);
2183 SVGA3dShaderDestToken tmp
=
2184 writemask(get_temp( emit
), TGSI_WRITEMASK_X
);
2186 if (!submit_op2(emit
, inst_token( SVGA3DOP_POW
), tmp
, src0
, src1
))
2189 return submit_op1(emit
, inst_token( SVGA3DOP_MOV
),
2190 dst
, scalar(src(tmp
), 0) );
2193 return submit_op2(emit
, inst_token( SVGA3DOP_POW
), dst
, src0
, src1
);
2199 * Translate/emit TGSI XPD (vector cross product) instruction.
2202 emit_xpd(struct svga_shader_emitter
*emit
,
2203 const struct tgsi_full_instruction
*insn
)
2205 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2206 const struct src_register src0
= translate_src_register(
2207 emit
, &insn
->Src
[0] );
2208 const struct src_register src1
= translate_src_register(
2209 emit
, &insn
->Src
[1] );
2210 boolean need_dst_tmp
= FALSE
;
2212 /* XPD can only output to a temporary */
2213 if (SVGA3dShaderGetRegType(dst
.value
) != SVGA3DREG_TEMP
)
2214 need_dst_tmp
= TRUE
;
2216 /* The dst reg must not be the same as src0 or src1*/
2217 if (alias_src_dst(src0
, dst
) ||
2218 alias_src_dst(src1
, dst
))
2219 need_dst_tmp
= TRUE
;
2222 SVGA3dShaderDestToken tmp
= get_temp( emit
);
2224 /* Obey DX9 restrictions on mask:
2226 tmp
.mask
= dst
.mask
& TGSI_WRITEMASK_XYZ
;
2228 if (!submit_op2(emit
, inst_token( SVGA3DOP_CRS
), tmp
, src0
, src1
))
2231 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), dst
, src( tmp
)))
2235 if (!submit_op2(emit
, inst_token( SVGA3DOP_CRS
), dst
, src0
, src1
))
2239 /* Need to emit 1.0 to dst.w?
2241 if (dst
.mask
& TGSI_WRITEMASK_W
) {
2242 struct src_register one
= get_one_immediate( emit
);
2244 if (!submit_op1(emit
,
2245 inst_token( SVGA3DOP_MOV
),
2246 writemask(dst
, TGSI_WRITEMASK_W
),
2256 * Emit a LRP (linear interpolation) instruction.
2259 submit_lrp(struct svga_shader_emitter
*emit
,
2260 SVGA3dShaderDestToken dst
,
2261 struct src_register src0
,
2262 struct src_register src1
,
2263 struct src_register src2
)
2265 SVGA3dShaderDestToken tmp
;
2266 boolean need_dst_tmp
= FALSE
;
2268 /* The dst reg must be a temporary, and not be the same as src0 or src2 */
2269 if (SVGA3dShaderGetRegType(dst
.value
) != SVGA3DREG_TEMP
||
2270 alias_src_dst(src0
, dst
) ||
2271 alias_src_dst(src2
, dst
))
2272 need_dst_tmp
= TRUE
;
2275 tmp
= get_temp( emit
);
2276 tmp
.mask
= dst
.mask
;
2282 if (!submit_op3(emit
, inst_token( SVGA3DOP_LRP
), tmp
, src0
, src1
, src2
))
2286 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), dst
, src( tmp
)))
2295 * Translate/emit LRP (Linear Interpolation) instruction.
2298 emit_lrp(struct svga_shader_emitter
*emit
,
2299 const struct tgsi_full_instruction
*insn
)
2301 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2302 const struct src_register src0
= translate_src_register(
2303 emit
, &insn
->Src
[0] );
2304 const struct src_register src1
= translate_src_register(
2305 emit
, &insn
->Src
[1] );
2306 const struct src_register src2
= translate_src_register(
2307 emit
, &insn
->Src
[2] );
2309 return submit_lrp(emit
, dst
, src0
, src1
, src2
);
2313 * Translate/emit DST (Distance function) instruction.
2316 emit_dst_insn(struct svga_shader_emitter
*emit
,
2317 const struct tgsi_full_instruction
*insn
)
2319 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2320 /* SVGA/DX9 has a DST instruction, but only for vertex shaders:
2322 return emit_simple_instruction(emit
, SVGA3DOP_DST
, insn
);
2325 /* result[0] = 1 * 1;
2326 * result[1] = a[1] * b[1];
2327 * result[2] = a[2] * 1;
2328 * result[3] = 1 * b[3];
2330 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2331 SVGA3dShaderDestToken tmp
;
2332 const struct src_register src0
= translate_src_register(
2333 emit
, &insn
->Src
[0] );
2334 const struct src_register src1
= translate_src_register(
2335 emit
, &insn
->Src
[1] );
2336 boolean need_tmp
= FALSE
;
2338 if (SVGA3dShaderGetRegType(dst
.value
) != SVGA3DREG_TEMP
||
2339 alias_src_dst(src0
, dst
) ||
2340 alias_src_dst(src1
, dst
))
2344 tmp
= get_temp( emit
);
2352 if (tmp
.mask
& TGSI_WRITEMASK_XW
) {
2353 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2354 writemask(tmp
, TGSI_WRITEMASK_XW
),
2355 get_one_immediate(emit
)))
2361 if (tmp
.mask
& TGSI_WRITEMASK_YZ
) {
2362 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2363 writemask(tmp
, TGSI_WRITEMASK_YZ
),
2368 /* tmp.yw = tmp * src1
2370 if (tmp
.mask
& TGSI_WRITEMASK_YW
) {
2371 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
2372 writemask(tmp
, TGSI_WRITEMASK_YW
),
2381 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2393 emit_exp(struct svga_shader_emitter
*emit
,
2394 const struct tgsi_full_instruction
*insn
)
2396 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2397 struct src_register src0
=
2398 translate_src_register( emit
, &insn
->Src
[0] );
2399 SVGA3dShaderDestToken fraction
;
2401 if (dst
.mask
& TGSI_WRITEMASK_Y
)
2403 else if (dst
.mask
& TGSI_WRITEMASK_X
)
2404 fraction
= get_temp( emit
);
2408 /* If y is being written, fill it with src0 - floor(src0).
2410 if (dst
.mask
& TGSI_WRITEMASK_XY
) {
2411 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
),
2412 writemask( fraction
, TGSI_WRITEMASK_Y
),
2417 /* If x is being written, fill it with 2 ^ floor(src0).
2419 if (dst
.mask
& TGSI_WRITEMASK_X
) {
2420 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
),
2421 writemask( dst
, TGSI_WRITEMASK_X
),
2423 scalar( negate( src( fraction
) ), TGSI_SWIZZLE_Y
) ) )
2426 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXP
),
2427 writemask( dst
, TGSI_WRITEMASK_X
),
2428 scalar( src( dst
), TGSI_SWIZZLE_X
) ) )
2431 if (!(dst
.mask
& TGSI_WRITEMASK_Y
))
2432 release_temp( emit
, fraction
);
2435 /* If z is being written, fill it with 2 ^ src0 (partial precision).
2437 if (dst
.mask
& TGSI_WRITEMASK_Z
) {
2438 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXPP
),
2439 writemask( dst
, TGSI_WRITEMASK_Z
),
2444 /* If w is being written, fill it with one.
2446 if (dst
.mask
& TGSI_WRITEMASK_W
) {
2447 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2448 writemask(dst
, TGSI_WRITEMASK_W
),
2449 get_one_immediate(emit
)))
2458 * Translate/emit LIT (Lighting helper) instruction.
2461 emit_lit(struct svga_shader_emitter
*emit
,
2462 const struct tgsi_full_instruction
*insn
)
2464 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2465 /* SVGA/DX9 has a LIT instruction, but only for vertex shaders:
2467 return emit_simple_instruction(emit
, SVGA3DOP_LIT
, insn
);
2470 /* D3D vs. GL semantics can be fairly easily accomodated by
2471 * variations on this sequence.
2475 * tmp.z = pow(src.y,src.w)
2476 * p0 = src0.xxxx > 0
2477 * result = zero.wxxw
2478 * (p0) result.yz = tmp
2482 * tmp.z = pow(src.y,src.w)
2483 * p0 = src0.xxyy > 0
2484 * result = zero.wxxw
2485 * (p0) result.yz = tmp
2487 * Will implement the GL version for now.
2489 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2490 SVGA3dShaderDestToken tmp
= get_temp( emit
);
2491 const struct src_register src0
= translate_src_register(
2492 emit
, &insn
->Src
[0] );
2494 /* tmp = pow(src.y, src.w)
2496 if (dst
.mask
& TGSI_WRITEMASK_Z
) {
2497 if (!submit_op2(emit
, inst_token( SVGA3DOP_POW
),
2506 if (dst
.mask
& TGSI_WRITEMASK_Y
) {
2507 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2508 writemask(tmp
, TGSI_WRITEMASK_Y
),
2513 /* Can't quite do this with emit conditional due to the extra
2514 * writemask on the predicated mov:
2517 SVGA3dShaderDestToken pred_reg
= dst_register( SVGA3DREG_PREDICATE
, 0 );
2518 struct src_register predsrc
;
2520 /* D3D vs GL semantics:
2523 predsrc
= swizzle(src0
, 0, 0, 1, 1); /* D3D */
2525 predsrc
= swizzle(src0
, 0, 0, 0, 0); /* GL */
2527 /* SETP src0.xxyy, GT, {0}.x */
2528 if (!submit_op2( emit
,
2529 inst_token_setp(SVGA3DOPCOMP_GT
),
2532 get_zero_immediate(emit
)))
2536 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
,
2537 get_immediate(emit
, 1.0f
, 0.0f
, 0.0f
, 1.0f
)))
2540 /* MOV dst.yz, tmp (predicated)
2542 * Note that the predicate reg (and possible modifiers) is passed
2543 * as the first source argument.
2545 if (dst
.mask
& TGSI_WRITEMASK_YZ
) {
2546 if (!submit_op2( emit
,
2547 inst_token_predicated(SVGA3DOP_MOV
),
2548 writemask(dst
, TGSI_WRITEMASK_YZ
),
2549 src( pred_reg
), src( tmp
) ))
2560 emit_ex2(struct svga_shader_emitter
*emit
,
2561 const struct tgsi_full_instruction
*insn
)
2563 SVGA3dShaderInstToken inst
;
2564 SVGA3dShaderDestToken dst
;
2565 struct src_register src0
;
2567 inst
= inst_token( SVGA3DOP_EXP
);
2568 dst
= translate_dst_register( emit
, insn
, 0 );
2569 src0
= translate_src_register( emit
, &insn
->Src
[0] );
2570 src0
= scalar( src0
, TGSI_SWIZZLE_X
);
2572 if (dst
.mask
!= TGSI_WRITEMASK_XYZW
) {
2573 SVGA3dShaderDestToken tmp
= get_temp( emit
);
2575 if (!submit_op1( emit
, inst
, tmp
, src0
))
2578 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2580 scalar( src( tmp
), TGSI_SWIZZLE_X
) );
2583 return submit_op1( emit
, inst
, dst
, src0
);
2588 emit_log(struct svga_shader_emitter
*emit
,
2589 const struct tgsi_full_instruction
*insn
)
2591 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2592 struct src_register src0
=
2593 translate_src_register( emit
, &insn
->Src
[0] );
2594 SVGA3dShaderDestToken abs_tmp
;
2595 struct src_register abs_src0
;
2596 SVGA3dShaderDestToken log2_abs
;
2600 if (dst
.mask
& TGSI_WRITEMASK_Z
)
2602 else if (dst
.mask
& TGSI_WRITEMASK_XY
)
2603 log2_abs
= get_temp( emit
);
2607 /* If z is being written, fill it with log2( abs( src0 ) ).
2609 if (dst
.mask
& TGSI_WRITEMASK_XYZ
) {
2610 if (!src0
.base
.srcMod
|| src0
.base
.srcMod
== SVGA3DSRCMOD_ABS
)
2613 abs_tmp
= get_temp( emit
);
2615 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2620 abs_src0
= src( abs_tmp
);
2623 abs_src0
= absolute( scalar( abs_src0
, TGSI_SWIZZLE_X
) );
2625 if (!submit_op1( emit
, inst_token( SVGA3DOP_LOG
),
2626 writemask( log2_abs
, TGSI_WRITEMASK_Z
),
2631 if (dst
.mask
& TGSI_WRITEMASK_XY
) {
2632 SVGA3dShaderDestToken floor_log2
;
2634 if (dst
.mask
& TGSI_WRITEMASK_X
)
2637 floor_log2
= get_temp( emit
);
2639 /* If x is being written, fill it with floor( log2( abs( src0 ) ) ).
2641 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
),
2642 writemask( floor_log2
, TGSI_WRITEMASK_X
),
2643 scalar( src( log2_abs
), TGSI_SWIZZLE_Z
) ) )
2646 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
),
2647 writemask( floor_log2
, TGSI_WRITEMASK_X
),
2648 scalar( src( log2_abs
), TGSI_SWIZZLE_Z
),
2649 negate( src( floor_log2
) ) ) )
2652 /* If y is being written, fill it with
2653 * abs ( src0 ) / ( 2 ^ floor( log2( abs( src0 ) ) ) ).
2655 if (dst
.mask
& TGSI_WRITEMASK_Y
) {
2656 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXP
),
2657 writemask( dst
, TGSI_WRITEMASK_Y
),
2658 negate( scalar( src( floor_log2
),
2659 TGSI_SWIZZLE_X
) ) ) )
2662 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
2663 writemask( dst
, TGSI_WRITEMASK_Y
),
2669 if (!(dst
.mask
& TGSI_WRITEMASK_X
))
2670 release_temp( emit
, floor_log2
);
2672 if (!(dst
.mask
& TGSI_WRITEMASK_Z
))
2673 release_temp( emit
, log2_abs
);
2676 if (dst
.mask
& TGSI_WRITEMASK_XYZ
&& src0
.base
.srcMod
&&
2677 src0
.base
.srcMod
!= SVGA3DSRCMOD_ABS
)
2678 release_temp( emit
, abs_tmp
);
2680 /* If w is being written, fill it with one.
2682 if (dst
.mask
& TGSI_WRITEMASK_W
) {
2683 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2684 writemask(dst
, TGSI_WRITEMASK_W
),
2685 get_one_immediate(emit
)))
2694 * Translate TGSI TRUNC or ROUND instruction.
2695 * We need to truncate toward zero. Ex: trunc(-1.9) = -1
2696 * Different approaches are needed for VS versus PS.
2699 emit_trunc_round(struct svga_shader_emitter
*emit
,
2700 const struct tgsi_full_instruction
*insn
,
2703 SVGA3dShaderDestToken dst
= translate_dst_register(emit
, insn
, 0);
2704 const struct src_register src0
=
2705 translate_src_register(emit
, &insn
->Src
[0] );
2706 SVGA3dShaderDestToken t1
= get_temp(emit
);
2709 SVGA3dShaderDestToken t0
= get_temp(emit
);
2710 struct src_register half
= get_half_immediate(emit
);
2712 /* t0 = abs(src0) + 0.5 */
2713 if (!submit_op2(emit
, inst_token(SVGA3DOP_ADD
), t0
,
2714 absolute(src0
), half
))
2717 /* t1 = fract(t0) */
2718 if (!submit_op1(emit
, inst_token(SVGA3DOP_FRC
), t1
, src(t0
)))
2722 if (!submit_op2(emit
, inst_token(SVGA3DOP_ADD
), t1
, src(t0
),
2729 /* t1 = fract(abs(src0)) */
2730 if (!submit_op1(emit
, inst_token(SVGA3DOP_FRC
), t1
, absolute(src0
)))
2733 /* t1 = abs(src0) - t1 */
2734 if (!submit_op2(emit
, inst_token(SVGA3DOP_ADD
), t1
, absolute(src0
),
2740 * Now we need to multiply t1 by the sign of the original value.
2742 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2743 /* For VS: use SGN instruction */
2744 /* Need two extra/dummy registers: */
2745 SVGA3dShaderDestToken t2
= get_temp(emit
), t3
= get_temp(emit
),
2746 t4
= get_temp(emit
);
2748 /* t2 = sign(src0) */
2749 if (!submit_op3(emit
, inst_token(SVGA3DOP_SGN
), t2
, src0
,
2754 if (!submit_op2(emit
, inst_token(SVGA3DOP_MUL
), dst
, src(t1
), src(t2
)))
2758 /* For FS: Use CMP instruction */
2759 return submit_op3(emit
, inst_token( SVGA3DOP_CMP
), dst
,
2760 src0
, src(t1
), negate(src(t1
)));
2768 * Translate/emit "begin subroutine" instruction/marker/label.
2771 emit_bgnsub(struct svga_shader_emitter
*emit
,
2773 const struct tgsi_full_instruction
*insn
)
2777 /* Note that we've finished the main function and are now emitting
2778 * subroutines. This affects how we terminate the generated
2781 emit
->in_main_func
= FALSE
;
2783 for (i
= 0; i
< emit
->nr_labels
; i
++) {
2784 if (emit
->label
[i
] == position
) {
2785 return (emit_instruction( emit
, inst_token( SVGA3DOP_RET
) ) &&
2786 emit_instruction( emit
, inst_token( SVGA3DOP_LABEL
) ) &&
2787 emit_src( emit
, src_register( SVGA3DREG_LABEL
, i
)));
2797 * Translate/emit subroutine call instruction.
2800 emit_call(struct svga_shader_emitter
*emit
,
2801 const struct tgsi_full_instruction
*insn
)
2803 unsigned position
= insn
->Label
.Label
;
2806 for (i
= 0; i
< emit
->nr_labels
; i
++) {
2807 if (emit
->label
[i
] == position
)
2811 if (emit
->nr_labels
== ARRAY_SIZE(emit
->label
))
2814 if (i
== emit
->nr_labels
) {
2815 emit
->label
[i
] = position
;
2819 return (emit_instruction( emit
, inst_token( SVGA3DOP_CALL
) ) &&
2820 emit_src( emit
, src_register( SVGA3DREG_LABEL
, i
)));
2825 * Called at the end of the shader. Actually, emit special "fix-up"
2826 * code for the vertex/fragment shader.
2829 emit_end(struct svga_shader_emitter
*emit
)
2831 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2832 return emit_vs_postamble( emit
);
2835 return emit_ps_postamble( emit
);
2841 * Translate any TGSI instruction to SVGA.
2844 svga_emit_instruction(struct svga_shader_emitter
*emit
,
2846 const struct tgsi_full_instruction
*insn
)
2848 switch (insn
->Instruction
.Opcode
) {
2850 case TGSI_OPCODE_ARL
:
2851 return emit_arl( emit
, insn
);
2853 case TGSI_OPCODE_TEX
:
2854 case TGSI_OPCODE_TXB
:
2855 case TGSI_OPCODE_TXP
:
2856 case TGSI_OPCODE_TXL
:
2857 case TGSI_OPCODE_TXD
:
2858 return emit_tex( emit
, insn
);
2860 case TGSI_OPCODE_DDX
:
2861 case TGSI_OPCODE_DDY
:
2862 return emit_deriv( emit
, insn
);
2864 case TGSI_OPCODE_BGNSUB
:
2865 return emit_bgnsub( emit
, position
, insn
);
2867 case TGSI_OPCODE_ENDSUB
:
2870 case TGSI_OPCODE_CAL
:
2871 return emit_call( emit
, insn
);
2873 case TGSI_OPCODE_FLR
:
2874 return emit_floor( emit
, insn
);
2876 case TGSI_OPCODE_TRUNC
:
2877 return emit_trunc_round( emit
, insn
, FALSE
);
2879 case TGSI_OPCODE_ROUND
:
2880 return emit_trunc_round( emit
, insn
, TRUE
);
2882 case TGSI_OPCODE_CEIL
:
2883 return emit_ceil( emit
, insn
);
2885 case TGSI_OPCODE_CMP
:
2886 return emit_cmp( emit
, insn
);
2888 case TGSI_OPCODE_DIV
:
2889 return emit_div( emit
, insn
);
2891 case TGSI_OPCODE_DP2
:
2892 return emit_dp2( emit
, insn
);
2894 case TGSI_OPCODE_COS
:
2895 return emit_cos( emit
, insn
);
2897 case TGSI_OPCODE_SIN
:
2898 return emit_sin( emit
, insn
);
2900 case TGSI_OPCODE_SCS
:
2901 return emit_sincos( emit
, insn
);
2903 case TGSI_OPCODE_END
:
2904 /* TGSI always finishes the main func with an END */
2905 return emit_end( emit
);
2907 case TGSI_OPCODE_KILL_IF
:
2908 return emit_kill_if( emit
, insn
);
2910 /* Selection opcodes. The underlying language is fairly
2911 * non-orthogonal about these.
2913 case TGSI_OPCODE_SEQ
:
2914 return emit_select_op( emit
, PIPE_FUNC_EQUAL
, insn
);
2916 case TGSI_OPCODE_SNE
:
2917 return emit_select_op( emit
, PIPE_FUNC_NOTEQUAL
, insn
);
2919 case TGSI_OPCODE_SGT
:
2920 return emit_select_op( emit
, PIPE_FUNC_GREATER
, insn
);
2922 case TGSI_OPCODE_SGE
:
2923 return emit_select_op( emit
, PIPE_FUNC_GEQUAL
, insn
);
2925 case TGSI_OPCODE_SLT
:
2926 return emit_select_op( emit
, PIPE_FUNC_LESS
, insn
);
2928 case TGSI_OPCODE_SLE
:
2929 return emit_select_op( emit
, PIPE_FUNC_LEQUAL
, insn
);
2931 case TGSI_OPCODE_POW
:
2932 return emit_pow( emit
, insn
);
2934 case TGSI_OPCODE_EX2
:
2935 return emit_ex2( emit
, insn
);
2937 case TGSI_OPCODE_EXP
:
2938 return emit_exp( emit
, insn
);
2940 case TGSI_OPCODE_LOG
:
2941 return emit_log( emit
, insn
);
2943 case TGSI_OPCODE_LG2
:
2944 return emit_scalar_op1( emit
, SVGA3DOP_LOG
, insn
);
2946 case TGSI_OPCODE_RSQ
:
2947 return emit_scalar_op1( emit
, SVGA3DOP_RSQ
, insn
);
2949 case TGSI_OPCODE_RCP
:
2950 return emit_scalar_op1( emit
, SVGA3DOP_RCP
, insn
);
2952 case TGSI_OPCODE_CONT
:
2953 /* not expected (we return PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED = 0) */
2956 case TGSI_OPCODE_RET
:
2957 /* This is a noop -- we tell mesa that we can't support RET
2958 * within a function (early return), so this will always be
2959 * followed by an ENDSUB.
2963 /* These aren't actually used by any of the frontends we care
2966 case TGSI_OPCODE_AND
:
2967 case TGSI_OPCODE_OR
:
2968 case TGSI_OPCODE_I2F
:
2969 case TGSI_OPCODE_NOT
:
2970 case TGSI_OPCODE_SHL
:
2971 case TGSI_OPCODE_ISHR
:
2972 case TGSI_OPCODE_XOR
:
2975 case TGSI_OPCODE_IF
:
2976 return emit_if( emit
, insn
);
2977 case TGSI_OPCODE_ELSE
:
2978 return emit_else( emit
, insn
);
2979 case TGSI_OPCODE_ENDIF
:
2980 return emit_endif( emit
, insn
);
2982 case TGSI_OPCODE_BGNLOOP
:
2983 return emit_bgnloop( emit
, insn
);
2984 case TGSI_OPCODE_ENDLOOP
:
2985 return emit_endloop( emit
, insn
);
2986 case TGSI_OPCODE_BRK
:
2987 return emit_brk( emit
, insn
);
2989 case TGSI_OPCODE_XPD
:
2990 return emit_xpd( emit
, insn
);
2992 case TGSI_OPCODE_KILL
:
2993 return emit_kill( emit
, insn
);
2995 case TGSI_OPCODE_DST
:
2996 return emit_dst_insn( emit
, insn
);
2998 case TGSI_OPCODE_LIT
:
2999 return emit_lit( emit
, insn
);
3001 case TGSI_OPCODE_LRP
:
3002 return emit_lrp( emit
, insn
);
3004 case TGSI_OPCODE_SSG
:
3005 return emit_ssg( emit
, insn
);
3007 case TGSI_OPCODE_MOV
:
3008 return emit_mov( emit
, insn
);
3012 unsigned opcode
= translate_opcode(insn
->Instruction
.Opcode
);
3014 if (opcode
== SVGA3DOP_LAST_INST
)
3017 if (!emit_simple_instruction( emit
, opcode
, insn
))
3027 * Translate/emit a TGSI IMMEDIATE declaration.
3028 * An immediate vector is a constant that's hard-coded into the shader.
3031 svga_emit_immediate(struct svga_shader_emitter
*emit
,
3032 const struct tgsi_full_immediate
*imm
)
3034 static const float id
[4] = {0,0,0,1};
3038 assert(1 <= imm
->Immediate
.NrTokens
&& imm
->Immediate
.NrTokens
<= 5);
3039 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++) {
3040 float f
= imm
->u
[i
].Float
;
3041 value
[i
] = util_is_inf_or_nan(f
) ? 0.0f
: f
;
3044 /* If the immediate has less than four values, fill in the remaining
3045 * positions from id={0,0,0,1}.
3047 for ( ; i
< 4; i
++ )
3050 return emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
3051 emit
->imm_start
+ emit
->internal_imm_count
++,
3052 value
[0], value
[1], value
[2], value
[3]);
3057 make_immediate(struct svga_shader_emitter
*emit
,
3058 float a
, float b
, float c
, float d
,
3059 struct src_register
*out
)
3061 unsigned idx
= emit
->nr_hw_float_const
++;
3063 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
3067 *out
= src_register( SVGA3DREG_CONST
, idx
);
3074 * Emit special VS instructions at top of shader.
3077 emit_vs_preamble(struct svga_shader_emitter
*emit
)
3079 if (!emit
->key
.vs
.need_prescale
) {
3080 if (!make_immediate( emit
, 0, 0, .5, .5,
3090 * Emit special PS instructions at top of shader.
3093 emit_ps_preamble(struct svga_shader_emitter
*emit
)
3095 if (emit
->ps_reads_pos
&& emit
->info
.reads_z
) {
3097 * Assemble the position from various bits of inputs. Depth and W are
3098 * passed in a texcoord this is due to D3D's vPos not hold Z or W.
3099 * Also fixup the perspective interpolation.
3101 * temp_pos.xy = vPos.xy
3102 * temp_pos.w = rcp(texcoord1.w);
3103 * temp_pos.z = texcoord1.z * temp_pos.w;
3105 if (!submit_op1( emit
,
3106 inst_token(SVGA3DOP_MOV
),
3107 writemask( emit
->ps_temp_pos
, TGSI_WRITEMASK_XY
),
3108 emit
->ps_true_pos
))
3111 if (!submit_op1( emit
,
3112 inst_token(SVGA3DOP_RCP
),
3113 writemask( emit
->ps_temp_pos
, TGSI_WRITEMASK_W
),
3114 scalar( emit
->ps_depth_pos
, TGSI_SWIZZLE_W
) ))
3117 if (!submit_op2( emit
,
3118 inst_token(SVGA3DOP_MUL
),
3119 writemask( emit
->ps_temp_pos
, TGSI_WRITEMASK_Z
),
3120 scalar( emit
->ps_depth_pos
, TGSI_SWIZZLE_Z
),
3121 scalar( src(emit
->ps_temp_pos
), TGSI_SWIZZLE_W
) ))
3130 * Emit special PS instructions at end of shader.
3133 emit_ps_postamble(struct svga_shader_emitter
*emit
)
3137 /* PS oDepth is incredibly fragile and it's very hard to catch the
3138 * types of usage that break it during shader emit. Easier just to
3139 * redirect the main program to a temporary and then only touch
3140 * oDepth with a hand-crafted MOV below.
3142 if (SVGA3dShaderGetRegType(emit
->true_pos
.value
) != 0) {
3143 if (!submit_op1( emit
,
3144 inst_token(SVGA3DOP_MOV
),
3146 scalar(src(emit
->temp_pos
), TGSI_SWIZZLE_Z
) ))
3150 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
3151 if (SVGA3dShaderGetRegType(emit
->true_color_output
[i
].value
) != 0) {
3152 /* Potentially override output colors with white for XOR
3153 * logicop workaround.
3155 if (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
3156 emit
->key
.fs
.white_fragments
) {
3157 struct src_register one
= get_one_immediate(emit
);
3159 if (!submit_op1( emit
,
3160 inst_token(SVGA3DOP_MOV
),
3161 emit
->true_color_output
[i
],
3165 else if (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
3166 i
< emit
->key
.fs
.write_color0_to_n_cbufs
) {
3167 /* Write temp color output [0] to true output [i] */
3168 if (!submit_op1(emit
, inst_token(SVGA3DOP_MOV
),
3169 emit
->true_color_output
[i
],
3170 src(emit
->temp_color_output
[0]))) {
3175 if (!submit_op1( emit
,
3176 inst_token(SVGA3DOP_MOV
),
3177 emit
->true_color_output
[i
],
3178 src(emit
->temp_color_output
[i
]) ))
3189 * Emit special VS instructions at end of shader.
3192 emit_vs_postamble(struct svga_shader_emitter
*emit
)
3194 /* PSIZ output is incredibly fragile and it's very hard to catch
3195 * the types of usage that break it during shader emit. Easier
3196 * just to redirect the main program to a temporary and then only
3197 * touch PSIZ with a hand-crafted MOV below.
3199 if (SVGA3dShaderGetRegType(emit
->true_psiz
.value
) != 0) {
3200 if (!submit_op1( emit
,
3201 inst_token(SVGA3DOP_MOV
),
3203 scalar(src(emit
->temp_psiz
), TGSI_SWIZZLE_X
) ))
3207 /* Need to perform various manipulations on vertex position to cope
3208 * with the different GL and D3D clip spaces.
3210 if (emit
->key
.vs
.need_prescale
) {
3211 SVGA3dShaderDestToken temp_pos
= emit
->temp_pos
;
3212 SVGA3dShaderDestToken depth
= emit
->depth_pos
;
3213 SVGA3dShaderDestToken pos
= emit
->true_pos
;
3214 unsigned offset
= emit
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
3215 struct src_register prescale_scale
= src_register( SVGA3DREG_CONST
,
3217 struct src_register prescale_trans
= src_register( SVGA3DREG_CONST
,
3220 if (!submit_op1( emit
,
3221 inst_token(SVGA3DOP_MOV
),
3222 writemask(depth
, TGSI_WRITEMASK_W
),
3223 scalar(src(temp_pos
), TGSI_SWIZZLE_W
) ))
3226 /* MUL temp_pos.xyz, temp_pos, prescale.scale
3227 * MAD result.position, temp_pos.wwww, prescale.trans, temp_pos
3228 * --> Note that prescale.trans.w == 0
3230 if (!submit_op2( emit
,
3231 inst_token(SVGA3DOP_MUL
),
3232 writemask(temp_pos
, TGSI_WRITEMASK_XYZ
),
3237 if (!submit_op3( emit
,
3238 inst_token(SVGA3DOP_MAD
),
3240 swizzle(src(temp_pos
), 3, 3, 3, 3),
3245 /* Also write to depth value */
3246 if (!submit_op3( emit
,
3247 inst_token(SVGA3DOP_MAD
),
3248 writemask(depth
, TGSI_WRITEMASK_Z
),
3249 swizzle(src(temp_pos
), 3, 3, 3, 3),
3255 SVGA3dShaderDestToken temp_pos
= emit
->temp_pos
;
3256 SVGA3dShaderDestToken depth
= emit
->depth_pos
;
3257 SVGA3dShaderDestToken pos
= emit
->true_pos
;
3258 struct src_register imm_0055
= emit
->imm_0055
;
3260 /* Adjust GL clipping coordinate space to hardware (D3D-style):
3262 * DP4 temp_pos.z, {0,0,.5,.5}, temp_pos
3263 * MOV result.position, temp_pos
3265 if (!submit_op2( emit
,
3266 inst_token(SVGA3DOP_DP4
),
3267 writemask(temp_pos
, TGSI_WRITEMASK_Z
),
3272 if (!submit_op1( emit
,
3273 inst_token(SVGA3DOP_MOV
),
3278 /* Move the manipulated depth into the extra texcoord reg */
3279 if (!submit_op1( emit
,
3280 inst_token(SVGA3DOP_MOV
),
3281 writemask(depth
, TGSI_WRITEMASK_ZW
),
3291 * For the pixel shader: emit the code which chooses the front
3292 * or back face color depending on triangle orientation.
3293 * This happens at the top of the fragment shader.
3296 * 1: COLOR = FrontColor;
3298 * 3: COLOR = BackColor;
3302 emit_light_twoside(struct svga_shader_emitter
*emit
)
3304 struct src_register vface
, zero
;
3305 struct src_register front
[2];
3306 struct src_register back
[2];
3307 SVGA3dShaderDestToken color
[2];
3308 int count
= emit
->internal_color_count
;
3310 SVGA3dShaderInstToken if_token
;
3315 vface
= get_vface( emit
);
3316 zero
= get_zero_immediate(emit
);
3318 /* Can't use get_temp() to allocate the color reg as such
3319 * temporaries will be reclaimed after each instruction by the call
3320 * to reset_temp_regs().
3322 for (i
= 0; i
< count
; i
++) {
3323 color
[i
] = dst_register( SVGA3DREG_TEMP
, emit
->nr_hw_temp
++ );
3324 front
[i
] = emit
->input_map
[emit
->internal_color_idx
[i
]];
3326 /* Back is always the next input:
3329 back
[i
].base
.num
= front
[i
].base
.num
+ 1;
3331 /* Reassign the input_map to the actual front-face color:
3333 emit
->input_map
[emit
->internal_color_idx
[i
]] = src(color
[i
]);
3336 if_token
= inst_token( SVGA3DOP_IFC
);
3338 if (emit
->key
.fs
.front_ccw
)
3339 if_token
.control
= SVGA3DOPCOMP_LT
;
3341 if_token
.control
= SVGA3DOPCOMP_GT
;
3343 if (!(emit_instruction( emit
, if_token
) &&
3344 emit_src( emit
, vface
) &&
3345 emit_src( emit
, zero
) ))
3348 for (i
= 0; i
< count
; i
++) {
3349 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), color
[i
], front
[i
] ))
3353 if (!(emit_instruction( emit
, inst_token( SVGA3DOP_ELSE
))))
3356 for (i
= 0; i
< count
; i
++) {
3357 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), color
[i
], back
[i
] ))
3361 if (!emit_instruction( emit
, inst_token( SVGA3DOP_ENDIF
) ))
3369 * Emit special setup code for the front/back face register in the FS.
3370 * 0: SETP_GT TEMP, VFACE, 0
3371 * where TEMP is a fake frontface register
3374 emit_frontface(struct svga_shader_emitter
*emit
)
3376 struct src_register vface
;
3377 SVGA3dShaderDestToken temp
;
3378 struct src_register pass
, fail
;
3380 vface
= get_vface( emit
);
3382 /* Can't use get_temp() to allocate the fake frontface reg as such
3383 * temporaries will be reclaimed after each instruction by the call
3384 * to reset_temp_regs().
3386 temp
= dst_register( SVGA3DREG_TEMP
,
3387 emit
->nr_hw_temp
++ );
3389 if (emit
->key
.fs
.front_ccw
) {
3390 pass
= get_zero_immediate(emit
);
3391 fail
= get_one_immediate(emit
);
3393 pass
= get_one_immediate(emit
);
3394 fail
= get_zero_immediate(emit
);
3397 if (!emit_conditional(emit
, PIPE_FUNC_GREATER
,
3398 temp
, vface
, get_zero_immediate(emit
),
3402 /* Reassign the input_map to the actual front-face color:
3404 emit
->input_map
[emit
->internal_frontface_idx
] = src(temp
);
3411 * Emit code to invert the T component of the incoming texture coordinate.
3412 * This is used for drawing point sprites when
3413 * pipe_rasterizer_state::sprite_coord_mode == PIPE_SPRITE_COORD_LOWER_LEFT.
3416 emit_inverted_texcoords(struct svga_shader_emitter
*emit
)
3418 unsigned inverted_texcoords
= emit
->inverted_texcoords
;
3420 while (inverted_texcoords
) {
3421 const unsigned unit
= ffs(inverted_texcoords
) - 1;
3423 assert(emit
->inverted_texcoords
& (1 << unit
));
3425 assert(unit
< ARRAY_SIZE(emit
->ps_true_texcoord
));
3427 assert(unit
< ARRAY_SIZE(emit
->ps_inverted_texcoord_input
));
3429 assert(emit
->ps_inverted_texcoord_input
[unit
]
3430 < ARRAY_SIZE(emit
->input_map
));
3432 /* inverted = coord * (1, -1, 1, 1) + (0, 1, 0, 0) */
3433 if (!submit_op3(emit
,
3434 inst_token(SVGA3DOP_MAD
),
3435 dst(emit
->ps_inverted_texcoord
[unit
]),
3436 emit
->ps_true_texcoord
[unit
],
3437 get_immediate(emit
, 1.0f
, -1.0f
, 1.0f
, 1.0f
),
3438 get_immediate(emit
, 0.0f
, 1.0f
, 0.0f
, 0.0f
)))
3441 /* Reassign the input_map entry to the new texcoord register */
3442 emit
->input_map
[emit
->ps_inverted_texcoord_input
[unit
]] =
3443 emit
->ps_inverted_texcoord
[unit
];
3445 inverted_texcoords
&= ~(1 << unit
);
3453 * Emit code to adjust vertex shader inputs/attributes:
3454 * - Change range from [0,1] to [-1,1] (for normalized byte/short attribs).
3455 * - Set attrib W component = 1.
3458 emit_adjusted_vertex_attribs(struct svga_shader_emitter
*emit
)
3460 unsigned adjust_mask
= (emit
->key
.vs
.adjust_attrib_range
|
3461 emit
->key
.vs
.adjust_attrib_w_1
);
3463 while (adjust_mask
) {
3464 /* Adjust vertex attrib range and/or set W component = 1 */
3465 const unsigned index
= u_bit_scan(&adjust_mask
);
3466 struct src_register tmp
;
3468 /* allocate a temp reg */
3469 tmp
= src_register(SVGA3DREG_TEMP
, emit
->nr_hw_temp
);
3472 if (emit
->key
.vs
.adjust_attrib_range
& (1 << index
)) {
3473 /* The vertex input/attribute is supposed to be a signed value in
3474 * the range [-1,1] but we actually fetched/converted it to the
3475 * range [0,1]. This most likely happens when the app specifies a
3476 * signed byte attribute but we interpreted it as unsigned bytes.
3477 * See also svga_translate_vertex_format().
3479 * Here, we emit some extra instructions to adjust
3480 * the attribute values from [0,1] to [-1,1].
3482 * The adjustment we implement is:
3483 * new_attrib = attrib * 2.0;
3484 * if (attrib >= 0.5)
3485 * new_attrib = new_attrib - 2.0;
3486 * This isn't exactly right (it's off by a bit or so) but close enough.
3488 SVGA3dShaderDestToken pred_reg
= dst_register(SVGA3DREG_PREDICATE
, 0);
3490 /* tmp = attrib * 2.0 */
3491 if (!submit_op2(emit
,
3492 inst_token(SVGA3DOP_MUL
),
3494 emit
->input_map
[index
],
3495 get_two_immediate(emit
)))
3498 /* pred = (attrib >= 0.5) */
3499 if (!submit_op2(emit
,
3500 inst_token_setp(SVGA3DOPCOMP_GE
),
3502 emit
->input_map
[index
], /* vert attrib */
3503 get_half_immediate(emit
))) /* 0.5 */
3506 /* sub(pred) tmp, tmp, 2.0 */
3507 if (!submit_op3(emit
,
3508 inst_token_predicated(SVGA3DOP_SUB
),
3512 get_two_immediate(emit
)))
3516 /* just copy the vertex input attrib to the temp register */
3517 if (!submit_op1(emit
,
3518 inst_token(SVGA3DOP_MOV
),
3520 emit
->input_map
[index
]))
3524 if (emit
->key
.vs
.adjust_attrib_w_1
& (1 << index
)) {
3525 /* move 1 into W position of tmp */
3526 if (!submit_op1(emit
,
3527 inst_token(SVGA3DOP_MOV
),
3528 writemask(dst(tmp
), TGSI_WRITEMASK_W
),
3529 get_one_immediate(emit
)))
3533 /* Reassign the input_map entry to the new tmp register */
3534 emit
->input_map
[index
] = tmp
;
3542 * Determine if we need to create the "common" immediate value which is
3543 * used for generating useful vector constants such as {0,0,0,0} and
3545 * We could just do this all the time except that we want to conserve
3546 * registers whenever possible.
3549 needs_to_create_common_immediate(const struct svga_shader_emitter
*emit
)
3553 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
3554 if (emit
->key
.fs
.light_twoside
)
3557 if (emit
->key
.fs
.white_fragments
)
3560 if (emit
->emit_frontface
)
3563 if (emit
->info
.opcode_count
[TGSI_OPCODE_DST
] >= 1 ||
3564 emit
->info
.opcode_count
[TGSI_OPCODE_SSG
] >= 1 ||
3565 emit
->info
.opcode_count
[TGSI_OPCODE_LIT
] >= 1)
3568 if (emit
->inverted_texcoords
)
3571 /* look for any PIPE_SWIZZLE_0/ONE terms */
3572 for (i
= 0; i
< emit
->key
.num_textures
; i
++) {
3573 if (emit
->key
.tex
[i
].swizzle_r
> PIPE_SWIZZLE_W
||
3574 emit
->key
.tex
[i
].swizzle_g
> PIPE_SWIZZLE_W
||
3575 emit
->key
.tex
[i
].swizzle_b
> PIPE_SWIZZLE_W
||
3576 emit
->key
.tex
[i
].swizzle_a
> PIPE_SWIZZLE_W
)
3580 for (i
= 0; i
< emit
->key
.num_textures
; i
++) {
3581 if (emit
->key
.tex
[i
].compare_mode
3582 == PIPE_TEX_COMPARE_R_TO_TEXTURE
)
3586 else if (emit
->unit
== PIPE_SHADER_VERTEX
) {
3587 if (emit
->info
.opcode_count
[TGSI_OPCODE_CMP
] >= 1)
3589 if (emit
->key
.vs
.adjust_attrib_range
||
3590 emit
->key
.vs
.adjust_attrib_w_1
)
3594 if (emit
->info
.opcode_count
[TGSI_OPCODE_IF
] >= 1 ||
3595 emit
->info
.opcode_count
[TGSI_OPCODE_BGNLOOP
] >= 1 ||
3596 emit
->info
.opcode_count
[TGSI_OPCODE_DDX
] >= 1 ||
3597 emit
->info
.opcode_count
[TGSI_OPCODE_DDY
] >= 1 ||
3598 emit
->info
.opcode_count
[TGSI_OPCODE_ROUND
] >= 1 ||
3599 emit
->info
.opcode_count
[TGSI_OPCODE_SGE
] >= 1 ||
3600 emit
->info
.opcode_count
[TGSI_OPCODE_SGT
] >= 1 ||
3601 emit
->info
.opcode_count
[TGSI_OPCODE_SLE
] >= 1 ||
3602 emit
->info
.opcode_count
[TGSI_OPCODE_SLT
] >= 1 ||
3603 emit
->info
.opcode_count
[TGSI_OPCODE_SNE
] >= 1 ||
3604 emit
->info
.opcode_count
[TGSI_OPCODE_SEQ
] >= 1 ||
3605 emit
->info
.opcode_count
[TGSI_OPCODE_EXP
] >= 1 ||
3606 emit
->info
.opcode_count
[TGSI_OPCODE_LOG
] >= 1 ||
3607 emit
->info
.opcode_count
[TGSI_OPCODE_XPD
] >= 1 ||
3608 emit
->info
.opcode_count
[TGSI_OPCODE_KILL
] >= 1)
3616 * Do we need to create a looping constant?
3619 needs_to_create_loop_const(const struct svga_shader_emitter
*emit
)
3621 return (emit
->info
.opcode_count
[TGSI_OPCODE_BGNLOOP
] >= 1);
3626 needs_to_create_arl_consts(const struct svga_shader_emitter
*emit
)
3628 return (emit
->num_arl_consts
> 0);
3633 pre_parse_add_indirect( struct svga_shader_emitter
*emit
,
3634 int num
, int current_arl
)
3639 for (i
= 0; i
< emit
->num_arl_consts
; ++i
) {
3640 if (emit
->arl_consts
[i
].arl_num
== current_arl
)
3644 if (emit
->num_arl_consts
== i
) {
3645 ++emit
->num_arl_consts
;
3647 emit
->arl_consts
[i
].number
= (emit
->arl_consts
[i
].number
> num
) ?
3649 emit
->arl_consts
[i
].number
;
3650 emit
->arl_consts
[i
].arl_num
= current_arl
;
3656 pre_parse_instruction( struct svga_shader_emitter
*emit
,
3657 const struct tgsi_full_instruction
*insn
,
3660 if (insn
->Src
[0].Register
.Indirect
&&
3661 insn
->Src
[0].Indirect
.File
== TGSI_FILE_ADDRESS
) {
3662 const struct tgsi_full_src_register
*reg
= &insn
->Src
[0];
3663 if (reg
->Register
.Index
< 0) {
3664 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
3668 if (insn
->Src
[1].Register
.Indirect
&&
3669 insn
->Src
[1].Indirect
.File
== TGSI_FILE_ADDRESS
) {
3670 const struct tgsi_full_src_register
*reg
= &insn
->Src
[1];
3671 if (reg
->Register
.Index
< 0) {
3672 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
3676 if (insn
->Src
[2].Register
.Indirect
&&
3677 insn
->Src
[2].Indirect
.File
== TGSI_FILE_ADDRESS
) {
3678 const struct tgsi_full_src_register
*reg
= &insn
->Src
[2];
3679 if (reg
->Register
.Index
< 0) {
3680 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
3689 pre_parse_tokens( struct svga_shader_emitter
*emit
,
3690 const struct tgsi_token
*tokens
)
3692 struct tgsi_parse_context parse
;
3693 int current_arl
= 0;
3695 tgsi_parse_init( &parse
, tokens
);
3697 while (!tgsi_parse_end_of_tokens( &parse
)) {
3698 tgsi_parse_token( &parse
);
3699 switch (parse
.FullToken
.Token
.Type
) {
3700 case TGSI_TOKEN_TYPE_IMMEDIATE
:
3701 case TGSI_TOKEN_TYPE_DECLARATION
:
3703 case TGSI_TOKEN_TYPE_INSTRUCTION
:
3704 if (parse
.FullToken
.FullInstruction
.Instruction
.Opcode
==
3708 if (!pre_parse_instruction( emit
, &parse
.FullToken
.FullInstruction
,
3722 svga_shader_emit_helpers(struct svga_shader_emitter
*emit
)
3724 if (needs_to_create_common_immediate( emit
)) {
3725 create_common_immediate( emit
);
3727 if (needs_to_create_loop_const( emit
)) {
3728 create_loop_const( emit
);
3730 if (needs_to_create_arl_consts( emit
)) {
3731 create_arl_consts( emit
);
3734 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
3735 if (!svga_shader_emit_samplers_decl( emit
))
3738 if (!emit_ps_preamble( emit
))
3741 if (emit
->key
.fs
.light_twoside
) {
3742 if (!emit_light_twoside( emit
))
3745 if (emit
->emit_frontface
) {
3746 if (!emit_frontface( emit
))
3749 if (emit
->inverted_texcoords
) {
3750 if (!emit_inverted_texcoords( emit
))
3755 assert(emit
->unit
== PIPE_SHADER_VERTEX
);
3756 if (emit
->key
.vs
.adjust_attrib_range
) {
3757 if (!emit_adjusted_vertex_attribs(emit
) ||
3758 emit
->key
.vs
.adjust_attrib_w_1
) {
3769 * This is the main entrypoint into the TGSI instruction translater.
3770 * Translate TGSI shader tokens into an SVGA shader.
3773 svga_shader_emit_instructions(struct svga_shader_emitter
*emit
,
3774 const struct tgsi_token
*tokens
)
3776 struct tgsi_parse_context parse
;
3777 const struct tgsi_token
*new_tokens
= NULL
;
3779 boolean helpers_emitted
= FALSE
;
3780 unsigned line_nr
= 0;
3782 if (emit
->unit
== PIPE_SHADER_FRAGMENT
&& emit
->key
.fs
.pstipple
) {
3785 new_tokens
= util_pstipple_create_fragment_shader(tokens
, &unit
, 0,
3789 /* Setup texture state for stipple */
3790 emit
->sampler_target
[unit
] = TGSI_TEXTURE_2D
;
3791 emit
->key
.tex
[unit
].swizzle_r
= TGSI_SWIZZLE_X
;
3792 emit
->key
.tex
[unit
].swizzle_g
= TGSI_SWIZZLE_Y
;
3793 emit
->key
.tex
[unit
].swizzle_b
= TGSI_SWIZZLE_Z
;
3794 emit
->key
.tex
[unit
].swizzle_a
= TGSI_SWIZZLE_W
;
3796 emit
->pstipple_sampler_unit
= unit
;
3798 tokens
= new_tokens
;
3802 tgsi_parse_init( &parse
, tokens
);
3803 emit
->internal_imm_count
= 0;
3805 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
3806 ret
= emit_vs_preamble( emit
);
3811 pre_parse_tokens(emit
, tokens
);
3813 while (!tgsi_parse_end_of_tokens( &parse
)) {
3814 tgsi_parse_token( &parse
);
3816 switch (parse
.FullToken
.Token
.Type
) {
3817 case TGSI_TOKEN_TYPE_IMMEDIATE
:
3818 ret
= svga_emit_immediate( emit
, &parse
.FullToken
.FullImmediate
);
3823 case TGSI_TOKEN_TYPE_DECLARATION
:
3824 ret
= svga_translate_decl_sm30( emit
, &parse
.FullToken
.FullDeclaration
);
3829 case TGSI_TOKEN_TYPE_INSTRUCTION
:
3830 if (!helpers_emitted
) {
3831 if (!svga_shader_emit_helpers( emit
))
3833 helpers_emitted
= TRUE
;
3835 ret
= svga_emit_instruction( emit
,
3837 &parse
.FullToken
.FullInstruction
);
3845 reset_temp_regs( emit
);
3848 /* Need to terminate the current subroutine. Note that the
3849 * hardware doesn't tolerate shaders without sub-routines
3850 * terminating with RET+END.
3852 if (!emit
->in_main_func
) {
3853 ret
= emit_instruction( emit
, inst_token( SVGA3DOP_RET
) );
3858 assert(emit
->dynamic_branching_level
== 0);
3860 /* Need to terminate the whole shader:
3862 ret
= emit_instruction( emit
, inst_token( SVGA3DOP_END
) );
3867 tgsi_parse_free( &parse
);
3869 tgsi_free_tokens(new_tokens
);