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 assert(!"svga: unexpected opcode in translate_opcode()");
60 return SVGA3DOP_LAST_INST
;
66 translate_file(unsigned file
)
69 case TGSI_FILE_TEMPORARY
: return SVGA3DREG_TEMP
;
70 case TGSI_FILE_INPUT
: return SVGA3DREG_INPUT
;
71 case TGSI_FILE_OUTPUT
: return SVGA3DREG_OUTPUT
; /* VS3.0+ only */
72 case TGSI_FILE_IMMEDIATE
: return SVGA3DREG_CONST
;
73 case TGSI_FILE_CONSTANT
: return SVGA3DREG_CONST
;
74 case TGSI_FILE_SAMPLER
: return SVGA3DREG_SAMPLER
;
75 case TGSI_FILE_ADDRESS
: return SVGA3DREG_ADDR
;
77 assert(!"svga: unexpected register file in translate_file()");
78 return SVGA3DREG_TEMP
;
84 * Translate a TGSI destination register to an SVGA3DShaderDestToken.
85 * \param insn the TGSI instruction
86 * \param idx which TGSI dest register to translate (usually (always?) zero)
88 static SVGA3dShaderDestToken
89 translate_dst_register( struct svga_shader_emitter
*emit
,
90 const struct tgsi_full_instruction
*insn
,
93 const struct tgsi_full_dst_register
*reg
= &insn
->Dst
[idx
];
94 SVGA3dShaderDestToken dest
;
96 switch (reg
->Register
.File
) {
97 case TGSI_FILE_OUTPUT
:
98 /* Output registers encode semantic information in their name.
99 * Need to lookup a table built at decl time:
101 dest
= emit
->output_map
[reg
->Register
.Index
];
106 unsigned index
= reg
->Register
.Index
;
107 assert(index
< SVGA3D_TEMPREG_MAX
);
108 index
= MIN2(index
, SVGA3D_TEMPREG_MAX
- 1);
109 dest
= dst_register(translate_file(reg
->Register
.File
), index
);
114 if (reg
->Register
.Indirect
) {
115 debug_warning("Indirect indexing of dest registers is not supported!\n");
118 dest
.mask
= reg
->Register
.WriteMask
;
121 if (insn
->Instruction
.Saturate
)
122 dest
.dstMod
= SVGA3DDSTMOD_SATURATE
;
129 * Apply a swizzle to a src_register, returning a new src_register
130 * Ex: swizzle(SRC.ZZYY, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_X, SWIZZLE_Y)
131 * would return SRC.YYZZ
133 static struct src_register
134 swizzle(struct src_register src
,
135 unsigned x
, unsigned y
, unsigned z
, unsigned w
)
141 x
= (src
.base
.swizzle
>> (x
* 2)) & 0x3;
142 y
= (src
.base
.swizzle
>> (y
* 2)) & 0x3;
143 z
= (src
.base
.swizzle
>> (z
* 2)) & 0x3;
144 w
= (src
.base
.swizzle
>> (w
* 2)) & 0x3;
146 src
.base
.swizzle
= TRANSLATE_SWIZZLE(x
, y
, z
, w
);
153 * Apply a "scalar" swizzle to a src_register returning a new
154 * src_register where all the swizzle terms are the same.
155 * Ex: scalar(SRC.WZYX, SWIZZLE_Y) would return SRC.ZZZZ
157 static struct src_register
158 scalar(struct src_register src
, unsigned comp
)
161 return swizzle( src
, comp
, comp
, comp
, comp
);
166 svga_arl_needs_adjustment( const struct svga_shader_emitter
*emit
)
170 for (i
= 0; i
< emit
->num_arl_consts
; ++i
) {
171 if (emit
->arl_consts
[i
].arl_num
== emit
->current_arl
)
179 svga_arl_adjustment( const struct svga_shader_emitter
*emit
)
183 for (i
= 0; i
< emit
->num_arl_consts
; ++i
) {
184 if (emit
->arl_consts
[i
].arl_num
== emit
->current_arl
)
185 return emit
->arl_consts
[i
].number
;
192 * Translate a TGSI src register to a src_register.
194 static struct src_register
195 translate_src_register( const struct svga_shader_emitter
*emit
,
196 const struct tgsi_full_src_register
*reg
)
198 struct src_register src
;
200 switch (reg
->Register
.File
) {
201 case TGSI_FILE_INPUT
:
202 /* Input registers are referred to by their semantic name rather
203 * than by index. Use the mapping build up from the decls:
205 src
= emit
->input_map
[reg
->Register
.Index
];
208 case TGSI_FILE_IMMEDIATE
:
209 /* Immediates are appended after TGSI constants in the D3D
212 src
= src_register( translate_file( reg
->Register
.File
),
213 reg
->Register
.Index
+ emit
->imm_start
);
217 src
= src_register( translate_file( reg
->Register
.File
),
218 reg
->Register
.Index
);
222 /* Indirect addressing.
224 if (reg
->Register
.Indirect
) {
225 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
226 /* Pixel shaders have only loop registers for relative
227 * addressing into inputs. Ignore the redundant address
228 * register, the contents of aL should be in sync with it.
230 if (reg
->Register
.File
== TGSI_FILE_INPUT
) {
231 src
.base
.relAddr
= 1;
232 src
.indirect
= src_token(SVGA3DREG_LOOP
, 0);
236 /* Constant buffers only.
238 if (reg
->Register
.File
== TGSI_FILE_CONSTANT
) {
239 /* we shift the offset towards the minimum */
240 if (svga_arl_needs_adjustment( emit
)) {
241 src
.base
.num
-= svga_arl_adjustment( emit
);
243 src
.base
.relAddr
= 1;
245 /* Not really sure what should go in the second token:
247 src
.indirect
= src_token( SVGA3DREG_ADDR
,
248 reg
->Indirect
.Index
);
250 src
.indirect
.swizzle
= SWIZZLE_XXXX
;
256 reg
->Register
.SwizzleX
,
257 reg
->Register
.SwizzleY
,
258 reg
->Register
.SwizzleZ
,
259 reg
->Register
.SwizzleW
);
261 /* src.mod isn't a bitfield, unfortunately:
262 * See tgsi_util_get_full_src_register_sign_mode for implementation details.
264 if (reg
->Register
.Absolute
) {
265 if (reg
->Register
.Negate
)
266 src
.base
.srcMod
= SVGA3DSRCMOD_ABSNEG
;
268 src
.base
.srcMod
= SVGA3DSRCMOD_ABS
;
271 if (reg
->Register
.Negate
)
272 src
.base
.srcMod
= SVGA3DSRCMOD_NEG
;
274 src
.base
.srcMod
= SVGA3DSRCMOD_NONE
;
282 * Get a temporary register.
283 * Note: if we exceed the temporary register limit we just use
284 * register SVGA3D_TEMPREG_MAX - 1.
286 static SVGA3dShaderDestToken
287 get_temp( struct svga_shader_emitter
*emit
)
289 int i
= emit
->nr_hw_temp
+ emit
->internal_temp_count
++;
290 assert(i
< SVGA3D_TEMPREG_MAX
);
291 i
= MIN2(i
, SVGA3D_TEMPREG_MAX
- 1);
292 return dst_register( SVGA3DREG_TEMP
, i
);
297 * Release a single temp. Currently only effective if it was the last
298 * allocated temp, otherwise release will be delayed until the next
299 * call to reset_temp_regs().
302 release_temp( struct svga_shader_emitter
*emit
,
303 SVGA3dShaderDestToken temp
)
305 if (temp
.num
== emit
->internal_temp_count
- 1)
306 emit
->internal_temp_count
--;
314 reset_temp_regs(struct svga_shader_emitter
*emit
)
316 emit
->internal_temp_count
= 0;
320 /** Emit bytecode for a src_register */
322 emit_src(struct svga_shader_emitter
*emit
, const struct src_register src
)
324 if (src
.base
.relAddr
) {
325 assert(src
.base
.reserved0
);
326 assert(src
.indirect
.reserved0
);
327 return (svga_shader_emit_dword( emit
, src
.base
.value
) &&
328 svga_shader_emit_dword( emit
, src
.indirect
.value
));
331 assert(src
.base
.reserved0
);
332 return svga_shader_emit_dword( emit
, src
.base
.value
);
337 /** Emit bytecode for a dst_register */
339 emit_dst(struct svga_shader_emitter
*emit
, SVGA3dShaderDestToken dest
)
341 assert(dest
.reserved0
);
343 return svga_shader_emit_dword( emit
, dest
.value
);
347 /** Emit bytecode for a 1-operand instruction */
349 emit_op1(struct svga_shader_emitter
*emit
,
350 SVGA3dShaderInstToken inst
,
351 SVGA3dShaderDestToken dest
,
352 struct src_register src0
)
354 return (emit_instruction(emit
, inst
) &&
355 emit_dst(emit
, dest
) &&
356 emit_src(emit
, src0
));
360 /** Emit bytecode for a 2-operand instruction */
362 emit_op2(struct svga_shader_emitter
*emit
,
363 SVGA3dShaderInstToken inst
,
364 SVGA3dShaderDestToken dest
,
365 struct src_register src0
,
366 struct src_register src1
)
368 return (emit_instruction(emit
, inst
) &&
369 emit_dst(emit
, dest
) &&
370 emit_src(emit
, src0
) &&
371 emit_src(emit
, src1
));
375 /** Emit bytecode for a 3-operand instruction */
377 emit_op3(struct svga_shader_emitter
*emit
,
378 SVGA3dShaderInstToken inst
,
379 SVGA3dShaderDestToken dest
,
380 struct src_register src0
,
381 struct src_register src1
,
382 struct src_register src2
)
384 return (emit_instruction(emit
, inst
) &&
385 emit_dst(emit
, dest
) &&
386 emit_src(emit
, src0
) &&
387 emit_src(emit
, src1
) &&
388 emit_src(emit
, src2
));
392 /** Emit bytecode for a 4-operand instruction */
394 emit_op4(struct svga_shader_emitter
*emit
,
395 SVGA3dShaderInstToken inst
,
396 SVGA3dShaderDestToken dest
,
397 struct src_register src0
,
398 struct src_register src1
,
399 struct src_register src2
,
400 struct src_register src3
)
402 return (emit_instruction(emit
, inst
) &&
403 emit_dst(emit
, dest
) &&
404 emit_src(emit
, src0
) &&
405 emit_src(emit
, src1
) &&
406 emit_src(emit
, src2
) &&
407 emit_src(emit
, src3
));
412 * Apply the absolute value modifier to the given src_register, returning
413 * a new src_register.
415 static struct src_register
416 absolute(struct src_register src
)
418 src
.base
.srcMod
= SVGA3DSRCMOD_ABS
;
424 * Apply the negation modifier to the given src_register, returning
425 * a new src_register.
427 static struct src_register
428 negate(struct src_register src
)
430 switch (src
.base
.srcMod
) {
431 case SVGA3DSRCMOD_ABS
:
432 src
.base
.srcMod
= SVGA3DSRCMOD_ABSNEG
;
434 case SVGA3DSRCMOD_ABSNEG
:
435 src
.base
.srcMod
= SVGA3DSRCMOD_ABS
;
437 case SVGA3DSRCMOD_NEG
:
438 src
.base
.srcMod
= SVGA3DSRCMOD_NONE
;
440 case SVGA3DSRCMOD_NONE
:
441 src
.base
.srcMod
= SVGA3DSRCMOD_NEG
;
449 /* Replace the src with the temporary specified in the dst, but copying
450 * only the necessary channels, and preserving the original swizzle (which is
451 * important given that several opcodes have constraints in the allowed
455 emit_repl(struct svga_shader_emitter
*emit
,
456 SVGA3dShaderDestToken dst
,
457 struct src_register
*src0
)
459 unsigned src0_swizzle
;
462 assert(SVGA3dShaderGetRegType(dst
.value
) == SVGA3DREG_TEMP
);
464 src0_swizzle
= src0
->base
.swizzle
;
467 for (chan
= 0; chan
< 4; ++chan
) {
468 unsigned swizzle
= (src0_swizzle
>> (chan
*2)) & 0x3;
469 dst
.mask
|= 1 << swizzle
;
473 src0
->base
.swizzle
= SVGA3DSWIZZLE_NONE
;
475 if (!emit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, *src0
))
479 src0
->base
.swizzle
= src0_swizzle
;
486 * Submit/emit an instruction with zero operands.
489 submit_op0(struct svga_shader_emitter
*emit
,
490 SVGA3dShaderInstToken inst
,
491 SVGA3dShaderDestToken dest
)
493 return (emit_instruction( emit
, inst
) &&
494 emit_dst( emit
, dest
));
499 * Submit/emit an instruction with one operand.
502 submit_op1(struct svga_shader_emitter
*emit
,
503 SVGA3dShaderInstToken inst
,
504 SVGA3dShaderDestToken dest
,
505 struct src_register src0
)
507 return emit_op1( emit
, inst
, dest
, src0
);
512 * Submit/emit an instruction with two operands.
514 * SVGA shaders may not refer to >1 constant register in a single
515 * instruction. This function checks for that usage and inserts a
516 * move to temporary if detected.
518 * The same applies to input registers -- at most a single input
519 * register may be read by any instruction.
522 submit_op2(struct svga_shader_emitter
*emit
,
523 SVGA3dShaderInstToken inst
,
524 SVGA3dShaderDestToken dest
,
525 struct src_register src0
,
526 struct src_register src1
)
528 SVGA3dShaderDestToken temp
;
529 SVGA3dShaderRegType type0
, type1
;
530 boolean need_temp
= FALSE
;
533 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
534 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
536 if (type0
== SVGA3DREG_CONST
&&
537 type1
== SVGA3DREG_CONST
&&
538 src0
.base
.num
!= src1
.base
.num
)
541 if (type0
== SVGA3DREG_INPUT
&&
542 type1
== SVGA3DREG_INPUT
&&
543 src0
.base
.num
!= src1
.base
.num
)
547 temp
= get_temp( emit
);
549 if (!emit_repl( emit
, temp
, &src0
))
553 if (!emit_op2( emit
, inst
, dest
, src0
, src1
))
557 release_temp( emit
, temp
);
564 * Submit/emit an instruction with three operands.
566 * SVGA shaders may not refer to >1 constant register in a single
567 * instruction. This function checks for that usage and inserts a
568 * move to temporary if detected.
571 submit_op3(struct svga_shader_emitter
*emit
,
572 SVGA3dShaderInstToken inst
,
573 SVGA3dShaderDestToken dest
,
574 struct src_register src0
,
575 struct src_register src1
,
576 struct src_register src2
)
578 SVGA3dShaderDestToken temp0
;
579 SVGA3dShaderDestToken temp1
;
580 boolean need_temp0
= FALSE
;
581 boolean need_temp1
= FALSE
;
582 SVGA3dShaderRegType type0
, type1
, type2
;
586 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
587 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
588 type2
= SVGA3dShaderGetRegType( src2
.base
.value
);
590 if (inst
.op
!= SVGA3DOP_SINCOS
) {
591 if (type0
== SVGA3DREG_CONST
&&
592 ((type1
== SVGA3DREG_CONST
&& src0
.base
.num
!= src1
.base
.num
) ||
593 (type2
== SVGA3DREG_CONST
&& src0
.base
.num
!= src2
.base
.num
)))
596 if (type1
== SVGA3DREG_CONST
&&
597 (type2
== SVGA3DREG_CONST
&& src1
.base
.num
!= src2
.base
.num
))
601 if (type0
== SVGA3DREG_INPUT
&&
602 ((type1
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src1
.base
.num
) ||
603 (type2
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src2
.base
.num
)))
606 if (type1
== SVGA3DREG_INPUT
&&
607 (type2
== SVGA3DREG_INPUT
&& src1
.base
.num
!= src2
.base
.num
))
611 temp0
= get_temp( emit
);
613 if (!emit_repl( emit
, temp0
, &src0
))
618 temp1
= get_temp( emit
);
620 if (!emit_repl( emit
, temp1
, &src1
))
624 if (!emit_op3( emit
, inst
, dest
, src0
, src1
, src2
))
628 release_temp( emit
, temp1
);
630 release_temp( emit
, temp0
);
636 * Submit/emit an instruction with four operands.
638 * SVGA shaders may not refer to >1 constant register in a single
639 * instruction. This function checks for that usage and inserts a
640 * move to temporary if detected.
643 submit_op4(struct svga_shader_emitter
*emit
,
644 SVGA3dShaderInstToken inst
,
645 SVGA3dShaderDestToken dest
,
646 struct src_register src0
,
647 struct src_register src1
,
648 struct src_register src2
,
649 struct src_register src3
)
651 SVGA3dShaderDestToken temp0
;
652 SVGA3dShaderDestToken temp3
;
653 boolean need_temp0
= FALSE
;
654 boolean need_temp3
= FALSE
;
655 SVGA3dShaderRegType type0
, type1
, type2
, type3
;
659 type0
= SVGA3dShaderGetRegType( src0
.base
.value
);
660 type1
= SVGA3dShaderGetRegType( src1
.base
.value
);
661 type2
= SVGA3dShaderGetRegType( src2
.base
.value
);
662 type3
= SVGA3dShaderGetRegType( src2
.base
.value
);
664 /* Make life a little easier - this is only used by the TXD
665 * instruction which is guaranteed not to have a constant/input reg
666 * in one slot at least:
668 assert(type1
== SVGA3DREG_SAMPLER
);
670 if (type0
== SVGA3DREG_CONST
&&
671 ((type3
== SVGA3DREG_CONST
&& src0
.base
.num
!= src3
.base
.num
) ||
672 (type2
== SVGA3DREG_CONST
&& src0
.base
.num
!= src2
.base
.num
)))
675 if (type3
== SVGA3DREG_CONST
&&
676 (type2
== SVGA3DREG_CONST
&& src3
.base
.num
!= src2
.base
.num
))
679 if (type0
== SVGA3DREG_INPUT
&&
680 ((type3
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src3
.base
.num
) ||
681 (type2
== SVGA3DREG_INPUT
&& src0
.base
.num
!= src2
.base
.num
)))
684 if (type3
== SVGA3DREG_INPUT
&&
685 (type2
== SVGA3DREG_INPUT
&& src3
.base
.num
!= src2
.base
.num
))
689 temp0
= get_temp( emit
);
691 if (!emit_repl( emit
, temp0
, &src0
))
696 temp3
= get_temp( emit
);
698 if (!emit_repl( emit
, temp3
, &src3
))
702 if (!emit_op4( emit
, inst
, dest
, src0
, src1
, src2
, src3
))
706 release_temp( emit
, temp3
);
708 release_temp( emit
, temp0
);
714 * Do the src and dest registers refer to the same register?
717 alias_src_dst(struct src_register src
,
718 SVGA3dShaderDestToken dst
)
720 if (src
.base
.num
!= dst
.num
)
723 if (SVGA3dShaderGetRegType(dst
.value
) !=
724 SVGA3dShaderGetRegType(src
.base
.value
))
732 * Translate/emit a LRP (linear interpolation) instruction.
735 submit_lrp(struct svga_shader_emitter
*emit
,
736 SVGA3dShaderDestToken dst
,
737 struct src_register src0
,
738 struct src_register src1
,
739 struct src_register src2
)
741 SVGA3dShaderDestToken tmp
;
742 boolean need_dst_tmp
= FALSE
;
744 /* The dst reg must be a temporary, and not be the same as src0 or src2 */
745 if (SVGA3dShaderGetRegType(dst
.value
) != SVGA3DREG_TEMP
||
746 alias_src_dst(src0
, dst
) ||
747 alias_src_dst(src2
, dst
))
751 tmp
= get_temp( emit
);
758 if (!submit_op3(emit
, inst_token( SVGA3DOP_LRP
), tmp
, src0
, src1
, src2
))
762 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), dst
, src( tmp
)))
771 * Helper for emitting SVGA immediate values using the SVGA3DOP_DEF[I]
775 emit_def_const(struct svga_shader_emitter
*emit
,
776 SVGA3dShaderConstType type
,
777 unsigned idx
, float a
, float b
, float c
, float d
)
780 SVGA3dShaderInstToken opcode
;
783 case SVGA3D_CONST_TYPE_FLOAT
:
784 opcode
= inst_token( SVGA3DOP_DEF
);
785 def
.dst
= dst_register( SVGA3DREG_CONST
, idx
);
786 def
.constValues
[0] = a
;
787 def
.constValues
[1] = b
;
788 def
.constValues
[2] = c
;
789 def
.constValues
[3] = d
;
791 case SVGA3D_CONST_TYPE_INT
:
792 opcode
= inst_token( SVGA3DOP_DEFI
);
793 def
.dst
= dst_register( SVGA3DREG_CONSTINT
, idx
);
794 def
.constIValues
[0] = (int)a
;
795 def
.constIValues
[1] = (int)b
;
796 def
.constIValues
[2] = (int)c
;
797 def
.constIValues
[3] = (int)d
;
801 opcode
= inst_token( SVGA3DOP_NOP
);
805 if (!emit_instruction(emit
, opcode
) ||
806 !svga_shader_emit_dwords( emit
, def
.values
, Elements(def
.values
)))
814 * Create/emit a constant with values {0, 0.5, -1, 1}.
815 * We can swizzle this to produce other useful constants such as
816 * {0, 0, 0, 0}, {1, 1, 1, 1}, etc.
819 create_zero_immediate( struct svga_shader_emitter
*emit
)
821 unsigned idx
= emit
->nr_hw_float_const
++;
823 /* Emit the constant (0, 0.5, -1, 1) and use swizzling to generate
824 * other useful vectors.
826 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
827 idx
, 0, 0.5, -1, 1 ))
830 emit
->zero_immediate_idx
= idx
;
831 emit
->created_zero_immediate
= TRUE
;
838 create_loop_const( struct svga_shader_emitter
*emit
)
840 unsigned idx
= emit
->nr_hw_int_const
++;
842 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_INT
, idx
,
843 255, /* iteration count */
844 0, /* initial value */
846 0 /* not used, must be 0 */))
849 emit
->loop_const_idx
= idx
;
850 emit
->created_loop_const
= TRUE
;
856 create_arl_consts( struct svga_shader_emitter
*emit
)
860 for (i
= 0; i
< emit
->num_arl_consts
; i
+= 4) {
862 unsigned idx
= emit
->nr_hw_float_const
++;
864 for (j
= 0; j
< 4 && (j
+ i
) < emit
->num_arl_consts
; ++j
) {
865 vals
[j
] = (float) emit
->arl_consts
[i
+ j
].number
;
866 emit
->arl_consts
[i
+ j
].idx
= idx
;
869 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_X
;
872 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_Y
;
875 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_Z
;
878 emit
->arl_consts
[i
+ 0].swizzle
= TGSI_SWIZZLE_W
;
885 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
, idx
,
896 * Return the register which holds the pixel shaders front/back-
899 static struct src_register
900 get_vface( struct svga_shader_emitter
*emit
)
902 assert(emit
->emitted_vface
);
903 return src_register(SVGA3DREG_MISCTYPE
, SVGA3DMISCREG_FACE
);
908 * returns {0, 0, 0, 1} immediate
910 static struct src_register
911 get_zero_immediate( struct svga_shader_emitter
*emit
)
913 assert(emit
->created_zero_immediate
);
914 assert(emit
->zero_immediate_idx
>= 0);
915 return swizzle(src_register( SVGA3DREG_CONST
,
916 emit
->zero_immediate_idx
),
922 * returns {1, 1, 1, -1} immediate
924 static struct src_register
925 get_pos_neg_one_immediate( struct svga_shader_emitter
*emit
)
927 assert(emit
->created_zero_immediate
);
928 assert(emit
->zero_immediate_idx
>= 0);
929 return swizzle(src_register( SVGA3DREG_CONST
,
930 emit
->zero_immediate_idx
),
936 * returns {0.5, 0.5, 0.5, 0.5} immediate
938 static struct src_register
939 get_half_immediate( struct svga_shader_emitter
*emit
)
941 assert(emit
->created_zero_immediate
);
942 assert(emit
->zero_immediate_idx
>= 0);
943 return swizzle(src_register(SVGA3DREG_CONST
, emit
->zero_immediate_idx
),
949 * returns the loop const
951 static struct src_register
952 get_loop_const( struct svga_shader_emitter
*emit
)
954 assert(emit
->created_loop_const
);
955 assert(emit
->loop_const_idx
>= 0);
956 return src_register( SVGA3DREG_CONSTINT
,
957 emit
->loop_const_idx
);
961 static struct src_register
962 get_fake_arl_const( struct svga_shader_emitter
*emit
)
964 struct src_register reg
;
965 int idx
= 0, swizzle
= 0, i
;
967 for (i
= 0; i
< emit
->num_arl_consts
; ++ i
) {
968 if (emit
->arl_consts
[i
].arl_num
== emit
->current_arl
) {
969 idx
= emit
->arl_consts
[i
].idx
;
970 swizzle
= emit
->arl_consts
[i
].swizzle
;
974 reg
= src_register( SVGA3DREG_CONST
, idx
);
975 return scalar(reg
, swizzle
);
980 * Return a register which holds the width and height of the texture
981 * currently bound to the given sampler.
983 static struct src_register
984 get_tex_dimensions( struct svga_shader_emitter
*emit
, int sampler_num
)
987 struct src_register reg
;
989 /* the width/height indexes start right after constants */
990 idx
= emit
->key
.fkey
.tex
[sampler_num
].width_height_idx
+
991 emit
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
993 reg
= src_register( SVGA3DREG_CONST
, idx
);
999 emit_fake_arl(struct svga_shader_emitter
*emit
,
1000 const struct tgsi_full_instruction
*insn
)
1002 const struct src_register src0
=
1003 translate_src_register(emit
, &insn
->Src
[0] );
1004 struct src_register src1
= get_fake_arl_const( emit
);
1005 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1006 SVGA3dShaderDestToken tmp
= get_temp( emit
);
1008 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), tmp
, src0
))
1011 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), tmp
, src( tmp
),
1015 /* replicate the original swizzle */
1017 src1
.base
.swizzle
= src0
.base
.swizzle
;
1019 return submit_op1( emit
, inst_token( SVGA3DOP_MOVA
),
1025 emit_if(struct svga_shader_emitter
*emit
,
1026 const struct tgsi_full_instruction
*insn
)
1028 struct src_register src0
=
1029 translate_src_register(emit
, &insn
->Src
[0]);
1030 struct src_register zero
= get_zero_immediate( emit
);
1031 SVGA3dShaderInstToken if_token
= inst_token( SVGA3DOP_IFC
);
1033 if_token
.control
= SVGA3DOPCOMPC_NE
;
1034 zero
= scalar(zero
, TGSI_SWIZZLE_X
);
1036 if (SVGA3dShaderGetRegType(src0
.base
.value
) == SVGA3DREG_CONST
) {
1038 * Max different constant registers readable per IFC instruction is 1.
1040 SVGA3dShaderDestToken tmp
= get_temp( emit
);
1042 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), tmp
, src0
))
1045 src0
= scalar(src( tmp
), TGSI_SWIZZLE_X
);
1048 emit
->dynamic_branching_level
++;
1050 return (emit_instruction( emit
, if_token
) &&
1051 emit_src( emit
, src0
) &&
1052 emit_src( emit
, zero
) );
1057 emit_endif(struct svga_shader_emitter
*emit
,
1058 const struct tgsi_full_instruction
*insn
)
1060 emit
->dynamic_branching_level
--;
1062 return emit_instruction(emit
, inst_token(SVGA3DOP_ENDIF
));
1067 emit_else(struct svga_shader_emitter
*emit
,
1068 const struct tgsi_full_instruction
*insn
)
1070 return emit_instruction(emit
, inst_token(SVGA3DOP_ELSE
));
1075 * Translate the following TGSI FLR instruction.
1077 * To the following SVGA3D instruction sequence.
1082 emit_floor(struct svga_shader_emitter
*emit
,
1083 const struct tgsi_full_instruction
*insn
)
1085 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1086 const struct src_register src0
=
1087 translate_src_register(emit
, &insn
->Src
[0] );
1088 SVGA3dShaderDestToken temp
= get_temp( emit
);
1091 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
), temp
, src0
))
1094 /* SUB DST, SRC, TMP */
1095 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
, src0
,
1096 negate( src( temp
) ) ))
1104 * Translate the following TGSI CEIL instruction.
1106 * To the following SVGA3D instruction sequence.
1111 emit_ceil(struct svga_shader_emitter
*emit
,
1112 const struct tgsi_full_instruction
*insn
)
1114 SVGA3dShaderDestToken dst
= translate_dst_register(emit
, insn
, 0);
1115 const struct src_register src0
=
1116 translate_src_register(emit
, &insn
->Src
[0]);
1117 SVGA3dShaderDestToken temp
= get_temp(emit
);
1120 if (!submit_op1(emit
, inst_token(SVGA3DOP_FRC
), temp
, negate(src0
)))
1123 /* ADD DST, SRC, TMP */
1124 if (!submit_op2(emit
, inst_token(SVGA3DOP_ADD
), dst
, src0
, src(temp
)))
1132 * Translate the following TGSI DIV instruction.
1133 * DIV DST.xy, SRC0, SRC1
1134 * To the following SVGA3D instruction sequence.
1135 * RCP TMP.x, SRC1.xxxx
1136 * RCP TMP.y, SRC1.yyyy
1137 * MUL DST.xy, SRC0, TMP
1140 emit_div(struct svga_shader_emitter
*emit
,
1141 const struct tgsi_full_instruction
*insn
)
1143 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1144 const struct src_register src0
=
1145 translate_src_register(emit
, &insn
->Src
[0] );
1146 const struct src_register src1
=
1147 translate_src_register(emit
, &insn
->Src
[1] );
1148 SVGA3dShaderDestToken temp
= get_temp( emit
);
1151 /* For each enabled element, perform a RCP instruction. Note that
1152 * RCP is scalar in SVGA3D:
1154 for (i
= 0; i
< 4; i
++) {
1155 unsigned channel
= 1 << i
;
1156 if (dst
.mask
& channel
) {
1157 /* RCP TMP.?, SRC1.???? */
1158 if (!submit_op1( emit
, inst_token( SVGA3DOP_RCP
),
1159 writemask(temp
, channel
),
1166 * MUL DST, SRC0, TMP
1168 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
), dst
, src0
,
1177 * Translate the following TGSI DP2 instruction.
1178 * DP2 DST, SRC1, SRC2
1179 * To the following SVGA3D instruction sequence.
1180 * MUL TMP, SRC1, SRC2
1181 * ADD DST, TMP.xxxx, TMP.yyyy
1184 emit_dp2(struct svga_shader_emitter
*emit
,
1185 const struct tgsi_full_instruction
*insn
)
1187 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1188 const struct src_register src0
=
1189 translate_src_register(emit
, &insn
->Src
[0]);
1190 const struct src_register src1
=
1191 translate_src_register(emit
, &insn
->Src
[1]);
1192 SVGA3dShaderDestToken temp
= get_temp( emit
);
1193 struct src_register temp_src0
, temp_src1
;
1195 /* MUL TMP, SRC1, SRC2 */
1196 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
), temp
, src0
, src1
))
1199 temp_src0
= scalar(src( temp
), TGSI_SWIZZLE_X
);
1200 temp_src1
= scalar(src( temp
), TGSI_SWIZZLE_Y
);
1202 /* ADD DST, TMP.xxxx, TMP.yyyy */
1203 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
,
1204 temp_src0
, temp_src1
))
1212 * Translate the following TGSI DPH instruction.
1213 * DPH DST, SRC1, SRC2
1214 * To the following SVGA3D instruction sequence.
1215 * DP3 TMP, SRC1, SRC2
1216 * ADD DST, TMP, SRC2.wwww
1219 emit_dph(struct svga_shader_emitter
*emit
,
1220 const struct tgsi_full_instruction
*insn
)
1222 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1223 const struct src_register src0
= translate_src_register(
1224 emit
, &insn
->Src
[0] );
1225 struct src_register src1
=
1226 translate_src_register(emit
, &insn
->Src
[1]);
1227 SVGA3dShaderDestToken temp
= get_temp( emit
);
1229 /* DP3 TMP, SRC1, SRC2 */
1230 if (!submit_op2( emit
, inst_token( SVGA3DOP_DP3
), temp
, src0
, src1
))
1233 src1
= scalar(src1
, TGSI_SWIZZLE_W
);
1235 /* ADD DST, TMP, SRC2.wwww */
1236 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
,
1237 src( temp
), src1
))
1245 * Translate the following TGSI DST instruction.
1247 * To the following SVGA3D instruction sequence.
1253 emit_nrm(struct svga_shader_emitter
*emit
,
1254 const struct tgsi_full_instruction
*insn
)
1256 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1257 const struct src_register src0
=
1258 translate_src_register(emit
, &insn
->Src
[0]);
1259 SVGA3dShaderDestToken temp
= get_temp( emit
);
1261 /* DP3 TMP, SRC, SRC */
1262 if (!submit_op2( emit
, inst_token( SVGA3DOP_DP3
), temp
, src0
, src0
))
1266 if (!submit_op1( emit
, inst_token( SVGA3DOP_RSQ
), temp
, src( temp
)))
1269 /* MUL DST, SRC, TMP */
1270 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
), dst
,
1279 * Sine / Cosine helper function.
1282 do_emit_sincos(struct svga_shader_emitter
*emit
,
1283 SVGA3dShaderDestToken dst
,
1284 struct src_register src0
)
1286 src0
= scalar(src0
, TGSI_SWIZZLE_X
);
1287 return submit_op1(emit
, inst_token(SVGA3DOP_SINCOS
), dst
, src0
);
1292 * Translate/emit a TGSI SIN, COS or CSC instruction.
1295 emit_sincos(struct svga_shader_emitter
*emit
,
1296 const struct tgsi_full_instruction
*insn
)
1298 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1299 struct src_register src0
= translate_src_register(emit
, &insn
->Src
[0]);
1300 SVGA3dShaderDestToken temp
= get_temp( emit
);
1303 if (!do_emit_sincos(emit
, writemask(temp
, TGSI_WRITEMASK_XY
), src0
))
1307 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src( temp
) ))
1315 * Translate TGSI SIN instruction into:
1320 emit_sin(struct svga_shader_emitter
*emit
,
1321 const struct tgsi_full_instruction
*insn
)
1323 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1324 struct src_register src0
=
1325 translate_src_register(emit
, &insn
->Src
[0] );
1326 SVGA3dShaderDestToken temp
= get_temp( emit
);
1329 if (!do_emit_sincos(emit
, writemask(temp
, TGSI_WRITEMASK_Y
), src0
))
1332 src0
= scalar(src( temp
), TGSI_SWIZZLE_Y
);
1334 /* MOV DST TMP.yyyy */
1335 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src0
))
1343 * Translate TGSI COS instruction into:
1348 emit_cos(struct svga_shader_emitter
*emit
,
1349 const struct tgsi_full_instruction
*insn
)
1351 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1352 struct src_register src0
=
1353 translate_src_register(emit
, &insn
->Src
[0] );
1354 SVGA3dShaderDestToken temp
= get_temp( emit
);
1357 if (!do_emit_sincos( emit
, writemask(temp
, TGSI_WRITEMASK_X
), src0
))
1360 src0
= scalar(src( temp
), TGSI_SWIZZLE_X
);
1362 /* MOV DST TMP.xxxx */
1363 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src0
))
1371 * Translate/emit TGSI SSG (Set Sign: -1, 0, +1) instruction.
1374 emit_ssg(struct svga_shader_emitter
*emit
,
1375 const struct tgsi_full_instruction
*insn
)
1377 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1378 struct src_register src0
=
1379 translate_src_register(emit
, &insn
->Src
[0] );
1380 SVGA3dShaderDestToken temp0
= get_temp( emit
);
1381 SVGA3dShaderDestToken temp1
= get_temp( emit
);
1382 struct src_register zero
, one
;
1384 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1385 /* SGN DST, SRC0, TMP0, TMP1 */
1386 return submit_op3( emit
, inst_token( SVGA3DOP_SGN
), dst
, src0
,
1387 src( temp0
), src( temp1
) );
1390 zero
= get_zero_immediate( emit
);
1391 one
= scalar( zero
, TGSI_SWIZZLE_W
);
1392 zero
= scalar( zero
, TGSI_SWIZZLE_X
);
1394 /* CMP TMP0, SRC0, one, zero */
1395 if (!submit_op3( emit
, inst_token( SVGA3DOP_CMP
),
1396 writemask( temp0
, dst
.mask
), src0
, one
, zero
))
1399 /* CMP TMP1, negate(SRC0), negate(one), zero */
1400 if (!submit_op3( emit
, inst_token( SVGA3DOP_CMP
),
1401 writemask( temp1
, dst
.mask
), negate( src0
), negate( one
),
1405 /* ADD DST, TMP0, TMP1 */
1406 return submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
, src( temp0
),
1412 * Translate/emit TGSI SUB instruction as:
1413 * ADD DST, SRC0, negate(SRC1)
1416 emit_sub(struct svga_shader_emitter
*emit
,
1417 const struct tgsi_full_instruction
*insn
)
1419 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1420 struct src_register src0
= translate_src_register(
1421 emit
, &insn
->Src
[0] );
1422 struct src_register src1
= translate_src_register(
1423 emit
, &insn
->Src
[1] );
1425 src1
= negate(src1
);
1427 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
), dst
,
1436 * Translate/emit KILL_IF instruction (kill if any of X,Y,Z,W are negative).
1439 emit_kill_if(struct svga_shader_emitter
*emit
,
1440 const struct tgsi_full_instruction
*insn
)
1442 const struct tgsi_full_src_register
*reg
= &insn
->Src
[0];
1443 struct src_register src0
, srcIn
;
1444 const boolean special
= (reg
->Register
.Absolute
||
1445 reg
->Register
.Negate
||
1446 reg
->Register
.Indirect
||
1447 reg
->Register
.SwizzleX
!= 0 ||
1448 reg
->Register
.SwizzleY
!= 1 ||
1449 reg
->Register
.SwizzleZ
!= 2 ||
1450 reg
->Register
.File
!= TGSI_FILE_TEMPORARY
);
1451 SVGA3dShaderDestToken temp
;
1453 src0
= srcIn
= translate_src_register( emit
, reg
);
1456 /* need a temp reg */
1457 temp
= get_temp( emit
);
1461 /* move the source into a temp register */
1462 submit_op1(emit
, inst_token(SVGA3DOP_MOV
), temp
, src0
);
1467 /* Do the texkill by checking if any of the XYZW components are < 0.
1468 * Note that ps_2_0 and later take XYZW in consideration, while ps_1_x
1469 * only used XYZ. The MSDN documentation about this is incorrect.
1471 if (!submit_op0( emit
, inst_token( SVGA3DOP_TEXKILL
), dst(src0
) ))
1479 * Translate/emit unconditional kill instruction (usually found inside
1480 * an IF/ELSE/ENDIF block).
1483 emit_kill(struct svga_shader_emitter
*emit
,
1484 const struct tgsi_full_instruction
*insn
)
1486 SVGA3dShaderDestToken temp
;
1487 struct src_register one
= scalar( get_zero_immediate( emit
),
1489 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_TEXKILL
);
1491 /* texkill doesn't allow negation on the operand so lets move
1492 * negation of {1} to a temp register */
1493 temp
= get_temp( emit
);
1494 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), temp
,
1498 return submit_op0( emit
, inst
, temp
);
1503 * Test if r1 and r2 are the same register.
1506 same_register(struct src_register r1
, struct src_register r2
)
1508 return (r1
.base
.num
== r2
.base
.num
&&
1509 r1
.base
.type_upper
== r2
.base
.type_upper
&&
1510 r1
.base
.type_lower
== r2
.base
.type_lower
);
1516 * Implement conditionals by initializing destination reg to 'fail',
1517 * then set predicate reg with UFOP_SETP, then move 'pass' to dest
1518 * based on predicate reg.
1520 * SETP src0, cmp, src1 -- do this first to avoid aliasing problems.
1525 emit_conditional(struct svga_shader_emitter
*emit
,
1526 unsigned compare_func
,
1527 SVGA3dShaderDestToken dst
,
1528 struct src_register src0
,
1529 struct src_register src1
,
1530 struct src_register pass
,
1531 struct src_register fail
)
1533 SVGA3dShaderDestToken pred_reg
= dst_register( SVGA3DREG_PREDICATE
, 0 );
1534 SVGA3dShaderInstToken setp_token
, mov_token
;
1535 setp_token
= inst_token( SVGA3DOP_SETP
);
1537 switch (compare_func
) {
1538 case PIPE_FUNC_NEVER
:
1539 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1542 case PIPE_FUNC_LESS
:
1543 setp_token
.control
= SVGA3DOPCOMP_LT
;
1545 case PIPE_FUNC_EQUAL
:
1546 setp_token
.control
= SVGA3DOPCOMP_EQ
;
1548 case PIPE_FUNC_LEQUAL
:
1549 setp_token
.control
= SVGA3DOPCOMP_LE
;
1551 case PIPE_FUNC_GREATER
:
1552 setp_token
.control
= SVGA3DOPCOMP_GT
;
1554 case PIPE_FUNC_NOTEQUAL
:
1555 setp_token
.control
= SVGA3DOPCOMPC_NE
;
1557 case PIPE_FUNC_GEQUAL
:
1558 setp_token
.control
= SVGA3DOPCOMP_GE
;
1560 case PIPE_FUNC_ALWAYS
:
1561 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1566 if (same_register(src(dst
), pass
)) {
1567 /* We'll get bad results if the dst and pass registers are the same
1568 * so use a temp register containing pass.
1570 SVGA3dShaderDestToken temp
= get_temp(emit
);
1571 if (!submit_op1(emit
, inst_token(SVGA3DOP_MOV
), temp
, pass
))
1576 /* SETP src0, COMPOP, src1 */
1577 if (!submit_op2( emit
, setp_token
, pred_reg
,
1581 mov_token
= inst_token( SVGA3DOP_MOV
);
1584 if (!submit_op1( emit
, mov_token
, dst
,
1588 /* MOV dst, pass (predicated)
1590 * Note that the predicate reg (and possible modifiers) is passed
1591 * as the first source argument.
1593 mov_token
.predicated
= 1;
1594 if (!submit_op2( emit
, mov_token
, dst
,
1595 src( pred_reg
), pass
))
1603 * Helper for emiting 'selection' commands. Basically:
1610 emit_select(struct svga_shader_emitter
*emit
,
1611 unsigned compare_func
,
1612 SVGA3dShaderDestToken dst
,
1613 struct src_register src0
,
1614 struct src_register src1
)
1616 /* There are some SVGA instructions which implement some selects
1617 * directly, but they are only available in the vertex shader.
1619 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1620 switch (compare_func
) {
1621 case PIPE_FUNC_GEQUAL
:
1622 return submit_op2( emit
, inst_token( SVGA3DOP_SGE
), dst
, src0
, src1
);
1623 case PIPE_FUNC_LEQUAL
:
1624 return submit_op2( emit
, inst_token( SVGA3DOP_SGE
), dst
, src1
, src0
);
1625 case PIPE_FUNC_GREATER
:
1626 return submit_op2( emit
, inst_token( SVGA3DOP_SLT
), dst
, src1
, src0
);
1627 case PIPE_FUNC_LESS
:
1628 return submit_op2( emit
, inst_token( SVGA3DOP_SLT
), dst
, src0
, src1
);
1634 /* Otherwise, need to use the setp approach:
1637 struct src_register one
, zero
;
1638 /* zero immediate is 0,0,0,1 */
1639 zero
= get_zero_immediate( emit
);
1640 one
= scalar( zero
, TGSI_SWIZZLE_W
);
1641 zero
= scalar( zero
, TGSI_SWIZZLE_X
);
1643 return emit_conditional(emit
, compare_func
, dst
, src0
, src1
, one
, zero
);
1649 * Translate/emit a TGSI SEQ, SNE, SLT, SGE, etc. instruction.
1652 emit_select_op(struct svga_shader_emitter
*emit
,
1654 const struct tgsi_full_instruction
*insn
)
1656 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1657 struct src_register src0
= translate_src_register(
1658 emit
, &insn
->Src
[0] );
1659 struct src_register src1
= translate_src_register(
1660 emit
, &insn
->Src
[1] );
1662 return emit_select( emit
, compare
, dst
, src0
, src1
);
1667 * Translate TGSI CMP instruction. Component-wise:
1668 * dst = (src0 < 0.0) ? src1 : src2
1671 emit_cmp(struct svga_shader_emitter
*emit
,
1672 const struct tgsi_full_instruction
*insn
)
1674 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
1675 const struct src_register src0
=
1676 translate_src_register(emit
, &insn
->Src
[0] );
1677 const struct src_register src1
=
1678 translate_src_register(emit
, &insn
->Src
[1] );
1679 const struct src_register src2
=
1680 translate_src_register(emit
, &insn
->Src
[2] );
1682 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1683 struct src_register zero
=
1684 scalar(get_zero_immediate(emit
), TGSI_SWIZZLE_X
);
1685 /* We used to simulate CMP with SLT+LRP. But that didn't work when
1686 * src1 or src2 was Inf/NaN. In particular, GLSL sqrt(0) failed
1687 * because it involves a CMP to handle the 0 case.
1688 * Use a conditional expression instead.
1690 return emit_conditional(emit
, PIPE_FUNC_LESS
, dst
,
1691 src0
, zero
, src1
, src2
);
1694 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
1696 /* CMP DST, SRC0, SRC2, SRC1 */
1697 return submit_op3( emit
, inst_token( SVGA3DOP_CMP
), dst
,
1704 * Translate/emit 2-operand (coord, sampler) texture instructions.
1707 emit_tex2(struct svga_shader_emitter
*emit
,
1708 const struct tgsi_full_instruction
*insn
,
1709 SVGA3dShaderDestToken dst
)
1711 SVGA3dShaderInstToken inst
;
1712 struct src_register texcoord
;
1713 struct src_register sampler
;
1714 SVGA3dShaderDestToken tmp
;
1718 switch (insn
->Instruction
.Opcode
) {
1719 case TGSI_OPCODE_TEX
:
1720 inst
.op
= SVGA3DOP_TEX
;
1722 case TGSI_OPCODE_TXP
:
1723 inst
.op
= SVGA3DOP_TEX
;
1724 inst
.control
= SVGA3DOPCONT_PROJECT
;
1726 case TGSI_OPCODE_TXB
:
1727 inst
.op
= SVGA3DOP_TEX
;
1728 inst
.control
= SVGA3DOPCONT_BIAS
;
1730 case TGSI_OPCODE_TXL
:
1731 inst
.op
= SVGA3DOP_TEXLDL
;
1738 texcoord
= translate_src_register( emit
, &insn
->Src
[0] );
1739 sampler
= translate_src_register( emit
, &insn
->Src
[1] );
1741 if (emit
->key
.fkey
.tex
[sampler
.base
.num
].unnormalized
||
1742 emit
->dynamic_branching_level
> 0)
1743 tmp
= get_temp( emit
);
1745 /* Can't do mipmapping inside dynamic branch constructs. Force LOD
1746 * zero in that case.
1748 if (emit
->dynamic_branching_level
> 0 &&
1749 inst
.op
== SVGA3DOP_TEX
&&
1750 SVGA3dShaderGetRegType(texcoord
.base
.value
) == SVGA3DREG_TEMP
) {
1751 struct src_register zero
= get_zero_immediate( emit
);
1753 /* MOV tmp, texcoord */
1754 if (!submit_op1( emit
,
1755 inst_token( SVGA3DOP_MOV
),
1760 /* MOV tmp.w, zero */
1761 if (!submit_op1( emit
,
1762 inst_token( SVGA3DOP_MOV
),
1763 writemask( tmp
, TGSI_WRITEMASK_W
),
1764 scalar( zero
, TGSI_SWIZZLE_X
)))
1767 texcoord
= src( tmp
);
1768 inst
.op
= SVGA3DOP_TEXLDL
;
1771 /* Explicit normalization of texcoords:
1773 if (emit
->key
.fkey
.tex
[sampler
.base
.num
].unnormalized
) {
1774 struct src_register wh
= get_tex_dimensions( emit
, sampler
.base
.num
);
1776 /* MUL tmp, SRC0, WH */
1777 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
1778 tmp
, texcoord
, wh
))
1781 texcoord
= src( tmp
);
1784 return submit_op2( emit
, inst
, dst
, texcoord
, sampler
);
1789 * Translate/emit 4-operand (coord, ddx, ddy, sampler) texture instructions.
1792 emit_tex4(struct svga_shader_emitter
*emit
,
1793 const struct tgsi_full_instruction
*insn
,
1794 SVGA3dShaderDestToken dst
)
1796 SVGA3dShaderInstToken inst
;
1797 struct src_register texcoord
;
1798 struct src_register ddx
;
1799 struct src_register ddy
;
1800 struct src_register sampler
;
1802 texcoord
= translate_src_register( emit
, &insn
->Src
[0] );
1803 ddx
= translate_src_register( emit
, &insn
->Src
[1] );
1804 ddy
= translate_src_register( emit
, &insn
->Src
[2] );
1805 sampler
= translate_src_register( emit
, &insn
->Src
[3] );
1809 switch (insn
->Instruction
.Opcode
) {
1810 case TGSI_OPCODE_TXD
:
1811 inst
.op
= SVGA3DOP_TEXLDD
; /* 4 args! */
1818 return submit_op4( emit
, inst
, dst
, texcoord
, sampler
, ddx
, ddy
);
1823 * Emit texture swizzle code. We do this here since SVGA samplers don't
1824 * directly support swizzles.
1827 emit_tex_swizzle(struct svga_shader_emitter
*emit
,
1828 SVGA3dShaderDestToken dst
,
1829 struct src_register src
,
1835 const unsigned swizzleIn
[4] = {swizzle_x
, swizzle_y
, swizzle_z
, swizzle_w
};
1836 unsigned srcSwizzle
[4];
1837 unsigned srcWritemask
= 0x0, zeroWritemask
= 0x0, oneWritemask
= 0x0;
1840 /* build writemasks and srcSwizzle terms */
1841 for (i
= 0; i
< 4; i
++) {
1842 if (swizzleIn
[i
] == PIPE_SWIZZLE_ZERO
) {
1843 srcSwizzle
[i
] = TGSI_SWIZZLE_X
+ i
;
1844 zeroWritemask
|= (1 << i
);
1846 else if (swizzleIn
[i
] == PIPE_SWIZZLE_ONE
) {
1847 srcSwizzle
[i
] = TGSI_SWIZZLE_X
+ i
;
1848 oneWritemask
|= (1 << i
);
1851 srcSwizzle
[i
] = swizzleIn
[i
];
1852 srcWritemask
|= (1 << i
);
1856 /* write x/y/z/w comps */
1857 if (dst
.mask
& srcWritemask
) {
1858 if (!submit_op1(emit
,
1859 inst_token(SVGA3DOP_MOV
),
1860 writemask(dst
, srcWritemask
),
1870 if (dst
.mask
& zeroWritemask
) {
1871 if (!submit_op1(emit
,
1872 inst_token(SVGA3DOP_MOV
),
1873 writemask(dst
, zeroWritemask
),
1874 scalar(get_zero_immediate(emit
), TGSI_SWIZZLE_X
)))
1879 if (dst
.mask
& oneWritemask
) {
1880 if (!submit_op1(emit
,
1881 inst_token(SVGA3DOP_MOV
),
1882 writemask(dst
, oneWritemask
),
1883 scalar(get_zero_immediate(emit
), TGSI_SWIZZLE_W
)))
1892 * Translate/emit a TGSI texture sample instruction.
1895 emit_tex(struct svga_shader_emitter
*emit
,
1896 const struct tgsi_full_instruction
*insn
)
1898 SVGA3dShaderDestToken dst
=
1899 translate_dst_register( emit
, insn
, 0 );
1900 struct src_register src0
=
1901 translate_src_register( emit
, &insn
->Src
[0] );
1902 struct src_register src1
=
1903 translate_src_register( emit
, &insn
->Src
[1] );
1905 SVGA3dShaderDestToken tex_result
;
1906 const unsigned unit
= src1
.base
.num
;
1908 /* check for shadow samplers */
1909 boolean compare
= (emit
->key
.fkey
.tex
[unit
].compare_mode
==
1910 PIPE_TEX_COMPARE_R_TO_TEXTURE
);
1912 /* texture swizzle */
1913 boolean swizzle
= (emit
->key
.fkey
.tex
[unit
].swizzle_r
!= PIPE_SWIZZLE_RED
||
1914 emit
->key
.fkey
.tex
[unit
].swizzle_g
!= PIPE_SWIZZLE_GREEN
||
1915 emit
->key
.fkey
.tex
[unit
].swizzle_b
!= PIPE_SWIZZLE_BLUE
||
1916 emit
->key
.fkey
.tex
[unit
].swizzle_a
!= PIPE_SWIZZLE_ALPHA
);
1918 boolean saturate
= insn
->Instruction
.Saturate
!= TGSI_SAT_NONE
;
1920 /* If doing compare processing or tex swizzle or saturation, we need to put
1921 * the fetched color into a temporary so it can be used as a source later on.
1923 if (compare
|| swizzle
|| saturate
) {
1924 tex_result
= get_temp( emit
);
1930 switch(insn
->Instruction
.Opcode
) {
1931 case TGSI_OPCODE_TEX
:
1932 case TGSI_OPCODE_TXB
:
1933 case TGSI_OPCODE_TXP
:
1934 case TGSI_OPCODE_TXL
:
1935 if (!emit_tex2( emit
, insn
, tex_result
))
1938 case TGSI_OPCODE_TXD
:
1939 if (!emit_tex4( emit
, insn
, tex_result
))
1947 SVGA3dShaderDestToken dst2
;
1949 if (swizzle
|| saturate
)
1954 if (dst
.mask
& TGSI_WRITEMASK_XYZ
) {
1955 SVGA3dShaderDestToken src0_zdivw
= get_temp( emit
);
1956 /* When sampling a depth texture, the result of the comparison is in
1959 struct src_register tex_src_x
= scalar(src(tex_result
), TGSI_SWIZZLE_Y
);
1960 struct src_register r_coord
;
1962 if (insn
->Instruction
.Opcode
== TGSI_OPCODE_TXP
) {
1963 /* Divide texcoord R by Q */
1964 if (!submit_op1( emit
, inst_token( SVGA3DOP_RCP
),
1965 writemask(src0_zdivw
, TGSI_WRITEMASK_X
),
1966 scalar(src0
, TGSI_SWIZZLE_W
) ))
1969 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
1970 writemask(src0_zdivw
, TGSI_WRITEMASK_X
),
1971 scalar(src0
, TGSI_SWIZZLE_Z
),
1972 scalar(src(src0_zdivw
), TGSI_SWIZZLE_X
) ))
1975 r_coord
= scalar(src(src0_zdivw
), TGSI_SWIZZLE_X
);
1978 r_coord
= scalar(src0
, TGSI_SWIZZLE_Z
);
1981 /* Compare texture sample value against R component of texcoord */
1982 if (!emit_select(emit
,
1983 emit
->key
.fkey
.tex
[unit
].compare_func
,
1984 writemask( dst2
, TGSI_WRITEMASK_XYZ
),
1990 if (dst
.mask
& TGSI_WRITEMASK_W
) {
1991 struct src_register one
=
1992 scalar( get_zero_immediate( emit
), TGSI_SWIZZLE_W
);
1994 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
1995 writemask( dst2
, TGSI_WRITEMASK_W
),
2001 if (saturate
&& !swizzle
) {
2002 /* MOV_SAT real_dst, dst */
2003 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
, src(tex_result
) ))
2007 /* swizzle from tex_result to dst (handles saturation too, if any) */
2008 emit_tex_swizzle(emit
,
2009 dst
, src(tex_result
),
2010 emit
->key
.fkey
.tex
[unit
].swizzle_r
,
2011 emit
->key
.fkey
.tex
[unit
].swizzle_g
,
2012 emit
->key
.fkey
.tex
[unit
].swizzle_b
,
2013 emit
->key
.fkey
.tex
[unit
].swizzle_a
);
2021 emit_bgnloop(struct svga_shader_emitter
*emit
,
2022 const struct tgsi_full_instruction
*insn
)
2024 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_LOOP
);
2025 struct src_register loop_reg
= src_register( SVGA3DREG_LOOP
, 0 );
2026 struct src_register const_int
= get_loop_const( emit
);
2028 emit
->dynamic_branching_level
++;
2030 return (emit_instruction( emit
, inst
) &&
2031 emit_src( emit
, loop_reg
) &&
2032 emit_src( emit
, const_int
) );
2037 emit_endloop(struct svga_shader_emitter
*emit
,
2038 const struct tgsi_full_instruction
*insn
)
2040 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_ENDLOOP
);
2042 emit
->dynamic_branching_level
--;
2044 return emit_instruction( emit
, inst
);
2049 * Translate/emit TGSI BREAK (out of loop) instruction.
2052 emit_brk(struct svga_shader_emitter
*emit
,
2053 const struct tgsi_full_instruction
*insn
)
2055 SVGA3dShaderInstToken inst
= inst_token( SVGA3DOP_BREAK
);
2056 return emit_instruction( emit
, inst
);
2061 * Emit simple instruction which operates on one scalar value (not
2062 * a vector). Ex: LG2, RCP, RSQ.
2065 emit_scalar_op1(struct svga_shader_emitter
*emit
,
2067 const struct tgsi_full_instruction
*insn
)
2069 SVGA3dShaderInstToken inst
;
2070 SVGA3dShaderDestToken dst
;
2071 struct src_register src
;
2073 inst
= inst_token( opcode
);
2074 dst
= translate_dst_register( emit
, insn
, 0 );
2075 src
= translate_src_register( emit
, &insn
->Src
[0] );
2076 src
= scalar( src
, TGSI_SWIZZLE_X
);
2078 return submit_op1( emit
, inst
, dst
, src
);
2083 * Translate/emit a simple instruction (one which has no special-case
2084 * code) such as ADD, MUL, MIN, MAX.
2087 emit_simple_instruction(struct svga_shader_emitter
*emit
,
2089 const struct tgsi_full_instruction
*insn
)
2091 const struct tgsi_full_src_register
*src
= insn
->Src
;
2092 SVGA3dShaderInstToken inst
;
2093 SVGA3dShaderDestToken dst
;
2095 inst
= inst_token( opcode
);
2096 dst
= translate_dst_register( emit
, insn
, 0 );
2098 switch (insn
->Instruction
.NumSrcRegs
) {
2100 return submit_op0( emit
, inst
, dst
);
2102 return submit_op1( emit
, inst
, dst
,
2103 translate_src_register( emit
, &src
[0] ));
2105 return submit_op2( emit
, inst
, dst
,
2106 translate_src_register( emit
, &src
[0] ),
2107 translate_src_register( emit
, &src
[1] ) );
2109 return submit_op3( emit
, inst
, dst
,
2110 translate_src_register( emit
, &src
[0] ),
2111 translate_src_register( emit
, &src
[1] ),
2112 translate_src_register( emit
, &src
[2] ) );
2121 * Translate/emit TGSI DDX, DDY instructions.
2124 emit_deriv(struct svga_shader_emitter
*emit
,
2125 const struct tgsi_full_instruction
*insn
)
2127 if (emit
->dynamic_branching_level
> 0 &&
2128 insn
->Src
[0].Register
.File
== TGSI_FILE_TEMPORARY
)
2130 struct src_register zero
= get_zero_immediate( emit
);
2131 SVGA3dShaderDestToken dst
=
2132 translate_dst_register( emit
, insn
, 0 );
2134 /* Deriv opcodes not valid inside dynamic branching, workaround
2135 * by zeroing out the destination.
2137 if (!submit_op1(emit
,
2138 inst_token( SVGA3DOP_MOV
),
2140 scalar(zero
, TGSI_SWIZZLE_X
)))
2147 const struct tgsi_full_src_register
*reg
= &insn
->Src
[0];
2148 SVGA3dShaderInstToken inst
;
2149 SVGA3dShaderDestToken dst
;
2150 struct src_register src0
;
2152 switch (insn
->Instruction
.Opcode
) {
2153 case TGSI_OPCODE_DDX
:
2154 opcode
= SVGA3DOP_DSX
;
2156 case TGSI_OPCODE_DDY
:
2157 opcode
= SVGA3DOP_DSY
;
2163 inst
= inst_token( opcode
);
2164 dst
= translate_dst_register( emit
, insn
, 0 );
2165 src0
= translate_src_register( emit
, reg
);
2167 /* We cannot use negate or abs on source to dsx/dsy instruction.
2169 if (reg
->Register
.Absolute
||
2170 reg
->Register
.Negate
) {
2171 SVGA3dShaderDestToken temp
= get_temp( emit
);
2173 if (!emit_repl( emit
, temp
, &src0
))
2177 return submit_op1( emit
, inst
, dst
, src0
);
2183 * Translate/emit ARL (Address Register Load) instruction. Used to
2184 * move a value into the special 'address' register. Used to implement
2185 * indirect/variable indexing into arrays.
2188 emit_arl(struct svga_shader_emitter
*emit
,
2189 const struct tgsi_full_instruction
*insn
)
2191 ++emit
->current_arl
;
2192 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
2193 /* MOVA not present in pixel shader instruction set.
2194 * Ignore this instruction altogether since it is
2195 * only used for loop counters -- and for that
2196 * we reference aL directly.
2200 if (svga_arl_needs_adjustment( emit
)) {
2201 return emit_fake_arl( emit
, insn
);
2203 /* no need to adjust, just emit straight arl */
2204 return emit_simple_instruction(emit
, SVGA3DOP_MOVA
, insn
);
2210 emit_pow(struct svga_shader_emitter
*emit
,
2211 const struct tgsi_full_instruction
*insn
)
2213 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2214 struct src_register src0
= translate_src_register(
2215 emit
, &insn
->Src
[0] );
2216 struct src_register src1
= translate_src_register(
2217 emit
, &insn
->Src
[1] );
2218 boolean need_tmp
= FALSE
;
2220 /* POW can only output to a temporary */
2221 if (insn
->Dst
[0].Register
.File
!= TGSI_FILE_TEMPORARY
)
2224 /* POW src1 must not be the same register as dst */
2225 if (alias_src_dst( src1
, dst
))
2228 /* it's a scalar op */
2229 src0
= scalar( src0
, TGSI_SWIZZLE_X
);
2230 src1
= scalar( src1
, TGSI_SWIZZLE_X
);
2233 SVGA3dShaderDestToken tmp
=
2234 writemask(get_temp( emit
), TGSI_WRITEMASK_X
);
2236 if (!submit_op2(emit
, inst_token( SVGA3DOP_POW
), tmp
, src0
, src1
))
2239 return submit_op1(emit
, inst_token( SVGA3DOP_MOV
),
2240 dst
, scalar(src(tmp
), 0) );
2243 return submit_op2(emit
, inst_token( SVGA3DOP_POW
), dst
, src0
, src1
);
2249 * Translate/emit TGSI XPD (vector cross product) instruction.
2252 emit_xpd(struct svga_shader_emitter
*emit
,
2253 const struct tgsi_full_instruction
*insn
)
2255 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2256 const struct src_register src0
= translate_src_register(
2257 emit
, &insn
->Src
[0] );
2258 const struct src_register src1
= translate_src_register(
2259 emit
, &insn
->Src
[1] );
2260 boolean need_dst_tmp
= FALSE
;
2262 /* XPD can only output to a temporary */
2263 if (SVGA3dShaderGetRegType(dst
.value
) != SVGA3DREG_TEMP
)
2264 need_dst_tmp
= TRUE
;
2266 /* The dst reg must not be the same as src0 or src1*/
2267 if (alias_src_dst(src0
, dst
) ||
2268 alias_src_dst(src1
, dst
))
2269 need_dst_tmp
= TRUE
;
2272 SVGA3dShaderDestToken tmp
= get_temp( emit
);
2274 /* Obey DX9 restrictions on mask:
2276 tmp
.mask
= dst
.mask
& TGSI_WRITEMASK_XYZ
;
2278 if (!submit_op2(emit
, inst_token( SVGA3DOP_CRS
), tmp
, src0
, src1
))
2281 if (!submit_op1(emit
, inst_token( SVGA3DOP_MOV
), dst
, src( tmp
)))
2285 if (!submit_op2(emit
, inst_token( SVGA3DOP_CRS
), dst
, src0
, src1
))
2289 /* Need to emit 1.0 to dst.w?
2291 if (dst
.mask
& TGSI_WRITEMASK_W
) {
2292 struct src_register zero
= get_zero_immediate( emit
);
2294 if (!submit_op1(emit
,
2295 inst_token( SVGA3DOP_MOV
),
2296 writemask(dst
, TGSI_WRITEMASK_W
),
2306 * Translate/emit LRP (Linear Interpolation) instruction.
2309 emit_lrp(struct svga_shader_emitter
*emit
,
2310 const struct tgsi_full_instruction
*insn
)
2312 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2313 const struct src_register src0
= translate_src_register(
2314 emit
, &insn
->Src
[0] );
2315 const struct src_register src1
= translate_src_register(
2316 emit
, &insn
->Src
[1] );
2317 const struct src_register src2
= translate_src_register(
2318 emit
, &insn
->Src
[2] );
2320 return submit_lrp(emit
, dst
, src0
, src1
, src2
);
2324 * Translate/emit DST (Distance function) instruction.
2327 emit_dst_insn(struct svga_shader_emitter
*emit
,
2328 const struct tgsi_full_instruction
*insn
)
2330 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2331 /* SVGA/DX9 has a DST instruction, but only for vertex shaders:
2333 return emit_simple_instruction(emit
, SVGA3DOP_DST
, insn
);
2336 /* result[0] = 1 * 1;
2337 * result[1] = a[1] * b[1];
2338 * result[2] = a[2] * 1;
2339 * result[3] = 1 * b[3];
2341 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2342 SVGA3dShaderDestToken tmp
;
2343 const struct src_register src0
= translate_src_register(
2344 emit
, &insn
->Src
[0] );
2345 const struct src_register src1
= translate_src_register(
2346 emit
, &insn
->Src
[1] );
2347 struct src_register zero
= get_zero_immediate( emit
);
2348 boolean need_tmp
= FALSE
;
2350 if (SVGA3dShaderGetRegType(dst
.value
) != SVGA3DREG_TEMP
||
2351 alias_src_dst(src0
, dst
) ||
2352 alias_src_dst(src1
, dst
))
2356 tmp
= get_temp( emit
);
2364 if (tmp
.mask
& TGSI_WRITEMASK_XW
) {
2365 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2366 writemask(tmp
, TGSI_WRITEMASK_XW
),
2373 if (tmp
.mask
& TGSI_WRITEMASK_YZ
) {
2374 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2375 writemask(tmp
, TGSI_WRITEMASK_YZ
),
2380 /* tmp.yw = tmp * src1
2382 if (tmp
.mask
& TGSI_WRITEMASK_YW
) {
2383 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
2384 writemask(tmp
, TGSI_WRITEMASK_YW
),
2393 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2405 emit_exp(struct svga_shader_emitter
*emit
,
2406 const struct tgsi_full_instruction
*insn
)
2408 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2409 struct src_register src0
=
2410 translate_src_register( emit
, &insn
->Src
[0] );
2411 struct src_register zero
= get_zero_immediate( emit
);
2412 SVGA3dShaderDestToken fraction
;
2414 if (dst
.mask
& TGSI_WRITEMASK_Y
)
2416 else if (dst
.mask
& TGSI_WRITEMASK_X
)
2417 fraction
= get_temp( emit
);
2421 /* If y is being written, fill it with src0 - floor(src0).
2423 if (dst
.mask
& TGSI_WRITEMASK_XY
) {
2424 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
),
2425 writemask( fraction
, TGSI_WRITEMASK_Y
),
2430 /* If x is being written, fill it with 2 ^ floor(src0).
2432 if (dst
.mask
& TGSI_WRITEMASK_X
) {
2433 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
),
2434 writemask( dst
, TGSI_WRITEMASK_X
),
2436 scalar( negate( src( fraction
) ), TGSI_SWIZZLE_Y
) ) )
2439 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXP
),
2440 writemask( dst
, TGSI_WRITEMASK_X
),
2441 scalar( src( dst
), TGSI_SWIZZLE_X
) ) )
2444 if (!(dst
.mask
& TGSI_WRITEMASK_Y
))
2445 release_temp( emit
, fraction
);
2448 /* If z is being written, fill it with 2 ^ src0 (partial precision).
2450 if (dst
.mask
& TGSI_WRITEMASK_Z
) {
2451 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXPP
),
2452 writemask( dst
, TGSI_WRITEMASK_Z
),
2457 /* If w is being written, fill it with one.
2459 if (dst
.mask
& TGSI_WRITEMASK_W
) {
2460 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2461 writemask(dst
, TGSI_WRITEMASK_W
),
2462 scalar( zero
, TGSI_SWIZZLE_W
) ))
2471 * Translate/emit LIT (Lighting helper) instruction.
2474 emit_lit(struct svga_shader_emitter
*emit
,
2475 const struct tgsi_full_instruction
*insn
)
2477 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2478 /* SVGA/DX9 has a LIT instruction, but only for vertex shaders:
2480 return emit_simple_instruction(emit
, SVGA3DOP_LIT
, insn
);
2483 /* D3D vs. GL semantics can be fairly easily accomodated by
2484 * variations on this sequence.
2488 * tmp.z = pow(src.y,src.w)
2489 * p0 = src0.xxxx > 0
2490 * result = zero.wxxw
2491 * (p0) result.yz = tmp
2495 * tmp.z = pow(src.y,src.w)
2496 * p0 = src0.xxyy > 0
2497 * result = zero.wxxw
2498 * (p0) result.yz = tmp
2500 * Will implement the GL version for now.
2502 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2503 SVGA3dShaderDestToken tmp
= get_temp( emit
);
2504 const struct src_register src0
= translate_src_register(
2505 emit
, &insn
->Src
[0] );
2506 struct src_register zero
= get_zero_immediate( emit
);
2508 /* tmp = pow(src.y, src.w)
2510 if (dst
.mask
& TGSI_WRITEMASK_Z
) {
2511 if (!submit_op2(emit
, inst_token( SVGA3DOP_POW
),
2520 if (dst
.mask
& TGSI_WRITEMASK_Y
) {
2521 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2522 writemask(tmp
, TGSI_WRITEMASK_Y
),
2527 /* Can't quite do this with emit conditional due to the extra
2528 * writemask on the predicated mov:
2531 SVGA3dShaderDestToken pred_reg
= dst_register( SVGA3DREG_PREDICATE
, 0 );
2532 SVGA3dShaderInstToken setp_token
, mov_token
;
2533 struct src_register predsrc
;
2535 setp_token
= inst_token( SVGA3DOP_SETP
);
2536 mov_token
= inst_token( SVGA3DOP_MOV
);
2538 setp_token
.control
= SVGA3DOPCOMP_GT
;
2540 /* D3D vs GL semantics:
2543 predsrc
= swizzle(src0
, 0, 0, 1, 1); /* D3D */
2545 predsrc
= swizzle(src0
, 0, 0, 0, 0); /* GL */
2547 /* SETP src0.xxyy, GT, {0}.x */
2548 if (!submit_op2( emit
, setp_token
, pred_reg
,
2550 swizzle(zero
, 0, 0, 0, 0) ))
2554 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), dst
,
2555 swizzle(zero
, 3, 0, 0, 3 )))
2558 /* MOV dst.yz, tmp (predicated)
2560 * Note that the predicate reg (and possible modifiers) is passed
2561 * as the first source argument.
2563 if (dst
.mask
& TGSI_WRITEMASK_YZ
) {
2564 mov_token
.predicated
= 1;
2565 if (!submit_op2( emit
, mov_token
,
2566 writemask(dst
, TGSI_WRITEMASK_YZ
),
2567 src( pred_reg
), src( tmp
) ))
2578 emit_ex2(struct svga_shader_emitter
*emit
,
2579 const struct tgsi_full_instruction
*insn
)
2581 SVGA3dShaderInstToken inst
;
2582 SVGA3dShaderDestToken dst
;
2583 struct src_register src0
;
2585 inst
= inst_token( SVGA3DOP_EXP
);
2586 dst
= translate_dst_register( emit
, insn
, 0 );
2587 src0
= translate_src_register( emit
, &insn
->Src
[0] );
2588 src0
= scalar( src0
, TGSI_SWIZZLE_X
);
2590 if (dst
.mask
!= TGSI_WRITEMASK_XYZW
) {
2591 SVGA3dShaderDestToken tmp
= get_temp( emit
);
2593 if (!submit_op1( emit
, inst
, tmp
, src0
))
2596 return submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2598 scalar( src( tmp
), TGSI_SWIZZLE_X
) );
2601 return submit_op1( emit
, inst
, dst
, src0
);
2606 emit_log(struct svga_shader_emitter
*emit
,
2607 const struct tgsi_full_instruction
*insn
)
2609 SVGA3dShaderDestToken dst
= translate_dst_register( emit
, insn
, 0 );
2610 struct src_register src0
=
2611 translate_src_register( emit
, &insn
->Src
[0] );
2612 struct src_register zero
= get_zero_immediate( emit
);
2613 SVGA3dShaderDestToken abs_tmp
;
2614 struct src_register abs_src0
;
2615 SVGA3dShaderDestToken log2_abs
;
2619 if (dst
.mask
& TGSI_WRITEMASK_Z
)
2621 else if (dst
.mask
& TGSI_WRITEMASK_XY
)
2622 log2_abs
= get_temp( emit
);
2626 /* If z is being written, fill it with log2( abs( src0 ) ).
2628 if (dst
.mask
& TGSI_WRITEMASK_XYZ
) {
2629 if (!src0
.base
.srcMod
|| src0
.base
.srcMod
== SVGA3DSRCMOD_ABS
)
2632 abs_tmp
= get_temp( emit
);
2634 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2639 abs_src0
= src( abs_tmp
);
2642 abs_src0
= absolute( scalar( abs_src0
, TGSI_SWIZZLE_X
) );
2644 if (!submit_op1( emit
, inst_token( SVGA3DOP_LOG
),
2645 writemask( log2_abs
, TGSI_WRITEMASK_Z
),
2650 if (dst
.mask
& TGSI_WRITEMASK_XY
) {
2651 SVGA3dShaderDestToken floor_log2
;
2653 if (dst
.mask
& TGSI_WRITEMASK_X
)
2656 floor_log2
= get_temp( emit
);
2658 /* If x is being written, fill it with floor( log2( abs( src0 ) ) ).
2660 if (!submit_op1( emit
, inst_token( SVGA3DOP_FRC
),
2661 writemask( floor_log2
, TGSI_WRITEMASK_X
),
2662 scalar( src( log2_abs
), TGSI_SWIZZLE_Z
) ) )
2665 if (!submit_op2( emit
, inst_token( SVGA3DOP_ADD
),
2666 writemask( floor_log2
, TGSI_WRITEMASK_X
),
2667 scalar( src( log2_abs
), TGSI_SWIZZLE_Z
),
2668 negate( src( floor_log2
) ) ) )
2671 /* If y is being written, fill it with
2672 * abs ( src0 ) / ( 2 ^ floor( log2( abs( src0 ) ) ) ).
2674 if (dst
.mask
& TGSI_WRITEMASK_Y
) {
2675 if (!submit_op1( emit
, inst_token( SVGA3DOP_EXP
),
2676 writemask( dst
, TGSI_WRITEMASK_Y
),
2677 negate( scalar( src( floor_log2
),
2678 TGSI_SWIZZLE_X
) ) ) )
2681 if (!submit_op2( emit
, inst_token( SVGA3DOP_MUL
),
2682 writemask( dst
, TGSI_WRITEMASK_Y
),
2688 if (!(dst
.mask
& TGSI_WRITEMASK_X
))
2689 release_temp( emit
, floor_log2
);
2691 if (!(dst
.mask
& TGSI_WRITEMASK_Z
))
2692 release_temp( emit
, log2_abs
);
2695 if (dst
.mask
& TGSI_WRITEMASK_XYZ
&& src0
.base
.srcMod
&&
2696 src0
.base
.srcMod
!= SVGA3DSRCMOD_ABS
)
2697 release_temp( emit
, abs_tmp
);
2699 /* If w is being written, fill it with one.
2701 if (dst
.mask
& TGSI_WRITEMASK_W
) {
2702 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
),
2703 writemask(dst
, TGSI_WRITEMASK_W
),
2704 scalar( zero
, TGSI_SWIZZLE_W
) ))
2713 * Translate TGSI TRUNC or ROUND instruction.
2714 * We need to truncate toward zero. Ex: trunc(-1.9) = -1
2715 * Different approaches are needed for VS versus PS.
2718 emit_trunc_round(struct svga_shader_emitter
*emit
,
2719 const struct tgsi_full_instruction
*insn
,
2722 SVGA3dShaderDestToken dst
= translate_dst_register(emit
, insn
, 0);
2723 const struct src_register src0
=
2724 translate_src_register(emit
, &insn
->Src
[0] );
2725 SVGA3dShaderDestToken t1
= get_temp(emit
);
2728 SVGA3dShaderDestToken t0
= get_temp(emit
);
2729 struct src_register half
= get_half_immediate(emit
);
2731 /* t0 = abs(src0) + 0.5 */
2732 if (!submit_op2(emit
, inst_token(SVGA3DOP_ADD
), t0
,
2733 absolute(src0
), half
))
2736 /* t1 = fract(t0) */
2737 if (!submit_op1(emit
, inst_token(SVGA3DOP_FRC
), t1
, src(t0
)))
2741 if (!submit_op2(emit
, inst_token(SVGA3DOP_ADD
), t1
, src(t0
),
2748 /* t1 = fract(abs(src0)) */
2749 if (!submit_op1(emit
, inst_token(SVGA3DOP_FRC
), t1
, absolute(src0
)))
2752 /* t1 = abs(src0) - t1 */
2753 if (!submit_op2(emit
, inst_token(SVGA3DOP_ADD
), t1
, absolute(src0
),
2759 * Now we need to multiply t1 by the sign of the original value.
2761 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2762 /* For VS: use SGN instruction */
2763 /* Need two extra/dummy registers: */
2764 SVGA3dShaderDestToken t2
= get_temp(emit
), t3
= get_temp(emit
),
2765 t4
= get_temp(emit
);
2767 /* t2 = sign(src0) */
2768 if (!submit_op3(emit
, inst_token(SVGA3DOP_SGN
), t2
, src0
,
2773 if (!submit_op2(emit
, inst_token(SVGA3DOP_MUL
), dst
, src(t1
), src(t2
)))
2777 /* For FS: Use CMP instruction */
2778 return submit_op3(emit
, inst_token( SVGA3DOP_CMP
), dst
,
2779 src0
, src(t1
), negate(src(t1
)));
2787 * Translate/emit "begin subroutine" instruction/marker/label.
2790 emit_bgnsub(struct svga_shader_emitter
*emit
,
2792 const struct tgsi_full_instruction
*insn
)
2796 /* Note that we've finished the main function and are now emitting
2797 * subroutines. This affects how we terminate the generated
2800 emit
->in_main_func
= FALSE
;
2802 for (i
= 0; i
< emit
->nr_labels
; i
++) {
2803 if (emit
->label
[i
] == position
) {
2804 return (emit_instruction( emit
, inst_token( SVGA3DOP_RET
) ) &&
2805 emit_instruction( emit
, inst_token( SVGA3DOP_LABEL
) ) &&
2806 emit_src( emit
, src_register( SVGA3DREG_LABEL
, i
)));
2816 * Translate/emit subroutine call instruction.
2819 emit_call(struct svga_shader_emitter
*emit
,
2820 const struct tgsi_full_instruction
*insn
)
2822 unsigned position
= insn
->Label
.Label
;
2825 for (i
= 0; i
< emit
->nr_labels
; i
++) {
2826 if (emit
->label
[i
] == position
)
2830 if (emit
->nr_labels
== Elements(emit
->label
))
2833 if (i
== emit
->nr_labels
) {
2834 emit
->label
[i
] = position
;
2838 return (emit_instruction( emit
, inst_token( SVGA3DOP_CALL
) ) &&
2839 emit_src( emit
, src_register( SVGA3DREG_LABEL
, i
)));
2844 * Called at the end of the shader. Actually, emit special "fix-up"
2845 * code for the vertex/fragment shader.
2848 emit_end(struct svga_shader_emitter
*emit
)
2850 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2851 return emit_vs_postamble( emit
);
2854 return emit_ps_postamble( emit
);
2860 * Translate any TGSI instruction to SVGA.
2863 svga_emit_instruction(struct svga_shader_emitter
*emit
,
2865 const struct tgsi_full_instruction
*insn
)
2867 switch (insn
->Instruction
.Opcode
) {
2869 case TGSI_OPCODE_ARL
:
2870 return emit_arl( emit
, insn
);
2872 case TGSI_OPCODE_TEX
:
2873 case TGSI_OPCODE_TXB
:
2874 case TGSI_OPCODE_TXP
:
2875 case TGSI_OPCODE_TXL
:
2876 case TGSI_OPCODE_TXD
:
2877 return emit_tex( emit
, insn
);
2879 case TGSI_OPCODE_DDX
:
2880 case TGSI_OPCODE_DDY
:
2881 return emit_deriv( emit
, insn
);
2883 case TGSI_OPCODE_BGNSUB
:
2884 return emit_bgnsub( emit
, position
, insn
);
2886 case TGSI_OPCODE_ENDSUB
:
2889 case TGSI_OPCODE_CAL
:
2890 return emit_call( emit
, insn
);
2892 case TGSI_OPCODE_FLR
:
2893 return emit_floor( emit
, insn
);
2895 case TGSI_OPCODE_TRUNC
:
2896 return emit_trunc_round( emit
, insn
, FALSE
);
2898 case TGSI_OPCODE_ROUND
:
2899 return emit_trunc_round( emit
, insn
, TRUE
);
2901 case TGSI_OPCODE_CEIL
:
2902 return emit_ceil( emit
, insn
);
2904 case TGSI_OPCODE_CMP
:
2905 return emit_cmp( emit
, insn
);
2907 case TGSI_OPCODE_DIV
:
2908 return emit_div( emit
, insn
);
2910 case TGSI_OPCODE_DP2
:
2911 return emit_dp2( emit
, insn
);
2913 case TGSI_OPCODE_DPH
:
2914 return emit_dph( emit
, insn
);
2916 case TGSI_OPCODE_NRM
:
2917 return emit_nrm( emit
, insn
);
2919 case TGSI_OPCODE_COS
:
2920 return emit_cos( emit
, insn
);
2922 case TGSI_OPCODE_SIN
:
2923 return emit_sin( emit
, insn
);
2925 case TGSI_OPCODE_SCS
:
2926 return emit_sincos( emit
, insn
);
2928 case TGSI_OPCODE_END
:
2929 /* TGSI always finishes the main func with an END */
2930 return emit_end( emit
);
2932 case TGSI_OPCODE_KILL_IF
:
2933 return emit_kill_if( emit
, insn
);
2935 /* Selection opcodes. The underlying language is fairly
2936 * non-orthogonal about these.
2938 case TGSI_OPCODE_SEQ
:
2939 return emit_select_op( emit
, PIPE_FUNC_EQUAL
, insn
);
2941 case TGSI_OPCODE_SNE
:
2942 return emit_select_op( emit
, PIPE_FUNC_NOTEQUAL
, insn
);
2944 case TGSI_OPCODE_SGT
:
2945 return emit_select_op( emit
, PIPE_FUNC_GREATER
, insn
);
2947 case TGSI_OPCODE_SGE
:
2948 return emit_select_op( emit
, PIPE_FUNC_GEQUAL
, insn
);
2950 case TGSI_OPCODE_SLT
:
2951 return emit_select_op( emit
, PIPE_FUNC_LESS
, insn
);
2953 case TGSI_OPCODE_SLE
:
2954 return emit_select_op( emit
, PIPE_FUNC_LEQUAL
, insn
);
2956 case TGSI_OPCODE_SUB
:
2957 return emit_sub( emit
, insn
);
2959 case TGSI_OPCODE_POW
:
2960 return emit_pow( emit
, insn
);
2962 case TGSI_OPCODE_EX2
:
2963 return emit_ex2( emit
, insn
);
2965 case TGSI_OPCODE_EXP
:
2966 return emit_exp( emit
, insn
);
2968 case TGSI_OPCODE_LOG
:
2969 return emit_log( emit
, insn
);
2971 case TGSI_OPCODE_LG2
:
2972 return emit_scalar_op1( emit
, SVGA3DOP_LOG
, insn
);
2974 case TGSI_OPCODE_RSQ
:
2975 return emit_scalar_op1( emit
, SVGA3DOP_RSQ
, insn
);
2977 case TGSI_OPCODE_RCP
:
2978 return emit_scalar_op1( emit
, SVGA3DOP_RCP
, insn
);
2980 case TGSI_OPCODE_CONT
:
2981 /* not expected (we return PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED = 0) */
2984 case TGSI_OPCODE_RET
:
2985 /* This is a noop -- we tell mesa that we can't support RET
2986 * within a function (early return), so this will always be
2987 * followed by an ENDSUB.
2991 /* These aren't actually used by any of the frontends we care
2994 case TGSI_OPCODE_CLAMP
:
2995 case TGSI_OPCODE_AND
:
2996 case TGSI_OPCODE_OR
:
2997 case TGSI_OPCODE_I2F
:
2998 case TGSI_OPCODE_NOT
:
2999 case TGSI_OPCODE_SHL
:
3000 case TGSI_OPCODE_ISHR
:
3001 case TGSI_OPCODE_XOR
:
3004 case TGSI_OPCODE_IF
:
3005 return emit_if( emit
, insn
);
3006 case TGSI_OPCODE_ELSE
:
3007 return emit_else( emit
, insn
);
3008 case TGSI_OPCODE_ENDIF
:
3009 return emit_endif( emit
, insn
);
3011 case TGSI_OPCODE_BGNLOOP
:
3012 return emit_bgnloop( emit
, insn
);
3013 case TGSI_OPCODE_ENDLOOP
:
3014 return emit_endloop( emit
, insn
);
3015 case TGSI_OPCODE_BRK
:
3016 return emit_brk( emit
, insn
);
3018 case TGSI_OPCODE_XPD
:
3019 return emit_xpd( emit
, insn
);
3021 case TGSI_OPCODE_KILL
:
3022 return emit_kill( emit
, insn
);
3024 case TGSI_OPCODE_DST
:
3025 return emit_dst_insn( emit
, insn
);
3027 case TGSI_OPCODE_LIT
:
3028 return emit_lit( emit
, insn
);
3030 case TGSI_OPCODE_LRP
:
3031 return emit_lrp( emit
, insn
);
3033 case TGSI_OPCODE_SSG
:
3034 return emit_ssg( emit
, insn
);
3038 unsigned opcode
= translate_opcode(insn
->Instruction
.Opcode
);
3040 if (opcode
== SVGA3DOP_LAST_INST
)
3043 if (!emit_simple_instruction( emit
, opcode
, insn
))
3053 * Translate/emit a TGSI IMMEDIATE declaration.
3054 * An immediate vector is a constant that's hard-coded into the shader.
3057 svga_emit_immediate(struct svga_shader_emitter
*emit
,
3058 const struct tgsi_full_immediate
*imm
)
3060 static const float id
[4] = {0,0,0,1};
3064 assert(1 <= imm
->Immediate
.NrTokens
&& imm
->Immediate
.NrTokens
<= 5);
3065 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++) {
3066 float f
= imm
->u
[i
].Float
;
3067 value
[i
] = util_is_inf_or_nan(f
) ? 0.0f
: f
;
3070 /* If the immediate has less than four values, fill in the remaining
3071 * positions from id={0,0,0,1}.
3073 for ( ; i
< 4; i
++ )
3076 return emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
3077 emit
->imm_start
+ emit
->internal_imm_count
++,
3078 value
[0], value
[1], value
[2], value
[3]);
3083 make_immediate(struct svga_shader_emitter
*emit
,
3084 float a
, float b
, float c
, float d
,
3085 struct src_register
*out
)
3087 unsigned idx
= emit
->nr_hw_float_const
++;
3089 if (!emit_def_const( emit
, SVGA3D_CONST_TYPE_FLOAT
,
3093 *out
= src_register( SVGA3DREG_CONST
, idx
);
3100 * Emit special VS instructions at top of shader.
3103 emit_vs_preamble(struct svga_shader_emitter
*emit
)
3105 if (!emit
->key
.vkey
.need_prescale
) {
3106 if (!make_immediate( emit
, 0, 0, .5, .5,
3116 * Emit special PS instructions at top of shader.
3119 emit_ps_preamble(struct svga_shader_emitter
*emit
)
3121 if (emit
->ps_reads_pos
&& emit
->info
.reads_z
) {
3123 * Assemble the position from various bits of inputs. Depth and W are
3124 * passed in a texcoord this is due to D3D's vPos not hold Z or W.
3125 * Also fixup the perspective interpolation.
3127 * temp_pos.xy = vPos.xy
3128 * temp_pos.w = rcp(texcoord1.w);
3129 * temp_pos.z = texcoord1.z * temp_pos.w;
3131 if (!submit_op1( emit
,
3132 inst_token(SVGA3DOP_MOV
),
3133 writemask( emit
->ps_temp_pos
, TGSI_WRITEMASK_XY
),
3134 emit
->ps_true_pos
))
3137 if (!submit_op1( emit
,
3138 inst_token(SVGA3DOP_RCP
),
3139 writemask( emit
->ps_temp_pos
, TGSI_WRITEMASK_W
),
3140 scalar( emit
->ps_depth_pos
, TGSI_SWIZZLE_W
) ))
3143 if (!submit_op2( emit
,
3144 inst_token(SVGA3DOP_MUL
),
3145 writemask( emit
->ps_temp_pos
, TGSI_WRITEMASK_Z
),
3146 scalar( emit
->ps_depth_pos
, TGSI_SWIZZLE_Z
),
3147 scalar( src(emit
->ps_temp_pos
), TGSI_SWIZZLE_W
) ))
3156 * Emit special PS instructions at end of shader.
3159 emit_ps_postamble(struct svga_shader_emitter
*emit
)
3163 /* PS oDepth is incredibly fragile and it's very hard to catch the
3164 * types of usage that break it during shader emit. Easier just to
3165 * redirect the main program to a temporary and then only touch
3166 * oDepth with a hand-crafted MOV below.
3168 if (SVGA3dShaderGetRegType(emit
->true_pos
.value
) != 0) {
3169 if (!submit_op1( emit
,
3170 inst_token(SVGA3DOP_MOV
),
3172 scalar(src(emit
->temp_pos
), TGSI_SWIZZLE_Z
) ))
3176 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
3177 if (SVGA3dShaderGetRegType(emit
->true_color_output
[i
].value
) != 0) {
3178 /* Potentially override output colors with white for XOR
3179 * logicop workaround.
3181 if (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
3182 emit
->key
.fkey
.white_fragments
) {
3183 struct src_register one
= scalar( get_zero_immediate( emit
),
3186 if (!submit_op1( emit
,
3187 inst_token(SVGA3DOP_MOV
),
3188 emit
->true_color_output
[i
],
3192 else if (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
3193 i
< emit
->key
.fkey
.write_color0_to_n_cbufs
) {
3194 /* Write temp color output [0] to true output [i] */
3195 if (!submit_op1(emit
, inst_token(SVGA3DOP_MOV
),
3196 emit
->true_color_output
[i
],
3197 src(emit
->temp_color_output
[0]))) {
3202 if (!submit_op1( emit
,
3203 inst_token(SVGA3DOP_MOV
),
3204 emit
->true_color_output
[i
],
3205 src(emit
->temp_color_output
[i
]) ))
3216 * Emit special VS instructions at end of shader.
3219 emit_vs_postamble(struct svga_shader_emitter
*emit
)
3221 /* PSIZ output is incredibly fragile and it's very hard to catch
3222 * the types of usage that break it during shader emit. Easier
3223 * just to redirect the main program to a temporary and then only
3224 * touch PSIZ with a hand-crafted MOV below.
3226 if (SVGA3dShaderGetRegType(emit
->true_psiz
.value
) != 0) {
3227 if (!submit_op1( emit
,
3228 inst_token(SVGA3DOP_MOV
),
3230 scalar(src(emit
->temp_psiz
), TGSI_SWIZZLE_X
) ))
3234 /* Need to perform various manipulations on vertex position to cope
3235 * with the different GL and D3D clip spaces.
3237 if (emit
->key
.vkey
.need_prescale
) {
3238 SVGA3dShaderDestToken temp_pos
= emit
->temp_pos
;
3239 SVGA3dShaderDestToken depth
= emit
->depth_pos
;
3240 SVGA3dShaderDestToken pos
= emit
->true_pos
;
3241 unsigned offset
= emit
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
3242 struct src_register prescale_scale
= src_register( SVGA3DREG_CONST
,
3244 struct src_register prescale_trans
= src_register( SVGA3DREG_CONST
,
3247 if (!submit_op1( emit
,
3248 inst_token(SVGA3DOP_MOV
),
3249 writemask(depth
, TGSI_WRITEMASK_W
),
3250 scalar(src(temp_pos
), TGSI_SWIZZLE_W
) ))
3253 /* MUL temp_pos.xyz, temp_pos, prescale.scale
3254 * MAD result.position, temp_pos.wwww, prescale.trans, temp_pos
3255 * --> Note that prescale.trans.w == 0
3257 if (!submit_op2( emit
,
3258 inst_token(SVGA3DOP_MUL
),
3259 writemask(temp_pos
, TGSI_WRITEMASK_XYZ
),
3264 if (!submit_op3( emit
,
3265 inst_token(SVGA3DOP_MAD
),
3267 swizzle(src(temp_pos
), 3, 3, 3, 3),
3272 /* Also write to depth value */
3273 if (!submit_op3( emit
,
3274 inst_token(SVGA3DOP_MAD
),
3275 writemask(depth
, TGSI_WRITEMASK_Z
),
3276 swizzle(src(temp_pos
), 3, 3, 3, 3),
3282 SVGA3dShaderDestToken temp_pos
= emit
->temp_pos
;
3283 SVGA3dShaderDestToken depth
= emit
->depth_pos
;
3284 SVGA3dShaderDestToken pos
= emit
->true_pos
;
3285 struct src_register imm_0055
= emit
->imm_0055
;
3287 /* Adjust GL clipping coordinate space to hardware (D3D-style):
3289 * DP4 temp_pos.z, {0,0,.5,.5}, temp_pos
3290 * MOV result.position, temp_pos
3292 if (!submit_op2( emit
,
3293 inst_token(SVGA3DOP_DP4
),
3294 writemask(temp_pos
, TGSI_WRITEMASK_Z
),
3299 if (!submit_op1( emit
,
3300 inst_token(SVGA3DOP_MOV
),
3305 /* Move the manipulated depth into the extra texcoord reg */
3306 if (!submit_op1( emit
,
3307 inst_token(SVGA3DOP_MOV
),
3308 writemask(depth
, TGSI_WRITEMASK_ZW
),
3318 * For the pixel shader: emit the code which chooses the front
3319 * or back face color depending on triangle orientation.
3320 * This happens at the top of the fragment shader.
3323 * 1: COLOR = FrontColor;
3325 * 3: COLOR = BackColor;
3329 emit_light_twoside(struct svga_shader_emitter
*emit
)
3331 struct src_register vface
, zero
;
3332 struct src_register front
[2];
3333 struct src_register back
[2];
3334 SVGA3dShaderDestToken color
[2];
3335 int count
= emit
->internal_color_count
;
3337 SVGA3dShaderInstToken if_token
;
3342 vface
= get_vface( emit
);
3343 zero
= get_zero_immediate( emit
);
3345 /* Can't use get_temp() to allocate the color reg as such
3346 * temporaries will be reclaimed after each instruction by the call
3347 * to reset_temp_regs().
3349 for (i
= 0; i
< count
; i
++) {
3350 color
[i
] = dst_register( SVGA3DREG_TEMP
, emit
->nr_hw_temp
++ );
3351 front
[i
] = emit
->input_map
[emit
->internal_color_idx
[i
]];
3353 /* Back is always the next input:
3356 back
[i
].base
.num
= front
[i
].base
.num
+ 1;
3358 /* Reassign the input_map to the actual front-face color:
3360 emit
->input_map
[emit
->internal_color_idx
[i
]] = src(color
[i
]);
3363 if_token
= inst_token( SVGA3DOP_IFC
);
3365 if (emit
->key
.fkey
.front_ccw
)
3366 if_token
.control
= SVGA3DOPCOMP_LT
;
3368 if_token
.control
= SVGA3DOPCOMP_GT
;
3370 zero
= scalar(zero
, TGSI_SWIZZLE_X
);
3372 if (!(emit_instruction( emit
, if_token
) &&
3373 emit_src( emit
, vface
) &&
3374 emit_src( emit
, zero
) ))
3377 for (i
= 0; i
< count
; i
++) {
3378 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), color
[i
], front
[i
] ))
3382 if (!(emit_instruction( emit
, inst_token( SVGA3DOP_ELSE
))))
3385 for (i
= 0; i
< count
; i
++) {
3386 if (!submit_op1( emit
, inst_token( SVGA3DOP_MOV
), color
[i
], back
[i
] ))
3390 if (!emit_instruction( emit
, inst_token( SVGA3DOP_ENDIF
) ))
3398 * Emit special setup code for the front/back face register in the FS.
3399 * 0: SETP_GT TEMP, VFACE, 0
3400 * where TEMP is a fake frontface register
3403 emit_frontface(struct svga_shader_emitter
*emit
)
3405 struct src_register vface
, zero
;
3406 SVGA3dShaderDestToken temp
;
3407 struct src_register pass
, fail
;
3409 vface
= get_vface( emit
);
3410 zero
= get_zero_immediate( emit
);
3412 /* Can't use get_temp() to allocate the fake frontface reg as such
3413 * temporaries will be reclaimed after each instruction by the call
3414 * to reset_temp_regs().
3416 temp
= dst_register( SVGA3DREG_TEMP
,
3417 emit
->nr_hw_temp
++ );
3419 if (emit
->key
.fkey
.front_ccw
) {
3420 pass
= scalar( zero
, TGSI_SWIZZLE_X
);
3421 fail
= scalar( zero
, TGSI_SWIZZLE_W
);
3423 pass
= scalar( zero
, TGSI_SWIZZLE_W
);
3424 fail
= scalar( zero
, TGSI_SWIZZLE_X
);
3427 if (!emit_conditional(emit
, PIPE_FUNC_GREATER
,
3428 temp
, vface
, scalar( zero
, TGSI_SWIZZLE_X
),
3432 /* Reassign the input_map to the actual front-face color:
3434 emit
->input_map
[emit
->internal_frontface_idx
] = src(temp
);
3441 * Emit code to invert the T component of the incoming texture coordinate.
3442 * This is used for drawing point sprites when
3443 * pipe_rasterizer_state::sprite_coord_mode == PIPE_SPRITE_COORD_LOWER_LEFT.
3446 emit_inverted_texcoords(struct svga_shader_emitter
*emit
)
3448 struct src_register zero
= get_zero_immediate(emit
);
3449 struct src_register pos_neg_one
= get_pos_neg_one_immediate( emit
);
3450 unsigned inverted_texcoords
= emit
->inverted_texcoords
;
3452 while (inverted_texcoords
) {
3453 const unsigned unit
= ffs(inverted_texcoords
) - 1;
3455 assert(emit
->inverted_texcoords
& (1 << unit
));
3457 assert(unit
< Elements(emit
->ps_true_texcoord
));
3459 assert(unit
< Elements(emit
->ps_inverted_texcoord_input
));
3461 assert(emit
->ps_inverted_texcoord_input
[unit
]
3462 < Elements(emit
->input_map
));
3464 /* inverted = coord * (1, -1, 1, 1) + (0, 1, 0, 0) */
3465 if (!submit_op3(emit
,
3466 inst_token(SVGA3DOP_MAD
),
3467 dst(emit
->ps_inverted_texcoord
[unit
]),
3468 emit
->ps_true_texcoord
[unit
],
3469 swizzle(pos_neg_one
, 0, 3, 0, 0), /* (1, -1, 1, 1) */
3470 swizzle(zero
, 0, 3, 0, 0))) /* (0, 1, 0, 0) */
3473 /* Reassign the input_map entry to the new texcoord register */
3474 emit
->input_map
[emit
->ps_inverted_texcoord_input
[unit
]] =
3475 emit
->ps_inverted_texcoord
[unit
];
3477 inverted_texcoords
&= ~(1 << unit
);
3485 * Determine if we need to emit an immediate value with zeros.
3486 * We could just do this all the time except that we want to conserve
3487 * registers whenever possible.
3490 needs_to_create_zero(const struct svga_shader_emitter
*emit
)
3494 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
3495 if (emit
->key
.fkey
.light_twoside
)
3498 if (emit
->key
.fkey
.white_fragments
)
3501 if (emit
->emit_frontface
)
3504 if (emit
->info
.opcode_count
[TGSI_OPCODE_DST
] >= 1 ||
3505 emit
->info
.opcode_count
[TGSI_OPCODE_SSG
] >= 1 ||
3506 emit
->info
.opcode_count
[TGSI_OPCODE_LIT
] >= 1)
3509 if (emit
->inverted_texcoords
)
3512 /* look for any PIPE_SWIZZLE_ZERO/ONE terms */
3513 for (i
= 0; i
< emit
->key
.fkey
.num_textures
; i
++) {
3514 if (emit
->key
.fkey
.tex
[i
].swizzle_r
> PIPE_SWIZZLE_ALPHA
||
3515 emit
->key
.fkey
.tex
[i
].swizzle_g
> PIPE_SWIZZLE_ALPHA
||
3516 emit
->key
.fkey
.tex
[i
].swizzle_b
> PIPE_SWIZZLE_ALPHA
||
3517 emit
->key
.fkey
.tex
[i
].swizzle_a
> PIPE_SWIZZLE_ALPHA
)
3521 for (i
= 0; i
< emit
->key
.fkey
.num_textures
; i
++) {
3522 if (emit
->key
.fkey
.tex
[i
].compare_mode
3523 == PIPE_TEX_COMPARE_R_TO_TEXTURE
)
3528 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
3529 if (emit
->info
.opcode_count
[TGSI_OPCODE_CMP
] >= 1)
3533 if (emit
->info
.opcode_count
[TGSI_OPCODE_IF
] >= 1 ||
3534 emit
->info
.opcode_count
[TGSI_OPCODE_BGNLOOP
] >= 1 ||
3535 emit
->info
.opcode_count
[TGSI_OPCODE_DDX
] >= 1 ||
3536 emit
->info
.opcode_count
[TGSI_OPCODE_DDY
] >= 1 ||
3537 emit
->info
.opcode_count
[TGSI_OPCODE_ROUND
] >= 1 ||
3538 emit
->info
.opcode_count
[TGSI_OPCODE_SGE
] >= 1 ||
3539 emit
->info
.opcode_count
[TGSI_OPCODE_SGT
] >= 1 ||
3540 emit
->info
.opcode_count
[TGSI_OPCODE_SLE
] >= 1 ||
3541 emit
->info
.opcode_count
[TGSI_OPCODE_SLT
] >= 1 ||
3542 emit
->info
.opcode_count
[TGSI_OPCODE_SNE
] >= 1 ||
3543 emit
->info
.opcode_count
[TGSI_OPCODE_SEQ
] >= 1 ||
3544 emit
->info
.opcode_count
[TGSI_OPCODE_EXP
] >= 1 ||
3545 emit
->info
.opcode_count
[TGSI_OPCODE_LOG
] >= 1 ||
3546 emit
->info
.opcode_count
[TGSI_OPCODE_XPD
] >= 1 ||
3547 emit
->info
.opcode_count
[TGSI_OPCODE_KILL
] >= 1)
3555 * Do we need to create a looping constant?
3558 needs_to_create_loop_const(const struct svga_shader_emitter
*emit
)
3560 return (emit
->info
.opcode_count
[TGSI_OPCODE_BGNLOOP
] >= 1);
3565 needs_to_create_arl_consts(const struct svga_shader_emitter
*emit
)
3567 return (emit
->num_arl_consts
> 0);
3572 pre_parse_add_indirect( struct svga_shader_emitter
*emit
,
3573 int num
, int current_arl
)
3578 for (i
= 0; i
< emit
->num_arl_consts
; ++i
) {
3579 if (emit
->arl_consts
[i
].arl_num
== current_arl
)
3583 if (emit
->num_arl_consts
== i
) {
3584 ++emit
->num_arl_consts
;
3586 emit
->arl_consts
[i
].number
= (emit
->arl_consts
[i
].number
> num
) ?
3588 emit
->arl_consts
[i
].number
;
3589 emit
->arl_consts
[i
].arl_num
= current_arl
;
3595 pre_parse_instruction( struct svga_shader_emitter
*emit
,
3596 const struct tgsi_full_instruction
*insn
,
3599 if (insn
->Src
[0].Register
.Indirect
&&
3600 insn
->Src
[0].Indirect
.File
== TGSI_FILE_ADDRESS
) {
3601 const struct tgsi_full_src_register
*reg
= &insn
->Src
[0];
3602 if (reg
->Register
.Index
< 0) {
3603 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
3607 if (insn
->Src
[1].Register
.Indirect
&&
3608 insn
->Src
[1].Indirect
.File
== TGSI_FILE_ADDRESS
) {
3609 const struct tgsi_full_src_register
*reg
= &insn
->Src
[1];
3610 if (reg
->Register
.Index
< 0) {
3611 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
3615 if (insn
->Src
[2].Register
.Indirect
&&
3616 insn
->Src
[2].Indirect
.File
== TGSI_FILE_ADDRESS
) {
3617 const struct tgsi_full_src_register
*reg
= &insn
->Src
[2];
3618 if (reg
->Register
.Index
< 0) {
3619 pre_parse_add_indirect(emit
, reg
->Register
.Index
, current_arl
);
3628 pre_parse_tokens( struct svga_shader_emitter
*emit
,
3629 const struct tgsi_token
*tokens
)
3631 struct tgsi_parse_context parse
;
3632 int current_arl
= 0;
3634 tgsi_parse_init( &parse
, tokens
);
3636 while (!tgsi_parse_end_of_tokens( &parse
)) {
3637 tgsi_parse_token( &parse
);
3638 switch (parse
.FullToken
.Token
.Type
) {
3639 case TGSI_TOKEN_TYPE_IMMEDIATE
:
3640 case TGSI_TOKEN_TYPE_DECLARATION
:
3642 case TGSI_TOKEN_TYPE_INSTRUCTION
:
3643 if (parse
.FullToken
.FullInstruction
.Instruction
.Opcode
==
3647 if (!pre_parse_instruction( emit
, &parse
.FullToken
.FullInstruction
,
3661 svga_shader_emit_helpers(struct svga_shader_emitter
*emit
)
3663 if (needs_to_create_zero( emit
)) {
3664 create_zero_immediate( emit
);
3666 if (needs_to_create_loop_const( emit
)) {
3667 create_loop_const( emit
);
3669 if (needs_to_create_arl_consts( emit
)) {
3670 create_arl_consts( emit
);
3673 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
3674 if (!emit_ps_preamble( emit
))
3677 if (emit
->key
.fkey
.light_twoside
) {
3678 if (!emit_light_twoside( emit
))
3681 if (emit
->emit_frontface
) {
3682 if (!emit_frontface( emit
))
3685 if (emit
->inverted_texcoords
) {
3686 if (!emit_inverted_texcoords( emit
))
3696 * This is the main entrypoint into the TGSI instruction translater.
3697 * Translate TGSI shader tokens into an SVGA shader.
3700 svga_shader_emit_instructions(struct svga_shader_emitter
*emit
,
3701 const struct tgsi_token
*tokens
)
3703 struct tgsi_parse_context parse
;
3705 boolean helpers_emitted
= FALSE
;
3706 unsigned line_nr
= 0;
3708 tgsi_parse_init( &parse
, tokens
);
3709 emit
->internal_imm_count
= 0;
3711 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
3712 ret
= emit_vs_preamble( emit
);
3717 pre_parse_tokens(emit
, tokens
);
3719 while (!tgsi_parse_end_of_tokens( &parse
)) {
3720 tgsi_parse_token( &parse
);
3722 switch (parse
.FullToken
.Token
.Type
) {
3723 case TGSI_TOKEN_TYPE_IMMEDIATE
:
3724 ret
= svga_emit_immediate( emit
, &parse
.FullToken
.FullImmediate
);
3729 case TGSI_TOKEN_TYPE_DECLARATION
:
3730 ret
= svga_translate_decl_sm30( emit
, &parse
.FullToken
.FullDeclaration
);
3735 case TGSI_TOKEN_TYPE_INSTRUCTION
:
3736 if (!helpers_emitted
) {
3737 if (!svga_shader_emit_helpers( emit
))
3739 helpers_emitted
= TRUE
;
3741 ret
= svga_emit_instruction( emit
,
3743 &parse
.FullToken
.FullInstruction
);
3751 reset_temp_regs( emit
);
3754 /* Need to terminate the current subroutine. Note that the
3755 * hardware doesn't tolerate shaders without sub-routines
3756 * terminating with RET+END.
3758 if (!emit
->in_main_func
) {
3759 ret
= emit_instruction( emit
, inst_token( SVGA3DOP_RET
) );
3764 assert(emit
->dynamic_branching_level
== 0);
3766 /* Need to terminate the whole shader:
3768 ret
= emit_instruction( emit
, inst_token( SVGA3DOP_END
) );
3773 tgsi_parse_free( &parse
);