1 /**********************************************************
2 * Copyright 1998-2013 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 * @file svga_tgsi_vgpu10.c
29 * TGSI -> VGPU10 shader translation.
31 * \author Mingcheng Chen
35 #include "pipe/p_compiler.h"
36 #include "pipe/p_shader_tokens.h"
37 #include "pipe/p_defines.h"
38 #include "tgsi/tgsi_build.h"
39 #include "tgsi/tgsi_dump.h"
40 #include "tgsi/tgsi_info.h"
41 #include "tgsi/tgsi_parse.h"
42 #include "tgsi/tgsi_scan.h"
43 #include "tgsi/tgsi_two_side.h"
44 #include "tgsi/tgsi_aa_point.h"
45 #include "tgsi/tgsi_util.h"
46 #include "util/u_math.h"
47 #include "util/u_memory.h"
48 #include "util/u_bitmask.h"
49 #include "util/u_debug.h"
50 #include "util/u_pstipple.h"
52 #include "svga_context.h"
53 #include "svga_debug.h"
54 #include "svga_link.h"
55 #include "svga_shader.h"
56 #include "svga_tgsi.h"
58 #include "VGPU10ShaderTokens.h"
61 #define INVALID_INDEX 99999
62 #define MAX_INTERNAL_TEMPS 3
63 #define MAX_SYSTEM_VALUES 4
64 #define MAX_IMMEDIATE_COUNT \
65 (VGPU10_MAX_IMMEDIATE_CONSTANT_BUFFER_ELEMENT_COUNT/4)
66 #define MAX_TEMP_ARRAYS 64 /* Enough? */
70 * Clipping is complicated. There's four different cases which we
71 * handle during VS/GS shader translation:
75 CLIP_NONE
, /**< No clipping enabled */
76 CLIP_LEGACY
, /**< The shader has no clipping declarations or code but
77 * one or more user-defined clip planes are enabled. We
78 * generate extra code to emit clip distances.
80 CLIP_DISTANCE
, /**< The shader already declares clip distance output
81 * registers and has code to write to them.
83 CLIP_VERTEX
/**< The shader declares a clip vertex output register and
84 * has code that writes to the register. We convert the
85 * clipvertex position into one or more clip distances.
90 struct svga_shader_emitter_v10
92 /* The token output buffer */
97 /* Information about the shader and state (does not change) */
98 struct svga_compile_key key
;
99 struct tgsi_shader_info info
;
102 unsigned inst_start_token
;
103 boolean discard_instruction
; /**< throw away current instruction? */
105 union tgsi_immediate_data immediates
[MAX_IMMEDIATE_COUNT
][4];
106 unsigned num_immediates
; /**< Number of immediates emitted */
107 unsigned common_immediate_pos
[8]; /**< literals for common immediates */
108 unsigned num_common_immediates
;
109 boolean immediates_emitted
;
111 unsigned num_outputs
; /**< include any extra outputs */
112 /** The first extra output is reserved for
113 * non-adjusted vertex position for
114 * stream output purpose
117 /* Temporary Registers */
118 unsigned num_shader_temps
; /**< num of temps used by original shader */
119 unsigned internal_temp_count
; /**< currently allocated internal temps */
121 unsigned start
, size
;
122 } temp_arrays
[MAX_TEMP_ARRAYS
];
123 unsigned num_temp_arrays
;
125 /** Map TGSI temp registers to VGPU10 temp array IDs and indexes */
127 unsigned arrayId
, index
;
128 } temp_map
[VGPU10_MAX_TEMPS
]; /**< arrayId, element */
130 /** Number of constants used by original shader for each constant buffer.
131 * The size should probably always match with that of svga_state.constbufs.
133 unsigned num_shader_consts
[SVGA_MAX_CONST_BUFS
];
136 unsigned num_samplers
;
138 /* Address regs (really implemented with temps) */
139 unsigned num_address_regs
;
140 unsigned address_reg_index
[MAX_VGPU10_ADDR_REGS
];
142 /* Output register usage masks */
143 ubyte output_usage_mask
[PIPE_MAX_SHADER_OUTPUTS
];
145 /* To map TGSI system value index to VGPU shader input indexes */
146 ubyte system_value_indexes
[MAX_SYSTEM_VALUES
];
149 /* vertex position scale/translation */
150 unsigned out_index
; /**< the real position output reg */
151 unsigned tmp_index
; /**< the fake/temp position output reg */
152 unsigned so_index
; /**< the non-adjusted position output reg */
153 unsigned prescale_scale_index
, prescale_trans_index
;
154 boolean need_prescale
;
157 /* For vertex shaders only */
159 /* viewport constant */
160 unsigned viewport_index
;
162 /* temp index of adjusted vertex attributes */
163 unsigned adjusted_input
[PIPE_MAX_SHADER_INPUTS
];
166 /* For fragment shaders only */
169 unsigned color_out_index
[PIPE_MAX_COLOR_BUFS
]; /**< the real color output regs */
170 unsigned color_tmp_index
; /**< fake/temp color output reg */
171 unsigned alpha_ref_index
; /**< immediate constant for alpha ref */
174 unsigned face_input_index
; /**< real fragment shader face reg (bool) */
175 unsigned face_tmp_index
; /**< temp face reg converted to -1 / +1 */
177 unsigned pstipple_sampler_unit
;
179 unsigned fragcoord_input_index
; /**< real fragment position input reg */
180 unsigned fragcoord_tmp_index
; /**< 1/w modified position temp reg */
183 /* For geometry shaders only */
185 VGPU10_PRIMITIVE prim_type
;/**< VGPU10 primitive type */
186 VGPU10_PRIMITIVE_TOPOLOGY prim_topology
; /**< VGPU10 primitive topology */
187 unsigned input_size
; /**< size of input arrays */
188 unsigned prim_id_index
; /**< primitive id register index */
189 unsigned max_out_vertices
; /**< maximum number of output vertices */
192 /* For vertex or geometry shaders */
193 enum clipping_mode clip_mode
;
194 unsigned clip_dist_out_index
; /**< clip distance output register index */
195 unsigned clip_dist_tmp_index
; /**< clip distance temporary register */
196 unsigned clip_dist_so_index
; /**< clip distance shadow copy */
198 /** Index of temporary holding the clipvertex coordinate */
199 unsigned clip_vertex_out_index
; /**< clip vertex output register index */
200 unsigned clip_vertex_tmp_index
; /**< clip vertex temporary index */
202 /* user clip plane constant slot indexes */
203 unsigned clip_plane_const
[PIPE_MAX_CLIP_PLANES
];
205 unsigned num_output_writes
;
206 boolean constant_color_output
;
208 boolean uses_flat_interp
;
210 /* For all shaders: const reg index for RECT coord scaling */
211 unsigned texcoord_scale_index
[PIPE_MAX_SAMPLERS
];
213 /* For all shaders: const reg index for texture buffer size */
214 unsigned texture_buffer_size_index
[PIPE_MAX_SAMPLERS
];
216 /* VS/GS/FS Linkage info */
217 struct shader_linkage linkage
;
219 bool register_overflow
; /**< Set if we exceed a VGPU10 register limit */
224 emit_post_helpers(struct svga_shader_emitter_v10
*emit
);
227 emit_vertex(struct svga_shader_emitter_v10
*emit
,
228 const struct tgsi_full_instruction
*inst
);
230 static char err_buf
[128];
233 expand(struct svga_shader_emitter_v10
*emit
)
236 unsigned newsize
= emit
->size
* 2;
238 if (emit
->buf
!= err_buf
)
239 new_buf
= REALLOC(emit
->buf
, emit
->size
, newsize
);
243 if (new_buf
== NULL
) {
246 emit
->size
= sizeof(err_buf
);
250 emit
->size
= newsize
;
251 emit
->ptr
= new_buf
+ (emit
->ptr
- emit
->buf
);
257 * Create and initialize a new svga_shader_emitter_v10 object.
259 static struct svga_shader_emitter_v10
*
262 struct svga_shader_emitter_v10
*emit
= CALLOC(1, sizeof(*emit
));
267 /* to initialize the output buffer */
277 * Free an svga_shader_emitter_v10 object.
280 free_emitter(struct svga_shader_emitter_v10
*emit
)
283 FREE(emit
->buf
); /* will be NULL if translation succeeded */
287 static inline boolean
288 reserve(struct svga_shader_emitter_v10
*emit
,
291 while (emit
->ptr
- emit
->buf
+ nr_dwords
* sizeof(uint32
) >= emit
->size
) {
300 emit_dword(struct svga_shader_emitter_v10
*emit
, uint32 dword
)
302 if (!reserve(emit
, 1))
305 *(uint32
*)emit
->ptr
= dword
;
306 emit
->ptr
+= sizeof dword
;
311 emit_dwords(struct svga_shader_emitter_v10
*emit
,
312 const uint32
*dwords
,
315 if (!reserve(emit
, nr
))
318 memcpy(emit
->ptr
, dwords
, nr
* sizeof *dwords
);
319 emit
->ptr
+= nr
* sizeof *dwords
;
323 /** Return the number of tokens in the emitter's buffer */
325 emit_get_num_tokens(const struct svga_shader_emitter_v10
*emit
)
327 return (emit
->ptr
- emit
->buf
) / sizeof(unsigned);
332 * Check for register overflow. If we overflow we'll set an
333 * error flag. This function can be called for register declarations
334 * or use as src/dst instruction operands.
335 * \param type register type. One of VGPU10_OPERAND_TYPE_x
336 or VGPU10_OPCODE_DCL_x
337 * \param index the register index
340 check_register_index(struct svga_shader_emitter_v10
*emit
,
341 unsigned operandType
, unsigned index
)
343 bool overflow_before
= emit
->register_overflow
;
345 switch (operandType
) {
346 case VGPU10_OPERAND_TYPE_TEMP
:
347 case VGPU10_OPERAND_TYPE_INDEXABLE_TEMP
:
348 case VGPU10_OPCODE_DCL_TEMPS
:
349 if (index
>= VGPU10_MAX_TEMPS
) {
350 emit
->register_overflow
= TRUE
;
353 case VGPU10_OPERAND_TYPE_CONSTANT_BUFFER
:
354 case VGPU10_OPCODE_DCL_CONSTANT_BUFFER
:
355 if (index
>= VGPU10_MAX_CONSTANT_BUFFER_ELEMENT_COUNT
) {
356 emit
->register_overflow
= TRUE
;
359 case VGPU10_OPERAND_TYPE_INPUT
:
360 case VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID
:
361 case VGPU10_OPCODE_DCL_INPUT
:
362 case VGPU10_OPCODE_DCL_INPUT_SGV
:
363 case VGPU10_OPCODE_DCL_INPUT_SIV
:
364 case VGPU10_OPCODE_DCL_INPUT_PS
:
365 case VGPU10_OPCODE_DCL_INPUT_PS_SGV
:
366 case VGPU10_OPCODE_DCL_INPUT_PS_SIV
:
367 if ((emit
->unit
== PIPE_SHADER_VERTEX
&&
368 index
>= VGPU10_MAX_VS_INPUTS
) ||
369 (emit
->unit
== PIPE_SHADER_GEOMETRY
&&
370 index
>= VGPU10_MAX_GS_INPUTS
) ||
371 (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
372 index
>= VGPU10_MAX_FS_INPUTS
)) {
373 emit
->register_overflow
= TRUE
;
376 case VGPU10_OPERAND_TYPE_OUTPUT
:
377 case VGPU10_OPCODE_DCL_OUTPUT
:
378 case VGPU10_OPCODE_DCL_OUTPUT_SGV
:
379 case VGPU10_OPCODE_DCL_OUTPUT_SIV
:
380 if ((emit
->unit
== PIPE_SHADER_VERTEX
&&
381 index
>= VGPU10_MAX_VS_OUTPUTS
) ||
382 (emit
->unit
== PIPE_SHADER_GEOMETRY
&&
383 index
>= VGPU10_MAX_GS_OUTPUTS
) ||
384 (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
385 index
>= VGPU10_MAX_FS_OUTPUTS
)) {
386 emit
->register_overflow
= TRUE
;
389 case VGPU10_OPERAND_TYPE_SAMPLER
:
390 case VGPU10_OPCODE_DCL_SAMPLER
:
391 if (index
>= VGPU10_MAX_SAMPLERS
) {
392 emit
->register_overflow
= TRUE
;
395 case VGPU10_OPERAND_TYPE_RESOURCE
:
396 case VGPU10_OPCODE_DCL_RESOURCE
:
397 if (index
>= VGPU10_MAX_RESOURCES
) {
398 emit
->register_overflow
= TRUE
;
401 case VGPU10_OPERAND_TYPE_IMMEDIATE_CONSTANT_BUFFER
:
402 if (index
>= MAX_IMMEDIATE_COUNT
) {
403 emit
->register_overflow
= TRUE
;
411 if (emit
->register_overflow
&& !overflow_before
) {
412 debug_printf("svga: vgpu10 register overflow (reg %u, index %u)\n",
419 * Examine misc state to determine the clipping mode.
422 determine_clipping_mode(struct svga_shader_emitter_v10
*emit
)
424 if (emit
->info
.num_written_clipdistance
> 0) {
425 emit
->clip_mode
= CLIP_DISTANCE
;
427 else if (emit
->info
.writes_clipvertex
) {
428 emit
->clip_mode
= CLIP_VERTEX
;
430 else if (emit
->key
.clip_plane_enable
) {
431 emit
->clip_mode
= CLIP_LEGACY
;
434 emit
->clip_mode
= CLIP_NONE
;
440 * For clip distance register declarations and clip distance register
441 * writes we need to mask the declaration usage or instruction writemask
442 * (respectively) against the set of the really-enabled clipping planes.
444 * The piglit test spec/glsl-1.30/execution/clipping/vs-clip-distance-enables
445 * has a VS that writes to all 8 clip distance registers, but the plane enable
446 * flags are a subset of that.
448 * This function is used to apply the plane enable flags to the register
449 * declaration or instruction writemask.
451 * \param writemask the declaration usage mask or instruction writemask
452 * \param clip_reg_index which clip plane register is being declared/written.
453 * The legal values are 0 and 1 (two clip planes per
454 * register, for a total of 8 clip planes)
457 apply_clip_plane_mask(struct svga_shader_emitter_v10
*emit
,
458 unsigned writemask
, unsigned clip_reg_index
)
462 assert(clip_reg_index
< 2);
464 /* four clip planes per clip register: */
465 shift
= clip_reg_index
* 4;
466 writemask
&= ((emit
->key
.clip_plane_enable
>> shift
) & 0xf);
473 * Translate gallium shader type into VGPU10 type.
475 static VGPU10_PROGRAM_TYPE
476 translate_shader_type(unsigned type
)
479 case PIPE_SHADER_VERTEX
:
480 return VGPU10_VERTEX_SHADER
;
481 case PIPE_SHADER_GEOMETRY
:
482 return VGPU10_GEOMETRY_SHADER
;
483 case PIPE_SHADER_FRAGMENT
:
484 return VGPU10_PIXEL_SHADER
;
486 assert(!"Unexpected shader type");
487 return VGPU10_VERTEX_SHADER
;
493 * Translate a TGSI_OPCODE_x into a VGPU10_OPCODE_x
494 * Note: we only need to translate the opcodes for "simple" instructions,
495 * as seen below. All other opcodes are handled/translated specially.
497 static VGPU10_OPCODE_TYPE
498 translate_opcode(unsigned opcode
)
501 case TGSI_OPCODE_MOV
:
502 return VGPU10_OPCODE_MOV
;
503 case TGSI_OPCODE_MUL
:
504 return VGPU10_OPCODE_MUL
;
505 case TGSI_OPCODE_ADD
:
506 return VGPU10_OPCODE_ADD
;
507 case TGSI_OPCODE_DP3
:
508 return VGPU10_OPCODE_DP3
;
509 case TGSI_OPCODE_DP4
:
510 return VGPU10_OPCODE_DP4
;
511 case TGSI_OPCODE_MIN
:
512 return VGPU10_OPCODE_MIN
;
513 case TGSI_OPCODE_MAX
:
514 return VGPU10_OPCODE_MAX
;
515 case TGSI_OPCODE_MAD
:
516 return VGPU10_OPCODE_MAD
;
517 case TGSI_OPCODE_SQRT
:
518 return VGPU10_OPCODE_SQRT
;
519 case TGSI_OPCODE_FRC
:
520 return VGPU10_OPCODE_FRC
;
521 case TGSI_OPCODE_FLR
:
522 return VGPU10_OPCODE_ROUND_NI
;
523 case TGSI_OPCODE_FSEQ
:
524 return VGPU10_OPCODE_EQ
;
525 case TGSI_OPCODE_FSGE
:
526 return VGPU10_OPCODE_GE
;
527 case TGSI_OPCODE_FSNE
:
528 return VGPU10_OPCODE_NE
;
529 case TGSI_OPCODE_DDX
:
530 return VGPU10_OPCODE_DERIV_RTX
;
531 case TGSI_OPCODE_DDY
:
532 return VGPU10_OPCODE_DERIV_RTY
;
533 case TGSI_OPCODE_RET
:
534 return VGPU10_OPCODE_RET
;
535 case TGSI_OPCODE_DIV
:
536 return VGPU10_OPCODE_DIV
;
537 case TGSI_OPCODE_IDIV
:
538 return VGPU10_OPCODE_IDIV
;
539 case TGSI_OPCODE_DP2
:
540 return VGPU10_OPCODE_DP2
;
541 case TGSI_OPCODE_BRK
:
542 return VGPU10_OPCODE_BREAK
;
544 return VGPU10_OPCODE_IF
;
545 case TGSI_OPCODE_ELSE
:
546 return VGPU10_OPCODE_ELSE
;
547 case TGSI_OPCODE_ENDIF
:
548 return VGPU10_OPCODE_ENDIF
;
549 case TGSI_OPCODE_CEIL
:
550 return VGPU10_OPCODE_ROUND_PI
;
551 case TGSI_OPCODE_I2F
:
552 return VGPU10_OPCODE_ITOF
;
553 case TGSI_OPCODE_NOT
:
554 return VGPU10_OPCODE_NOT
;
555 case TGSI_OPCODE_TRUNC
:
556 return VGPU10_OPCODE_ROUND_Z
;
557 case TGSI_OPCODE_SHL
:
558 return VGPU10_OPCODE_ISHL
;
559 case TGSI_OPCODE_AND
:
560 return VGPU10_OPCODE_AND
;
562 return VGPU10_OPCODE_OR
;
563 case TGSI_OPCODE_XOR
:
564 return VGPU10_OPCODE_XOR
;
565 case TGSI_OPCODE_CONT
:
566 return VGPU10_OPCODE_CONTINUE
;
567 case TGSI_OPCODE_EMIT
:
568 return VGPU10_OPCODE_EMIT
;
569 case TGSI_OPCODE_ENDPRIM
:
570 return VGPU10_OPCODE_CUT
;
571 case TGSI_OPCODE_BGNLOOP
:
572 return VGPU10_OPCODE_LOOP
;
573 case TGSI_OPCODE_ENDLOOP
:
574 return VGPU10_OPCODE_ENDLOOP
;
575 case TGSI_OPCODE_ENDSUB
:
576 return VGPU10_OPCODE_RET
;
577 case TGSI_OPCODE_NOP
:
578 return VGPU10_OPCODE_NOP
;
579 case TGSI_OPCODE_BREAKC
:
580 return VGPU10_OPCODE_BREAKC
;
581 case TGSI_OPCODE_END
:
582 return VGPU10_OPCODE_RET
;
583 case TGSI_OPCODE_F2I
:
584 return VGPU10_OPCODE_FTOI
;
585 case TGSI_OPCODE_IMAX
:
586 return VGPU10_OPCODE_IMAX
;
587 case TGSI_OPCODE_IMIN
:
588 return VGPU10_OPCODE_IMIN
;
589 case TGSI_OPCODE_UDIV
:
590 case TGSI_OPCODE_UMOD
:
591 case TGSI_OPCODE_MOD
:
592 return VGPU10_OPCODE_UDIV
;
593 case TGSI_OPCODE_IMUL_HI
:
594 return VGPU10_OPCODE_IMUL
;
595 case TGSI_OPCODE_INEG
:
596 return VGPU10_OPCODE_INEG
;
597 case TGSI_OPCODE_ISHR
:
598 return VGPU10_OPCODE_ISHR
;
599 case TGSI_OPCODE_ISGE
:
600 return VGPU10_OPCODE_IGE
;
601 case TGSI_OPCODE_ISLT
:
602 return VGPU10_OPCODE_ILT
;
603 case TGSI_OPCODE_F2U
:
604 return VGPU10_OPCODE_FTOU
;
605 case TGSI_OPCODE_UADD
:
606 return VGPU10_OPCODE_IADD
;
607 case TGSI_OPCODE_U2F
:
608 return VGPU10_OPCODE_UTOF
;
609 case TGSI_OPCODE_UCMP
:
610 return VGPU10_OPCODE_MOVC
;
611 case TGSI_OPCODE_UMAD
:
612 return VGPU10_OPCODE_UMAD
;
613 case TGSI_OPCODE_UMAX
:
614 return VGPU10_OPCODE_UMAX
;
615 case TGSI_OPCODE_UMIN
:
616 return VGPU10_OPCODE_UMIN
;
617 case TGSI_OPCODE_UMUL
:
618 case TGSI_OPCODE_UMUL_HI
:
619 return VGPU10_OPCODE_UMUL
;
620 case TGSI_OPCODE_USEQ
:
621 return VGPU10_OPCODE_IEQ
;
622 case TGSI_OPCODE_USGE
:
623 return VGPU10_OPCODE_UGE
;
624 case TGSI_OPCODE_USHR
:
625 return VGPU10_OPCODE_USHR
;
626 case TGSI_OPCODE_USLT
:
627 return VGPU10_OPCODE_ULT
;
628 case TGSI_OPCODE_USNE
:
629 return VGPU10_OPCODE_INE
;
630 case TGSI_OPCODE_SWITCH
:
631 return VGPU10_OPCODE_SWITCH
;
632 case TGSI_OPCODE_CASE
:
633 return VGPU10_OPCODE_CASE
;
634 case TGSI_OPCODE_DEFAULT
:
635 return VGPU10_OPCODE_DEFAULT
;
636 case TGSI_OPCODE_ENDSWITCH
:
637 return VGPU10_OPCODE_ENDSWITCH
;
638 case TGSI_OPCODE_FSLT
:
639 return VGPU10_OPCODE_LT
;
640 case TGSI_OPCODE_ROUND
:
641 return VGPU10_OPCODE_ROUND_NE
;
643 assert(!"Unexpected TGSI opcode in translate_opcode()");
644 return VGPU10_OPCODE_NOP
;
650 * Translate a TGSI register file type into a VGPU10 operand type.
651 * \param array is the TGSI_FILE_TEMPORARY register an array?
653 static VGPU10_OPERAND_TYPE
654 translate_register_file(enum tgsi_file_type file
, boolean array
)
657 case TGSI_FILE_CONSTANT
:
658 return VGPU10_OPERAND_TYPE_CONSTANT_BUFFER
;
659 case TGSI_FILE_INPUT
:
660 return VGPU10_OPERAND_TYPE_INPUT
;
661 case TGSI_FILE_OUTPUT
:
662 return VGPU10_OPERAND_TYPE_OUTPUT
;
663 case TGSI_FILE_TEMPORARY
:
664 return array
? VGPU10_OPERAND_TYPE_INDEXABLE_TEMP
665 : VGPU10_OPERAND_TYPE_TEMP
;
666 case TGSI_FILE_IMMEDIATE
:
667 /* all immediates are 32-bit values at this time so
668 * VGPU10_OPERAND_TYPE_IMMEDIATE64 is not possible at this time.
670 return VGPU10_OPERAND_TYPE_IMMEDIATE_CONSTANT_BUFFER
;
671 case TGSI_FILE_SAMPLER
:
672 return VGPU10_OPERAND_TYPE_SAMPLER
;
673 case TGSI_FILE_SYSTEM_VALUE
:
674 return VGPU10_OPERAND_TYPE_INPUT
;
676 /* XXX TODO more cases to finish */
679 assert(!"Bad tgsi register file!");
680 return VGPU10_OPERAND_TYPE_NULL
;
686 * Emit a null dst register
689 emit_null_dst_register(struct svga_shader_emitter_v10
*emit
)
691 VGPU10OperandToken0 operand
;
694 operand
.operandType
= VGPU10_OPERAND_TYPE_NULL
;
695 operand
.numComponents
= VGPU10_OPERAND_0_COMPONENT
;
697 emit_dword(emit
, operand
.value
);
702 * If the given register is a temporary, return the array ID.
706 get_temp_array_id(const struct svga_shader_emitter_v10
*emit
,
707 unsigned file
, unsigned index
)
709 if (file
== TGSI_FILE_TEMPORARY
) {
710 return emit
->temp_map
[index
].arrayId
;
719 * If the given register is a temporary, convert the index from a TGSI
720 * TEMPORARY index to a VGPU10 temp index.
723 remap_temp_index(const struct svga_shader_emitter_v10
*emit
,
724 unsigned file
, unsigned index
)
726 if (file
== TGSI_FILE_TEMPORARY
) {
727 return emit
->temp_map
[index
].index
;
736 * Setup the operand0 fields related to indexing (1D, 2D, relative, etc).
737 * Note: the operandType field must already be initialized.
739 static VGPU10OperandToken0
740 setup_operand0_indexing(struct svga_shader_emitter_v10
*emit
,
741 VGPU10OperandToken0 operand0
,
743 boolean indirect
, boolean index2D
,
744 unsigned tempArrayID
)
746 unsigned indexDim
, index0Rep
, index1Rep
= VGPU10_OPERAND_INDEX_0D
;
749 * Compute index dimensions
751 if (operand0
.operandType
== VGPU10_OPERAND_TYPE_IMMEDIATE32
||
752 operand0
.operandType
== VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID
) {
753 /* there's no swizzle for in-line immediates */
754 indexDim
= VGPU10_OPERAND_INDEX_0D
;
755 assert(operand0
.selectionMode
== 0);
760 operand0
.operandType
== VGPU10_OPERAND_TYPE_CONSTANT_BUFFER
) {
761 indexDim
= VGPU10_OPERAND_INDEX_2D
;
764 indexDim
= VGPU10_OPERAND_INDEX_1D
;
769 * Compute index representations (immediate, relative, etc).
771 if (tempArrayID
> 0) {
772 assert(file
== TGSI_FILE_TEMPORARY
);
773 /* First index is the array ID, second index is the array element */
774 index0Rep
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
776 index1Rep
= VGPU10_OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE
;
779 index1Rep
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
783 if (file
== TGSI_FILE_CONSTANT
) {
784 /* index[0] indicates which constant buffer while index[1] indicates
785 * the position in the constant buffer.
787 index0Rep
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
788 index1Rep
= VGPU10_OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE
;
791 /* All other register files are 1-dimensional */
792 index0Rep
= VGPU10_OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE
;
796 index0Rep
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
797 index1Rep
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
800 operand0
.indexDimension
= indexDim
;
801 operand0
.index0Representation
= index0Rep
;
802 operand0
.index1Representation
= index1Rep
;
809 * Emit the operand for expressing an address register for indirect indexing.
810 * Note that the address register is really just a temp register.
811 * \param addr_reg_index which address register to use
814 emit_indirect_register(struct svga_shader_emitter_v10
*emit
,
815 unsigned addr_reg_index
)
817 unsigned tmp_reg_index
;
818 VGPU10OperandToken0 operand0
;
820 assert(addr_reg_index
< MAX_VGPU10_ADDR_REGS
);
822 tmp_reg_index
= emit
->address_reg_index
[addr_reg_index
];
824 /* operand0 is a simple temporary register, selecting one component */
826 operand0
.operandType
= VGPU10_OPERAND_TYPE_TEMP
;
827 operand0
.numComponents
= VGPU10_OPERAND_4_COMPONENT
;
828 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_1D
;
829 operand0
.index0Representation
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
830 operand0
.selectionMode
= VGPU10_OPERAND_4_COMPONENT_SELECT_1_MODE
;
831 operand0
.swizzleX
= 0;
832 operand0
.swizzleY
= 1;
833 operand0
.swizzleZ
= 2;
834 operand0
.swizzleW
= 3;
836 emit_dword(emit
, operand0
.value
);
837 emit_dword(emit
, remap_temp_index(emit
, TGSI_FILE_TEMPORARY
, tmp_reg_index
));
842 * Translate the dst register of a TGSI instruction and emit VGPU10 tokens.
843 * \param emit the emitter context
844 * \param reg the TGSI dst register to translate
847 emit_dst_register(struct svga_shader_emitter_v10
*emit
,
848 const struct tgsi_full_dst_register
*reg
)
850 unsigned file
= reg
->Register
.File
;
851 unsigned index
= reg
->Register
.Index
;
852 const unsigned sem_name
= emit
->info
.output_semantic_name
[index
];
853 const unsigned sem_index
= emit
->info
.output_semantic_index
[index
];
854 unsigned writemask
= reg
->Register
.WriteMask
;
855 const unsigned indirect
= reg
->Register
.Indirect
;
856 const unsigned tempArrayId
= get_temp_array_id(emit
, file
, index
);
857 const unsigned index2d
= reg
->Register
.Dimension
;
858 VGPU10OperandToken0 operand0
;
860 if (file
== TGSI_FILE_OUTPUT
) {
861 if (emit
->unit
== PIPE_SHADER_VERTEX
||
862 emit
->unit
== PIPE_SHADER_GEOMETRY
) {
863 if (index
== emit
->vposition
.out_index
&&
864 emit
->vposition
.tmp_index
!= INVALID_INDEX
) {
865 /* replace OUTPUT[POS] with TEMP[POS]. We need to store the
866 * vertex position result in a temporary so that we can modify
867 * it in the post_helper() code.
869 file
= TGSI_FILE_TEMPORARY
;
870 index
= emit
->vposition
.tmp_index
;
872 else if (sem_name
== TGSI_SEMANTIC_CLIPDIST
&&
873 emit
->clip_dist_tmp_index
!= INVALID_INDEX
) {
874 /* replace OUTPUT[CLIPDIST] with TEMP[CLIPDIST].
875 * We store the clip distance in a temporary first, then
876 * we'll copy it to the shadow copy and to CLIPDIST with the
877 * enabled planes mask in emit_clip_distance_instructions().
879 file
= TGSI_FILE_TEMPORARY
;
880 index
= emit
->clip_dist_tmp_index
+ sem_index
;
882 else if (sem_name
== TGSI_SEMANTIC_CLIPVERTEX
&&
883 emit
->clip_vertex_tmp_index
!= INVALID_INDEX
) {
884 /* replace the CLIPVERTEX output register with a temporary */
885 assert(emit
->clip_mode
== CLIP_VERTEX
);
886 assert(sem_index
== 0);
887 file
= TGSI_FILE_TEMPORARY
;
888 index
= emit
->clip_vertex_tmp_index
;
891 else if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
892 if (sem_name
== TGSI_SEMANTIC_POSITION
) {
893 /* Fragment depth output register */
895 operand0
.operandType
= VGPU10_OPERAND_TYPE_OUTPUT_DEPTH
;
896 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_0D
;
897 operand0
.numComponents
= VGPU10_OPERAND_1_COMPONENT
;
898 emit_dword(emit
, operand0
.value
);
901 else if (index
== emit
->fs
.color_out_index
[0] &&
902 emit
->fs
.color_tmp_index
!= INVALID_INDEX
) {
903 /* replace OUTPUT[COLOR] with TEMP[COLOR]. We need to store the
904 * fragment color result in a temporary so that we can read it
905 * it in the post_helper() code.
907 file
= TGSI_FILE_TEMPORARY
;
908 index
= emit
->fs
.color_tmp_index
;
911 /* Typically, for fragment shaders, the output register index
912 * matches the color semantic index. But not when we write to
913 * the fragment depth register. In that case, OUT[0] will be
914 * fragdepth and OUT[1] will be the 0th color output. We need
915 * to use the semantic index for color outputs.
917 assert(sem_name
== TGSI_SEMANTIC_COLOR
);
918 index
= emit
->info
.output_semantic_index
[index
];
920 emit
->num_output_writes
++;
925 /* init operand tokens to all zero */
928 operand0
.numComponents
= VGPU10_OPERAND_4_COMPONENT
;
930 /* the operand has a writemask */
931 operand0
.selectionMode
= VGPU10_OPERAND_4_COMPONENT_MASK_MODE
;
933 /* Which of the four dest components to write to. Note that we can use a
934 * simple assignment here since TGSI writemasks match VGPU10 writemasks.
936 STATIC_ASSERT(TGSI_WRITEMASK_X
== VGPU10_OPERAND_4_COMPONENT_MASK_X
);
937 operand0
.mask
= writemask
;
939 /* translate TGSI register file type to VGPU10 operand type */
940 operand0
.operandType
= translate_register_file(file
, tempArrayId
> 0);
942 check_register_index(emit
, operand0
.operandType
, index
);
944 operand0
= setup_operand0_indexing(emit
, operand0
, file
, indirect
,
945 index2d
, tempArrayId
);
948 emit_dword(emit
, operand0
.value
);
949 if (tempArrayId
> 0) {
950 emit_dword(emit
, tempArrayId
);
953 emit_dword(emit
, remap_temp_index(emit
, file
, index
));
956 emit_indirect_register(emit
, reg
->Indirect
.Index
);
962 * Translate a src register of a TGSI instruction and emit VGPU10 tokens.
965 emit_src_register(struct svga_shader_emitter_v10
*emit
,
966 const struct tgsi_full_src_register
*reg
)
968 unsigned file
= reg
->Register
.File
;
969 unsigned index
= reg
->Register
.Index
;
970 const unsigned indirect
= reg
->Register
.Indirect
;
971 const unsigned tempArrayId
= get_temp_array_id(emit
, file
, index
);
972 const unsigned index2d
= reg
->Register
.Dimension
;
973 const unsigned swizzleX
= reg
->Register
.SwizzleX
;
974 const unsigned swizzleY
= reg
->Register
.SwizzleY
;
975 const unsigned swizzleZ
= reg
->Register
.SwizzleZ
;
976 const unsigned swizzleW
= reg
->Register
.SwizzleW
;
977 const unsigned absolute
= reg
->Register
.Absolute
;
978 const unsigned negate
= reg
->Register
.Negate
;
979 bool is_prim_id
= FALSE
;
981 VGPU10OperandToken0 operand0
;
982 VGPU10OperandToken1 operand1
;
984 if (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
985 file
== TGSI_FILE_INPUT
) {
986 if (index
== emit
->fs
.face_input_index
) {
987 /* Replace INPUT[FACE] with TEMP[FACE] */
988 file
= TGSI_FILE_TEMPORARY
;
989 index
= emit
->fs
.face_tmp_index
;
991 else if (index
== emit
->fs
.fragcoord_input_index
) {
992 /* Replace INPUT[POSITION] with TEMP[POSITION] */
993 file
= TGSI_FILE_TEMPORARY
;
994 index
= emit
->fs
.fragcoord_tmp_index
;
997 /* We remap fragment shader inputs to that FS input indexes
998 * match up with VS/GS output indexes.
1000 index
= emit
->linkage
.input_map
[index
];
1003 else if (emit
->unit
== PIPE_SHADER_GEOMETRY
&&
1004 file
== TGSI_FILE_INPUT
) {
1005 is_prim_id
= (index
== emit
->gs
.prim_id_index
);
1006 index
= emit
->linkage
.input_map
[index
];
1008 else if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1009 if (file
== TGSI_FILE_INPUT
) {
1010 /* if input is adjusted... */
1011 if ((emit
->key
.vs
.adjust_attrib_w_1
|
1012 emit
->key
.vs
.adjust_attrib_itof
|
1013 emit
->key
.vs
.adjust_attrib_utof
|
1014 emit
->key
.vs
.attrib_is_bgra
|
1015 emit
->key
.vs
.attrib_puint_to_snorm
|
1016 emit
->key
.vs
.attrib_puint_to_uscaled
|
1017 emit
->key
.vs
.attrib_puint_to_sscaled
) & (1 << index
)) {
1018 file
= TGSI_FILE_TEMPORARY
;
1019 index
= emit
->vs
.adjusted_input
[index
];
1022 else if (file
== TGSI_FILE_SYSTEM_VALUE
) {
1023 assert(index
< Elements(emit
->system_value_indexes
));
1024 index
= emit
->system_value_indexes
[index
];
1028 operand0
.value
= operand1
.value
= 0;
1031 operand0
.numComponents
= VGPU10_OPERAND_0_COMPONENT
;
1032 operand0
.operandType
= VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID
;
1035 operand0
.numComponents
= VGPU10_OPERAND_4_COMPONENT
;
1036 operand0
.operandType
= translate_register_file(file
, tempArrayId
> 0);
1039 operand0
= setup_operand0_indexing(emit
, operand0
, file
, indirect
,
1040 index2d
, tempArrayId
);
1042 if (operand0
.operandType
!= VGPU10_OPERAND_TYPE_IMMEDIATE32
&&
1043 operand0
.operandType
!= VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID
) {
1044 /* there's no swizzle for in-line immediates */
1045 if (swizzleX
== swizzleY
&&
1046 swizzleX
== swizzleZ
&&
1047 swizzleX
== swizzleW
) {
1048 operand0
.selectionMode
= VGPU10_OPERAND_4_COMPONENT_SELECT_1_MODE
;
1051 operand0
.selectionMode
= VGPU10_OPERAND_4_COMPONENT_SWIZZLE_MODE
;
1054 operand0
.swizzleX
= swizzleX
;
1055 operand0
.swizzleY
= swizzleY
;
1056 operand0
.swizzleZ
= swizzleZ
;
1057 operand0
.swizzleW
= swizzleW
;
1059 if (absolute
|| negate
) {
1060 operand0
.extended
= 1;
1061 operand1
.extendedOperandType
= VGPU10_EXTENDED_OPERAND_MODIFIER
;
1062 if (absolute
&& !negate
)
1063 operand1
.operandModifier
= VGPU10_OPERAND_MODIFIER_ABS
;
1064 if (!absolute
&& negate
)
1065 operand1
.operandModifier
= VGPU10_OPERAND_MODIFIER_NEG
;
1066 if (absolute
&& negate
)
1067 operand1
.operandModifier
= VGPU10_OPERAND_MODIFIER_ABSNEG
;
1071 /* Emit the operand tokens */
1072 emit_dword(emit
, operand0
.value
);
1073 if (operand0
.extended
)
1074 emit_dword(emit
, operand1
.value
);
1076 if (operand0
.operandType
== VGPU10_OPERAND_TYPE_IMMEDIATE32
) {
1077 /* Emit the four float/int in-line immediate values */
1079 assert(index
< Elements(emit
->immediates
));
1080 assert(file
== TGSI_FILE_IMMEDIATE
);
1081 assert(swizzleX
< 4);
1082 assert(swizzleY
< 4);
1083 assert(swizzleZ
< 4);
1084 assert(swizzleW
< 4);
1085 c
= (unsigned *) emit
->immediates
[index
];
1086 emit_dword(emit
, c
[swizzleX
]);
1087 emit_dword(emit
, c
[swizzleY
]);
1088 emit_dword(emit
, c
[swizzleZ
]);
1089 emit_dword(emit
, c
[swizzleW
]);
1091 else if (operand0
.indexDimension
>= VGPU10_OPERAND_INDEX_1D
) {
1092 /* Emit the register index(es) */
1094 operand0
.operandType
== VGPU10_OPERAND_TYPE_CONSTANT_BUFFER
) {
1095 emit_dword(emit
, reg
->Dimension
.Index
);
1098 if (tempArrayId
> 0) {
1099 emit_dword(emit
, tempArrayId
);
1102 emit_dword(emit
, remap_temp_index(emit
, file
, index
));
1105 emit_indirect_register(emit
, reg
->Indirect
.Index
);
1112 * Emit a resource operand (for use with a SAMPLE instruction).
1115 emit_resource_register(struct svga_shader_emitter_v10
*emit
,
1116 unsigned resource_number
)
1118 VGPU10OperandToken0 operand0
;
1120 check_register_index(emit
, VGPU10_OPERAND_TYPE_RESOURCE
, resource_number
);
1125 operand0
.operandType
= VGPU10_OPERAND_TYPE_RESOURCE
;
1126 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_1D
;
1127 operand0
.numComponents
= VGPU10_OPERAND_4_COMPONENT
;
1128 operand0
.selectionMode
= VGPU10_OPERAND_4_COMPONENT_SWIZZLE_MODE
;
1129 operand0
.swizzleX
= VGPU10_COMPONENT_X
;
1130 operand0
.swizzleY
= VGPU10_COMPONENT_Y
;
1131 operand0
.swizzleZ
= VGPU10_COMPONENT_Z
;
1132 operand0
.swizzleW
= VGPU10_COMPONENT_W
;
1134 emit_dword(emit
, operand0
.value
);
1135 emit_dword(emit
, resource_number
);
1140 * Emit a sampler operand (for use with a SAMPLE instruction).
1143 emit_sampler_register(struct svga_shader_emitter_v10
*emit
,
1144 unsigned sampler_number
)
1146 VGPU10OperandToken0 operand0
;
1148 check_register_index(emit
, VGPU10_OPERAND_TYPE_SAMPLER
, sampler_number
);
1153 operand0
.operandType
= VGPU10_OPERAND_TYPE_SAMPLER
;
1154 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_1D
;
1156 emit_dword(emit
, operand0
.value
);
1157 emit_dword(emit
, sampler_number
);
1162 * Emit an operand which reads the IS_FRONT_FACING register.
1165 emit_face_register(struct svga_shader_emitter_v10
*emit
)
1167 VGPU10OperandToken0 operand0
;
1168 unsigned index
= emit
->linkage
.input_map
[emit
->fs
.face_input_index
];
1173 operand0
.operandType
= VGPU10_OPERAND_TYPE_INPUT
;
1174 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_1D
;
1175 operand0
.selectionMode
= VGPU10_OPERAND_4_COMPONENT_SELECT_1_MODE
;
1176 operand0
.numComponents
= VGPU10_OPERAND_4_COMPONENT
;
1178 operand0
.swizzleX
= VGPU10_COMPONENT_X
;
1179 operand0
.swizzleY
= VGPU10_COMPONENT_X
;
1180 operand0
.swizzleZ
= VGPU10_COMPONENT_X
;
1181 operand0
.swizzleW
= VGPU10_COMPONENT_X
;
1183 emit_dword(emit
, operand0
.value
);
1184 emit_dword(emit
, index
);
1189 * Emit the token for a VGPU10 opcode.
1190 * \param saturate clamp result to [0,1]?
1193 emit_opcode(struct svga_shader_emitter_v10
*emit
,
1194 unsigned vgpu10_opcode
, boolean saturate
)
1196 VGPU10OpcodeToken0 token0
;
1198 token0
.value
= 0; /* init all fields to zero */
1199 token0
.opcodeType
= vgpu10_opcode
;
1200 token0
.instructionLength
= 0; /* Filled in by end_emit_instruction() */
1201 token0
.saturate
= saturate
;
1203 emit_dword(emit
, token0
.value
);
1208 * Emit the token for a VGPU10 resinfo instruction.
1209 * \param modifier return type modifier, _uint or _rcpFloat.
1210 * TODO: We may want to remove this parameter if it will
1211 * only ever be used as _uint.
1214 emit_opcode_resinfo(struct svga_shader_emitter_v10
*emit
,
1215 VGPU10_RESINFO_RETURN_TYPE modifier
)
1217 VGPU10OpcodeToken0 token0
;
1219 token0
.value
= 0; /* init all fields to zero */
1220 token0
.opcodeType
= VGPU10_OPCODE_RESINFO
;
1221 token0
.instructionLength
= 0; /* Filled in by end_emit_instruction() */
1222 token0
.resinfoReturnType
= modifier
;
1224 emit_dword(emit
, token0
.value
);
1229 * Emit opcode tokens for a texture sample instruction. Texture instructions
1230 * can be rather complicated (texel offsets, etc) so we have this specialized
1234 emit_sample_opcode(struct svga_shader_emitter_v10
*emit
,
1235 unsigned vgpu10_opcode
, boolean saturate
,
1236 const int offsets
[3])
1238 VGPU10OpcodeToken0 token0
;
1239 VGPU10OpcodeToken1 token1
;
1241 token0
.value
= 0; /* init all fields to zero */
1242 token0
.opcodeType
= vgpu10_opcode
;
1243 token0
.instructionLength
= 0; /* Filled in by end_emit_instruction() */
1244 token0
.saturate
= saturate
;
1246 if (offsets
[0] || offsets
[1] || offsets
[2]) {
1247 assert(offsets
[0] >= VGPU10_MIN_TEXEL_FETCH_OFFSET
);
1248 assert(offsets
[1] >= VGPU10_MIN_TEXEL_FETCH_OFFSET
);
1249 assert(offsets
[2] >= VGPU10_MIN_TEXEL_FETCH_OFFSET
);
1250 assert(offsets
[0] <= VGPU10_MAX_TEXEL_FETCH_OFFSET
);
1251 assert(offsets
[1] <= VGPU10_MAX_TEXEL_FETCH_OFFSET
);
1252 assert(offsets
[2] <= VGPU10_MAX_TEXEL_FETCH_OFFSET
);
1254 token0
.extended
= 1;
1256 token1
.opcodeType
= VGPU10_EXTENDED_OPCODE_SAMPLE_CONTROLS
;
1257 token1
.offsetU
= offsets
[0];
1258 token1
.offsetV
= offsets
[1];
1259 token1
.offsetW
= offsets
[2];
1262 emit_dword(emit
, token0
.value
);
1263 if (token0
.extended
) {
1264 emit_dword(emit
, token1
.value
);
1270 * Emit a DISCARD opcode token.
1271 * If nonzero is set, we'll discard the fragment if the X component is not 0.
1272 * Otherwise, we'll discard the fragment if the X component is 0.
1275 emit_discard_opcode(struct svga_shader_emitter_v10
*emit
, boolean nonzero
)
1277 VGPU10OpcodeToken0 opcode0
;
1280 opcode0
.opcodeType
= VGPU10_OPCODE_DISCARD
;
1282 opcode0
.testBoolean
= VGPU10_INSTRUCTION_TEST_NONZERO
;
1284 emit_dword(emit
, opcode0
.value
);
1289 * We need to call this before we begin emitting a VGPU10 instruction.
1292 begin_emit_instruction(struct svga_shader_emitter_v10
*emit
)
1294 assert(emit
->inst_start_token
== 0);
1295 /* Save location of the instruction's VGPU10OpcodeToken0 token.
1296 * Note, we can't save a pointer because it would become invalid if
1297 * we have to realloc the output buffer.
1299 emit
->inst_start_token
= emit_get_num_tokens(emit
);
1304 * We need to call this after we emit the last token of a VGPU10 instruction.
1305 * This function patches in the opcode token's instructionLength field.
1308 end_emit_instruction(struct svga_shader_emitter_v10
*emit
)
1310 VGPU10OpcodeToken0
*tokens
= (VGPU10OpcodeToken0
*) emit
->buf
;
1311 unsigned inst_length
;
1313 assert(emit
->inst_start_token
> 0);
1315 if (emit
->discard_instruction
) {
1316 /* Back up the emit->ptr to where this instruction started so
1317 * that we discard the current instruction.
1319 emit
->ptr
= (char *) (tokens
+ emit
->inst_start_token
);
1322 /* Compute instruction length and patch that into the start of
1325 inst_length
= emit_get_num_tokens(emit
) - emit
->inst_start_token
;
1327 assert(inst_length
> 0);
1329 tokens
[emit
->inst_start_token
].instructionLength
= inst_length
;
1332 emit
->inst_start_token
= 0; /* reset to zero for error checking */
1333 emit
->discard_instruction
= FALSE
;
1338 * Return index for a free temporary register.
1341 get_temp_index(struct svga_shader_emitter_v10
*emit
)
1343 assert(emit
->internal_temp_count
< MAX_INTERNAL_TEMPS
);
1344 return emit
->num_shader_temps
+ emit
->internal_temp_count
++;
1349 * Release the temporaries which were generated by get_temp_index().
1352 free_temp_indexes(struct svga_shader_emitter_v10
*emit
)
1354 emit
->internal_temp_count
= 0;
1359 * Create a tgsi_full_src_register.
1361 static struct tgsi_full_src_register
1362 make_src_reg(unsigned file
, unsigned index
)
1364 struct tgsi_full_src_register reg
;
1366 memset(®
, 0, sizeof(reg
));
1367 reg
.Register
.File
= file
;
1368 reg
.Register
.Index
= index
;
1369 reg
.Register
.SwizzleX
= TGSI_SWIZZLE_X
;
1370 reg
.Register
.SwizzleY
= TGSI_SWIZZLE_Y
;
1371 reg
.Register
.SwizzleZ
= TGSI_SWIZZLE_Z
;
1372 reg
.Register
.SwizzleW
= TGSI_SWIZZLE_W
;
1378 * Create a tgsi_full_src_register for a temporary.
1380 static struct tgsi_full_src_register
1381 make_src_temp_reg(unsigned index
)
1383 return make_src_reg(TGSI_FILE_TEMPORARY
, index
);
1388 * Create a tgsi_full_src_register for a constant.
1390 static struct tgsi_full_src_register
1391 make_src_const_reg(unsigned index
)
1393 return make_src_reg(TGSI_FILE_CONSTANT
, index
);
1398 * Create a tgsi_full_src_register for an immediate constant.
1400 static struct tgsi_full_src_register
1401 make_src_immediate_reg(unsigned index
)
1403 return make_src_reg(TGSI_FILE_IMMEDIATE
, index
);
1408 * Create a tgsi_full_dst_register.
1410 static struct tgsi_full_dst_register
1411 make_dst_reg(unsigned file
, unsigned index
)
1413 struct tgsi_full_dst_register reg
;
1415 memset(®
, 0, sizeof(reg
));
1416 reg
.Register
.File
= file
;
1417 reg
.Register
.Index
= index
;
1418 reg
.Register
.WriteMask
= TGSI_WRITEMASK_XYZW
;
1424 * Create a tgsi_full_dst_register for a temporary.
1426 static struct tgsi_full_dst_register
1427 make_dst_temp_reg(unsigned index
)
1429 return make_dst_reg(TGSI_FILE_TEMPORARY
, index
);
1434 * Create a tgsi_full_dst_register for an output.
1436 static struct tgsi_full_dst_register
1437 make_dst_output_reg(unsigned index
)
1439 return make_dst_reg(TGSI_FILE_OUTPUT
, index
);
1444 * Create negated tgsi_full_src_register.
1446 static struct tgsi_full_src_register
1447 negate_src(const struct tgsi_full_src_register
*reg
)
1449 struct tgsi_full_src_register neg
= *reg
;
1450 neg
.Register
.Negate
= !reg
->Register
.Negate
;
1455 * Create absolute value of a tgsi_full_src_register.
1457 static struct tgsi_full_src_register
1458 absolute_src(const struct tgsi_full_src_register
*reg
)
1460 struct tgsi_full_src_register absolute
= *reg
;
1461 absolute
.Register
.Absolute
= 1;
1466 /** Return the named swizzle term from the src register */
1467 static inline unsigned
1468 get_swizzle(const struct tgsi_full_src_register
*reg
, unsigned term
)
1471 case TGSI_SWIZZLE_X
:
1472 return reg
->Register
.SwizzleX
;
1473 case TGSI_SWIZZLE_Y
:
1474 return reg
->Register
.SwizzleY
;
1475 case TGSI_SWIZZLE_Z
:
1476 return reg
->Register
.SwizzleZ
;
1477 case TGSI_SWIZZLE_W
:
1478 return reg
->Register
.SwizzleW
;
1480 assert(!"Bad swizzle");
1481 return TGSI_SWIZZLE_X
;
1487 * Create swizzled tgsi_full_src_register.
1489 static struct tgsi_full_src_register
1490 swizzle_src(const struct tgsi_full_src_register
*reg
,
1491 unsigned swizzleX
, unsigned swizzleY
,
1492 unsigned swizzleZ
, unsigned swizzleW
)
1494 struct tgsi_full_src_register swizzled
= *reg
;
1495 /* Note: we swizzle the current swizzle */
1496 swizzled
.Register
.SwizzleX
= get_swizzle(reg
, swizzleX
);
1497 swizzled
.Register
.SwizzleY
= get_swizzle(reg
, swizzleY
);
1498 swizzled
.Register
.SwizzleZ
= get_swizzle(reg
, swizzleZ
);
1499 swizzled
.Register
.SwizzleW
= get_swizzle(reg
, swizzleW
);
1505 * Create swizzled tgsi_full_src_register where all the swizzle
1506 * terms are the same.
1508 static struct tgsi_full_src_register
1509 scalar_src(const struct tgsi_full_src_register
*reg
, unsigned swizzle
)
1511 struct tgsi_full_src_register swizzled
= *reg
;
1512 /* Note: we swizzle the current swizzle */
1513 swizzled
.Register
.SwizzleX
=
1514 swizzled
.Register
.SwizzleY
=
1515 swizzled
.Register
.SwizzleZ
=
1516 swizzled
.Register
.SwizzleW
= get_swizzle(reg
, swizzle
);
1522 * Create new tgsi_full_dst_register with writemask.
1523 * \param mask bitmask of TGSI_WRITEMASK_[XYZW]
1525 static struct tgsi_full_dst_register
1526 writemask_dst(const struct tgsi_full_dst_register
*reg
, unsigned mask
)
1528 struct tgsi_full_dst_register masked
= *reg
;
1529 masked
.Register
.WriteMask
= mask
;
1535 * Check if the register's swizzle is XXXX, YYYY, ZZZZ, or WWWW.
1538 same_swizzle_terms(const struct tgsi_full_src_register
*reg
)
1540 return (reg
->Register
.SwizzleX
== reg
->Register
.SwizzleY
&&
1541 reg
->Register
.SwizzleY
== reg
->Register
.SwizzleZ
&&
1542 reg
->Register
.SwizzleZ
== reg
->Register
.SwizzleW
);
1547 * Search the vector for the value 'x' and return its position.
1550 find_imm_in_vec4(const union tgsi_immediate_data vec
[4],
1551 union tgsi_immediate_data x
)
1554 for (i
= 0; i
< 4; i
++) {
1555 if (vec
[i
].Int
== x
.Int
)
1563 * Helper used by make_immediate_reg(), make_immediate_reg_4().
1566 find_immediate(struct svga_shader_emitter_v10
*emit
,
1567 union tgsi_immediate_data x
, unsigned startIndex
)
1569 const unsigned endIndex
= emit
->num_immediates
;
1572 assert(emit
->immediates_emitted
);
1574 /* Search immediates for x, y, z, w */
1575 for (i
= startIndex
; i
< endIndex
; i
++) {
1576 if (x
.Int
== emit
->immediates
[i
][0].Int
||
1577 x
.Int
== emit
->immediates
[i
][1].Int
||
1578 x
.Int
== emit
->immediates
[i
][2].Int
||
1579 x
.Int
== emit
->immediates
[i
][3].Int
) {
1583 /* Should never try to use an immediate value that wasn't pre-declared */
1584 assert(!"find_immediate() failed!");
1590 * Return a tgsi_full_src_register for an immediate/literal
1591 * union tgsi_immediate_data[4] value.
1592 * Note: the values must have been previously declared/allocated in
1593 * emit_pre_helpers(). And, all of x,y,z,w must be located in the same
1596 static struct tgsi_full_src_register
1597 make_immediate_reg_4(struct svga_shader_emitter_v10
*emit
,
1598 const union tgsi_immediate_data imm
[4])
1600 struct tgsi_full_src_register reg
;
1603 for (i
= 0; i
< emit
->num_common_immediates
; i
++) {
1604 /* search for first component value */
1605 int immpos
= find_immediate(emit
, imm
[0], i
);
1608 assert(immpos
>= 0);
1610 /* find remaining components within the immediate vector */
1611 x
= find_imm_in_vec4(emit
->immediates
[immpos
], imm
[0]);
1612 y
= find_imm_in_vec4(emit
->immediates
[immpos
], imm
[1]);
1613 z
= find_imm_in_vec4(emit
->immediates
[immpos
], imm
[2]);
1614 w
= find_imm_in_vec4(emit
->immediates
[immpos
], imm
[3]);
1616 if (x
>=0 && y
>= 0 && z
>= 0 && w
>= 0) {
1617 /* found them all */
1618 memset(®
, 0, sizeof(reg
));
1619 reg
.Register
.File
= TGSI_FILE_IMMEDIATE
;
1620 reg
.Register
.Index
= immpos
;
1621 reg
.Register
.SwizzleX
= x
;
1622 reg
.Register
.SwizzleY
= y
;
1623 reg
.Register
.SwizzleZ
= z
;
1624 reg
.Register
.SwizzleW
= w
;
1627 /* else, keep searching */
1630 assert(!"Failed to find immediate register!");
1632 /* Just return IMM[0].xxxx */
1633 memset(®
, 0, sizeof(reg
));
1634 reg
.Register
.File
= TGSI_FILE_IMMEDIATE
;
1640 * Return a tgsi_full_src_register for an immediate/literal
1641 * union tgsi_immediate_data value of the form {value, value, value, value}.
1642 * \sa make_immediate_reg_4() regarding allowed values.
1644 static struct tgsi_full_src_register
1645 make_immediate_reg(struct svga_shader_emitter_v10
*emit
,
1646 union tgsi_immediate_data value
)
1648 struct tgsi_full_src_register reg
;
1649 int immpos
= find_immediate(emit
, value
, 0);
1651 assert(immpos
>= 0);
1653 memset(®
, 0, sizeof(reg
));
1654 reg
.Register
.File
= TGSI_FILE_IMMEDIATE
;
1655 reg
.Register
.Index
= immpos
;
1656 reg
.Register
.SwizzleX
=
1657 reg
.Register
.SwizzleY
=
1658 reg
.Register
.SwizzleZ
=
1659 reg
.Register
.SwizzleW
= find_imm_in_vec4(emit
->immediates
[immpos
], value
);
1666 * Return a tgsi_full_src_register for an immediate/literal float[4] value.
1667 * \sa make_immediate_reg_4() regarding allowed values.
1669 static struct tgsi_full_src_register
1670 make_immediate_reg_float4(struct svga_shader_emitter_v10
*emit
,
1671 float x
, float y
, float z
, float w
)
1673 union tgsi_immediate_data imm
[4];
1678 return make_immediate_reg_4(emit
, imm
);
1683 * Return a tgsi_full_src_register for an immediate/literal float value
1684 * of the form {value, value, value, value}.
1685 * \sa make_immediate_reg_4() regarding allowed values.
1687 static struct tgsi_full_src_register
1688 make_immediate_reg_float(struct svga_shader_emitter_v10
*emit
, float value
)
1690 union tgsi_immediate_data imm
;
1692 return make_immediate_reg(emit
, imm
);
1697 * Return a tgsi_full_src_register for an immediate/literal int[4] vector.
1699 static struct tgsi_full_src_register
1700 make_immediate_reg_int4(struct svga_shader_emitter_v10
*emit
,
1701 int x
, int y
, int z
, int w
)
1703 union tgsi_immediate_data imm
[4];
1708 return make_immediate_reg_4(emit
, imm
);
1713 * Return a tgsi_full_src_register for an immediate/literal int value
1714 * of the form {value, value, value, value}.
1715 * \sa make_immediate_reg_4() regarding allowed values.
1717 static struct tgsi_full_src_register
1718 make_immediate_reg_int(struct svga_shader_emitter_v10
*emit
, int value
)
1720 union tgsi_immediate_data imm
;
1722 return make_immediate_reg(emit
, imm
);
1727 * Allocate space for a union tgsi_immediate_data[4] immediate.
1728 * \return the index/position of the immediate.
1731 alloc_immediate_4(struct svga_shader_emitter_v10
*emit
,
1732 const union tgsi_immediate_data imm
[4])
1734 unsigned n
= emit
->num_immediates
++;
1735 assert(!emit
->immediates_emitted
);
1736 assert(n
< Elements(emit
->immediates
));
1737 emit
->immediates
[n
][0] = imm
[0];
1738 emit
->immediates
[n
][1] = imm
[1];
1739 emit
->immediates
[n
][2] = imm
[2];
1740 emit
->immediates
[n
][3] = imm
[3];
1746 * Allocate space for a float[4] immediate.
1747 * \return the index/position of the immediate.
1750 alloc_immediate_float4(struct svga_shader_emitter_v10
*emit
,
1751 float x
, float y
, float z
, float w
)
1753 union tgsi_immediate_data imm
[4];
1758 return alloc_immediate_4(emit
, imm
);
1763 * Allocate space for a int[4] immediate.
1764 * \return the index/position of the immediate.
1767 alloc_immediate_int4(struct svga_shader_emitter_v10
*emit
,
1768 int x
, int y
, int z
, int w
)
1770 union tgsi_immediate_data imm
[4];
1775 return alloc_immediate_4(emit
, imm
);
1780 * Allocate a shader input to store a system value.
1783 alloc_system_value_index(struct svga_shader_emitter_v10
*emit
, unsigned index
)
1785 const unsigned n
= emit
->info
.num_inputs
+ index
;
1786 assert(index
< Elements(emit
->system_value_indexes
));
1787 emit
->system_value_indexes
[index
] = n
;
1793 * Translate a TGSI immediate value (union tgsi_immediate_data[4]) to VGPU10.
1796 emit_vgpu10_immediate(struct svga_shader_emitter_v10
*emit
,
1797 const struct tgsi_full_immediate
*imm
)
1799 /* We don't actually emit any code here. We just save the
1800 * immediate values and emit them later.
1802 alloc_immediate_4(emit
, imm
->u
);
1808 * Emit a VGPU10_CUSTOMDATA_DCL_IMMEDIATE_CONSTANT_BUFFER block
1809 * containing all the immediate values previously allocated
1810 * with alloc_immediate_4().
1813 emit_vgpu10_immediates_block(struct svga_shader_emitter_v10
*emit
)
1815 VGPU10OpcodeToken0 token
;
1817 assert(!emit
->immediates_emitted
);
1820 token
.opcodeType
= VGPU10_OPCODE_CUSTOMDATA
;
1821 token
.customDataClass
= VGPU10_CUSTOMDATA_DCL_IMMEDIATE_CONSTANT_BUFFER
;
1823 /* Note: no begin/end_emit_instruction() calls */
1824 emit_dword(emit
, token
.value
);
1825 emit_dword(emit
, 2 + 4 * emit
->num_immediates
);
1826 emit_dwords(emit
, (unsigned *) emit
->immediates
, 4 * emit
->num_immediates
);
1828 emit
->immediates_emitted
= TRUE
;
1835 * Translate a fragment shader's TGSI_INTERPOLATE_x mode to a vgpu10
1836 * interpolation mode.
1837 * \return a VGPU10_INTERPOLATION_x value
1840 translate_interpolation(const struct svga_shader_emitter_v10
*emit
,
1841 unsigned interp
, unsigned interpolate_loc
)
1843 if (interp
== TGSI_INTERPOLATE_COLOR
) {
1844 interp
= emit
->key
.fs
.flatshade
?
1845 TGSI_INTERPOLATE_CONSTANT
: TGSI_INTERPOLATE_PERSPECTIVE
;
1849 case TGSI_INTERPOLATE_CONSTANT
:
1850 return VGPU10_INTERPOLATION_CONSTANT
;
1851 case TGSI_INTERPOLATE_LINEAR
:
1852 return interpolate_loc
== TGSI_INTERPOLATE_LOC_CENTROID
?
1853 VGPU10_INTERPOLATION_LINEAR_NOPERSPECTIVE_CENTROID
:
1854 VGPU10_INTERPOLATION_LINEAR_NOPERSPECTIVE
;
1855 case TGSI_INTERPOLATE_PERSPECTIVE
:
1856 return interpolate_loc
== TGSI_INTERPOLATE_LOC_CENTROID
?
1857 VGPU10_INTERPOLATION_LINEAR_CENTROID
:
1858 VGPU10_INTERPOLATION_LINEAR
;
1860 assert(!"Unexpected interpolation mode");
1861 return VGPU10_INTERPOLATION_CONSTANT
;
1867 * Translate a TGSI property to VGPU10.
1868 * Don't emit any instructions yet, only need to gather the primitive property information.
1869 * The output primitive topology might be changed later. The final property instructions
1870 * will be emitted as part of the pre-helper code.
1873 emit_vgpu10_property(struct svga_shader_emitter_v10
*emit
,
1874 const struct tgsi_full_property
*prop
)
1876 static const VGPU10_PRIMITIVE primType
[] = {
1877 VGPU10_PRIMITIVE_POINT
, /* PIPE_PRIM_POINTS */
1878 VGPU10_PRIMITIVE_LINE
, /* PIPE_PRIM_LINES */
1879 VGPU10_PRIMITIVE_LINE
, /* PIPE_PRIM_LINE_LOOP */
1880 VGPU10_PRIMITIVE_LINE
, /* PIPE_PRIM_LINE_STRIP */
1881 VGPU10_PRIMITIVE_TRIANGLE
, /* PIPE_PRIM_TRIANGLES */
1882 VGPU10_PRIMITIVE_TRIANGLE
, /* PIPE_PRIM_TRIANGLE_STRIP */
1883 VGPU10_PRIMITIVE_TRIANGLE
, /* PIPE_PRIM_TRIANGLE_FAN */
1884 VGPU10_PRIMITIVE_UNDEFINED
, /* PIPE_PRIM_QUADS */
1885 VGPU10_PRIMITIVE_UNDEFINED
, /* PIPE_PRIM_QUAD_STRIP */
1886 VGPU10_PRIMITIVE_UNDEFINED
, /* PIPE_PRIM_POLYGON */
1887 VGPU10_PRIMITIVE_LINE_ADJ
, /* PIPE_PRIM_LINES_ADJACENCY */
1888 VGPU10_PRIMITIVE_LINE_ADJ
, /* PIPE_PRIM_LINE_STRIP_ADJACENCY */
1889 VGPU10_PRIMITIVE_TRIANGLE_ADJ
, /* PIPE_PRIM_TRIANGLES_ADJACENCY */
1890 VGPU10_PRIMITIVE_TRIANGLE_ADJ
/* PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY */
1893 static const VGPU10_PRIMITIVE_TOPOLOGY primTopology
[] = {
1894 VGPU10_PRIMITIVE_TOPOLOGY_POINTLIST
, /* PIPE_PRIM_POINTS */
1895 VGPU10_PRIMITIVE_TOPOLOGY_LINELIST
, /* PIPE_PRIM_LINES */
1896 VGPU10_PRIMITIVE_TOPOLOGY_LINELIST
, /* PIPE_PRIM_LINE_LOOP */
1897 VGPU10_PRIMITIVE_TOPOLOGY_LINESTRIP
, /* PIPE_PRIM_LINE_STRIP */
1898 VGPU10_PRIMITIVE_TOPOLOGY_TRIANGLELIST
, /* PIPE_PRIM_TRIANGLES */
1899 VGPU10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP
, /* PIPE_PRIM_TRIANGLE_STRIP */
1900 VGPU10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP
, /* PIPE_PRIM_TRIANGLE_FAN */
1901 VGPU10_PRIMITIVE_TOPOLOGY_UNDEFINED
, /* PIPE_PRIM_QUADS */
1902 VGPU10_PRIMITIVE_TOPOLOGY_UNDEFINED
, /* PIPE_PRIM_QUAD_STRIP */
1903 VGPU10_PRIMITIVE_TOPOLOGY_UNDEFINED
, /* PIPE_PRIM_POLYGON */
1904 VGPU10_PRIMITIVE_TOPOLOGY_LINELIST_ADJ
, /* PIPE_PRIM_LINES_ADJACENCY */
1905 VGPU10_PRIMITIVE_TOPOLOGY_LINELIST_ADJ
, /* PIPE_PRIM_LINE_STRIP_ADJACENCY */
1906 VGPU10_PRIMITIVE_TOPOLOGY_TRIANGLELIST_ADJ
, /* PIPE_PRIM_TRIANGLES_ADJACENCY */
1907 VGPU10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP_ADJ
/* PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY */
1910 static const unsigned inputArraySize
[] = {
1911 0, /* VGPU10_PRIMITIVE_UNDEFINED */
1912 1, /* VGPU10_PRIMITIVE_POINT */
1913 2, /* VGPU10_PRIMITIVE_LINE */
1914 3, /* VGPU10_PRIMITIVE_TRIANGLE */
1917 4, /* VGPU10_PRIMITIVE_LINE_ADJ */
1918 6 /* VGPU10_PRIMITIVE_TRIANGLE_ADJ */
1921 switch (prop
->Property
.PropertyName
) {
1922 case TGSI_PROPERTY_GS_INPUT_PRIM
:
1923 assert(prop
->u
[0].Data
< Elements(primType
));
1924 emit
->gs
.prim_type
= primType
[prop
->u
[0].Data
];
1925 assert(emit
->gs
.prim_type
!= VGPU10_PRIMITIVE_UNDEFINED
);
1926 emit
->gs
.input_size
= inputArraySize
[emit
->gs
.prim_type
];
1929 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
1930 assert(prop
->u
[0].Data
< Elements(primTopology
));
1931 emit
->gs
.prim_topology
= primTopology
[prop
->u
[0].Data
];
1932 assert(emit
->gs
.prim_topology
!= VGPU10_PRIMITIVE_TOPOLOGY_UNDEFINED
);
1935 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
:
1936 emit
->gs
.max_out_vertices
= prop
->u
[0].Data
;
1948 emit_property_instruction(struct svga_shader_emitter_v10
*emit
,
1949 VGPU10OpcodeToken0 opcode0
, unsigned nData
,
1952 begin_emit_instruction(emit
);
1953 emit_dword(emit
, opcode0
.value
);
1955 emit_dword(emit
, data
);
1956 end_emit_instruction(emit
);
1961 * Emit property instructions
1964 emit_property_instructions(struct svga_shader_emitter_v10
*emit
)
1966 VGPU10OpcodeToken0 opcode0
;
1968 assert(emit
->unit
== PIPE_SHADER_GEOMETRY
);
1970 /* emit input primitive type declaration */
1972 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_GS_INPUT_PRIMITIVE
;
1973 opcode0
.primitive
= emit
->gs
.prim_type
;
1974 emit_property_instruction(emit
, opcode0
, 0, 0);
1976 /* emit output primitive topology declaration */
1978 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_GS_OUTPUT_PRIMITIVE_TOPOLOGY
;
1979 opcode0
.primitiveTopology
= emit
->gs
.prim_topology
;
1980 emit_property_instruction(emit
, opcode0
, 0, 0);
1982 /* emit max output vertices */
1984 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_MAX_OUTPUT_VERTEX_COUNT
;
1985 emit_property_instruction(emit
, opcode0
, 1, emit
->gs
.max_out_vertices
);
1990 * Emit a vgpu10 declaration "instruction".
1991 * \param index the register index
1992 * \param size array size of the operand. In most cases, it is 1,
1993 * but for inputs to geometry shader, the array size varies
1994 * depending on the primitive type.
1997 emit_decl_instruction(struct svga_shader_emitter_v10
*emit
,
1998 VGPU10OpcodeToken0 opcode0
,
1999 VGPU10OperandToken0 operand0
,
2000 VGPU10NameToken name_token
,
2001 unsigned index
, unsigned size
)
2003 assert(opcode0
.opcodeType
);
2004 assert(operand0
.mask
);
2006 begin_emit_instruction(emit
);
2007 emit_dword(emit
, opcode0
.value
);
2009 emit_dword(emit
, operand0
.value
);
2011 if (operand0
.indexDimension
== VGPU10_OPERAND_INDEX_1D
) {
2012 /* Next token is the index of the register to declare */
2013 emit_dword(emit
, index
);
2015 else if (operand0
.indexDimension
>= VGPU10_OPERAND_INDEX_2D
) {
2016 /* Next token is the size of the register */
2017 emit_dword(emit
, size
);
2019 /* Followed by the index of the register */
2020 emit_dword(emit
, index
);
2023 if (name_token
.value
) {
2024 emit_dword(emit
, name_token
.value
);
2027 end_emit_instruction(emit
);
2032 * Emit the declaration for a shader input.
2033 * \param opcodeType opcode type, one of VGPU10_OPCODE_DCL_INPUTx
2034 * \param operandType operand type, one of VGPU10_OPERAND_TYPE_INPUT_x
2035 * \param dim index dimension
2036 * \param index the input register index
2037 * \param size array size of the operand. In most cases, it is 1,
2038 * but for inputs to geometry shader, the array size varies
2039 * depending on the primitive type.
2040 * \param name one of VGPU10_NAME_x
2041 * \parma numComp number of components
2042 * \param selMode component selection mode
2043 * \param usageMask bitfield of VGPU10_OPERAND_4_COMPONENT_MASK_x values
2044 * \param interpMode interpolation mode
2047 emit_input_declaration(struct svga_shader_emitter_v10
*emit
,
2048 unsigned opcodeType
, unsigned operandType
,
2049 unsigned dim
, unsigned index
, unsigned size
,
2050 unsigned name
, unsigned numComp
,
2051 unsigned selMode
, unsigned usageMask
,
2052 unsigned interpMode
)
2054 VGPU10OpcodeToken0 opcode0
;
2055 VGPU10OperandToken0 operand0
;
2056 VGPU10NameToken name_token
;
2058 assert(usageMask
<= VGPU10_OPERAND_4_COMPONENT_MASK_ALL
);
2059 assert(opcodeType
== VGPU10_OPCODE_DCL_INPUT
||
2060 opcodeType
== VGPU10_OPCODE_DCL_INPUT_SIV
||
2061 opcodeType
== VGPU10_OPCODE_DCL_INPUT_PS
||
2062 opcodeType
== VGPU10_OPCODE_DCL_INPUT_PS_SGV
);
2063 assert(operandType
== VGPU10_OPERAND_TYPE_INPUT
||
2064 operandType
== VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID
);
2065 assert(numComp
<= VGPU10_OPERAND_4_COMPONENT
);
2066 assert(selMode
<= VGPU10_OPERAND_4_COMPONENT_MASK_MODE
);
2067 assert(dim
<= VGPU10_OPERAND_INDEX_3D
);
2068 assert(name
== VGPU10_NAME_UNDEFINED
||
2069 name
== VGPU10_NAME_POSITION
||
2070 name
== VGPU10_NAME_INSTANCE_ID
||
2071 name
== VGPU10_NAME_VERTEX_ID
||
2072 name
== VGPU10_NAME_PRIMITIVE_ID
||
2073 name
== VGPU10_NAME_IS_FRONT_FACE
);
2074 assert(interpMode
== VGPU10_INTERPOLATION_UNDEFINED
||
2075 interpMode
== VGPU10_INTERPOLATION_CONSTANT
||
2076 interpMode
== VGPU10_INTERPOLATION_LINEAR
||
2077 interpMode
== VGPU10_INTERPOLATION_LINEAR_CENTROID
||
2078 interpMode
== VGPU10_INTERPOLATION_LINEAR_NOPERSPECTIVE
||
2079 interpMode
== VGPU10_INTERPOLATION_LINEAR_NOPERSPECTIVE_CENTROID
);
2081 check_register_index(emit
, opcodeType
, index
);
2083 opcode0
.value
= operand0
.value
= name_token
.value
= 0;
2085 opcode0
.opcodeType
= opcodeType
;
2086 opcode0
.interpolationMode
= interpMode
;
2088 operand0
.operandType
= operandType
;
2089 operand0
.numComponents
= numComp
;
2090 operand0
.selectionMode
= selMode
;
2091 operand0
.mask
= usageMask
;
2092 operand0
.indexDimension
= dim
;
2093 operand0
.index0Representation
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
2094 if (dim
== VGPU10_OPERAND_INDEX_2D
)
2095 operand0
.index1Representation
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
2097 name_token
.name
= name
;
2099 emit_decl_instruction(emit
, opcode0
, operand0
, name_token
, index
, size
);
2104 * Emit the declaration for a shader output.
2105 * \param type one of VGPU10_OPCODE_DCL_OUTPUTx
2106 * \param index the output register index
2107 * \param name one of VGPU10_NAME_x
2108 * \param usageMask bitfield of VGPU10_OPERAND_4_COMPONENT_MASK_x values
2111 emit_output_declaration(struct svga_shader_emitter_v10
*emit
,
2112 unsigned type
, unsigned index
,
2113 unsigned name
, unsigned usageMask
)
2115 VGPU10OpcodeToken0 opcode0
;
2116 VGPU10OperandToken0 operand0
;
2117 VGPU10NameToken name_token
;
2119 assert(usageMask
<= VGPU10_OPERAND_4_COMPONENT_MASK_ALL
);
2120 assert(type
== VGPU10_OPCODE_DCL_OUTPUT
||
2121 type
== VGPU10_OPCODE_DCL_OUTPUT_SGV
||
2122 type
== VGPU10_OPCODE_DCL_OUTPUT_SIV
);
2123 assert(name
== VGPU10_NAME_UNDEFINED
||
2124 name
== VGPU10_NAME_POSITION
||
2125 name
== VGPU10_NAME_PRIMITIVE_ID
||
2126 name
== VGPU10_NAME_RENDER_TARGET_ARRAY_INDEX
||
2127 name
== VGPU10_NAME_CLIP_DISTANCE
);
2129 check_register_index(emit
, type
, index
);
2131 opcode0
.value
= operand0
.value
= name_token
.value
= 0;
2133 opcode0
.opcodeType
= type
;
2134 operand0
.operandType
= VGPU10_OPERAND_TYPE_OUTPUT
;
2135 operand0
.numComponents
= VGPU10_OPERAND_4_COMPONENT
;
2136 operand0
.selectionMode
= VGPU10_OPERAND_4_COMPONENT_MASK_MODE
;
2137 operand0
.mask
= usageMask
;
2138 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_1D
;
2139 operand0
.index0Representation
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
2141 name_token
.name
= name
;
2143 emit_decl_instruction(emit
, opcode0
, operand0
, name_token
, index
, 1);
2148 * Emit the declaration for the fragment depth output.
2151 emit_fragdepth_output_declaration(struct svga_shader_emitter_v10
*emit
)
2153 VGPU10OpcodeToken0 opcode0
;
2154 VGPU10OperandToken0 operand0
;
2155 VGPU10NameToken name_token
;
2157 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
2159 opcode0
.value
= operand0
.value
= name_token
.value
= 0;
2161 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_OUTPUT
;
2162 operand0
.operandType
= VGPU10_OPERAND_TYPE_OUTPUT_DEPTH
;
2163 operand0
.numComponents
= VGPU10_OPERAND_1_COMPONENT
;
2164 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_0D
;
2165 operand0
.mask
= VGPU10_OPERAND_4_COMPONENT_MASK_ALL
;
2167 emit_decl_instruction(emit
, opcode0
, operand0
, name_token
, 0, 1);
2172 * Emit the declaration for a system value input/output.
2175 emit_system_value_declaration(struct svga_shader_emitter_v10
*emit
,
2176 unsigned semantic_name
, unsigned index
)
2178 switch (semantic_name
) {
2179 case TGSI_SEMANTIC_INSTANCEID
:
2180 index
= alloc_system_value_index(emit
, index
);
2181 emit_input_declaration(emit
, VGPU10_OPCODE_DCL_INPUT_SIV
,
2182 VGPU10_OPERAND_TYPE_INPUT
,
2183 VGPU10_OPERAND_INDEX_1D
,
2185 VGPU10_NAME_INSTANCE_ID
,
2186 VGPU10_OPERAND_4_COMPONENT
,
2187 VGPU10_OPERAND_4_COMPONENT_MASK_MODE
,
2188 VGPU10_OPERAND_4_COMPONENT_MASK_X
,
2189 VGPU10_INTERPOLATION_UNDEFINED
);
2191 case TGSI_SEMANTIC_VERTEXID
:
2192 index
= alloc_system_value_index(emit
, index
);
2193 emit_input_declaration(emit
, VGPU10_OPCODE_DCL_INPUT_SIV
,
2194 VGPU10_OPERAND_TYPE_INPUT
,
2195 VGPU10_OPERAND_INDEX_1D
,
2197 VGPU10_NAME_VERTEX_ID
,
2198 VGPU10_OPERAND_4_COMPONENT
,
2199 VGPU10_OPERAND_4_COMPONENT_MASK_MODE
,
2200 VGPU10_OPERAND_4_COMPONENT_MASK_X
,
2201 VGPU10_INTERPOLATION_UNDEFINED
);
2209 * Translate a TGSI declaration to VGPU10.
2212 emit_vgpu10_declaration(struct svga_shader_emitter_v10
*emit
,
2213 const struct tgsi_full_declaration
*decl
)
2215 switch (decl
->Declaration
.File
) {
2216 case TGSI_FILE_INPUT
:
2217 /* do nothing - see emit_input_declarations() */
2220 case TGSI_FILE_OUTPUT
:
2221 assert(decl
->Range
.First
== decl
->Range
.Last
);
2222 emit
->output_usage_mask
[decl
->Range
.First
] = decl
->Declaration
.UsageMask
;
2225 case TGSI_FILE_TEMPORARY
:
2226 /* Don't declare the temps here. Just keep track of how many
2227 * and emit the declaration later.
2229 if (decl
->Declaration
.Array
) {
2230 /* Indexed temporary array. Save the start index of the array
2231 * and the size of the array.
2233 const unsigned arrayID
= MIN2(decl
->Array
.ArrayID
, MAX_TEMP_ARRAYS
);
2236 assert(arrayID
< ARRAY_SIZE(emit
->temp_arrays
));
2238 /* Save this array so we can emit the declaration for it later */
2239 emit
->temp_arrays
[arrayID
].start
= decl
->Range
.First
;
2240 emit
->temp_arrays
[arrayID
].size
=
2241 decl
->Range
.Last
- decl
->Range
.First
+ 1;
2243 emit
->num_temp_arrays
= MAX2(emit
->num_temp_arrays
, arrayID
+ 1);
2244 assert(emit
->num_temp_arrays
<= MAX_TEMP_ARRAYS
);
2245 emit
->num_temp_arrays
= MIN2(emit
->num_temp_arrays
, MAX_TEMP_ARRAYS
);
2247 /* Fill in the temp_map entries for this array */
2248 for (i
= decl
->Range
.First
; i
<= decl
->Range
.Last
; i
++) {
2249 emit
->temp_map
[i
].arrayId
= arrayID
;
2250 emit
->temp_map
[i
].index
= i
- decl
->Range
.First
;
2254 /* for all temps, indexed or not, keep track of highest index */
2255 emit
->num_shader_temps
= MAX2(emit
->num_shader_temps
,
2256 decl
->Range
.Last
+ 1);
2259 case TGSI_FILE_CONSTANT
:
2260 /* Don't declare constants here. Just keep track and emit later. */
2262 unsigned constbuf
= 0, num_consts
;
2263 if (decl
->Declaration
.Dimension
) {
2264 constbuf
= decl
->Dim
.Index2D
;
2266 /* We throw an assertion here when, in fact, the shader should never
2267 * have linked due to constbuf index out of bounds, so we shouldn't
2268 * have reached here.
2270 assert(constbuf
< Elements(emit
->num_shader_consts
));
2272 num_consts
= MAX2(emit
->num_shader_consts
[constbuf
],
2273 decl
->Range
.Last
+ 1);
2275 if (num_consts
> VGPU10_MAX_CONSTANT_BUFFER_ELEMENT_COUNT
) {
2276 debug_printf("Warning: constant buffer is declared to size [%u]"
2277 " but [%u] is the limit.\n",
2279 VGPU10_MAX_CONSTANT_BUFFER_ELEMENT_COUNT
);
2281 /* The linker doesn't enforce the max UBO size so we clamp here */
2282 emit
->num_shader_consts
[constbuf
] =
2283 MIN2(num_consts
, VGPU10_MAX_CONSTANT_BUFFER_ELEMENT_COUNT
);
2287 case TGSI_FILE_IMMEDIATE
:
2288 assert(!"TGSI_FILE_IMMEDIATE not handled yet!");
2291 case TGSI_FILE_SYSTEM_VALUE
:
2292 emit_system_value_declaration(emit
, decl
->Semantic
.Name
,
2296 case TGSI_FILE_SAMPLER
:
2297 /* Don't declare samplers here. Just keep track and emit later. */
2298 emit
->num_samplers
= MAX2(emit
->num_samplers
, decl
->Range
.Last
+ 1);
2301 case TGSI_FILE_RESOURCE
:
2302 /*opcode0.opcodeType = VGPU10_OPCODE_DCL_RESOURCE;*/
2303 /* XXX more, VGPU10_RETURN_TYPE_FLOAT */
2304 assert(!"TGSI_FILE_RESOURCE not handled yet");
2307 case TGSI_FILE_ADDRESS
:
2308 emit
->num_address_regs
= MAX2(emit
->num_address_regs
,
2309 decl
->Range
.Last
+ 1);
2312 case TGSI_FILE_SAMPLER_VIEW
:
2313 /* Not used at this time, but maybe in the future.
2314 * See emit_resource_declarations().
2319 assert(!"Unexpected type of declaration");
2327 * Emit all input declarations.
2330 emit_input_declarations(struct svga_shader_emitter_v10
*emit
)
2334 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
2336 for (i
= 0; i
< emit
->linkage
.num_inputs
; i
++) {
2337 unsigned semantic_name
= emit
->info
.input_semantic_name
[i
];
2338 unsigned usage_mask
= emit
->info
.input_usage_mask
[i
];
2339 unsigned index
= emit
->linkage
.input_map
[i
];
2340 unsigned type
, interpolationMode
, name
;
2342 if (usage_mask
== 0)
2343 continue; /* register is not actually used */
2345 if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
2346 /* fragment position input */
2347 type
= VGPU10_OPCODE_DCL_INPUT_PS_SGV
;
2348 interpolationMode
= VGPU10_INTERPOLATION_LINEAR
;
2349 name
= VGPU10_NAME_POSITION
;
2350 if (usage_mask
& TGSI_WRITEMASK_W
) {
2351 /* we need to replace use of 'w' with '1/w' */
2352 emit
->fs
.fragcoord_input_index
= i
;
2355 else if (semantic_name
== TGSI_SEMANTIC_FACE
) {
2356 /* fragment front-facing input */
2357 type
= VGPU10_OPCODE_DCL_INPUT_PS_SGV
;
2358 interpolationMode
= VGPU10_INTERPOLATION_CONSTANT
;
2359 name
= VGPU10_NAME_IS_FRONT_FACE
;
2360 emit
->fs
.face_input_index
= i
;
2362 else if (semantic_name
== TGSI_SEMANTIC_PRIMID
) {
2364 type
= VGPU10_OPCODE_DCL_INPUT_PS_SGV
;
2365 interpolationMode
= VGPU10_INTERPOLATION_CONSTANT
;
2366 name
= VGPU10_NAME_PRIMITIVE_ID
;
2369 /* general fragment input */
2370 type
= VGPU10_OPCODE_DCL_INPUT_PS
;
2372 translate_interpolation(emit
,
2373 emit
->info
.input_interpolate
[i
],
2374 emit
->info
.input_interpolate_loc
[i
]);
2376 /* keeps track if flat interpolation mode is being used */
2377 emit
->uses_flat_interp
= emit
->uses_flat_interp
||
2378 (interpolationMode
== VGPU10_INTERPOLATION_CONSTANT
);
2380 name
= VGPU10_NAME_UNDEFINED
;
2383 emit_input_declaration(emit
, type
,
2384 VGPU10_OPERAND_TYPE_INPUT
,
2385 VGPU10_OPERAND_INDEX_1D
, index
, 1,
2387 VGPU10_OPERAND_4_COMPONENT
,
2388 VGPU10_OPERAND_4_COMPONENT_MASK_MODE
,
2389 VGPU10_OPERAND_4_COMPONENT_MASK_ALL
,
2393 else if (emit
->unit
== PIPE_SHADER_GEOMETRY
) {
2395 for (i
= 0; i
< emit
->info
.num_inputs
; i
++) {
2396 unsigned semantic_name
= emit
->info
.input_semantic_name
[i
];
2397 unsigned usage_mask
= emit
->info
.input_usage_mask
[i
];
2398 unsigned index
= emit
->linkage
.input_map
[i
];
2399 unsigned opcodeType
, operandType
;
2400 unsigned numComp
, selMode
;
2404 if (usage_mask
== 0)
2405 continue; /* register is not actually used */
2407 opcodeType
= VGPU10_OPCODE_DCL_INPUT
;
2408 operandType
= VGPU10_OPERAND_TYPE_INPUT
;
2409 numComp
= VGPU10_OPERAND_4_COMPONENT
;
2410 selMode
= VGPU10_OPERAND_4_COMPONENT_MASK_MODE
;
2411 name
= VGPU10_NAME_UNDEFINED
;
2413 /* all geometry shader inputs are two dimensional except gl_PrimitiveID */
2414 dim
= VGPU10_OPERAND_INDEX_2D
;
2416 if (semantic_name
== TGSI_SEMANTIC_PRIMID
) {
2418 operandType
= VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID
;
2419 dim
= VGPU10_OPERAND_INDEX_0D
;
2420 numComp
= VGPU10_OPERAND_0_COMPONENT
;
2423 /* also save the register index so we can check for
2424 * primitive id when emit src register. We need to modify the
2425 * operand type, index dimension when emit primitive id src reg.
2427 emit
->gs
.prim_id_index
= i
;
2429 else if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
2430 /* vertex position input */
2431 opcodeType
= VGPU10_OPCODE_DCL_INPUT_SIV
;
2432 name
= VGPU10_NAME_POSITION
;
2435 emit_input_declaration(emit
, opcodeType
, operandType
,
2437 emit
->gs
.input_size
,
2440 VGPU10_OPERAND_4_COMPONENT_MASK_ALL
,
2441 VGPU10_INTERPOLATION_UNDEFINED
);
2445 assert(emit
->unit
== PIPE_SHADER_VERTEX
);
2447 for (i
= 0; i
< emit
->info
.num_inputs
; i
++) {
2448 unsigned usage_mask
= emit
->info
.input_usage_mask
[i
];
2451 if (usage_mask
== 0)
2452 continue; /* register is not actually used */
2454 emit_input_declaration(emit
, VGPU10_OPCODE_DCL_INPUT
,
2455 VGPU10_OPERAND_TYPE_INPUT
,
2456 VGPU10_OPERAND_INDEX_1D
, index
, 1,
2457 VGPU10_NAME_UNDEFINED
,
2458 VGPU10_OPERAND_4_COMPONENT
,
2459 VGPU10_OPERAND_4_COMPONENT_MASK_MODE
,
2460 VGPU10_OPERAND_4_COMPONENT_MASK_ALL
,
2461 VGPU10_INTERPOLATION_UNDEFINED
);
2470 * Emit all output declarations.
2473 emit_output_declarations(struct svga_shader_emitter_v10
*emit
)
2477 for (i
= 0; i
< emit
->info
.num_outputs
; i
++) {
2478 /*const unsigned usage_mask = emit->info.output_usage_mask[i];*/
2479 const unsigned semantic_name
= emit
->info
.output_semantic_name
[i
];
2480 const unsigned semantic_index
= emit
->info
.output_semantic_index
[i
];
2483 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
2484 if (semantic_name
== TGSI_SEMANTIC_COLOR
) {
2485 assert(semantic_index
< Elements(emit
->fs
.color_out_index
));
2487 emit
->fs
.color_out_index
[semantic_index
] = index
;
2489 /* The semantic index is the shader's color output/buffer index */
2490 emit_output_declaration(emit
,
2491 VGPU10_OPCODE_DCL_OUTPUT
, semantic_index
,
2492 VGPU10_NAME_UNDEFINED
,
2493 VGPU10_OPERAND_4_COMPONENT_MASK_ALL
);
2495 if (semantic_index
== 0) {
2496 if (emit
->key
.fs
.write_color0_to_n_cbufs
> 1) {
2497 /* Emit declarations for the additional color outputs
2501 for (j
= 1; j
< emit
->key
.fs
.write_color0_to_n_cbufs
; j
++) {
2502 /* Allocate a new output index */
2503 unsigned idx
= emit
->info
.num_outputs
+ j
- 1;
2504 emit
->fs
.color_out_index
[j
] = idx
;
2505 emit_output_declaration(emit
,
2506 VGPU10_OPCODE_DCL_OUTPUT
, idx
,
2507 VGPU10_NAME_UNDEFINED
,
2508 VGPU10_OPERAND_4_COMPONENT_MASK_ALL
);
2509 emit
->info
.output_semantic_index
[idx
] = j
;
2514 assert(!emit
->key
.fs
.write_color0_to_n_cbufs
);
2517 else if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
2518 /* Fragment depth output */
2519 emit_fragdepth_output_declaration(emit
);
2522 assert(!"Bad output semantic name");
2527 unsigned name
, type
;
2528 unsigned writemask
= VGPU10_OPERAND_4_COMPONENT_MASK_ALL
;
2530 switch (semantic_name
) {
2531 case TGSI_SEMANTIC_POSITION
:
2532 assert(emit
->unit
!= PIPE_SHADER_FRAGMENT
);
2533 type
= VGPU10_OPCODE_DCL_OUTPUT_SIV
;
2534 name
= VGPU10_NAME_POSITION
;
2535 /* Save the index of the vertex position output register */
2536 emit
->vposition
.out_index
= index
;
2538 case TGSI_SEMANTIC_CLIPDIST
:
2539 type
= VGPU10_OPCODE_DCL_OUTPUT_SIV
;
2540 name
= VGPU10_NAME_CLIP_DISTANCE
;
2541 /* save the starting index of the clip distance output register */
2542 if (semantic_index
== 0)
2543 emit
->clip_dist_out_index
= index
;
2544 writemask
= emit
->output_usage_mask
[index
];
2545 writemask
= apply_clip_plane_mask(emit
, writemask
, semantic_index
);
2546 if (writemask
== 0x0) {
2547 continue; /* discard this do-nothing declaration */
2550 case TGSI_SEMANTIC_PRIMID
:
2551 assert(emit
->unit
== PIPE_SHADER_GEOMETRY
);
2552 type
= VGPU10_OPCODE_DCL_OUTPUT_SGV
;
2553 name
= VGPU10_NAME_PRIMITIVE_ID
;
2555 case TGSI_SEMANTIC_LAYER
:
2556 assert(emit
->unit
== PIPE_SHADER_GEOMETRY
);
2557 type
= VGPU10_OPCODE_DCL_OUTPUT_SGV
;
2558 name
= VGPU10_NAME_RENDER_TARGET_ARRAY_INDEX
;
2560 case TGSI_SEMANTIC_CLIPVERTEX
:
2561 type
= VGPU10_OPCODE_DCL_OUTPUT
;
2562 name
= VGPU10_NAME_UNDEFINED
;
2563 emit
->clip_vertex_out_index
= index
;
2566 /* generic output */
2567 type
= VGPU10_OPCODE_DCL_OUTPUT
;
2568 name
= VGPU10_NAME_UNDEFINED
;
2571 emit_output_declaration(emit
, type
, index
, name
, writemask
);
2575 if (emit
->vposition
.so_index
!= INVALID_INDEX
&&
2576 emit
->vposition
.out_index
!= INVALID_INDEX
) {
2578 assert(emit
->unit
!= PIPE_SHADER_FRAGMENT
);
2580 /* Emit the declaration for the non-adjusted vertex position
2581 * for stream output purpose
2583 emit_output_declaration(emit
, VGPU10_OPCODE_DCL_OUTPUT
,
2584 emit
->vposition
.so_index
,
2585 VGPU10_NAME_UNDEFINED
,
2586 VGPU10_OPERAND_4_COMPONENT_MASK_ALL
);
2589 if (emit
->clip_dist_so_index
!= INVALID_INDEX
&&
2590 emit
->clip_dist_out_index
!= INVALID_INDEX
) {
2592 assert(emit
->unit
!= PIPE_SHADER_FRAGMENT
);
2594 /* Emit the declaration for the clip distance shadow copy which
2595 * will be used for stream output purpose and for clip distance
2598 emit_output_declaration(emit
, VGPU10_OPCODE_DCL_OUTPUT
,
2599 emit
->clip_dist_so_index
,
2600 VGPU10_NAME_UNDEFINED
,
2601 emit
->output_usage_mask
[emit
->clip_dist_out_index
]);
2603 if (emit
->info
.num_written_clipdistance
> 4) {
2604 /* for the second clip distance register, each handles 4 planes */
2605 emit_output_declaration(emit
, VGPU10_OPCODE_DCL_OUTPUT
,
2606 emit
->clip_dist_so_index
+ 1,
2607 VGPU10_NAME_UNDEFINED
,
2608 emit
->output_usage_mask
[emit
->clip_dist_out_index
+1]);
2617 * Emit the declaration for the temporary registers.
2620 emit_temporaries_declaration(struct svga_shader_emitter_v10
*emit
)
2622 unsigned total_temps
, reg
, i
;
2624 total_temps
= emit
->num_shader_temps
;
2626 /* Allocate extra temps for specially-implemented instructions,
2629 total_temps
+= MAX_INTERNAL_TEMPS
;
2631 if (emit
->unit
== PIPE_SHADER_VERTEX
|| emit
->unit
== PIPE_SHADER_GEOMETRY
) {
2632 if (emit
->vposition
.need_prescale
|| emit
->key
.vs
.undo_viewport
||
2633 emit
->key
.clip_plane_enable
||
2634 emit
->vposition
.so_index
!= INVALID_INDEX
) {
2635 emit
->vposition
.tmp_index
= total_temps
;
2639 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2640 unsigned attrib_mask
= (emit
->key
.vs
.adjust_attrib_w_1
|
2641 emit
->key
.vs
.adjust_attrib_itof
|
2642 emit
->key
.vs
.adjust_attrib_utof
|
2643 emit
->key
.vs
.attrib_is_bgra
|
2644 emit
->key
.vs
.attrib_puint_to_snorm
|
2645 emit
->key
.vs
.attrib_puint_to_uscaled
|
2646 emit
->key
.vs
.attrib_puint_to_sscaled
);
2647 while (attrib_mask
) {
2648 unsigned index
= u_bit_scan(&attrib_mask
);
2649 emit
->vs
.adjusted_input
[index
] = total_temps
++;
2653 if (emit
->clip_mode
== CLIP_DISTANCE
) {
2654 /* We need to write the clip distance to a temporary register
2655 * first. Then it will be copied to the shadow copy for
2656 * the clip distance varying variable and stream output purpose.
2657 * It will also be copied to the actual CLIPDIST register
2658 * according to the enabled clip planes
2660 emit
->clip_dist_tmp_index
= total_temps
++;
2661 if (emit
->info
.num_written_clipdistance
> 4)
2662 total_temps
++; /* second clip register */
2664 else if (emit
->clip_mode
== CLIP_VERTEX
) {
2665 /* We need to convert the TGSI CLIPVERTEX output to one or more
2666 * clip distances. Allocate a temp reg for the clipvertex here.
2668 assert(emit
->info
.writes_clipvertex
> 0);
2669 emit
->clip_vertex_tmp_index
= total_temps
;
2673 else if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
2674 if (emit
->key
.fs
.alpha_func
!= SVGA3D_CMP_ALWAYS
||
2675 emit
->key
.fs
.white_fragments
||
2676 emit
->key
.fs
.write_color0_to_n_cbufs
> 1) {
2677 /* Allocate a temp to hold the output color */
2678 emit
->fs
.color_tmp_index
= total_temps
;
2682 if (emit
->fs
.face_input_index
!= INVALID_INDEX
) {
2683 /* Allocate a temp for the +/-1 face register */
2684 emit
->fs
.face_tmp_index
= total_temps
;
2688 if (emit
->fs
.fragcoord_input_index
!= INVALID_INDEX
) {
2689 /* Allocate a temp for modified fragment position register */
2690 emit
->fs
.fragcoord_tmp_index
= total_temps
;
2695 for (i
= 0; i
< emit
->num_address_regs
; i
++) {
2696 emit
->address_reg_index
[i
] = total_temps
++;
2699 /* Initialize the temp_map array which maps TGSI temp indexes to VGPU10
2700 * temp indexes. Basically, we compact all the non-array temp register
2701 * indexes into a consecutive series.
2703 * Before, we may have some TGSI declarations like:
2704 * DCL TEMP[0..1], LOCAL
2705 * DCL TEMP[2..4], ARRAY(1), LOCAL
2706 * DCL TEMP[5..7], ARRAY(2), LOCAL
2707 * plus, some extra temps, like TEMP[8], TEMP[9] for misc things
2709 * After, we'll have a map like this:
2710 * temp_map[0] = { array 0, index 0 }
2711 * temp_map[1] = { array 0, index 1 }
2712 * temp_map[2] = { array 1, index 0 }
2713 * temp_map[3] = { array 1, index 1 }
2714 * temp_map[4] = { array 1, index 2 }
2715 * temp_map[5] = { array 2, index 0 }
2716 * temp_map[6] = { array 2, index 1 }
2717 * temp_map[7] = { array 2, index 2 }
2718 * temp_map[8] = { array 0, index 2 }
2719 * temp_map[9] = { array 0, index 3 }
2721 * We'll declare two arrays of 3 elements, plus a set of four non-indexed
2722 * temps numbered 0..3
2724 * Any time we emit a temporary register index, we'll have to use the
2725 * temp_map[] table to convert the TGSI index to the VGPU10 index.
2727 * Finally, we recompute the total_temps value here.
2730 for (i
= 0; i
< total_temps
; i
++) {
2731 if (emit
->temp_map
[i
].arrayId
== 0) {
2732 emit
->temp_map
[i
].index
= reg
++;
2738 debug_printf("total_temps %u\n", total_temps
);
2739 for (i
= 0; i
< 30; i
++) {
2740 debug_printf("temp %u -> array %u index %u\n",
2741 i
, emit
->temp_map
[i
].arrayId
, emit
->temp_map
[i
].index
);
2745 /* Emit declaration of ordinary temp registers */
2746 if (total_temps
> 0) {
2747 VGPU10OpcodeToken0 opcode0
;
2750 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_TEMPS
;
2752 begin_emit_instruction(emit
);
2753 emit_dword(emit
, opcode0
.value
);
2754 emit_dword(emit
, total_temps
);
2755 end_emit_instruction(emit
);
2758 /* Emit declarations for indexable temp arrays. Skip 0th entry since
2761 for (i
= 1; i
< emit
->num_temp_arrays
; i
++) {
2762 unsigned num_temps
= emit
->temp_arrays
[i
].size
;
2764 if (num_temps
> 0) {
2765 VGPU10OpcodeToken0 opcode0
;
2768 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_INDEXABLE_TEMP
;
2770 begin_emit_instruction(emit
);
2771 emit_dword(emit
, opcode0
.value
);
2772 emit_dword(emit
, i
); /* which array */
2773 emit_dword(emit
, num_temps
);
2774 emit_dword(emit
, 4); /* num components */
2775 end_emit_instruction(emit
);
2777 total_temps
+= num_temps
;
2781 /* Check that the grand total of all regular and indexed temps is
2784 check_register_index(emit
, VGPU10_OPCODE_DCL_TEMPS
, total_temps
- 1);
2791 emit_constant_declaration(struct svga_shader_emitter_v10
*emit
)
2793 VGPU10OpcodeToken0 opcode0
;
2794 VGPU10OperandToken0 operand0
;
2795 unsigned total_consts
, i
;
2798 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_CONSTANT_BUFFER
;
2799 opcode0
.accessPattern
= VGPU10_CB_IMMEDIATE_INDEXED
;
2800 /* XXX or, access pattern = VGPU10_CB_DYNAMIC_INDEXED */
2803 operand0
.numComponents
= VGPU10_OPERAND_4_COMPONENT
;
2804 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_2D
;
2805 operand0
.index0Representation
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
2806 operand0
.index1Representation
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
2807 operand0
.operandType
= VGPU10_OPERAND_TYPE_CONSTANT_BUFFER
;
2808 operand0
.selectionMode
= VGPU10_OPERAND_4_COMPONENT_SWIZZLE_MODE
;
2809 operand0
.swizzleX
= 0;
2810 operand0
.swizzleY
= 1;
2811 operand0
.swizzleZ
= 2;
2812 operand0
.swizzleW
= 3;
2815 * Emit declaration for constant buffer [0]. We also allocate
2816 * room for the extra constants here.
2818 total_consts
= emit
->num_shader_consts
[0];
2820 /* Now, allocate constant slots for the "extra" constants */
2822 /* Vertex position scale/translation */
2823 if (emit
->vposition
.need_prescale
) {
2824 emit
->vposition
.prescale_scale_index
= total_consts
++;
2825 emit
->vposition
.prescale_trans_index
= total_consts
++;
2828 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2829 if (emit
->key
.vs
.undo_viewport
) {
2830 emit
->vs
.viewport_index
= total_consts
++;
2834 /* user-defined clip planes */
2835 if (emit
->key
.clip_plane_enable
) {
2836 unsigned n
= util_bitcount(emit
->key
.clip_plane_enable
);
2837 assert(emit
->unit
== PIPE_SHADER_VERTEX
||
2838 emit
->unit
== PIPE_SHADER_GEOMETRY
);
2839 for (i
= 0; i
< n
; i
++) {
2840 emit
->clip_plane_const
[i
] = total_consts
++;
2844 /* Texcoord scale factors for RECT textures */
2846 for (i
= 0; i
< emit
->num_samplers
; i
++) {
2847 if (emit
->key
.tex
[i
].unnormalized
) {
2848 emit
->texcoord_scale_index
[i
] = total_consts
++;
2853 /* Texture buffer sizes */
2854 for (i
= 0; i
< emit
->num_samplers
; i
++) {
2855 if (emit
->key
.tex
[i
].texture_target
== PIPE_BUFFER
) {
2856 emit
->texture_buffer_size_index
[i
] = total_consts
++;
2860 if (total_consts
> 0) {
2861 begin_emit_instruction(emit
);
2862 emit_dword(emit
, opcode0
.value
);
2863 emit_dword(emit
, operand0
.value
);
2864 emit_dword(emit
, 0); /* which const buffer slot */
2865 emit_dword(emit
, total_consts
);
2866 end_emit_instruction(emit
);
2869 /* Declare remaining constant buffers (UBOs) */
2870 for (i
= 1; i
< Elements(emit
->num_shader_consts
); i
++) {
2871 if (emit
->num_shader_consts
[i
] > 0) {
2872 begin_emit_instruction(emit
);
2873 emit_dword(emit
, opcode0
.value
);
2874 emit_dword(emit
, operand0
.value
);
2875 emit_dword(emit
, i
); /* which const buffer slot */
2876 emit_dword(emit
, emit
->num_shader_consts
[i
]);
2877 end_emit_instruction(emit
);
2886 * Emit declarations for samplers.
2889 emit_sampler_declarations(struct svga_shader_emitter_v10
*emit
)
2893 for (i
= 0; i
< emit
->num_samplers
; i
++) {
2894 VGPU10OpcodeToken0 opcode0
;
2895 VGPU10OperandToken0 operand0
;
2898 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_SAMPLER
;
2899 opcode0
.samplerMode
= VGPU10_SAMPLER_MODE_DEFAULT
;
2902 operand0
.numComponents
= VGPU10_OPERAND_0_COMPONENT
;
2903 operand0
.operandType
= VGPU10_OPERAND_TYPE_SAMPLER
;
2904 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_1D
;
2905 operand0
.index0Representation
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
2907 begin_emit_instruction(emit
);
2908 emit_dword(emit
, opcode0
.value
);
2909 emit_dword(emit
, operand0
.value
);
2910 emit_dword(emit
, i
);
2911 end_emit_instruction(emit
);
2919 * Translate PIPE_TEXTURE_x to VGAPU10_RESOURCE_DIMENSION_x.
2922 pipe_texture_to_resource_dimension(unsigned target
, bool msaa
)
2926 return VGPU10_RESOURCE_DIMENSION_BUFFER
;
2927 case PIPE_TEXTURE_1D
:
2928 return VGPU10_RESOURCE_DIMENSION_TEXTURE1D
;
2929 case PIPE_TEXTURE_2D
:
2930 case PIPE_TEXTURE_RECT
:
2931 return msaa
? VGPU10_RESOURCE_DIMENSION_TEXTURE2DMS
2932 : VGPU10_RESOURCE_DIMENSION_TEXTURE2D
;
2933 case PIPE_TEXTURE_3D
:
2934 return VGPU10_RESOURCE_DIMENSION_TEXTURE3D
;
2935 case PIPE_TEXTURE_CUBE
:
2936 return VGPU10_RESOURCE_DIMENSION_TEXTURECUBE
;
2937 case PIPE_TEXTURE_1D_ARRAY
:
2938 return VGPU10_RESOURCE_DIMENSION_TEXTURE1DARRAY
;
2939 case PIPE_TEXTURE_2D_ARRAY
:
2940 return msaa
? VGPU10_RESOURCE_DIMENSION_TEXTURE2DMSARRAY
2941 : VGPU10_RESOURCE_DIMENSION_TEXTURE2DARRAY
;
2942 case PIPE_TEXTURE_CUBE_ARRAY
:
2943 return VGPU10_RESOURCE_DIMENSION_TEXTURECUBEARRAY
;
2945 assert(!"Unexpected resource type");
2946 return VGPU10_RESOURCE_DIMENSION_TEXTURE2D
;
2952 * Given a tgsi_return_type, return true iff it is an integer type.
2955 is_integer_type(enum tgsi_return_type type
)
2958 case TGSI_RETURN_TYPE_SINT
:
2959 case TGSI_RETURN_TYPE_UINT
:
2961 case TGSI_RETURN_TYPE_FLOAT
:
2962 case TGSI_RETURN_TYPE_UNORM
:
2963 case TGSI_RETURN_TYPE_SNORM
:
2965 case TGSI_RETURN_TYPE_COUNT
:
2967 assert(!"is_integer_type: Unknown tgsi_return_type");
2974 * Emit declarations for resources.
2975 * XXX When we're sure that all TGSI shaders will be generated with
2976 * sampler view declarations (Ex: DCL SVIEW[n], 2D, UINT) we may
2980 emit_resource_declarations(struct svga_shader_emitter_v10
*emit
)
2984 /* Emit resource decl for each sampler */
2985 for (i
= 0; i
< emit
->num_samplers
; i
++) {
2986 VGPU10OpcodeToken0 opcode0
;
2987 VGPU10OperandToken0 operand0
;
2988 VGPU10ResourceReturnTypeToken return_type
;
2989 VGPU10_RESOURCE_RETURN_TYPE rt
;
2992 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_RESOURCE
;
2993 opcode0
.resourceDimension
=
2994 pipe_texture_to_resource_dimension(emit
->key
.tex
[i
].texture_target
,
2995 emit
->key
.tex
[i
].texture_msaa
);
2997 operand0
.numComponents
= VGPU10_OPERAND_0_COMPONENT
;
2998 operand0
.operandType
= VGPU10_OPERAND_TYPE_RESOURCE
;
2999 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_1D
;
3000 operand0
.index0Representation
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
3003 /* convert TGSI_RETURN_TYPE_x to VGPU10_RETURN_TYPE_x */
3004 STATIC_ASSERT(VGPU10_RETURN_TYPE_UNORM
== TGSI_RETURN_TYPE_UNORM
+ 1);
3005 STATIC_ASSERT(VGPU10_RETURN_TYPE_SNORM
== TGSI_RETURN_TYPE_SNORM
+ 1);
3006 STATIC_ASSERT(VGPU10_RETURN_TYPE_SINT
== TGSI_RETURN_TYPE_SINT
+ 1);
3007 STATIC_ASSERT(VGPU10_RETURN_TYPE_UINT
== TGSI_RETURN_TYPE_UINT
+ 1);
3008 STATIC_ASSERT(VGPU10_RETURN_TYPE_FLOAT
== TGSI_RETURN_TYPE_FLOAT
+ 1);
3009 assert(emit
->key
.tex
[i
].return_type
<= TGSI_RETURN_TYPE_FLOAT
);
3010 rt
= emit
->key
.tex
[i
].return_type
+ 1;
3012 switch (emit
->key
.tex
[i
].return_type
) {
3013 case TGSI_RETURN_TYPE_UNORM
: rt
= VGPU10_RETURN_TYPE_UNORM
; break;
3014 case TGSI_RETURN_TYPE_SNORM
: rt
= VGPU10_RETURN_TYPE_SNORM
; break;
3015 case TGSI_RETURN_TYPE_SINT
: rt
= VGPU10_RETURN_TYPE_SINT
; break;
3016 case TGSI_RETURN_TYPE_UINT
: rt
= VGPU10_RETURN_TYPE_UINT
; break;
3017 case TGSI_RETURN_TYPE_FLOAT
: rt
= VGPU10_RETURN_TYPE_FLOAT
; break;
3018 case TGSI_RETURN_TYPE_COUNT
:
3020 rt
= VGPU10_RETURN_TYPE_FLOAT
;
3021 assert(!"emit_resource_declarations: Unknown tgsi_return_type");
3025 return_type
.value
= 0;
3026 return_type
.component0
= rt
;
3027 return_type
.component1
= rt
;
3028 return_type
.component2
= rt
;
3029 return_type
.component3
= rt
;
3031 begin_emit_instruction(emit
);
3032 emit_dword(emit
, opcode0
.value
);
3033 emit_dword(emit
, operand0
.value
);
3034 emit_dword(emit
, i
);
3035 emit_dword(emit
, return_type
.value
);
3036 end_emit_instruction(emit
);
3043 emit_instruction_op1(struct svga_shader_emitter_v10
*emit
,
3045 const struct tgsi_full_dst_register
*dst
,
3046 const struct tgsi_full_src_register
*src
,
3049 begin_emit_instruction(emit
);
3050 emit_opcode(emit
, opcode
, saturate
);
3051 emit_dst_register(emit
, dst
);
3052 emit_src_register(emit
, src
);
3053 end_emit_instruction(emit
);
3057 emit_instruction_op2(struct svga_shader_emitter_v10
*emit
,
3059 const struct tgsi_full_dst_register
*dst
,
3060 const struct tgsi_full_src_register
*src1
,
3061 const struct tgsi_full_src_register
*src2
,
3064 begin_emit_instruction(emit
);
3065 emit_opcode(emit
, opcode
, saturate
);
3066 emit_dst_register(emit
, dst
);
3067 emit_src_register(emit
, src1
);
3068 emit_src_register(emit
, src2
);
3069 end_emit_instruction(emit
);
3073 emit_instruction_op3(struct svga_shader_emitter_v10
*emit
,
3075 const struct tgsi_full_dst_register
*dst
,
3076 const struct tgsi_full_src_register
*src1
,
3077 const struct tgsi_full_src_register
*src2
,
3078 const struct tgsi_full_src_register
*src3
,
3081 begin_emit_instruction(emit
);
3082 emit_opcode(emit
, opcode
, saturate
);
3083 emit_dst_register(emit
, dst
);
3084 emit_src_register(emit
, src1
);
3085 emit_src_register(emit
, src2
);
3086 emit_src_register(emit
, src3
);
3087 end_emit_instruction(emit
);
3091 * Emit the actual clip distance instructions to be used for clipping
3092 * by copying the clip distance from the temporary registers to the
3093 * CLIPDIST registers written with the enabled planes mask.
3094 * Also copy the clip distance from the temporary to the clip distance
3095 * shadow copy register which will be referenced by the input shader
3098 emit_clip_distance_instructions(struct svga_shader_emitter_v10
*emit
)
3100 struct tgsi_full_src_register tmp_clip_dist_src
;
3101 struct tgsi_full_dst_register clip_dist_dst
;
3104 unsigned clip_plane_enable
= emit
->key
.clip_plane_enable
;
3105 unsigned clip_dist_tmp_index
= emit
->clip_dist_tmp_index
;
3106 int num_written_clipdist
= emit
->info
.num_written_clipdistance
;
3108 assert(emit
->clip_dist_out_index
!= INVALID_INDEX
);
3109 assert(emit
->clip_dist_tmp_index
!= INVALID_INDEX
);
3112 * Temporary reset the temporary clip dist register index so
3113 * that the copy to the real clip dist register will not
3114 * attempt to copy to the temporary register again
3116 emit
->clip_dist_tmp_index
= INVALID_INDEX
;
3118 for (i
= 0; i
< 2 && num_written_clipdist
> 0; i
++, num_written_clipdist
-=4) {
3120 tmp_clip_dist_src
= make_src_temp_reg(clip_dist_tmp_index
+ i
);
3123 * copy to the shadow copy for use by varying variable and
3124 * stream output. All clip distances
3125 * will be written regardless of the enabled clipping planes.
3127 clip_dist_dst
= make_dst_reg(TGSI_FILE_OUTPUT
,
3128 emit
->clip_dist_so_index
+ i
);
3130 /* MOV clip_dist_so, tmp_clip_dist */
3131 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &clip_dist_dst
,
3132 &tmp_clip_dist_src
, FALSE
);
3135 * copy those clip distances to enabled clipping planes
3136 * to CLIPDIST registers for clipping
3138 if (clip_plane_enable
& 0xf) {
3139 clip_dist_dst
= make_dst_reg(TGSI_FILE_OUTPUT
,
3140 emit
->clip_dist_out_index
+ i
);
3141 clip_dist_dst
= writemask_dst(&clip_dist_dst
, clip_plane_enable
& 0xf);
3143 /* MOV CLIPDIST, tmp_clip_dist */
3144 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &clip_dist_dst
,
3145 &tmp_clip_dist_src
, FALSE
);
3147 /* four clip planes per clip register */
3148 clip_plane_enable
>>= 4;
3151 * set the temporary clip dist register index back to the
3152 * temporary index for the next vertex
3154 emit
->clip_dist_tmp_index
= clip_dist_tmp_index
;
3157 /* Declare clip distance output registers for user-defined clip planes
3158 * or the TGSI_CLIPVERTEX output.
3161 emit_clip_distance_declarations(struct svga_shader_emitter_v10
*emit
)
3163 unsigned num_clip_planes
= util_bitcount(emit
->key
.clip_plane_enable
);
3164 unsigned index
= emit
->num_outputs
;
3165 unsigned plane_mask
;
3167 assert(emit
->unit
== PIPE_SHADER_VERTEX
||
3168 emit
->unit
== PIPE_SHADER_GEOMETRY
);
3169 assert(num_clip_planes
<= 8);
3171 if (emit
->clip_mode
!= CLIP_LEGACY
&&
3172 emit
->clip_mode
!= CLIP_VERTEX
) {
3176 if (num_clip_planes
== 0)
3179 /* Declare one or two clip output registers. The number of components
3180 * in the mask reflects the number of clip planes. For example, if 5
3181 * clip planes are needed, we'll declare outputs similar to:
3182 * dcl_output_siv o2.xyzw, clip_distance
3183 * dcl_output_siv o3.x, clip_distance
3185 emit
->clip_dist_out_index
= index
; /* save the starting clip dist reg index */
3187 plane_mask
= (1 << num_clip_planes
) - 1;
3188 if (plane_mask
& 0xf) {
3189 unsigned cmask
= plane_mask
& VGPU10_OPERAND_4_COMPONENT_MASK_ALL
;
3190 emit_output_declaration(emit
, VGPU10_OPCODE_DCL_OUTPUT_SIV
, index
,
3191 VGPU10_NAME_CLIP_DISTANCE
, cmask
);
3192 emit
->num_outputs
++;
3194 if (plane_mask
& 0xf0) {
3195 unsigned cmask
= (plane_mask
>> 4) & VGPU10_OPERAND_4_COMPONENT_MASK_ALL
;
3196 emit_output_declaration(emit
, VGPU10_OPCODE_DCL_OUTPUT_SIV
, index
+ 1,
3197 VGPU10_NAME_CLIP_DISTANCE
, cmask
);
3198 emit
->num_outputs
++;
3204 * Emit the instructions for writing to the clip distance registers
3205 * to handle legacy/automatic clip planes.
3206 * For each clip plane, the distance is the dot product of the vertex
3207 * position (found in TEMP[vpos_tmp_index]) and the clip plane coefficients.
3208 * This is not used when the shader has an explicit CLIPVERTEX or CLIPDISTANCE
3209 * output registers already declared.
3212 emit_clip_distance_from_vpos(struct svga_shader_emitter_v10
*emit
,
3213 unsigned vpos_tmp_index
)
3215 unsigned i
, num_clip_planes
= util_bitcount(emit
->key
.clip_plane_enable
);
3217 assert(emit
->clip_mode
== CLIP_LEGACY
);
3218 assert(num_clip_planes
<= 8);
3220 assert(emit
->unit
== PIPE_SHADER_VERTEX
||
3221 emit
->unit
== PIPE_SHADER_GEOMETRY
);
3223 for (i
= 0; i
< num_clip_planes
; i
++) {
3224 struct tgsi_full_dst_register dst
;
3225 struct tgsi_full_src_register plane_src
, vpos_src
;
3226 unsigned reg_index
= emit
->clip_dist_out_index
+ i
/ 4;
3227 unsigned comp
= i
% 4;
3228 unsigned writemask
= VGPU10_OPERAND_4_COMPONENT_MASK_X
<< comp
;
3230 /* create dst, src regs */
3231 dst
= make_dst_reg(TGSI_FILE_OUTPUT
, reg_index
);
3232 dst
= writemask_dst(&dst
, writemask
);
3234 plane_src
= make_src_const_reg(emit
->clip_plane_const
[i
]);
3235 vpos_src
= make_src_temp_reg(vpos_tmp_index
);
3237 /* DP4 clip_dist, plane, vpos */
3238 emit_instruction_op2(emit
, VGPU10_OPCODE_DP4
, &dst
,
3239 &plane_src
, &vpos_src
, FALSE
);
3245 * Emit the instructions for computing the clip distance results from
3246 * the clip vertex temporary.
3247 * For each clip plane, the distance is the dot product of the clip vertex
3248 * position (found in a temp reg) and the clip plane coefficients.
3251 emit_clip_vertex_instructions(struct svga_shader_emitter_v10
*emit
)
3253 const unsigned num_clip
= util_bitcount(emit
->key
.clip_plane_enable
);
3255 struct tgsi_full_dst_register dst
;
3256 struct tgsi_full_src_register clipvert_src
;
3257 const unsigned clip_vertex_tmp
= emit
->clip_vertex_tmp_index
;
3259 assert(emit
->unit
== PIPE_SHADER_VERTEX
||
3260 emit
->unit
== PIPE_SHADER_GEOMETRY
);
3262 assert(emit
->clip_mode
== CLIP_VERTEX
);
3264 clipvert_src
= make_src_temp_reg(clip_vertex_tmp
);
3266 for (i
= 0; i
< num_clip
; i
++) {
3267 struct tgsi_full_src_register plane_src
;
3268 unsigned reg_index
= emit
->clip_dist_out_index
+ i
/ 4;
3269 unsigned comp
= i
% 4;
3270 unsigned writemask
= VGPU10_OPERAND_4_COMPONENT_MASK_X
<< comp
;
3272 /* create dst, src regs */
3273 dst
= make_dst_reg(TGSI_FILE_OUTPUT
, reg_index
);
3274 dst
= writemask_dst(&dst
, writemask
);
3276 plane_src
= make_src_const_reg(emit
->clip_plane_const
[i
]);
3278 /* DP4 clip_dist, plane, vpos */
3279 emit_instruction_op2(emit
, VGPU10_OPCODE_DP4
, &dst
,
3280 &plane_src
, &clipvert_src
, FALSE
);
3283 /* copy temporary clip vertex register to the clip vertex register */
3285 assert(emit
->clip_vertex_out_index
!= INVALID_INDEX
);
3288 * temporary reset the temporary clip vertex register index so
3289 * that copy to the clip vertex register will not attempt
3290 * to copy to the temporary register again
3292 emit
->clip_vertex_tmp_index
= INVALID_INDEX
;
3294 /* MOV clip_vertex, clip_vertex_tmp */
3295 dst
= make_dst_reg(TGSI_FILE_OUTPUT
, emit
->clip_vertex_out_index
);
3296 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
,
3297 &dst
, &clipvert_src
, FALSE
);
3300 * set the temporary clip vertex register index back to the
3301 * temporary index for the next vertex
3303 emit
->clip_vertex_tmp_index
= clip_vertex_tmp
;
3307 * Emit code to convert RGBA to BGRA
3310 emit_swap_r_b(struct svga_shader_emitter_v10
*emit
,
3311 const struct tgsi_full_dst_register
*dst
,
3312 const struct tgsi_full_src_register
*src
)
3314 struct tgsi_full_src_register bgra_src
=
3315 swizzle_src(src
, TGSI_SWIZZLE_Z
, TGSI_SWIZZLE_Y
, TGSI_SWIZZLE_X
, TGSI_SWIZZLE_W
);
3317 begin_emit_instruction(emit
);
3318 emit_opcode(emit
, VGPU10_OPCODE_MOV
, FALSE
);
3319 emit_dst_register(emit
, dst
);
3320 emit_src_register(emit
, &bgra_src
);
3321 end_emit_instruction(emit
);
3325 /** Convert from 10_10_10_2 normalized to 10_10_10_2_snorm */
3327 emit_puint_to_snorm(struct svga_shader_emitter_v10
*emit
,
3328 const struct tgsi_full_dst_register
*dst
,
3329 const struct tgsi_full_src_register
*src
)
3331 struct tgsi_full_src_register half
= make_immediate_reg_float(emit
, 0.5f
);
3332 struct tgsi_full_src_register two
=
3333 make_immediate_reg_float4(emit
, 2.0f
, 2.0f
, 2.0f
, 3.0f
);
3334 struct tgsi_full_src_register neg_two
=
3335 make_immediate_reg_float4(emit
, -2.0f
, -2.0f
, -2.0f
, -1.66666f
);
3337 unsigned val_tmp
= get_temp_index(emit
);
3338 struct tgsi_full_dst_register val_dst
= make_dst_temp_reg(val_tmp
);
3339 struct tgsi_full_src_register val_src
= make_src_temp_reg(val_tmp
);
3341 unsigned bias_tmp
= get_temp_index(emit
);
3342 struct tgsi_full_dst_register bias_dst
= make_dst_temp_reg(bias_tmp
);
3343 struct tgsi_full_src_register bias_src
= make_src_temp_reg(bias_tmp
);
3345 /* val = src * 2.0 */
3346 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &val_dst
,
3349 /* bias = src > 0.5 */
3350 emit_instruction_op2(emit
, VGPU10_OPCODE_GE
, &bias_dst
,
3353 /* bias = bias & -2.0 */
3354 emit_instruction_op2(emit
, VGPU10_OPCODE_AND
, &bias_dst
,
3355 &bias_src
, &neg_two
, FALSE
);
3357 /* dst = val + bias */
3358 emit_instruction_op2(emit
, VGPU10_OPCODE_ADD
, dst
,
3359 &val_src
, &bias_src
, FALSE
);
3361 free_temp_indexes(emit
);
3365 /** Convert from 10_10_10_2_unorm to 10_10_10_2_uscaled */
3367 emit_puint_to_uscaled(struct svga_shader_emitter_v10
*emit
,
3368 const struct tgsi_full_dst_register
*dst
,
3369 const struct tgsi_full_src_register
*src
)
3371 struct tgsi_full_src_register scale
=
3372 make_immediate_reg_float4(emit
, 1023.0f
, 1023.0f
, 1023.0f
, 3.0f
);
3374 /* dst = src * scale */
3375 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, dst
, src
, &scale
, FALSE
);
3379 /** Convert from R32_UINT to 10_10_10_2_sscaled */
3381 emit_puint_to_sscaled(struct svga_shader_emitter_v10
*emit
,
3382 const struct tgsi_full_dst_register
*dst
,
3383 const struct tgsi_full_src_register
*src
)
3385 struct tgsi_full_src_register lshift
=
3386 make_immediate_reg_int4(emit
, 22, 12, 2, 0);
3387 struct tgsi_full_src_register rshift
=
3388 make_immediate_reg_int4(emit
, 22, 22, 22, 30);
3390 struct tgsi_full_src_register src_xxxx
= scalar_src(src
, TGSI_SWIZZLE_X
);
3392 unsigned tmp
= get_temp_index(emit
);
3393 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
3394 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
3397 * r = (pixel << 22) >> 22; # signed int in [511, -512]
3398 * g = (pixel << 12) >> 22; # signed int in [511, -512]
3399 * b = (pixel << 2) >> 22; # signed int in [511, -512]
3400 * a = (pixel << 0) >> 30; # signed int in [1, -2]
3401 * dst = i_to_f(r,g,b,a); # convert to float
3403 emit_instruction_op2(emit
, VGPU10_OPCODE_ISHL
, &tmp_dst
,
3404 &src_xxxx
, &lshift
, FALSE
);
3405 emit_instruction_op2(emit
, VGPU10_OPCODE_ISHR
, &tmp_dst
,
3406 &tmp_src
, &rshift
, FALSE
);
3407 emit_instruction_op1(emit
, VGPU10_OPCODE_ITOF
, dst
, &tmp_src
, FALSE
);
3409 free_temp_indexes(emit
);
3414 * Emit code for TGSI_OPCODE_ABS instruction.
3417 emit_abs(struct svga_shader_emitter_v10
*emit
,
3418 const struct tgsi_full_instruction
*inst
)
3425 struct tgsi_full_src_register abs_src0
= absolute_src(&inst
->Src
[0]);
3427 /* MOV dst, abs(s0) */
3428 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &inst
->Dst
[0],
3429 &abs_src0
, inst
->Instruction
.Saturate
);
3436 * Emit code for TGSI_OPCODE_ARL or TGSI_OPCODE_UARL instruction.
3439 emit_arl_uarl(struct svga_shader_emitter_v10
*emit
,
3440 const struct tgsi_full_instruction
*inst
)
3442 unsigned index
= inst
->Dst
[0].Register
.Index
;
3443 struct tgsi_full_dst_register dst
;
3446 assert(index
< MAX_VGPU10_ADDR_REGS
);
3447 dst
= make_dst_temp_reg(emit
->address_reg_index
[index
]);
3451 * FTOI address_tmp, s0
3455 * MOV address_tmp, s0
3457 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ARL
)
3458 opcode
= VGPU10_OPCODE_FTOI
;
3460 opcode
= VGPU10_OPCODE_MOV
;
3462 emit_instruction_op1(emit
, opcode
, &dst
, &inst
->Src
[0], FALSE
);
3469 * Emit code for TGSI_OPCODE_CAL instruction.
3472 emit_cal(struct svga_shader_emitter_v10
*emit
,
3473 const struct tgsi_full_instruction
*inst
)
3475 unsigned label
= inst
->Label
.Label
;
3476 VGPU10OperandToken0 operand
;
3478 operand
.operandType
= VGPU10_OPERAND_TYPE_LABEL
;
3480 begin_emit_instruction(emit
);
3481 emit_dword(emit
, operand
.value
);
3482 emit_dword(emit
, label
);
3483 end_emit_instruction(emit
);
3490 * Emit code for TGSI_OPCODE_IABS instruction.
3493 emit_iabs(struct svga_shader_emitter_v10
*emit
,
3494 const struct tgsi_full_instruction
*inst
)
3496 /* dst.x = (src0.x < 0) ? -src0.x : src0.x
3497 * dst.y = (src0.y < 0) ? -src0.y : src0.y
3498 * dst.z = (src0.z < 0) ? -src0.z : src0.z
3499 * dst.w = (src0.w < 0) ? -src0.w : src0.w
3502 * IMAX dst, src, neg(src)
3504 struct tgsi_full_src_register neg_src
= negate_src(&inst
->Src
[0]);
3505 emit_instruction_op2(emit
, VGPU10_OPCODE_IMAX
, &inst
->Dst
[0],
3506 &inst
->Src
[0], &neg_src
, FALSE
);
3513 * Emit code for TGSI_OPCODE_CMP instruction.
3516 emit_cmp(struct svga_shader_emitter_v10
*emit
,
3517 const struct tgsi_full_instruction
*inst
)
3519 /* dst.x = (src0.x < 0) ? src1.x : src2.x
3520 * dst.y = (src0.y < 0) ? src1.y : src2.y
3521 * dst.z = (src0.z < 0) ? src1.z : src2.z
3522 * dst.w = (src0.w < 0) ? src1.w : src2.w
3526 * MOVC dst, tmp, src1, src2
3528 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
3529 unsigned tmp
= get_temp_index(emit
);
3530 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
3531 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
3533 emit_instruction_op2(emit
, VGPU10_OPCODE_LT
, &tmp_dst
,
3534 &inst
->Src
[0], &zero
, FALSE
);
3535 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &inst
->Dst
[0],
3536 &tmp_src
, &inst
->Src
[1], &inst
->Src
[2],
3537 inst
->Instruction
.Saturate
);
3539 free_temp_indexes(emit
);
3546 * Emit code for TGSI_OPCODE_DP2A instruction.
3549 emit_dp2a(struct svga_shader_emitter_v10
*emit
,
3550 const struct tgsi_full_instruction
*inst
)
3552 /* dst.x = src0.x * src1.x + src0.y * src1.y + src2.x
3553 * dst.y = src0.x * src1.x + src0.y * src1.y + src2.x
3554 * dst.z = src0.x * src1.x + src0.y * src1.y + src2.x
3555 * dst.w = src0.x * src1.x + src0.y * src1.y + src2.x
3557 * MAD tmp.x, s0.y, s1.y, s2.x
3558 * MAD tmp.x, s0.x, s1.x, tmp.x
3559 * MOV dst.xyzw, tmp.xxxx
3561 unsigned tmp
= get_temp_index(emit
);
3562 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
3563 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
3565 struct tgsi_full_src_register tmp_src_xxxx
=
3566 scalar_src(&tmp_src
, TGSI_SWIZZLE_X
);
3567 struct tgsi_full_dst_register tmp_dst_x
=
3568 writemask_dst(&tmp_dst
, TGSI_WRITEMASK_X
);
3570 struct tgsi_full_src_register src0_xxxx
=
3571 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_X
);
3572 struct tgsi_full_src_register src0_yyyy
=
3573 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_Y
);
3574 struct tgsi_full_src_register src1_xxxx
=
3575 scalar_src(&inst
->Src
[1], TGSI_SWIZZLE_X
);
3576 struct tgsi_full_src_register src1_yyyy
=
3577 scalar_src(&inst
->Src
[1], TGSI_SWIZZLE_Y
);
3578 struct tgsi_full_src_register src2_xxxx
=
3579 scalar_src(&inst
->Src
[2], TGSI_SWIZZLE_X
);
3581 emit_instruction_op3(emit
, VGPU10_OPCODE_MAD
, &tmp_dst_x
, &src0_yyyy
,
3582 &src1_yyyy
, &src2_xxxx
, FALSE
);
3583 emit_instruction_op3(emit
, VGPU10_OPCODE_MAD
, &tmp_dst_x
, &src0_xxxx
,
3584 &src1_xxxx
, &tmp_src_xxxx
, FALSE
);
3585 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &inst
->Dst
[0],
3586 &tmp_src_xxxx
, inst
->Instruction
.Saturate
);
3588 free_temp_indexes(emit
);
3595 * Emit code for TGSI_OPCODE_DPH instruction.
3598 emit_dph(struct svga_shader_emitter_v10
*emit
,
3599 const struct tgsi_full_instruction
*inst
)
3603 * ADD dst, tmp, s1.wwww
3606 struct tgsi_full_src_register s1_wwww
=
3607 swizzle_src(&inst
->Src
[1], TGSI_SWIZZLE_W
, TGSI_SWIZZLE_W
,
3608 TGSI_SWIZZLE_W
, TGSI_SWIZZLE_W
);
3610 unsigned tmp
= get_temp_index(emit
);
3611 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
3612 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
3614 /* DP3 tmp, s0, s1 */
3615 emit_instruction_op2(emit
, VGPU10_OPCODE_DP3
, &tmp_dst
, &inst
->Src
[0],
3616 &inst
->Src
[1], FALSE
);
3618 /* ADD dst, tmp, s1.wwww */
3619 emit_instruction_op2(emit
, VGPU10_OPCODE_ADD
, &inst
->Dst
[0], &tmp_src
,
3620 &s1_wwww
, inst
->Instruction
.Saturate
);
3622 free_temp_indexes(emit
);
3629 * Emit code for TGSI_OPCODE_DST instruction.
3632 emit_dst(struct svga_shader_emitter_v10
*emit
,
3633 const struct tgsi_full_instruction
*inst
)
3637 * dst.y = src0.y * src1.y
3642 struct tgsi_full_src_register s0_yyyy
=
3643 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_Y
);
3644 struct tgsi_full_src_register s0_zzzz
=
3645 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_Z
);
3646 struct tgsi_full_src_register s1_yyyy
=
3647 scalar_src(&inst
->Src
[1], TGSI_SWIZZLE_Y
);
3648 struct tgsi_full_src_register s1_wwww
=
3649 scalar_src(&inst
->Src
[1], TGSI_SWIZZLE_W
);
3652 * If dst and either src0 and src1 are the same we need
3653 * to create a temporary for it and insert a extra move.
3655 unsigned tmp_move
= get_temp_index(emit
);
3656 struct tgsi_full_src_register move_src
= make_src_temp_reg(tmp_move
);
3657 struct tgsi_full_dst_register move_dst
= make_dst_temp_reg(tmp_move
);
3659 /* MOV dst.x, 1.0 */
3660 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_X
) {
3661 struct tgsi_full_dst_register dst_x
=
3662 writemask_dst(&move_dst
, TGSI_WRITEMASK_X
);
3663 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
3665 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_x
, &one
, FALSE
);
3668 /* MUL dst.y, s0.y, s1.y */
3669 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Y
) {
3670 struct tgsi_full_dst_register dst_y
=
3671 writemask_dst(&move_dst
, TGSI_WRITEMASK_Y
);
3673 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &dst_y
, &s0_yyyy
,
3674 &s1_yyyy
, inst
->Instruction
.Saturate
);
3677 /* MOV dst.z, s0.z */
3678 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Z
) {
3679 struct tgsi_full_dst_register dst_z
=
3680 writemask_dst(&move_dst
, TGSI_WRITEMASK_Z
);
3682 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_z
, &s0_zzzz
,
3683 inst
->Instruction
.Saturate
);
3686 /* MOV dst.w, s1.w */
3687 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_W
) {
3688 struct tgsi_full_dst_register dst_w
=
3689 writemask_dst(&move_dst
, TGSI_WRITEMASK_W
);
3691 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_w
, &s1_wwww
,
3692 inst
->Instruction
.Saturate
);
3695 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &inst
->Dst
[0], &move_src
,
3697 free_temp_indexes(emit
);
3705 * Emit code for TGSI_OPCODE_ENDPRIM (GS only)
3708 emit_endprim(struct svga_shader_emitter_v10
*emit
,
3709 const struct tgsi_full_instruction
*inst
)
3711 assert(emit
->unit
== PIPE_SHADER_GEOMETRY
);
3713 /* We can't use emit_simple() because the TGSI instruction has one
3714 * operand (vertex stream number) which we must ignore for VGPU10.
3716 begin_emit_instruction(emit
);
3717 emit_opcode(emit
, VGPU10_OPCODE_CUT
, FALSE
);
3718 end_emit_instruction(emit
);
3724 * Emit code for TGSI_OPCODE_EX2 (2^x) instruction.
3727 emit_ex2(struct svga_shader_emitter_v10
*emit
,
3728 const struct tgsi_full_instruction
*inst
)
3730 /* Note that TGSI_OPCODE_EX2 computes only one value from src.x
3731 * while VGPU10 computes four values.
3734 * dst.xyzw = 2.0 ^ src.x
3737 struct tgsi_full_src_register src_xxxx
=
3738 swizzle_src(&inst
->Src
[0], TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
,
3739 TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
);
3741 /* EXP tmp, s0.xxxx */
3742 emit_instruction_op1(emit
, VGPU10_OPCODE_EXP
, &inst
->Dst
[0], &src_xxxx
,
3743 inst
->Instruction
.Saturate
);
3750 * Emit code for TGSI_OPCODE_EXP instruction.
3753 emit_exp(struct svga_shader_emitter_v10
*emit
,
3754 const struct tgsi_full_instruction
*inst
)
3757 * dst.x = 2 ^ floor(s0.x)
3758 * dst.y = s0.x - floor(s0.x)
3763 struct tgsi_full_src_register src_xxxx
=
3764 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_X
);
3765 unsigned tmp
= get_temp_index(emit
);
3766 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
3767 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
3770 * If dst and src are the same we need to create
3771 * a temporary for it and insert a extra move.
3773 unsigned tmp_move
= get_temp_index(emit
);
3774 struct tgsi_full_src_register move_src
= make_src_temp_reg(tmp_move
);
3775 struct tgsi_full_dst_register move_dst
= make_dst_temp_reg(tmp_move
);
3777 /* only use X component of temp reg */
3778 tmp_dst
= writemask_dst(&tmp_dst
, TGSI_WRITEMASK_X
);
3779 tmp_src
= scalar_src(&tmp_src
, TGSI_SWIZZLE_X
);
3781 /* ROUND_NI tmp.x, s0.x */
3782 emit_instruction_op1(emit
, VGPU10_OPCODE_ROUND_NI
, &tmp_dst
,
3783 &src_xxxx
, FALSE
); /* round to -infinity */
3785 /* EXP dst.x, tmp.x */
3786 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_X
) {
3787 struct tgsi_full_dst_register dst_x
=
3788 writemask_dst(&move_dst
, TGSI_WRITEMASK_X
);
3790 emit_instruction_op1(emit
, VGPU10_OPCODE_EXP
, &dst_x
, &tmp_src
,
3791 inst
->Instruction
.Saturate
);
3794 /* ADD dst.y, s0.x, -tmp */
3795 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Y
) {
3796 struct tgsi_full_dst_register dst_y
=
3797 writemask_dst(&move_dst
, TGSI_WRITEMASK_Y
);
3798 struct tgsi_full_src_register neg_tmp_src
= negate_src(&tmp_src
);
3800 emit_instruction_op2(emit
, VGPU10_OPCODE_ADD
, &dst_y
, &src_xxxx
,
3801 &neg_tmp_src
, inst
->Instruction
.Saturate
);
3804 /* EXP dst.z, s0.x */
3805 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Z
) {
3806 struct tgsi_full_dst_register dst_z
=
3807 writemask_dst(&move_dst
, TGSI_WRITEMASK_Z
);
3809 emit_instruction_op1(emit
, VGPU10_OPCODE_EXP
, &dst_z
, &src_xxxx
,
3810 inst
->Instruction
.Saturate
);
3813 /* MOV dst.w, 1.0 */
3814 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_W
) {
3815 struct tgsi_full_dst_register dst_w
=
3816 writemask_dst(&move_dst
, TGSI_WRITEMASK_W
);
3817 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
3819 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_w
, &one
,
3823 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &inst
->Dst
[0], &move_src
,
3826 free_temp_indexes(emit
);
3833 * Emit code for TGSI_OPCODE_IF instruction.
3836 emit_if(struct svga_shader_emitter_v10
*emit
,
3837 const struct tgsi_full_instruction
*inst
)
3839 VGPU10OpcodeToken0 opcode0
;
3841 /* The src register should be a scalar */
3842 assert(inst
->Src
[0].Register
.SwizzleX
== inst
->Src
[0].Register
.SwizzleY
&&
3843 inst
->Src
[0].Register
.SwizzleX
== inst
->Src
[0].Register
.SwizzleZ
&&
3844 inst
->Src
[0].Register
.SwizzleX
== inst
->Src
[0].Register
.SwizzleW
);
3846 /* The only special thing here is that we need to set the
3847 * VGPU10_INSTRUCTION_TEST_NONZERO flag since we want to test if
3848 * src.x is non-zero.
3851 opcode0
.opcodeType
= VGPU10_OPCODE_IF
;
3852 opcode0
.testBoolean
= VGPU10_INSTRUCTION_TEST_NONZERO
;
3854 begin_emit_instruction(emit
);
3855 emit_dword(emit
, opcode0
.value
);
3856 emit_src_register(emit
, &inst
->Src
[0]);
3857 end_emit_instruction(emit
);
3864 * Emit code for TGSI_OPCODE_KILL_IF instruction (kill fragment if any of
3865 * the register components are negative).
3868 emit_kill_if(struct svga_shader_emitter_v10
*emit
,
3869 const struct tgsi_full_instruction
*inst
)
3871 unsigned tmp
= get_temp_index(emit
);
3872 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
3873 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
3875 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
3877 struct tgsi_full_dst_register tmp_dst_x
=
3878 writemask_dst(&tmp_dst
, TGSI_WRITEMASK_X
);
3879 struct tgsi_full_src_register tmp_src_xxxx
=
3880 scalar_src(&tmp_src
, TGSI_SWIZZLE_X
);
3882 /* tmp = src[0] < 0.0 */
3883 emit_instruction_op2(emit
, VGPU10_OPCODE_LT
, &tmp_dst
, &inst
->Src
[0],
3886 if (!same_swizzle_terms(&inst
->Src
[0])) {
3887 /* If the swizzle is not XXXX, YYYY, ZZZZ or WWWW we need to
3888 * logically OR the swizzle terms. Most uses of KILL_IF only
3889 * test one channel so it's good to avoid these extra steps.
3891 struct tgsi_full_src_register tmp_src_yyyy
=
3892 scalar_src(&tmp_src
, TGSI_SWIZZLE_Y
);
3893 struct tgsi_full_src_register tmp_src_zzzz
=
3894 scalar_src(&tmp_src
, TGSI_SWIZZLE_Z
);
3895 struct tgsi_full_src_register tmp_src_wwww
=
3896 scalar_src(&tmp_src
, TGSI_SWIZZLE_W
);
3898 emit_instruction_op2(emit
, VGPU10_OPCODE_OR
, &tmp_dst_x
, &tmp_src_xxxx
,
3899 &tmp_src_yyyy
, FALSE
);
3900 emit_instruction_op2(emit
, VGPU10_OPCODE_OR
, &tmp_dst_x
, &tmp_src_xxxx
,
3901 &tmp_src_zzzz
, FALSE
);
3902 emit_instruction_op2(emit
, VGPU10_OPCODE_OR
, &tmp_dst_x
, &tmp_src_xxxx
,
3903 &tmp_src_wwww
, FALSE
);
3906 begin_emit_instruction(emit
);
3907 emit_discard_opcode(emit
, TRUE
); /* discard if src0.x is non-zero */
3908 emit_src_register(emit
, &tmp_src_xxxx
);
3909 end_emit_instruction(emit
);
3911 free_temp_indexes(emit
);
3918 * Emit code for TGSI_OPCODE_KILL instruction (unconditional discard).
3921 emit_kill(struct svga_shader_emitter_v10
*emit
,
3922 const struct tgsi_full_instruction
*inst
)
3924 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
3926 /* DISCARD if 0.0 is zero */
3927 begin_emit_instruction(emit
);
3928 emit_discard_opcode(emit
, FALSE
);
3929 emit_src_register(emit
, &zero
);
3930 end_emit_instruction(emit
);
3937 * Emit code for TGSI_OPCODE_LG2 instruction.
3940 emit_lg2(struct svga_shader_emitter_v10
*emit
,
3941 const struct tgsi_full_instruction
*inst
)
3943 /* Note that TGSI_OPCODE_LG2 computes only one value from src.x
3944 * while VGPU10 computes four values.
3947 * dst.xyzw = log2(src.x)
3950 struct tgsi_full_src_register src_xxxx
=
3951 swizzle_src(&inst
->Src
[0], TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
,
3952 TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
);
3954 /* LOG tmp, s0.xxxx */
3955 emit_instruction_op1(emit
, VGPU10_OPCODE_LOG
, &inst
->Dst
[0], &src_xxxx
,
3956 inst
->Instruction
.Saturate
);
3963 * Emit code for TGSI_OPCODE_LIT instruction.
3966 emit_lit(struct svga_shader_emitter_v10
*emit
,
3967 const struct tgsi_full_instruction
*inst
)
3969 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
3972 * If dst and src are the same we need to create
3973 * a temporary for it and insert a extra move.
3975 unsigned tmp_move
= get_temp_index(emit
);
3976 struct tgsi_full_src_register move_src
= make_src_temp_reg(tmp_move
);
3977 struct tgsi_full_dst_register move_dst
= make_dst_temp_reg(tmp_move
);
3981 * dst.y = max(src.x, 0)
3982 * dst.z = (src.x > 0) ? max(src.y, 0)^{clamp(src.w, -128, 128))} : 0
3986 /* MOV dst.x, 1.0 */
3987 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_X
) {
3988 struct tgsi_full_dst_register dst_x
=
3989 writemask_dst(&move_dst
, TGSI_WRITEMASK_X
);
3990 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_x
, &one
, FALSE
);
3993 /* MOV dst.w, 1.0 */
3994 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_W
) {
3995 struct tgsi_full_dst_register dst_w
=
3996 writemask_dst(&move_dst
, TGSI_WRITEMASK_W
);
3997 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_w
, &one
, FALSE
);
4000 /* MAX dst.y, src.x, 0.0 */
4001 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Y
) {
4002 struct tgsi_full_dst_register dst_y
=
4003 writemask_dst(&move_dst
, TGSI_WRITEMASK_Y
);
4004 struct tgsi_full_src_register zero
=
4005 make_immediate_reg_float(emit
, 0.0f
);
4006 struct tgsi_full_src_register src_xxxx
=
4007 swizzle_src(&inst
->Src
[0], TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
,
4008 TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
);
4010 emit_instruction_op2(emit
, VGPU10_OPCODE_MAX
, &dst_y
, &src_xxxx
,
4011 &zero
, inst
->Instruction
.Saturate
);
4015 * tmp1 = clamp(src.w, -128, 128);
4016 * MAX tmp1, src.w, -128
4017 * MIN tmp1, tmp1, 128
4019 * tmp2 = max(tmp2, 0);
4020 * MAX tmp2, src.y, 0
4022 * tmp1 = pow(tmp2, tmp1);
4024 * MUL tmp1, tmp2, tmp1
4027 * tmp1 = (src.w == 0) ? 1 : tmp1;
4029 * MOVC tmp1, tmp2, 1.0, tmp1
4031 * dst.z = (0 < src.x) ? tmp1 : 0;
4033 * MOVC dst.z, tmp2, tmp1, 0.0
4035 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Z
) {
4036 struct tgsi_full_dst_register dst_z
=
4037 writemask_dst(&move_dst
, TGSI_WRITEMASK_Z
);
4039 unsigned tmp1
= get_temp_index(emit
);
4040 struct tgsi_full_src_register tmp1_src
= make_src_temp_reg(tmp1
);
4041 struct tgsi_full_dst_register tmp1_dst
= make_dst_temp_reg(tmp1
);
4042 unsigned tmp2
= get_temp_index(emit
);
4043 struct tgsi_full_src_register tmp2_src
= make_src_temp_reg(tmp2
);
4044 struct tgsi_full_dst_register tmp2_dst
= make_dst_temp_reg(tmp2
);
4046 struct tgsi_full_src_register src_xxxx
=
4047 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_X
);
4048 struct tgsi_full_src_register src_yyyy
=
4049 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_Y
);
4050 struct tgsi_full_src_register src_wwww
=
4051 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_W
);
4053 struct tgsi_full_src_register zero
=
4054 make_immediate_reg_float(emit
, 0.0f
);
4055 struct tgsi_full_src_register lowerbound
=
4056 make_immediate_reg_float(emit
, -128.0f
);
4057 struct tgsi_full_src_register upperbound
=
4058 make_immediate_reg_float(emit
, 128.0f
);
4060 emit_instruction_op2(emit
, VGPU10_OPCODE_MAX
, &tmp1_dst
, &src_wwww
,
4061 &lowerbound
, FALSE
);
4062 emit_instruction_op2(emit
, VGPU10_OPCODE_MIN
, &tmp1_dst
, &tmp1_src
,
4063 &upperbound
, FALSE
);
4064 emit_instruction_op2(emit
, VGPU10_OPCODE_MAX
, &tmp2_dst
, &src_yyyy
,
4067 /* POW tmp1, tmp2, tmp1 */
4068 /* LOG tmp2, tmp2 */
4069 emit_instruction_op1(emit
, VGPU10_OPCODE_LOG
, &tmp2_dst
, &tmp2_src
,
4072 /* MUL tmp1, tmp2, tmp1 */
4073 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &tmp1_dst
, &tmp2_src
,
4076 /* EXP tmp1, tmp1 */
4077 emit_instruction_op1(emit
, VGPU10_OPCODE_EXP
, &tmp1_dst
, &tmp1_src
,
4080 /* EQ tmp2, 0, src.w */
4081 emit_instruction_op2(emit
, VGPU10_OPCODE_EQ
, &tmp2_dst
, &zero
,
4083 /* MOVC tmp1.z, tmp2, tmp1, 1.0 */
4084 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &tmp1_dst
,
4085 &tmp2_src
, &one
, &tmp1_src
, FALSE
);
4087 /* LT tmp2, 0, src.x */
4088 emit_instruction_op2(emit
, VGPU10_OPCODE_LT
, &tmp2_dst
, &zero
,
4090 /* MOVC dst.z, tmp2, tmp1, 0.0 */
4091 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &dst_z
,
4092 &tmp2_src
, &tmp1_src
, &zero
, FALSE
);
4095 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &inst
->Dst
[0], &move_src
,
4097 free_temp_indexes(emit
);
4104 * Emit code for TGSI_OPCODE_LOG instruction.
4107 emit_log(struct svga_shader_emitter_v10
*emit
,
4108 const struct tgsi_full_instruction
*inst
)
4111 * dst.x = floor(lg2(abs(s0.x)))
4112 * dst.y = abs(s0.x) / (2 ^ floor(lg2(abs(s0.x))))
4113 * dst.z = lg2(abs(s0.x))
4117 struct tgsi_full_src_register src_xxxx
=
4118 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_X
);
4119 unsigned tmp
= get_temp_index(emit
);
4120 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4121 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4122 struct tgsi_full_src_register abs_src_xxxx
= absolute_src(&src_xxxx
);
4124 /* only use X component of temp reg */
4125 tmp_dst
= writemask_dst(&tmp_dst
, TGSI_WRITEMASK_X
);
4126 tmp_src
= scalar_src(&tmp_src
, TGSI_SWIZZLE_X
);
4128 /* LOG tmp.x, abs(s0.x) */
4129 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_XYZ
) {
4130 emit_instruction_op1(emit
, VGPU10_OPCODE_LOG
, &tmp_dst
,
4131 &abs_src_xxxx
, FALSE
);
4134 /* MOV dst.z, tmp.x */
4135 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Z
) {
4136 struct tgsi_full_dst_register dst_z
=
4137 writemask_dst(&inst
->Dst
[0], TGSI_WRITEMASK_Z
);
4139 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_z
,
4140 &tmp_src
, inst
->Instruction
.Saturate
);
4143 /* FLR tmp.x, tmp.x */
4144 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_XY
) {
4145 emit_instruction_op1(emit
, VGPU10_OPCODE_ROUND_NI
, &tmp_dst
,
4149 /* MOV dst.x, tmp.x */
4150 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_X
) {
4151 struct tgsi_full_dst_register dst_x
=
4152 writemask_dst(&inst
->Dst
[0], TGSI_WRITEMASK_X
);
4154 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_x
, &tmp_src
,
4155 inst
->Instruction
.Saturate
);
4158 /* EXP tmp.x, tmp.x */
4159 /* DIV dst.y, abs(s0.x), tmp.x */
4160 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Y
) {
4161 struct tgsi_full_dst_register dst_y
=
4162 writemask_dst(&inst
->Dst
[0], TGSI_WRITEMASK_Y
);
4164 emit_instruction_op1(emit
, VGPU10_OPCODE_EXP
, &tmp_dst
, &tmp_src
,
4166 emit_instruction_op2(emit
, VGPU10_OPCODE_DIV
, &dst_y
, &abs_src_xxxx
,
4167 &tmp_src
, inst
->Instruction
.Saturate
);
4170 /* MOV dst.w, 1.0 */
4171 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_W
) {
4172 struct tgsi_full_dst_register dst_w
=
4173 writemask_dst(&inst
->Dst
[0], TGSI_WRITEMASK_W
);
4174 struct tgsi_full_src_register one
=
4175 make_immediate_reg_float(emit
, 1.0f
);
4177 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_w
, &one
, FALSE
);
4180 free_temp_indexes(emit
);
4187 * Emit code for TGSI_OPCODE_LRP instruction.
4190 emit_lrp(struct svga_shader_emitter_v10
*emit
,
4191 const struct tgsi_full_instruction
*inst
)
4193 /* dst = LRP(s0, s1, s2):
4194 * dst = s0 * (s1 - s2) + s2
4196 * SUB tmp, s1, s2; tmp = s1 - s2
4197 * MAD dst, s0, tmp, s2; dst = s0 * t1 + s2
4199 unsigned tmp
= get_temp_index(emit
);
4200 struct tgsi_full_src_register src_tmp
= make_src_temp_reg(tmp
);
4201 struct tgsi_full_dst_register dst_tmp
= make_dst_temp_reg(tmp
);
4202 struct tgsi_full_src_register neg_src2
= negate_src(&inst
->Src
[2]);
4204 /* ADD tmp, s1, -s2 */
4205 emit_instruction_op2(emit
, VGPU10_OPCODE_ADD
, &dst_tmp
,
4206 &inst
->Src
[1], &neg_src2
, FALSE
);
4208 /* MAD dst, s1, tmp, s3 */
4209 emit_instruction_op3(emit
, VGPU10_OPCODE_MAD
, &inst
->Dst
[0],
4210 &inst
->Src
[0], &src_tmp
, &inst
->Src
[2],
4211 inst
->Instruction
.Saturate
);
4213 free_temp_indexes(emit
);
4220 * Emit code for TGSI_OPCODE_POW instruction.
4223 emit_pow(struct svga_shader_emitter_v10
*emit
,
4224 const struct tgsi_full_instruction
*inst
)
4226 /* Note that TGSI_OPCODE_POW computes only one value from src0.x and
4227 * src1.x while VGPU10 computes four values.
4229 * dst = POW(src0, src1):
4230 * dst.xyzw = src0.x ^ src1.x
4232 unsigned tmp
= get_temp_index(emit
);
4233 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4234 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4235 struct tgsi_full_src_register src0_xxxx
=
4236 swizzle_src(&inst
->Src
[0], TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
,
4237 TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
);
4238 struct tgsi_full_src_register src1_xxxx
=
4239 swizzle_src(&inst
->Src
[1], TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
,
4240 TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
);
4242 /* LOG tmp, s0.xxxx */
4243 emit_instruction_op1(emit
, VGPU10_OPCODE_LOG
, &tmp_dst
, &src0_xxxx
,
4246 /* MUL tmp, tmp, s1.xxxx */
4247 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &tmp_dst
, &tmp_src
,
4250 /* EXP tmp, s0.xxxx */
4251 emit_instruction_op1(emit
, VGPU10_OPCODE_EXP
, &inst
->Dst
[0],
4252 &tmp_src
, inst
->Instruction
.Saturate
);
4255 free_temp_indexes(emit
);
4262 * Emit code for TGSI_OPCODE_RCP (reciprocal) instruction.
4265 emit_rcp(struct svga_shader_emitter_v10
*emit
,
4266 const struct tgsi_full_instruction
*inst
)
4268 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
4270 unsigned tmp
= get_temp_index(emit
);
4271 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4272 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4274 struct tgsi_full_dst_register tmp_dst_x
=
4275 writemask_dst(&tmp_dst
, TGSI_WRITEMASK_X
);
4276 struct tgsi_full_src_register tmp_src_xxxx
=
4277 scalar_src(&tmp_src
, TGSI_SWIZZLE_X
);
4279 /* DIV tmp.x, 1.0, s0 */
4280 emit_instruction_op2(emit
, VGPU10_OPCODE_DIV
, &tmp_dst_x
, &one
,
4281 &inst
->Src
[0], FALSE
);
4283 /* MOV dst, tmp.xxxx */
4284 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &inst
->Dst
[0],
4285 &tmp_src_xxxx
, inst
->Instruction
.Saturate
);
4287 free_temp_indexes(emit
);
4294 * Emit code for TGSI_OPCODE_RSQ instruction.
4297 emit_rsq(struct svga_shader_emitter_v10
*emit
,
4298 const struct tgsi_full_instruction
*inst
)
4301 * dst.xyzw = 1 / sqrt(src.x)
4307 unsigned tmp
= get_temp_index(emit
);
4308 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4309 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4311 struct tgsi_full_dst_register tmp_dst_x
=
4312 writemask_dst(&tmp_dst
, TGSI_WRITEMASK_X
);
4313 struct tgsi_full_src_register tmp_src_xxxx
=
4314 scalar_src(&tmp_src
, TGSI_SWIZZLE_X
);
4316 /* RSQ tmp, src.x */
4317 emit_instruction_op1(emit
, VGPU10_OPCODE_RSQ
, &tmp_dst_x
,
4318 &inst
->Src
[0], FALSE
);
4320 /* MOV dst, tmp.xxxx */
4321 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &inst
->Dst
[0],
4322 &tmp_src_xxxx
, inst
->Instruction
.Saturate
);
4325 free_temp_indexes(emit
);
4332 * Emit code for TGSI_OPCODE_SCS instruction.
4335 emit_scs(struct svga_shader_emitter_v10
*emit
,
4336 const struct tgsi_full_instruction
*inst
)
4338 /* dst.x = cos(src.x)
4339 * dst.y = sin(src.x)
4343 struct tgsi_full_dst_register dst_x
=
4344 writemask_dst(&inst
->Dst
[0], TGSI_WRITEMASK_X
);
4345 struct tgsi_full_dst_register dst_y
=
4346 writemask_dst(&inst
->Dst
[0], TGSI_WRITEMASK_Y
);
4347 struct tgsi_full_dst_register dst_zw
=
4348 writemask_dst(&inst
->Dst
[0], TGSI_WRITEMASK_ZW
);
4350 struct tgsi_full_src_register zero_one
=
4351 make_immediate_reg_float4(emit
, 0.0f
, 0.0f
, 0.0f
, 1.0f
);
4353 begin_emit_instruction(emit
);
4354 emit_opcode(emit
, VGPU10_OPCODE_SINCOS
, inst
->Instruction
.Saturate
);
4355 emit_dst_register(emit
, &dst_y
);
4356 emit_dst_register(emit
, &dst_x
);
4357 emit_src_register(emit
, &inst
->Src
[0]);
4358 end_emit_instruction(emit
);
4360 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
,
4361 &dst_zw
, &zero_one
, inst
->Instruction
.Saturate
);
4368 * Emit code for TGSI_OPCODE_SEQ (Set Equal) instruction.
4371 emit_seq(struct svga_shader_emitter_v10
*emit
,
4372 const struct tgsi_full_instruction
*inst
)
4374 /* dst = SEQ(s0, s1):
4375 * dst = s0 == s1 ? 1.0 : 0.0 (per component)
4377 * EQ tmp, s0, s1; tmp = s0 == s1 : 0xffffffff : 0 (per comp)
4378 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
4380 unsigned tmp
= get_temp_index(emit
);
4381 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4382 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4383 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
4384 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
4386 /* EQ tmp, s0, s1 */
4387 emit_instruction_op2(emit
, VGPU10_OPCODE_EQ
, &tmp_dst
, &inst
->Src
[0],
4388 &inst
->Src
[1], FALSE
);
4390 /* MOVC dst, tmp, one, zero */
4391 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &inst
->Dst
[0], &tmp_src
,
4392 &one
, &zero
, FALSE
);
4394 free_temp_indexes(emit
);
4401 * Emit code for TGSI_OPCODE_SGE (Set Greater than or Equal) instruction.
4404 emit_sge(struct svga_shader_emitter_v10
*emit
,
4405 const struct tgsi_full_instruction
*inst
)
4407 /* dst = SGE(s0, s1):
4408 * dst = s0 >= s1 ? 1.0 : 0.0 (per component)
4410 * GE tmp, s0, s1; tmp = s0 >= s1 : 0xffffffff : 0 (per comp)
4411 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
4413 unsigned tmp
= get_temp_index(emit
);
4414 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4415 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4416 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
4417 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
4419 /* GE tmp, s0, s1 */
4420 emit_instruction_op2(emit
, VGPU10_OPCODE_GE
, &tmp_dst
, &inst
->Src
[0],
4421 &inst
->Src
[1], FALSE
);
4423 /* MOVC dst, tmp, one, zero */
4424 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &inst
->Dst
[0], &tmp_src
,
4425 &one
, &zero
, FALSE
);
4427 free_temp_indexes(emit
);
4434 * Emit code for TGSI_OPCODE_SGT (Set Greater than) instruction.
4437 emit_sgt(struct svga_shader_emitter_v10
*emit
,
4438 const struct tgsi_full_instruction
*inst
)
4440 /* dst = SGT(s0, s1):
4441 * dst = s0 > s1 ? 1.0 : 0.0 (per component)
4443 * LT tmp, s1, s0; tmp = s1 < s0 ? 0xffffffff : 0 (per comp)
4444 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
4446 unsigned tmp
= get_temp_index(emit
);
4447 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4448 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4449 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
4450 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
4452 /* LT tmp, s1, s0 */
4453 emit_instruction_op2(emit
, VGPU10_OPCODE_LT
, &tmp_dst
, &inst
->Src
[1],
4454 &inst
->Src
[0], FALSE
);
4456 /* MOVC dst, tmp, one, zero */
4457 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &inst
->Dst
[0], &tmp_src
,
4458 &one
, &zero
, FALSE
);
4460 free_temp_indexes(emit
);
4467 * Emit code for TGSI_OPCODE_SIN and TGSI_OPCODE_COS instructions.
4470 emit_sincos(struct svga_shader_emitter_v10
*emit
,
4471 const struct tgsi_full_instruction
*inst
)
4473 unsigned tmp
= get_temp_index(emit
);
4474 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4475 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4477 struct tgsi_full_src_register tmp_src_xxxx
=
4478 scalar_src(&tmp_src
, TGSI_SWIZZLE_X
);
4479 struct tgsi_full_dst_register tmp_dst_x
=
4480 writemask_dst(&tmp_dst
, TGSI_WRITEMASK_X
);
4482 begin_emit_instruction(emit
);
4483 emit_opcode(emit
, VGPU10_OPCODE_SINCOS
, FALSE
);
4485 if(inst
->Instruction
.Opcode
== TGSI_OPCODE_SIN
)
4487 emit_dst_register(emit
, &tmp_dst_x
); /* first destination register */
4488 emit_null_dst_register(emit
); /* second destination register */
4491 emit_null_dst_register(emit
);
4492 emit_dst_register(emit
, &tmp_dst_x
);
4495 emit_src_register(emit
, &inst
->Src
[0]);
4496 end_emit_instruction(emit
);
4498 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &inst
->Dst
[0],
4499 &tmp_src_xxxx
, inst
->Instruction
.Saturate
);
4501 free_temp_indexes(emit
);
4508 * Emit code for TGSI_OPCODE_SLE (Set Less than or Equal) instruction.
4511 emit_sle(struct svga_shader_emitter_v10
*emit
,
4512 const struct tgsi_full_instruction
*inst
)
4514 /* dst = SLE(s0, s1):
4515 * dst = s0 <= s1 ? 1.0 : 0.0 (per component)
4517 * GE tmp, s1, s0; tmp = s1 >= s0 : 0xffffffff : 0 (per comp)
4518 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
4520 unsigned tmp
= get_temp_index(emit
);
4521 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4522 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4523 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
4524 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
4526 /* GE tmp, s1, s0 */
4527 emit_instruction_op2(emit
, VGPU10_OPCODE_GE
, &tmp_dst
, &inst
->Src
[1],
4528 &inst
->Src
[0], FALSE
);
4530 /* MOVC dst, tmp, one, zero */
4531 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &inst
->Dst
[0], &tmp_src
,
4532 &one
, &zero
, FALSE
);
4534 free_temp_indexes(emit
);
4541 * Emit code for TGSI_OPCODE_SLT (Set Less than) instruction.
4544 emit_slt(struct svga_shader_emitter_v10
*emit
,
4545 const struct tgsi_full_instruction
*inst
)
4547 /* dst = SLT(s0, s1):
4548 * dst = s0 < s1 ? 1.0 : 0.0 (per component)
4550 * LT tmp, s0, s1; tmp = s0 < s1 ? 0xffffffff : 0 (per comp)
4551 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
4553 unsigned tmp
= get_temp_index(emit
);
4554 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4555 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4556 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
4557 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
4559 /* LT tmp, s0, s1 */
4560 emit_instruction_op2(emit
, VGPU10_OPCODE_LT
, &tmp_dst
, &inst
->Src
[0],
4561 &inst
->Src
[1], FALSE
);
4563 /* MOVC dst, tmp, one, zero */
4564 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &inst
->Dst
[0], &tmp_src
,
4565 &one
, &zero
, FALSE
);
4567 free_temp_indexes(emit
);
4574 * Emit code for TGSI_OPCODE_SNE (Set Not Equal) instruction.
4577 emit_sne(struct svga_shader_emitter_v10
*emit
,
4578 const struct tgsi_full_instruction
*inst
)
4580 /* dst = SNE(s0, s1):
4581 * dst = s0 != s1 ? 1.0 : 0.0 (per component)
4583 * EQ tmp, s0, s1; tmp = s0 == s1 : 0xffffffff : 0 (per comp)
4584 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
4586 unsigned tmp
= get_temp_index(emit
);
4587 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4588 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4589 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
4590 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
4592 /* NE tmp, s0, s1 */
4593 emit_instruction_op2(emit
, VGPU10_OPCODE_NE
, &tmp_dst
, &inst
->Src
[0],
4594 &inst
->Src
[1], FALSE
);
4596 /* MOVC dst, tmp, one, zero */
4597 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &inst
->Dst
[0], &tmp_src
,
4598 &one
, &zero
, FALSE
);
4600 free_temp_indexes(emit
);
4607 * Emit code for TGSI_OPCODE_SSG (Set Sign) instruction.
4610 emit_ssg(struct svga_shader_emitter_v10
*emit
,
4611 const struct tgsi_full_instruction
*inst
)
4613 /* dst.x = (src.x > 0.0) ? 1.0 : (src.x < 0.0) ? -1.0 : 0.0
4614 * dst.y = (src.y > 0.0) ? 1.0 : (src.y < 0.0) ? -1.0 : 0.0
4615 * dst.z = (src.z > 0.0) ? 1.0 : (src.z < 0.0) ? -1.0 : 0.0
4616 * dst.w = (src.w > 0.0) ? 1.0 : (src.w < 0.0) ? -1.0 : 0.0
4618 * LT tmp1, src, zero; tmp1 = src < zero ? 0xffffffff : 0 (per comp)
4619 * MOVC tmp2, tmp1, -1.0, 0.0; tmp2 = tmp1 ? -1.0 : 0.0 (per component)
4620 * LT tmp1, zero, src; tmp1 = zero < src ? 0xffffffff : 0 (per comp)
4621 * MOVC dst, tmp1, 1.0, tmp2; dst = tmp1 ? 1.0 : tmp2 (per component)
4623 struct tgsi_full_src_register zero
=
4624 make_immediate_reg_float(emit
, 0.0f
);
4625 struct tgsi_full_src_register one
=
4626 make_immediate_reg_float(emit
, 1.0f
);
4627 struct tgsi_full_src_register neg_one
=
4628 make_immediate_reg_float(emit
, -1.0f
);
4630 unsigned tmp1
= get_temp_index(emit
);
4631 struct tgsi_full_src_register tmp1_src
= make_src_temp_reg(tmp1
);
4632 struct tgsi_full_dst_register tmp1_dst
= make_dst_temp_reg(tmp1
);
4634 unsigned tmp2
= get_temp_index(emit
);
4635 struct tgsi_full_src_register tmp2_src
= make_src_temp_reg(tmp2
);
4636 struct tgsi_full_dst_register tmp2_dst
= make_dst_temp_reg(tmp2
);
4638 emit_instruction_op2(emit
, VGPU10_OPCODE_LT
, &tmp1_dst
, &inst
->Src
[0],
4640 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &tmp2_dst
, &tmp1_src
,
4641 &neg_one
, &zero
, FALSE
);
4642 emit_instruction_op2(emit
, VGPU10_OPCODE_LT
, &tmp1_dst
, &zero
,
4643 &inst
->Src
[0], FALSE
);
4644 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &inst
->Dst
[0], &tmp1_src
,
4645 &one
, &tmp2_src
, FALSE
);
4647 free_temp_indexes(emit
);
4654 * Emit code for TGSI_OPCODE_ISSG (Integer Set Sign) instruction.
4657 emit_issg(struct svga_shader_emitter_v10
*emit
,
4658 const struct tgsi_full_instruction
*inst
)
4660 /* dst.x = (src.x > 0) ? 1 : (src.x < 0) ? -1 : 0
4661 * dst.y = (src.y > 0) ? 1 : (src.y < 0) ? -1 : 0
4662 * dst.z = (src.z > 0) ? 1 : (src.z < 0) ? -1 : 0
4663 * dst.w = (src.w > 0) ? 1 : (src.w < 0) ? -1 : 0
4665 * ILT tmp1, src, 0 tmp1 = src < 0 ? -1 : 0 (per component)
4666 * ILT tmp2, 0, src tmp2 = 0 < src ? -1 : 0 (per component)
4667 * IADD dst, tmp1, neg(tmp2) dst = tmp1 - tmp2 (per component)
4669 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
4671 unsigned tmp1
= get_temp_index(emit
);
4672 struct tgsi_full_src_register tmp1_src
= make_src_temp_reg(tmp1
);
4673 struct tgsi_full_dst_register tmp1_dst
= make_dst_temp_reg(tmp1
);
4675 unsigned tmp2
= get_temp_index(emit
);
4676 struct tgsi_full_src_register tmp2_src
= make_src_temp_reg(tmp2
);
4677 struct tgsi_full_dst_register tmp2_dst
= make_dst_temp_reg(tmp2
);
4679 struct tgsi_full_src_register neg_tmp2
= negate_src(&tmp2_src
);
4681 emit_instruction_op2(emit
, VGPU10_OPCODE_ILT
, &tmp1_dst
,
4682 &inst
->Src
[0], &zero
, FALSE
);
4683 emit_instruction_op2(emit
, VGPU10_OPCODE_ILT
, &tmp2_dst
,
4684 &zero
, &inst
->Src
[0], FALSE
);
4685 emit_instruction_op2(emit
, VGPU10_OPCODE_IADD
, &inst
->Dst
[0],
4686 &tmp1_src
, &neg_tmp2
, FALSE
);
4688 free_temp_indexes(emit
);
4695 * Emit code for TGSI_OPCODE_SUB instruction.
4698 emit_sub(struct svga_shader_emitter_v10
*emit
,
4699 const struct tgsi_full_instruction
*inst
)
4701 /* dst = SUB(s0, s1):
4704 * ADD dst, s0, neg(s1)
4706 struct tgsi_full_src_register neg_src1
= negate_src(&inst
->Src
[1]);
4708 /* ADD dst, s0, neg(s1) */
4709 emit_instruction_op2(emit
, VGPU10_OPCODE_ADD
, &inst
->Dst
[0],
4710 &inst
->Src
[0], &neg_src1
,
4711 inst
->Instruction
.Saturate
);
4718 * Emit a comparison instruction. The dest register will get
4719 * 0 or ~0 values depending on the outcome of comparing src0 to src1.
4722 emit_comparison(struct svga_shader_emitter_v10
*emit
,
4724 const struct tgsi_full_dst_register
*dst
,
4725 const struct tgsi_full_src_register
*src0
,
4726 const struct tgsi_full_src_register
*src1
)
4728 struct tgsi_full_src_register immediate
;
4729 VGPU10OpcodeToken0 opcode0
;
4730 boolean swapSrc
= FALSE
;
4732 /* Sanity checks for svga vs. gallium enums */
4733 STATIC_ASSERT(SVGA3D_CMP_LESS
== (PIPE_FUNC_LESS
+ 1));
4734 STATIC_ASSERT(SVGA3D_CMP_GREATEREQUAL
== (PIPE_FUNC_GEQUAL
+ 1));
4739 case SVGA3D_CMP_NEVER
:
4740 immediate
= make_immediate_reg_int(emit
, 0);
4742 begin_emit_instruction(emit
);
4743 emit_dword(emit
, VGPU10_OPCODE_MOV
);
4744 emit_dst_register(emit
, dst
);
4745 emit_src_register(emit
, &immediate
);
4746 end_emit_instruction(emit
);
4748 case SVGA3D_CMP_ALWAYS
:
4749 immediate
= make_immediate_reg_int(emit
, -1);
4751 begin_emit_instruction(emit
);
4752 emit_dword(emit
, VGPU10_OPCODE_MOV
);
4753 emit_dst_register(emit
, dst
);
4754 emit_src_register(emit
, &immediate
);
4755 end_emit_instruction(emit
);
4757 case SVGA3D_CMP_LESS
:
4758 opcode0
.opcodeType
= VGPU10_OPCODE_LT
;
4760 case SVGA3D_CMP_EQUAL
:
4761 opcode0
.opcodeType
= VGPU10_OPCODE_EQ
;
4763 case SVGA3D_CMP_LESSEQUAL
:
4764 opcode0
.opcodeType
= VGPU10_OPCODE_GE
;
4767 case SVGA3D_CMP_GREATER
:
4768 opcode0
.opcodeType
= VGPU10_OPCODE_LT
;
4771 case SVGA3D_CMP_NOTEQUAL
:
4772 opcode0
.opcodeType
= VGPU10_OPCODE_NE
;
4774 case SVGA3D_CMP_GREATEREQUAL
:
4775 opcode0
.opcodeType
= VGPU10_OPCODE_GE
;
4778 assert(!"Unexpected comparison mode");
4779 opcode0
.opcodeType
= VGPU10_OPCODE_EQ
;
4782 begin_emit_instruction(emit
);
4783 emit_dword(emit
, opcode0
.value
);
4784 emit_dst_register(emit
, dst
);
4786 emit_src_register(emit
, src1
);
4787 emit_src_register(emit
, src0
);
4790 emit_src_register(emit
, src0
);
4791 emit_src_register(emit
, src1
);
4793 end_emit_instruction(emit
);
4798 * Get texel/address offsets for a texture instruction.
4801 get_texel_offsets(const struct svga_shader_emitter_v10
*emit
,
4802 const struct tgsi_full_instruction
*inst
, int offsets
[3])
4804 if (inst
->Texture
.NumOffsets
== 1) {
4805 /* According to OpenGL Shader Language spec the offsets are only
4806 * fetched from a previously-declared immediate/literal.
4808 const struct tgsi_texture_offset
*off
= inst
->TexOffsets
;
4809 const unsigned index
= off
[0].Index
;
4810 const unsigned swizzleX
= off
[0].SwizzleX
;
4811 const unsigned swizzleY
= off
[0].SwizzleY
;
4812 const unsigned swizzleZ
= off
[0].SwizzleZ
;
4813 const union tgsi_immediate_data
*imm
= emit
->immediates
[index
];
4815 assert(inst
->TexOffsets
[0].File
== TGSI_FILE_IMMEDIATE
);
4817 offsets
[0] = imm
[swizzleX
].Int
;
4818 offsets
[1] = imm
[swizzleY
].Int
;
4819 offsets
[2] = imm
[swizzleZ
].Int
;
4822 offsets
[0] = offsets
[1] = offsets
[2] = 0;
4828 * Set up the coordinate register for texture sampling.
4829 * When we're sampling from a RECT texture we have to scale the
4830 * unnormalized coordinate to a normalized coordinate.
4831 * We do that by multiplying the coordinate by an "extra" constant.
4832 * An alternative would be to use the RESINFO instruction to query the
4835 static struct tgsi_full_src_register
4836 setup_texcoord(struct svga_shader_emitter_v10
*emit
,
4838 const struct tgsi_full_src_register
*coord
)
4840 if (emit
->key
.tex
[unit
].unnormalized
) {
4841 unsigned scale_index
= emit
->texcoord_scale_index
[unit
];
4842 unsigned tmp
= get_temp_index(emit
);
4843 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4844 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4845 struct tgsi_full_src_register scale_src
= make_src_const_reg(scale_index
);
4847 /* MUL tmp, coord, const[] */
4848 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &tmp_dst
,
4849 coord
, &scale_src
, FALSE
);
4853 /* use texcoord as-is */
4860 * For SAMPLE_C instructions, emit the extra src register which indicates
4861 * the reference/comparision value.
4864 emit_tex_compare_refcoord(struct svga_shader_emitter_v10
*emit
,
4866 const struct tgsi_full_src_register
*coord
)
4868 struct tgsi_full_src_register coord_src_ref
;
4871 assert(tgsi_is_shadow_target(target
));
4873 assert(target
!= TGSI_TEXTURE_SHADOWCUBE_ARRAY
); /* XXX not implemented */
4874 if (target
== TGSI_TEXTURE_SHADOW2D_ARRAY
||
4875 target
== TGSI_TEXTURE_SHADOWCUBE
)
4876 component
= TGSI_SWIZZLE_W
;
4878 component
= TGSI_SWIZZLE_Z
;
4880 coord_src_ref
= scalar_src(coord
, component
);
4882 emit_src_register(emit
, &coord_src_ref
);
4887 * Info for implementing texture swizzles.
4888 * The begin_tex_swizzle(), get_tex_swizzle_dst() and end_tex_swizzle()
4889 * functions use this to encapsulate the extra steps needed to perform
4890 * a texture swizzle, or shadow/depth comparisons.
4891 * The shadow/depth comparison is only done here if for the cases where
4892 * there's no VGPU10 opcode (like texture bias lookup w/ shadow compare).
4894 struct tex_swizzle_info
4897 boolean shadow_compare
;
4899 unsigned texture_target
; /**< TGSI_TEXTURE_x */
4900 struct tgsi_full_src_register tmp_src
;
4901 struct tgsi_full_dst_register tmp_dst
;
4902 const struct tgsi_full_dst_register
*inst_dst
;
4903 const struct tgsi_full_src_register
*coord_src
;
4908 * Do setup for handling texture swizzles or shadow compares.
4909 * \param unit the texture unit
4910 * \param inst the TGSI texture instruction
4911 * \param shadow_compare do shadow/depth comparison?
4912 * \param swz returns the swizzle info
4915 begin_tex_swizzle(struct svga_shader_emitter_v10
*emit
,
4917 const struct tgsi_full_instruction
*inst
,
4918 boolean shadow_compare
,
4919 struct tex_swizzle_info
*swz
)
4921 swz
->swizzled
= (emit
->key
.tex
[unit
].swizzle_r
!= TGSI_SWIZZLE_X
||
4922 emit
->key
.tex
[unit
].swizzle_g
!= TGSI_SWIZZLE_Y
||
4923 emit
->key
.tex
[unit
].swizzle_b
!= TGSI_SWIZZLE_Z
||
4924 emit
->key
.tex
[unit
].swizzle_a
!= TGSI_SWIZZLE_W
);
4926 swz
->shadow_compare
= shadow_compare
;
4927 swz
->texture_target
= inst
->Texture
.Texture
;
4929 if (swz
->swizzled
|| shadow_compare
) {
4930 /* Allocate temp register for the result of the SAMPLE instruction
4931 * and the source of the MOV/compare/swizzle instructions.
4933 unsigned tmp
= get_temp_index(emit
);
4934 swz
->tmp_src
= make_src_temp_reg(tmp
);
4935 swz
->tmp_dst
= make_dst_temp_reg(tmp
);
4939 swz
->inst_dst
= &inst
->Dst
[0];
4940 swz
->coord_src
= &inst
->Src
[0];
4945 * Returns the register to put the SAMPLE instruction results into.
4946 * This will either be the original instruction dst reg (if no swizzle
4947 * and no shadow comparison) or a temporary reg if there is a swizzle.
4949 static const struct tgsi_full_dst_register
*
4950 get_tex_swizzle_dst(const struct tex_swizzle_info
*swz
)
4952 return (swz
->swizzled
|| swz
->shadow_compare
)
4953 ? &swz
->tmp_dst
: swz
->inst_dst
;
4958 * This emits the MOV instruction that actually implements a texture swizzle
4959 * and/or shadow comparison.
4962 end_tex_swizzle(struct svga_shader_emitter_v10
*emit
,
4963 const struct tex_swizzle_info
*swz
)
4965 if (swz
->shadow_compare
) {
4966 /* Emit extra instructions to compare the fetched texel value against
4967 * a texture coordinate component. The result of the comparison
4970 struct tgsi_full_src_register coord_src
;
4971 struct tgsi_full_src_register texel_src
=
4972 scalar_src(&swz
->tmp_src
, TGSI_SWIZZLE_X
);
4973 struct tgsi_full_src_register one
=
4974 make_immediate_reg_float(emit
, 1.0f
);
4975 /* convert gallium comparison func to SVGA comparison func */
4976 SVGA3dCmpFunc compare_func
= emit
->key
.tex
[swz
->unit
].compare_func
+ 1;
4978 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
4980 switch (swz
->texture_target
) {
4981 case TGSI_TEXTURE_SHADOW2D
:
4982 case TGSI_TEXTURE_SHADOWRECT
:
4983 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
4984 coord_src
= scalar_src(swz
->coord_src
, TGSI_SWIZZLE_Z
);
4986 case TGSI_TEXTURE_SHADOW1D
:
4987 coord_src
= scalar_src(swz
->coord_src
, TGSI_SWIZZLE_Y
);
4989 case TGSI_TEXTURE_SHADOWCUBE
:
4990 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
4991 coord_src
= scalar_src(swz
->coord_src
, TGSI_SWIZZLE_W
);
4994 assert(!"Unexpected texture target in end_tex_swizzle()");
4995 coord_src
= scalar_src(swz
->coord_src
, TGSI_SWIZZLE_Z
);
4998 /* COMPARE tmp, coord, texel */
4999 /* XXX it would seem that the texel and coord arguments should
5000 * be transposed here, but piglit tests indicate otherwise.
5002 emit_comparison(emit
, compare_func
,
5003 &swz
->tmp_dst
, &texel_src
, &coord_src
);
5005 /* AND dest, tmp, {1.0} */
5006 begin_emit_instruction(emit
);
5007 emit_opcode(emit
, VGPU10_OPCODE_AND
, FALSE
);
5008 if (swz
->swizzled
) {
5009 emit_dst_register(emit
, &swz
->tmp_dst
);
5012 emit_dst_register(emit
, swz
->inst_dst
);
5014 emit_src_register(emit
, &swz
->tmp_src
);
5015 emit_src_register(emit
, &one
);
5016 end_emit_instruction(emit
);
5019 if (swz
->swizzled
) {
5020 unsigned swz_r
= emit
->key
.tex
[swz
->unit
].swizzle_r
;
5021 unsigned swz_g
= emit
->key
.tex
[swz
->unit
].swizzle_g
;
5022 unsigned swz_b
= emit
->key
.tex
[swz
->unit
].swizzle_b
;
5023 unsigned swz_a
= emit
->key
.tex
[swz
->unit
].swizzle_a
;
5024 unsigned writemask_0
= 0, writemask_1
= 0;
5025 boolean int_tex
= is_integer_type(emit
->key
.tex
[swz
->unit
].return_type
);
5027 /* Swizzle w/out zero/one terms */
5028 struct tgsi_full_src_register src_swizzled
=
5029 swizzle_src(&swz
->tmp_src
,
5030 swz_r
< PIPE_SWIZZLE_ZERO
? swz_r
: PIPE_SWIZZLE_RED
,
5031 swz_g
< PIPE_SWIZZLE_ZERO
? swz_g
: PIPE_SWIZZLE_GREEN
,
5032 swz_b
< PIPE_SWIZZLE_ZERO
? swz_b
: PIPE_SWIZZLE_BLUE
,
5033 swz_a
< PIPE_SWIZZLE_ZERO
? swz_a
: PIPE_SWIZZLE_ALPHA
);
5035 /* MOV dst, color(tmp).<swizzle> */
5036 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
,
5037 swz
->inst_dst
, &src_swizzled
, FALSE
);
5039 /* handle swizzle zero terms */
5040 writemask_0
= (((swz_r
== PIPE_SWIZZLE_ZERO
) << 0) |
5041 ((swz_g
== PIPE_SWIZZLE_ZERO
) << 1) |
5042 ((swz_b
== PIPE_SWIZZLE_ZERO
) << 2) |
5043 ((swz_a
== PIPE_SWIZZLE_ZERO
) << 3));
5046 struct tgsi_full_src_register zero
= int_tex
?
5047 make_immediate_reg_int(emit
, 0) :
5048 make_immediate_reg_float(emit
, 0.0f
);
5049 struct tgsi_full_dst_register dst
=
5050 writemask_dst(swz
->inst_dst
, writemask_0
);
5052 /* MOV dst.writemask_0, {0,0,0,0} */
5053 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
,
5054 &dst
, &zero
, FALSE
);
5057 /* handle swizzle one terms */
5058 writemask_1
= (((swz_r
== PIPE_SWIZZLE_ONE
) << 0) |
5059 ((swz_g
== PIPE_SWIZZLE_ONE
) << 1) |
5060 ((swz_b
== PIPE_SWIZZLE_ONE
) << 2) |
5061 ((swz_a
== PIPE_SWIZZLE_ONE
) << 3));
5064 struct tgsi_full_src_register one
= int_tex
?
5065 make_immediate_reg_int(emit
, 1) :
5066 make_immediate_reg_float(emit
, 1.0f
);
5067 struct tgsi_full_dst_register dst
=
5068 writemask_dst(swz
->inst_dst
, writemask_1
);
5070 /* MOV dst.writemask_1, {1,1,1,1} */
5071 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst
, &one
, FALSE
);
5078 * Emit code for TGSI_OPCODE_SAMPLE instruction.
5081 emit_sample(struct svga_shader_emitter_v10
*emit
,
5082 const struct tgsi_full_instruction
*inst
)
5084 const unsigned resource_unit
= inst
->Src
[1].Register
.Index
;
5085 const unsigned sampler_unit
= inst
->Src
[2].Register
.Index
;
5086 struct tgsi_full_src_register coord
;
5088 struct tex_swizzle_info swz_info
;
5090 begin_tex_swizzle(emit
, sampler_unit
, inst
, FALSE
, &swz_info
);
5092 get_texel_offsets(emit
, inst
, offsets
);
5094 coord
= setup_texcoord(emit
, resource_unit
, &inst
->Src
[0]);
5096 /* SAMPLE dst, coord(s0), resource, sampler */
5097 begin_emit_instruction(emit
);
5099 emit_sample_opcode(emit
, VGPU10_OPCODE_SAMPLE
,
5100 inst
->Instruction
.Saturate
, offsets
);
5101 emit_dst_register(emit
, get_tex_swizzle_dst(&swz_info
));
5102 emit_src_register(emit
, &coord
);
5103 emit_resource_register(emit
, resource_unit
);
5104 emit_sampler_register(emit
, sampler_unit
);
5105 end_emit_instruction(emit
);
5107 end_tex_swizzle(emit
, &swz_info
);
5109 free_temp_indexes(emit
);
5116 * Check if a texture instruction is valid.
5117 * An example of an invalid texture instruction is doing shadow comparison
5118 * with an integer-valued texture.
5119 * If we detect an invalid texture instruction, we replace it with:
5120 * MOV dst, {1,1,1,1};
5121 * \return TRUE if valid, FALSE if invalid.
5124 is_valid_tex_instruction(struct svga_shader_emitter_v10
*emit
,
5125 const struct tgsi_full_instruction
*inst
)
5127 const unsigned unit
= inst
->Src
[1].Register
.Index
;
5128 const unsigned target
= inst
->Texture
.Texture
;
5129 boolean valid
= TRUE
;
5131 if (tgsi_is_shadow_target(target
) &&
5132 is_integer_type(emit
->key
.tex
[unit
].return_type
)) {
5133 debug_printf("Invalid SAMPLE_C with an integer texture!\n");
5136 /* XXX might check for other conditions in the future here */
5139 /* emit a MOV dst, {1,1,1,1} instruction. */
5140 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
5141 begin_emit_instruction(emit
);
5142 emit_opcode(emit
, VGPU10_OPCODE_MOV
, FALSE
);
5143 emit_dst_register(emit
, &inst
->Dst
[0]);
5144 emit_src_register(emit
, &one
);
5145 end_emit_instruction(emit
);
5153 * Emit code for TGSI_OPCODE_TEX (simple texture lookup)
5156 emit_tex(struct svga_shader_emitter_v10
*emit
,
5157 const struct tgsi_full_instruction
*inst
)
5159 const uint unit
= inst
->Src
[1].Register
.Index
;
5160 unsigned target
= inst
->Texture
.Texture
;
5162 struct tgsi_full_src_register coord
;
5164 struct tex_swizzle_info swz_info
;
5166 /* check that the sampler returns a float */
5167 if (!is_valid_tex_instruction(emit
, inst
))
5170 begin_tex_swizzle(emit
, unit
, inst
, FALSE
, &swz_info
);
5172 get_texel_offsets(emit
, inst
, offsets
);
5174 coord
= setup_texcoord(emit
, unit
, &inst
->Src
[0]);
5176 /* SAMPLE dst, coord(s0), resource, sampler */
5177 begin_emit_instruction(emit
);
5179 if (tgsi_is_shadow_target(target
))
5180 opcode
= VGPU10_OPCODE_SAMPLE_C
;
5182 opcode
= VGPU10_OPCODE_SAMPLE
;
5184 emit_sample_opcode(emit
, opcode
, inst
->Instruction
.Saturate
, offsets
);
5185 emit_dst_register(emit
, get_tex_swizzle_dst(&swz_info
));
5186 emit_src_register(emit
, &coord
);
5187 emit_resource_register(emit
, unit
);
5188 emit_sampler_register(emit
, unit
);
5189 if (opcode
== VGPU10_OPCODE_SAMPLE_C
) {
5190 emit_tex_compare_refcoord(emit
, target
, &coord
);
5192 end_emit_instruction(emit
);
5194 end_tex_swizzle(emit
, &swz_info
);
5196 free_temp_indexes(emit
);
5203 * Emit code for TGSI_OPCODE_TXP (projective texture)
5206 emit_txp(struct svga_shader_emitter_v10
*emit
,
5207 const struct tgsi_full_instruction
*inst
)
5209 const uint unit
= inst
->Src
[1].Register
.Index
;
5210 unsigned target
= inst
->Texture
.Texture
;
5213 unsigned tmp
= get_temp_index(emit
);
5214 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
5215 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
5216 struct tgsi_full_src_register src0_wwww
=
5217 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_W
);
5218 struct tgsi_full_src_register coord
;
5219 struct tex_swizzle_info swz_info
;
5221 /* check that the sampler returns a float */
5222 if (!is_valid_tex_instruction(emit
, inst
))
5225 begin_tex_swizzle(emit
, unit
, inst
, FALSE
, &swz_info
);
5227 get_texel_offsets(emit
, inst
, offsets
);
5229 coord
= setup_texcoord(emit
, unit
, &inst
->Src
[0]);
5231 /* DIV tmp, coord, coord.wwww */
5232 emit_instruction_op2(emit
, VGPU10_OPCODE_DIV
, &tmp_dst
,
5233 &coord
, &src0_wwww
, FALSE
);
5235 /* SAMPLE dst, coord(tmp), resource, sampler */
5236 begin_emit_instruction(emit
);
5238 if (tgsi_is_shadow_target(target
))
5239 opcode
= VGPU10_OPCODE_SAMPLE_C
;
5241 opcode
= VGPU10_OPCODE_SAMPLE
;
5243 emit_sample_opcode(emit
, opcode
, inst
->Instruction
.Saturate
, offsets
);
5244 emit_dst_register(emit
, get_tex_swizzle_dst(&swz_info
));
5245 emit_src_register(emit
, &tmp_src
); /* projected coord */
5246 emit_resource_register(emit
, unit
);
5247 emit_sampler_register(emit
, unit
);
5248 if (opcode
== VGPU10_OPCODE_SAMPLE_C
) {
5249 emit_tex_compare_refcoord(emit
, target
, &tmp_src
);
5251 end_emit_instruction(emit
);
5253 end_tex_swizzle(emit
, &swz_info
);
5255 free_temp_indexes(emit
);
5262 * Emit code for TGSI_OPCODE_XPD instruction.
5265 emit_xpd(struct svga_shader_emitter_v10
*emit
,
5266 const struct tgsi_full_instruction
*inst
)
5268 /* dst.x = src0.y * src1.z - src1.y * src0.z
5269 * dst.y = src0.z * src1.x - src1.z * src0.x
5270 * dst.z = src0.x * src1.y - src1.x * src0.y
5273 struct tgsi_full_src_register s0_xxxx
=
5274 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_X
);
5275 struct tgsi_full_src_register s0_yyyy
=
5276 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_Y
);
5277 struct tgsi_full_src_register s0_zzzz
=
5278 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_Z
);
5280 struct tgsi_full_src_register s1_xxxx
=
5281 scalar_src(&inst
->Src
[1], TGSI_SWIZZLE_X
);
5282 struct tgsi_full_src_register s1_yyyy
=
5283 scalar_src(&inst
->Src
[1], TGSI_SWIZZLE_Y
);
5284 struct tgsi_full_src_register s1_zzzz
=
5285 scalar_src(&inst
->Src
[1], TGSI_SWIZZLE_Z
);
5287 unsigned tmp1
= get_temp_index(emit
);
5288 struct tgsi_full_src_register tmp1_src
= make_src_temp_reg(tmp1
);
5289 struct tgsi_full_dst_register tmp1_dst
= make_dst_temp_reg(tmp1
);
5291 unsigned tmp2
= get_temp_index(emit
);
5292 struct tgsi_full_src_register tmp2_src
= make_src_temp_reg(tmp2
);
5293 struct tgsi_full_dst_register tmp2_dst
= make_dst_temp_reg(tmp2
);
5294 struct tgsi_full_src_register neg_tmp2_src
= negate_src(&tmp2_src
);
5296 unsigned tmp3
= get_temp_index(emit
);
5297 struct tgsi_full_src_register tmp3_src
= make_src_temp_reg(tmp3
);
5298 struct tgsi_full_dst_register tmp3_dst
= make_dst_temp_reg(tmp3
);
5299 struct tgsi_full_dst_register tmp3_dst_x
=
5300 writemask_dst(&tmp3_dst
, TGSI_WRITEMASK_X
);
5301 struct tgsi_full_dst_register tmp3_dst_y
=
5302 writemask_dst(&tmp3_dst
, TGSI_WRITEMASK_Y
);
5303 struct tgsi_full_dst_register tmp3_dst_z
=
5304 writemask_dst(&tmp3_dst
, TGSI_WRITEMASK_Z
);
5305 struct tgsi_full_dst_register tmp3_dst_w
=
5306 writemask_dst(&tmp3_dst
, TGSI_WRITEMASK_W
);
5308 /* Note: we put all the intermediate computations into tmp3 in case
5309 * the XPD dest register is that same as one of the src regs (in which
5310 * case we could clobber a src reg before we're done with it) .
5312 * Note: we could get by with just one temp register instead of three
5313 * since we're doing scalar operations and there's enough room in one
5314 * temp for everything.
5317 /* MUL tmp1, src0.y, src1.z */
5318 /* MUL tmp2, src1.y, src0.z */
5319 /* ADD tmp3.x, tmp1, -tmp2 */
5320 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_X
) {
5321 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &tmp1_dst
,
5322 &s0_yyyy
, &s1_zzzz
, FALSE
);
5323 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &tmp2_dst
,
5324 &s1_yyyy
, &s0_zzzz
, FALSE
);
5325 emit_instruction_op2(emit
, VGPU10_OPCODE_ADD
, &tmp3_dst_x
,
5326 &tmp1_src
, &neg_tmp2_src
, FALSE
);
5329 /* MUL tmp1, src0.z, src1.x */
5330 /* MUL tmp2, src1.z, src0.x */
5331 /* ADD tmp3.y, tmp1, -tmp2 */
5332 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Y
) {
5333 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &tmp1_dst
, &s0_zzzz
,
5335 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &tmp2_dst
, &s1_zzzz
,
5337 emit_instruction_op2(emit
, VGPU10_OPCODE_ADD
, &tmp3_dst_y
,
5338 &tmp1_src
, &neg_tmp2_src
, FALSE
);
5341 /* MUL tmp1, src0.x, src1.y */
5342 /* MUL tmp2, src1.x, src0.y */
5343 /* ADD tmp3.z, tmp1, -tmp2 */
5344 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Z
) {
5345 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &tmp1_dst
, &s0_xxxx
,
5347 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &tmp2_dst
, &s1_xxxx
,
5349 emit_instruction_op2(emit
, VGPU10_OPCODE_ADD
, &tmp3_dst_z
,
5350 &tmp1_src
, &neg_tmp2_src
, FALSE
);
5353 /* MOV tmp3.w, 1.0 */
5354 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_W
) {
5355 struct tgsi_full_src_register one
=
5356 make_immediate_reg_float(emit
, 1.0f
);
5358 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &tmp3_dst_w
, &one
, FALSE
);
5362 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &inst
->Dst
[0], &tmp3_src
,
5363 inst
->Instruction
.Saturate
);
5366 free_temp_indexes(emit
);
5373 * Emit code for TGSI_OPCODE_TXD (explicit derivatives)
5376 emit_txd(struct svga_shader_emitter_v10
*emit
,
5377 const struct tgsi_full_instruction
*inst
)
5379 const uint unit
= inst
->Src
[3].Register
.Index
;
5380 unsigned target
= inst
->Texture
.Texture
;
5382 struct tgsi_full_src_register coord
;
5383 struct tex_swizzle_info swz_info
;
5385 begin_tex_swizzle(emit
, unit
, inst
, tgsi_is_shadow_target(target
),
5388 get_texel_offsets(emit
, inst
, offsets
);
5390 coord
= setup_texcoord(emit
, unit
, &inst
->Src
[0]);
5392 /* SAMPLE_D dst, coord(s0), resource, sampler, Xderiv(s1), Yderiv(s2) */
5393 begin_emit_instruction(emit
);
5394 emit_sample_opcode(emit
, VGPU10_OPCODE_SAMPLE_D
,
5395 inst
->Instruction
.Saturate
, offsets
);
5396 emit_dst_register(emit
, get_tex_swizzle_dst(&swz_info
));
5397 emit_src_register(emit
, &coord
);
5398 emit_resource_register(emit
, unit
);
5399 emit_sampler_register(emit
, unit
);
5400 emit_src_register(emit
, &inst
->Src
[1]); /* Xderiv */
5401 emit_src_register(emit
, &inst
->Src
[2]); /* Yderiv */
5402 end_emit_instruction(emit
);
5404 end_tex_swizzle(emit
, &swz_info
);
5406 free_temp_indexes(emit
);
5413 * Emit code for TGSI_OPCODE_TXF (texel fetch)
5416 emit_txf(struct svga_shader_emitter_v10
*emit
,
5417 const struct tgsi_full_instruction
*inst
)
5419 const uint unit
= inst
->Src
[1].Register
.Index
;
5420 const unsigned msaa
= emit
->key
.tex
[unit
].texture_msaa
;
5422 struct tex_swizzle_info swz_info
;
5424 begin_tex_swizzle(emit
, unit
, inst
, FALSE
, &swz_info
);
5426 get_texel_offsets(emit
, inst
, offsets
);
5429 /* Fetch one sample from an MSAA texture */
5430 struct tgsi_full_src_register sampleIndex
=
5431 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_W
);
5432 /* LD_MS dst, coord(s0), resource, sampleIndex */
5433 begin_emit_instruction(emit
);
5434 emit_sample_opcode(emit
, VGPU10_OPCODE_LD_MS
,
5435 inst
->Instruction
.Saturate
, offsets
);
5436 emit_dst_register(emit
, get_tex_swizzle_dst(&swz_info
));
5437 emit_src_register(emit
, &inst
->Src
[0]);
5438 emit_resource_register(emit
, unit
);
5439 emit_src_register(emit
, &sampleIndex
);
5440 end_emit_instruction(emit
);
5443 /* Fetch one texel specified by integer coordinate */
5444 /* LD dst, coord(s0), resource */
5445 begin_emit_instruction(emit
);
5446 emit_sample_opcode(emit
, VGPU10_OPCODE_LD
,
5447 inst
->Instruction
.Saturate
, offsets
);
5448 emit_dst_register(emit
, get_tex_swizzle_dst(&swz_info
));
5449 emit_src_register(emit
, &inst
->Src
[0]);
5450 emit_resource_register(emit
, unit
);
5451 end_emit_instruction(emit
);
5454 end_tex_swizzle(emit
, &swz_info
);
5456 free_temp_indexes(emit
);
5463 * Emit code for TGSI_OPCODE_TXL (explicit LOD) or TGSI_OPCODE_TXB (LOD bias)
5464 * or TGSI_OPCODE_TXB2 (for cube shadow maps).
5467 emit_txl_txb(struct svga_shader_emitter_v10
*emit
,
5468 const struct tgsi_full_instruction
*inst
)
5470 unsigned target
= inst
->Texture
.Texture
;
5471 unsigned opcode
, unit
;
5473 struct tgsi_full_src_register coord
, lod_bias
;
5474 struct tex_swizzle_info swz_info
;
5476 assert(inst
->Instruction
.Opcode
== TGSI_OPCODE_TXL
||
5477 inst
->Instruction
.Opcode
== TGSI_OPCODE_TXB
||
5478 inst
->Instruction
.Opcode
== TGSI_OPCODE_TXB2
);
5480 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_TXB2
) {
5481 lod_bias
= scalar_src(&inst
->Src
[1], TGSI_SWIZZLE_X
);
5482 unit
= inst
->Src
[2].Register
.Index
;
5485 lod_bias
= scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_W
);
5486 unit
= inst
->Src
[1].Register
.Index
;
5489 begin_tex_swizzle(emit
, unit
, inst
, tgsi_is_shadow_target(target
),
5492 get_texel_offsets(emit
, inst
, offsets
);
5494 coord
= setup_texcoord(emit
, unit
, &inst
->Src
[0]);
5496 /* SAMPLE_L/B dst, coord(s0), resource, sampler, lod(s3) */
5497 begin_emit_instruction(emit
);
5498 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_TXL
) {
5499 opcode
= VGPU10_OPCODE_SAMPLE_L
;
5502 opcode
= VGPU10_OPCODE_SAMPLE_B
;
5504 emit_sample_opcode(emit
, opcode
, inst
->Instruction
.Saturate
, offsets
);
5505 emit_dst_register(emit
, get_tex_swizzle_dst(&swz_info
));
5506 emit_src_register(emit
, &coord
);
5507 emit_resource_register(emit
, unit
);
5508 emit_sampler_register(emit
, unit
);
5509 emit_src_register(emit
, &lod_bias
);
5510 end_emit_instruction(emit
);
5512 end_tex_swizzle(emit
, &swz_info
);
5514 free_temp_indexes(emit
);
5521 * Emit code for TGSI_OPCODE_TXQ (texture query) instruction.
5524 emit_txq(struct svga_shader_emitter_v10
*emit
,
5525 const struct tgsi_full_instruction
*inst
)
5527 const uint unit
= inst
->Src
[1].Register
.Index
;
5529 if (emit
->key
.tex
[unit
].texture_target
== PIPE_BUFFER
) {
5530 /* RESINFO does not support querying texture buffers, so we instead
5531 * store texture buffer sizes in shader constants, then copy them to
5532 * implement TXQ instead of emitting RESINFO.
5533 * MOV dst, const[texture_buffer_size_index[unit]]
5535 struct tgsi_full_src_register size_src
=
5536 make_src_const_reg(emit
->texture_buffer_size_index
[unit
]);
5537 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &inst
->Dst
[0], &size_src
,
5540 /* RESINFO dst, srcMipLevel, resource */
5541 begin_emit_instruction(emit
);
5542 emit_opcode_resinfo(emit
, VGPU10_RESINFO_RETURN_UINT
);
5543 emit_dst_register(emit
, &inst
->Dst
[0]);
5544 emit_src_register(emit
, &inst
->Src
[0]);
5545 emit_resource_register(emit
, unit
);
5546 end_emit_instruction(emit
);
5549 free_temp_indexes(emit
);
5556 * Emit a simple instruction (like ADD, MUL, MIN, etc).
5559 emit_simple(struct svga_shader_emitter_v10
*emit
,
5560 const struct tgsi_full_instruction
*inst
)
5562 const unsigned opcode
= inst
->Instruction
.Opcode
;
5563 const struct tgsi_opcode_info
*op
= tgsi_get_opcode_info(opcode
);
5566 begin_emit_instruction(emit
);
5567 emit_opcode(emit
, translate_opcode(inst
->Instruction
.Opcode
),
5568 inst
->Instruction
.Saturate
);
5569 for (i
= 0; i
< op
->num_dst
; i
++) {
5570 emit_dst_register(emit
, &inst
->Dst
[i
]);
5572 for (i
= 0; i
< op
->num_src
; i
++) {
5573 emit_src_register(emit
, &inst
->Src
[i
]);
5575 end_emit_instruction(emit
);
5582 * We only special case the MOV instruction to try to detect constant
5583 * color writes in the fragment shader.
5586 emit_mov(struct svga_shader_emitter_v10
*emit
,
5587 const struct tgsi_full_instruction
*inst
)
5589 const struct tgsi_full_src_register
*src
= &inst
->Src
[0];
5590 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[0];
5592 if (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
5593 dst
->Register
.File
== TGSI_FILE_OUTPUT
&&
5594 dst
->Register
.Index
== 0 &&
5595 src
->Register
.File
== TGSI_FILE_CONSTANT
&&
5596 !src
->Register
.Indirect
) {
5597 emit
->constant_color_output
= TRUE
;
5600 return emit_simple(emit
, inst
);
5605 * Emit a simple VGPU10 instruction which writes to multiple dest registers,
5606 * where TGSI only uses one dest register.
5609 emit_simple_1dst(struct svga_shader_emitter_v10
*emit
,
5610 const struct tgsi_full_instruction
*inst
,
5614 const unsigned opcode
= inst
->Instruction
.Opcode
;
5615 const struct tgsi_opcode_info
*op
= tgsi_get_opcode_info(opcode
);
5618 begin_emit_instruction(emit
);
5619 emit_opcode(emit
, translate_opcode(inst
->Instruction
.Opcode
),
5620 inst
->Instruction
.Saturate
);
5622 for (i
= 0; i
< dst_count
; i
++) {
5623 if (i
== dst_index
) {
5624 emit_dst_register(emit
, &inst
->Dst
[0]);
5626 emit_null_dst_register(emit
);
5630 for (i
= 0; i
< op
->num_src
; i
++) {
5631 emit_src_register(emit
, &inst
->Src
[i
]);
5633 end_emit_instruction(emit
);
5640 * Translate a single TGSI instruction to VGPU10.
5643 emit_vgpu10_instruction(struct svga_shader_emitter_v10
*emit
,
5644 unsigned inst_number
,
5645 const struct tgsi_full_instruction
*inst
)
5647 const unsigned opcode
= inst
->Instruction
.Opcode
;
5650 case TGSI_OPCODE_ADD
:
5651 case TGSI_OPCODE_AND
:
5652 case TGSI_OPCODE_BGNLOOP
:
5653 case TGSI_OPCODE_BRK
:
5654 case TGSI_OPCODE_CEIL
:
5655 case TGSI_OPCODE_CONT
:
5656 case TGSI_OPCODE_DDX
:
5657 case TGSI_OPCODE_DDY
:
5658 case TGSI_OPCODE_DIV
:
5659 case TGSI_OPCODE_DP2
:
5660 case TGSI_OPCODE_DP3
:
5661 case TGSI_OPCODE_DP4
:
5662 case TGSI_OPCODE_ELSE
:
5663 case TGSI_OPCODE_ENDIF
:
5664 case TGSI_OPCODE_ENDLOOP
:
5665 case TGSI_OPCODE_ENDSUB
:
5666 case TGSI_OPCODE_F2I
:
5667 case TGSI_OPCODE_F2U
:
5668 case TGSI_OPCODE_FLR
:
5669 case TGSI_OPCODE_FRC
:
5670 case TGSI_OPCODE_FSEQ
:
5671 case TGSI_OPCODE_FSGE
:
5672 case TGSI_OPCODE_FSLT
:
5673 case TGSI_OPCODE_FSNE
:
5674 case TGSI_OPCODE_I2F
:
5675 case TGSI_OPCODE_IMAX
:
5676 case TGSI_OPCODE_IMIN
:
5677 case TGSI_OPCODE_INEG
:
5678 case TGSI_OPCODE_ISGE
:
5679 case TGSI_OPCODE_ISHR
:
5680 case TGSI_OPCODE_ISLT
:
5681 case TGSI_OPCODE_MAD
:
5682 case TGSI_OPCODE_MAX
:
5683 case TGSI_OPCODE_MIN
:
5684 case TGSI_OPCODE_MUL
:
5685 case TGSI_OPCODE_NOP
:
5686 case TGSI_OPCODE_NOT
:
5687 case TGSI_OPCODE_OR
:
5688 case TGSI_OPCODE_RET
:
5689 case TGSI_OPCODE_UADD
:
5690 case TGSI_OPCODE_USEQ
:
5691 case TGSI_OPCODE_USGE
:
5692 case TGSI_OPCODE_USLT
:
5693 case TGSI_OPCODE_UMIN
:
5694 case TGSI_OPCODE_UMAD
:
5695 case TGSI_OPCODE_UMAX
:
5696 case TGSI_OPCODE_ROUND
:
5697 case TGSI_OPCODE_SQRT
:
5698 case TGSI_OPCODE_SHL
:
5699 case TGSI_OPCODE_TRUNC
:
5700 case TGSI_OPCODE_U2F
:
5701 case TGSI_OPCODE_UCMP
:
5702 case TGSI_OPCODE_USHR
:
5703 case TGSI_OPCODE_USNE
:
5704 case TGSI_OPCODE_XOR
:
5705 /* simple instructions */
5706 return emit_simple(emit
, inst
);
5708 case TGSI_OPCODE_MOV
:
5709 return emit_mov(emit
, inst
);
5710 case TGSI_OPCODE_EMIT
:
5711 return emit_vertex(emit
, inst
);
5712 case TGSI_OPCODE_ENDPRIM
:
5713 return emit_endprim(emit
, inst
);
5714 case TGSI_OPCODE_ABS
:
5715 return emit_abs(emit
, inst
);
5716 case TGSI_OPCODE_IABS
:
5717 return emit_iabs(emit
, inst
);
5718 case TGSI_OPCODE_ARL
:
5720 case TGSI_OPCODE_UARL
:
5721 return emit_arl_uarl(emit
, inst
);
5722 case TGSI_OPCODE_BGNSUB
:
5725 case TGSI_OPCODE_CAL
:
5726 return emit_cal(emit
, inst
);
5727 case TGSI_OPCODE_CMP
:
5728 return emit_cmp(emit
, inst
);
5729 case TGSI_OPCODE_COS
:
5730 return emit_sincos(emit
, inst
);
5731 case TGSI_OPCODE_DP2A
:
5732 return emit_dp2a(emit
, inst
);
5733 case TGSI_OPCODE_DPH
:
5734 return emit_dph(emit
, inst
);
5735 case TGSI_OPCODE_DST
:
5736 return emit_dst(emit
, inst
);
5737 case TGSI_OPCODE_EX2
:
5738 return emit_ex2(emit
, inst
);
5739 case TGSI_OPCODE_EXP
:
5740 return emit_exp(emit
, inst
);
5741 case TGSI_OPCODE_IF
:
5742 return emit_if(emit
, inst
);
5743 case TGSI_OPCODE_KILL
:
5744 return emit_kill(emit
, inst
);
5745 case TGSI_OPCODE_KILL_IF
:
5746 return emit_kill_if(emit
, inst
);
5747 case TGSI_OPCODE_LG2
:
5748 return emit_lg2(emit
, inst
);
5749 case TGSI_OPCODE_LIT
:
5750 return emit_lit(emit
, inst
);
5751 case TGSI_OPCODE_LOG
:
5752 return emit_log(emit
, inst
);
5753 case TGSI_OPCODE_LRP
:
5754 return emit_lrp(emit
, inst
);
5755 case TGSI_OPCODE_POW
:
5756 return emit_pow(emit
, inst
);
5757 case TGSI_OPCODE_RCP
:
5758 return emit_rcp(emit
, inst
);
5759 case TGSI_OPCODE_RSQ
:
5760 return emit_rsq(emit
, inst
);
5761 case TGSI_OPCODE_SAMPLE
:
5762 return emit_sample(emit
, inst
);
5763 case TGSI_OPCODE_SCS
:
5764 return emit_scs(emit
, inst
);
5765 case TGSI_OPCODE_SEQ
:
5766 return emit_seq(emit
, inst
);
5767 case TGSI_OPCODE_SGE
:
5768 return emit_sge(emit
, inst
);
5769 case TGSI_OPCODE_SGT
:
5770 return emit_sgt(emit
, inst
);
5771 case TGSI_OPCODE_SIN
:
5772 return emit_sincos(emit
, inst
);
5773 case TGSI_OPCODE_SLE
:
5774 return emit_sle(emit
, inst
);
5775 case TGSI_OPCODE_SLT
:
5776 return emit_slt(emit
, inst
);
5777 case TGSI_OPCODE_SNE
:
5778 return emit_sne(emit
, inst
);
5779 case TGSI_OPCODE_SSG
:
5780 return emit_ssg(emit
, inst
);
5781 case TGSI_OPCODE_ISSG
:
5782 return emit_issg(emit
, inst
);
5783 case TGSI_OPCODE_SUB
:
5784 return emit_sub(emit
, inst
);
5785 case TGSI_OPCODE_TEX
:
5786 return emit_tex(emit
, inst
);
5787 case TGSI_OPCODE_TXP
:
5788 return emit_txp(emit
, inst
);
5789 case TGSI_OPCODE_TXB
:
5790 case TGSI_OPCODE_TXB2
:
5791 case TGSI_OPCODE_TXL
:
5792 return emit_txl_txb(emit
, inst
);
5793 case TGSI_OPCODE_TXD
:
5794 return emit_txd(emit
, inst
);
5795 case TGSI_OPCODE_TXF
:
5796 return emit_txf(emit
, inst
);
5797 case TGSI_OPCODE_TXQ
:
5798 return emit_txq(emit
, inst
);
5799 case TGSI_OPCODE_UIF
:
5800 return emit_if(emit
, inst
);
5801 case TGSI_OPCODE_XPD
:
5802 return emit_xpd(emit
, inst
);
5803 case TGSI_OPCODE_UMUL_HI
:
5804 case TGSI_OPCODE_IMUL_HI
:
5805 case TGSI_OPCODE_UDIV
:
5806 case TGSI_OPCODE_IDIV
:
5807 /* These cases use only the FIRST of two destination registers */
5808 return emit_simple_1dst(emit
, inst
, 2, 0);
5809 case TGSI_OPCODE_UMUL
:
5810 case TGSI_OPCODE_UMOD
:
5811 case TGSI_OPCODE_MOD
:
5812 /* These cases use only the SECOND of two destination registers */
5813 return emit_simple_1dst(emit
, inst
, 2, 1);
5814 case TGSI_OPCODE_END
:
5815 if (!emit_post_helpers(emit
))
5817 return emit_simple(emit
, inst
);
5820 debug_printf("Unimplemented tgsi instruction %s\n",
5821 tgsi_get_opcode_name(opcode
));
5830 * Emit the extra instructions to adjust the vertex position.
5831 * There are two possible adjustments:
5832 * 1. Converting from Gallium to VGPU10 coordinate space by applying the
5833 * "prescale" and "pretranslate" values.
5834 * 2. Undoing the viewport transformation when we use the swtnl/draw path.
5835 * \param vs_pos_tmp_index which temporary register contains the vertex pos.
5838 emit_vpos_instructions(struct svga_shader_emitter_v10
*emit
,
5839 unsigned vs_pos_tmp_index
)
5841 struct tgsi_full_src_register tmp_pos_src
;
5842 struct tgsi_full_dst_register pos_dst
;
5844 /* Don't bother to emit any extra vertex instructions if vertex position is
5847 if (emit
->vposition
.out_index
== INVALID_INDEX
)
5850 tmp_pos_src
= make_src_temp_reg(vs_pos_tmp_index
);
5851 pos_dst
= make_dst_output_reg(emit
->vposition
.out_index
);
5853 /* If non-adjusted vertex position register index
5854 * is valid, copy the vertex position from the temporary
5855 * vertex position register before it is modified by the
5856 * prescale computation.
5858 if (emit
->vposition
.so_index
!= INVALID_INDEX
) {
5859 struct tgsi_full_dst_register pos_so_dst
=
5860 make_dst_output_reg(emit
->vposition
.so_index
);
5862 /* MOV pos_so, tmp_pos */
5863 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &pos_so_dst
,
5864 &tmp_pos_src
, FALSE
);
5867 if (emit
->vposition
.need_prescale
) {
5868 /* This code adjusts the vertex position to match the VGPU10 convention.
5869 * If p is the position computed by the shader (usually by applying the
5870 * modelview and projection matrices), the new position q is computed by:
5872 * q.x = p.w * trans.x + p.x * scale.x
5873 * q.y = p.w * trans.y + p.y * scale.y
5874 * q.z = p.w * trans.z + p.z * scale.z;
5875 * q.w = p.w * trans.w + p.w;
5877 struct tgsi_full_src_register tmp_pos_src_w
=
5878 scalar_src(&tmp_pos_src
, TGSI_SWIZZLE_W
);
5879 struct tgsi_full_dst_register tmp_pos_dst
=
5880 make_dst_temp_reg(vs_pos_tmp_index
);
5881 struct tgsi_full_dst_register tmp_pos_dst_xyz
=
5882 writemask_dst(&tmp_pos_dst
, TGSI_WRITEMASK_XYZ
);
5884 struct tgsi_full_src_register prescale_scale
=
5885 make_src_const_reg(emit
->vposition
.prescale_scale_index
);
5886 struct tgsi_full_src_register prescale_trans
=
5887 make_src_const_reg(emit
->vposition
.prescale_trans_index
);
5889 /* MUL tmp_pos.xyz, tmp_pos, prescale.scale */
5890 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &tmp_pos_dst_xyz
,
5891 &tmp_pos_src
, &prescale_scale
, FALSE
);
5893 /* MAD pos, tmp_pos.wwww, prescale.trans, tmp_pos */
5894 emit_instruction_op3(emit
, VGPU10_OPCODE_MAD
, &pos_dst
, &tmp_pos_src_w
,
5895 &prescale_trans
, &tmp_pos_src
, FALSE
);
5897 else if (emit
->key
.vs
.undo_viewport
) {
5898 /* This code computes the final vertex position from the temporary
5899 * vertex position by undoing the viewport transformation and the
5900 * divide-by-W operation (we convert window coords back to clip coords).
5901 * This is needed when we use the 'draw' module for fallbacks.
5902 * If p is the temp pos in window coords, then the NDC coord q is:
5903 * q.x = (p.x - vp.x_trans) / vp.x_scale * p.w
5904 * q.y = (p.y - vp.y_trans) / vp.y_scale * p.w
5907 * CONST[vs_viewport_index] contains:
5908 * { 1/vp.x_scale, 1/vp.y_scale, -vp.x_trans, -vp.y_trans }
5910 struct tgsi_full_dst_register tmp_pos_dst
=
5911 make_dst_temp_reg(vs_pos_tmp_index
);
5912 struct tgsi_full_dst_register tmp_pos_dst_xy
=
5913 writemask_dst(&tmp_pos_dst
, TGSI_WRITEMASK_XY
);
5914 struct tgsi_full_src_register tmp_pos_src_wwww
=
5915 scalar_src(&tmp_pos_src
, TGSI_SWIZZLE_W
);
5917 struct tgsi_full_dst_register pos_dst_xyz
=
5918 writemask_dst(&pos_dst
, TGSI_WRITEMASK_XYZ
);
5919 struct tgsi_full_dst_register pos_dst_w
=
5920 writemask_dst(&pos_dst
, TGSI_WRITEMASK_W
);
5922 struct tgsi_full_src_register vp_xyzw
=
5923 make_src_const_reg(emit
->vs
.viewport_index
);
5924 struct tgsi_full_src_register vp_zwww
=
5925 swizzle_src(&vp_xyzw
, TGSI_SWIZZLE_Z
, TGSI_SWIZZLE_W
,
5926 TGSI_SWIZZLE_W
, TGSI_SWIZZLE_W
);
5928 /* ADD tmp_pos.xy, tmp_pos.xy, viewport.zwww */
5929 emit_instruction_op2(emit
, VGPU10_OPCODE_ADD
, &tmp_pos_dst_xy
,
5930 &tmp_pos_src
, &vp_zwww
, FALSE
);
5932 /* MUL tmp_pos.xy, tmp_pos.xyzw, viewport.xyzy */
5933 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &tmp_pos_dst_xy
,
5934 &tmp_pos_src
, &vp_xyzw
, FALSE
);
5936 /* MUL pos.xyz, tmp_pos.xyz, tmp_pos.www */
5937 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &pos_dst_xyz
,
5938 &tmp_pos_src
, &tmp_pos_src_wwww
, FALSE
);
5940 /* MOV pos.w, tmp_pos.w */
5941 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &pos_dst_w
,
5942 &tmp_pos_src
, FALSE
);
5944 else if (vs_pos_tmp_index
!= INVALID_INDEX
) {
5945 /* This code is to handle the case where the temporary vertex
5946 * position register is created when the vertex shader has stream
5947 * output and prescale is disabled because rasterization is to be
5950 struct tgsi_full_dst_register pos_dst
=
5951 make_dst_output_reg(emit
->vposition
.out_index
);
5953 /* MOV pos, tmp_pos */
5954 begin_emit_instruction(emit
);
5955 emit_opcode(emit
, VGPU10_OPCODE_MOV
, FALSE
);
5956 emit_dst_register(emit
, &pos_dst
);
5957 emit_src_register(emit
, &tmp_pos_src
);
5958 end_emit_instruction(emit
);
5963 emit_clipping_instructions(struct svga_shader_emitter_v10
*emit
)
5965 if (emit
->clip_mode
== CLIP_DISTANCE
) {
5966 /* Copy from copy distance temporary to CLIPDIST & the shadow copy */
5967 emit_clip_distance_instructions(emit
);
5969 } else if (emit
->clip_mode
== CLIP_VERTEX
) {
5970 /* Convert TGSI CLIPVERTEX to CLIPDIST */
5971 emit_clip_vertex_instructions(emit
);
5975 * Emit vertex position and take care of legacy user planes only if
5976 * there is a valid vertex position register index.
5977 * This is to take care of the case
5978 * where the shader doesn't output vertex position. Then in
5979 * this case, don't bother to emit more vertex instructions.
5981 if (emit
->vposition
.out_index
== INVALID_INDEX
)
5985 * Emit per-vertex clipping instructions for legacy user defined clip planes.
5986 * NOTE: we must emit the clip distance instructions before the
5987 * emit_vpos_instructions() call since the later function will change
5988 * the TEMP[vs_pos_tmp_index] value.
5990 if (emit
->clip_mode
== CLIP_LEGACY
) {
5991 /* Emit CLIPDIST for legacy user defined clip planes */
5992 emit_clip_distance_from_vpos(emit
, emit
->vposition
.tmp_index
);
5998 * Emit extra per-vertex instructions. This includes clip-coordinate
5999 * space conversion and computing clip distances. This is called for
6000 * each GS emit-vertex instruction and at the end of VS translation.
6003 emit_vertex_instructions(struct svga_shader_emitter_v10
*emit
)
6005 const unsigned vs_pos_tmp_index
= emit
->vposition
.tmp_index
;
6007 /* Emit clipping instructions based on clipping mode */
6008 emit_clipping_instructions(emit
);
6011 * Reset the temporary vertex position register index
6012 * so that emit_dst_register() will use the real vertex position output
6014 emit
->vposition
.tmp_index
= INVALID_INDEX
;
6016 /* Emit vertex position instructions */
6017 emit_vpos_instructions(emit
, vs_pos_tmp_index
);
6019 /* Restore original vposition.tmp_index value for the next GS vertex.
6020 * It doesn't matter for VS.
6022 emit
->vposition
.tmp_index
= vs_pos_tmp_index
;
6026 * Translate the TGSI_OPCODE_EMIT GS instruction.
6029 emit_vertex(struct svga_shader_emitter_v10
*emit
,
6030 const struct tgsi_full_instruction
*inst
)
6032 unsigned ret
= TRUE
;
6034 assert(emit
->unit
== PIPE_SHADER_GEOMETRY
);
6036 emit_vertex_instructions(emit
);
6038 /* We can't use emit_simple() because the TGSI instruction has one
6039 * operand (vertex stream number) which we must ignore for VGPU10.
6041 begin_emit_instruction(emit
);
6042 emit_opcode(emit
, VGPU10_OPCODE_EMIT
, FALSE
);
6043 end_emit_instruction(emit
);
6050 * Emit the extra code to convert from VGPU10's boolean front-face
6051 * register to TGSI's signed front-face register.
6053 * TODO: Make temporary front-face register a scalar.
6056 emit_frontface_instructions(struct svga_shader_emitter_v10
*emit
)
6058 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
6060 if (emit
->fs
.face_input_index
!= INVALID_INDEX
) {
6061 /* convert vgpu10 boolean face register to gallium +/-1 value */
6062 struct tgsi_full_dst_register tmp_dst
=
6063 make_dst_temp_reg(emit
->fs
.face_tmp_index
);
6064 struct tgsi_full_src_register one
=
6065 make_immediate_reg_float(emit
, 1.0f
);
6066 struct tgsi_full_src_register neg_one
=
6067 make_immediate_reg_float(emit
, -1.0f
);
6069 /* MOVC face_tmp, IS_FRONT_FACE.x, 1.0, -1.0 */
6070 begin_emit_instruction(emit
);
6071 emit_opcode(emit
, VGPU10_OPCODE_MOVC
, FALSE
);
6072 emit_dst_register(emit
, &tmp_dst
);
6073 emit_face_register(emit
);
6074 emit_src_register(emit
, &one
);
6075 emit_src_register(emit
, &neg_one
);
6076 end_emit_instruction(emit
);
6082 * Emit the extra code to convert from VGPU10's fragcoord.w value to 1/w.
6085 emit_fragcoord_instructions(struct svga_shader_emitter_v10
*emit
)
6087 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
6089 if (emit
->fs
.fragcoord_input_index
!= INVALID_INDEX
) {
6090 struct tgsi_full_dst_register tmp_dst
=
6091 make_dst_temp_reg(emit
->fs
.fragcoord_tmp_index
);
6092 struct tgsi_full_dst_register tmp_dst_xyz
=
6093 writemask_dst(&tmp_dst
, TGSI_WRITEMASK_XYZ
);
6094 struct tgsi_full_dst_register tmp_dst_w
=
6095 writemask_dst(&tmp_dst
, TGSI_WRITEMASK_W
);
6096 struct tgsi_full_src_register one
=
6097 make_immediate_reg_float(emit
, 1.0f
);
6098 struct tgsi_full_src_register fragcoord
=
6099 make_src_reg(TGSI_FILE_INPUT
, emit
->fs
.fragcoord_input_index
);
6101 /* save the input index */
6102 unsigned fragcoord_input_index
= emit
->fs
.fragcoord_input_index
;
6103 /* set to invalid to prevent substitution in emit_src_register() */
6104 emit
->fs
.fragcoord_input_index
= INVALID_INDEX
;
6106 /* MOV fragcoord_tmp.xyz, fragcoord.xyz */
6107 begin_emit_instruction(emit
);
6108 emit_opcode(emit
, VGPU10_OPCODE_MOV
, FALSE
);
6109 emit_dst_register(emit
, &tmp_dst_xyz
);
6110 emit_src_register(emit
, &fragcoord
);
6111 end_emit_instruction(emit
);
6113 /* DIV fragcoord_tmp.w, 1.0, fragcoord.w */
6114 begin_emit_instruction(emit
);
6115 emit_opcode(emit
, VGPU10_OPCODE_DIV
, FALSE
);
6116 emit_dst_register(emit
, &tmp_dst_w
);
6117 emit_src_register(emit
, &one
);
6118 emit_src_register(emit
, &fragcoord
);
6119 end_emit_instruction(emit
);
6121 /* restore saved value */
6122 emit
->fs
.fragcoord_input_index
= fragcoord_input_index
;
6128 * Emit extra instructions to adjust VS inputs/attributes. This can
6129 * mean casting a vertex attribute from int to float or setting the
6130 * W component to 1, or both.
6133 emit_vertex_attrib_instructions(struct svga_shader_emitter_v10
*emit
)
6135 const unsigned save_w_1_mask
= emit
->key
.vs
.adjust_attrib_w_1
;
6136 const unsigned save_itof_mask
= emit
->key
.vs
.adjust_attrib_itof
;
6137 const unsigned save_utof_mask
= emit
->key
.vs
.adjust_attrib_utof
;
6138 const unsigned save_is_bgra_mask
= emit
->key
.vs
.attrib_is_bgra
;
6139 const unsigned save_puint_to_snorm_mask
= emit
->key
.vs
.attrib_puint_to_snorm
;
6140 const unsigned save_puint_to_uscaled_mask
= emit
->key
.vs
.attrib_puint_to_uscaled
;
6141 const unsigned save_puint_to_sscaled_mask
= emit
->key
.vs
.attrib_puint_to_sscaled
;
6143 unsigned adjust_mask
= (save_w_1_mask
|
6147 save_puint_to_snorm_mask
|
6148 save_puint_to_uscaled_mask
|
6149 save_puint_to_sscaled_mask
);
6151 assert(emit
->unit
== PIPE_SHADER_VERTEX
);
6154 struct tgsi_full_src_register one
=
6155 make_immediate_reg_float(emit
, 1.0f
);
6157 struct tgsi_full_src_register one_int
=
6158 make_immediate_reg_int(emit
, 1);
6160 /* We need to turn off these bitmasks while emitting the
6161 * instructions below, then restore them afterward.
6163 emit
->key
.vs
.adjust_attrib_w_1
= 0;
6164 emit
->key
.vs
.adjust_attrib_itof
= 0;
6165 emit
->key
.vs
.adjust_attrib_utof
= 0;
6166 emit
->key
.vs
.attrib_is_bgra
= 0;
6167 emit
->key
.vs
.attrib_puint_to_snorm
= 0;
6168 emit
->key
.vs
.attrib_puint_to_uscaled
= 0;
6169 emit
->key
.vs
.attrib_puint_to_sscaled
= 0;
6171 while (adjust_mask
) {
6172 unsigned index
= u_bit_scan(&adjust_mask
);
6173 unsigned tmp
= emit
->vs
.adjusted_input
[index
];
6174 struct tgsi_full_src_register input_src
=
6175 make_src_reg(TGSI_FILE_INPUT
, index
);
6177 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
6178 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
6179 struct tgsi_full_dst_register tmp_dst_w
=
6180 writemask_dst(&tmp_dst
, TGSI_WRITEMASK_W
);
6182 /* ITOF/UTOF/MOV tmp, input[index] */
6183 if (save_itof_mask
& (1 << index
)) {
6184 emit_instruction_op1(emit
, VGPU10_OPCODE_ITOF
,
6185 &tmp_dst
, &input_src
, FALSE
);
6187 else if (save_utof_mask
& (1 << index
)) {
6188 emit_instruction_op1(emit
, VGPU10_OPCODE_UTOF
,
6189 &tmp_dst
, &input_src
, FALSE
);
6191 else if (save_puint_to_snorm_mask
& (1 << index
)) {
6192 emit_puint_to_snorm(emit
, &tmp_dst
, &input_src
);
6194 else if (save_puint_to_uscaled_mask
& (1 << index
)) {
6195 emit_puint_to_uscaled(emit
, &tmp_dst
, &input_src
);
6197 else if (save_puint_to_sscaled_mask
& (1 << index
)) {
6198 emit_puint_to_sscaled(emit
, &tmp_dst
, &input_src
);
6201 assert((save_w_1_mask
| save_is_bgra_mask
) & (1 << index
));
6202 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
,
6203 &tmp_dst
, &input_src
, FALSE
);
6206 if (save_is_bgra_mask
& (1 << index
)) {
6207 emit_swap_r_b(emit
, &tmp_dst
, &tmp_src
);
6210 if (save_w_1_mask
& (1 << index
)) {
6211 /* MOV tmp.w, 1.0 */
6212 if (emit
->key
.vs
.attrib_is_pure_int
& (1 << index
)) {
6213 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
,
6214 &tmp_dst_w
, &one_int
, FALSE
);
6217 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
,
6218 &tmp_dst_w
, &one
, FALSE
);
6223 emit
->key
.vs
.adjust_attrib_w_1
= save_w_1_mask
;
6224 emit
->key
.vs
.adjust_attrib_itof
= save_itof_mask
;
6225 emit
->key
.vs
.adjust_attrib_utof
= save_utof_mask
;
6226 emit
->key
.vs
.attrib_is_bgra
= save_is_bgra_mask
;
6227 emit
->key
.vs
.attrib_puint_to_snorm
= save_puint_to_snorm_mask
;
6228 emit
->key
.vs
.attrib_puint_to_uscaled
= save_puint_to_uscaled_mask
;
6229 emit
->key
.vs
.attrib_puint_to_sscaled
= save_puint_to_sscaled_mask
;
6235 * Some common values like 0.0, 1.0, 0.5, etc. are frequently needed
6236 * to implement some instructions. We pre-allocate those values here
6237 * in the immediate constant buffer.
6240 alloc_common_immediates(struct svga_shader_emitter_v10
*emit
)
6244 emit
->common_immediate_pos
[n
++] =
6245 alloc_immediate_float4(emit
, 0.0f
, 1.0f
, 0.5f
, -1.0f
);
6247 emit
->common_immediate_pos
[n
++] =
6248 alloc_immediate_float4(emit
, 128.0f
, -128.0f
, 2.0f
, 3.0f
);
6250 emit
->common_immediate_pos
[n
++] =
6251 alloc_immediate_int4(emit
, 0, 1, 0, -1);
6253 if (emit
->key
.vs
.attrib_puint_to_snorm
) {
6254 emit
->common_immediate_pos
[n
++] =
6255 alloc_immediate_float4(emit
, -2.0f
, -2.0f
, -2.0f
, -1.66666f
);
6258 if (emit
->key
.vs
.attrib_puint_to_uscaled
) {
6259 emit
->common_immediate_pos
[n
++] =
6260 alloc_immediate_float4(emit
, 1023.0f
, 3.0f
, 0.0f
, 0.0f
);
6263 if (emit
->key
.vs
.attrib_puint_to_sscaled
) {
6264 emit
->common_immediate_pos
[n
++] =
6265 alloc_immediate_int4(emit
, 22, 12, 2, 0);
6267 emit
->common_immediate_pos
[n
++] =
6268 alloc_immediate_int4(emit
, 22, 30, 0, 0);
6271 assert(n
<= Elements(emit
->common_immediate_pos
));
6272 emit
->num_common_immediates
= n
;
6277 * Emit any extra/helper declarations/code that we might need between
6278 * the declaration section and code section.
6281 emit_pre_helpers(struct svga_shader_emitter_v10
*emit
)
6284 if (emit
->unit
== PIPE_SHADER_GEOMETRY
)
6285 emit_property_instructions(emit
);
6287 /* Declare inputs */
6288 if (!emit_input_declarations(emit
))
6291 /* Declare outputs */
6292 if (!emit_output_declarations(emit
))
6295 /* Declare temporary registers */
6296 emit_temporaries_declaration(emit
);
6298 /* Declare constant registers */
6299 emit_constant_declaration(emit
);
6301 /* Declare samplers and resources */
6302 emit_sampler_declarations(emit
);
6303 emit_resource_declarations(emit
);
6305 /* Declare clip distance output registers */
6306 if (emit
->unit
== PIPE_SHADER_VERTEX
||
6307 emit
->unit
== PIPE_SHADER_GEOMETRY
) {
6308 emit_clip_distance_declarations(emit
);
6311 alloc_common_immediates(emit
);
6313 if (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
6314 emit
->key
.fs
.alpha_func
!= SVGA3D_CMP_ALWAYS
) {
6315 float alpha
= emit
->key
.fs
.alpha_ref
;
6316 emit
->fs
.alpha_ref_index
=
6317 alloc_immediate_float4(emit
, alpha
, alpha
, alpha
, alpha
);
6320 /* Now, emit the constant block containing all the immediates
6321 * declared by shader, as well as the extra ones seen above.
6323 emit_vgpu10_immediates_block(emit
);
6325 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
6326 emit_frontface_instructions(emit
);
6327 emit_fragcoord_instructions(emit
);
6329 else if (emit
->unit
== PIPE_SHADER_VERTEX
) {
6330 emit_vertex_attrib_instructions(emit
);
6338 * Emit alpha test code. This compares TEMP[fs_color_tmp_index].w
6339 * against the alpha reference value and discards the fragment if the
6343 emit_alpha_test_instructions(struct svga_shader_emitter_v10
*emit
,
6344 unsigned fs_color_tmp_index
)
6346 /* compare output color's alpha to alpha ref and kill */
6347 unsigned tmp
= get_temp_index(emit
);
6348 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
6349 struct tgsi_full_src_register tmp_src_x
=
6350 scalar_src(&tmp_src
, TGSI_SWIZZLE_X
);
6351 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
6352 struct tgsi_full_src_register color_src
=
6353 make_src_temp_reg(fs_color_tmp_index
);
6354 struct tgsi_full_src_register color_src_w
=
6355 scalar_src(&color_src
, TGSI_SWIZZLE_W
);
6356 struct tgsi_full_src_register ref_src
=
6357 make_src_immediate_reg(emit
->fs
.alpha_ref_index
);
6358 struct tgsi_full_dst_register color_dst
=
6359 make_dst_output_reg(emit
->fs
.color_out_index
[0]);
6361 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
6363 /* dst = src0 'alpha_func' src1 */
6364 emit_comparison(emit
, emit
->key
.fs
.alpha_func
, &tmp_dst
,
6365 &color_src_w
, &ref_src
);
6367 /* DISCARD if dst.x == 0 */
6368 begin_emit_instruction(emit
);
6369 emit_discard_opcode(emit
, FALSE
); /* discard if src0.x is zero */
6370 emit_src_register(emit
, &tmp_src_x
);
6371 end_emit_instruction(emit
);
6373 /* If we don't need to broadcast the color below or set fragments to
6374 * white, emit final color here.
6376 if (emit
->key
.fs
.write_color0_to_n_cbufs
<= 1 &&
6377 !emit
->key
.fs
.white_fragments
) {
6378 /* MOV output.color, tempcolor */
6379 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &color_dst
,
6380 &color_src
, FALSE
); /* XXX saturate? */
6383 free_temp_indexes(emit
);
6388 * When we need to emit white for all fragments (for emulating XOR logicop
6389 * mode), this function copies white into the temporary color output register.
6392 emit_set_color_white(struct svga_shader_emitter_v10
*emit
,
6393 unsigned fs_color_tmp_index
)
6395 struct tgsi_full_dst_register color_dst
=
6396 make_dst_temp_reg(fs_color_tmp_index
);
6397 struct tgsi_full_src_register white
=
6398 make_immediate_reg_float(emit
, 1.0f
);
6400 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &color_dst
, &white
, FALSE
);
6405 * Emit instructions for writing a single color output to multiple
6407 * This is used when the TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS (or
6408 * when key.fs.white_fragments is true).
6409 * property is set and the number of render targets is greater than one.
6410 * \param fs_color_tmp_index index of the temp register that holds the
6411 * color to broadcast.
6414 emit_broadcast_color_instructions(struct svga_shader_emitter_v10
*emit
,
6415 unsigned fs_color_tmp_index
)
6417 const unsigned n
= emit
->key
.fs
.write_color0_to_n_cbufs
;
6419 struct tgsi_full_src_register color_src
=
6420 make_src_temp_reg(fs_color_tmp_index
);
6422 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
6424 for (i
= 0; i
< n
; i
++) {
6425 unsigned output_reg
= emit
->fs
.color_out_index
[i
];
6426 struct tgsi_full_dst_register color_dst
=
6427 make_dst_output_reg(output_reg
);
6429 /* Fill in this semantic here since we'll use it later in
6430 * emit_dst_register().
6432 emit
->info
.output_semantic_name
[output_reg
] = TGSI_SEMANTIC_COLOR
;
6434 /* MOV output.color[i], tempcolor */
6435 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &color_dst
,
6436 &color_src
, FALSE
); /* XXX saturate? */
6442 * Emit extra helper code after the original shader code, but before the
6443 * last END/RET instruction.
6444 * For vertex shaders this means emitting the extra code to apply the
6445 * prescale scale/translation.
6448 emit_post_helpers(struct svga_shader_emitter_v10
*emit
)
6450 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
6451 emit_vertex_instructions(emit
);
6453 else if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
6454 const unsigned fs_color_tmp_index
= emit
->fs
.color_tmp_index
;
6456 /* We no longer want emit_dst_register() to substitute the
6457 * temporary fragment color register for the real color output.
6459 emit
->fs
.color_tmp_index
= INVALID_INDEX
;
6461 if (emit
->key
.fs
.alpha_func
!= SVGA3D_CMP_ALWAYS
) {
6462 emit_alpha_test_instructions(emit
, fs_color_tmp_index
);
6464 if (emit
->key
.fs
.white_fragments
) {
6465 emit_set_color_white(emit
, fs_color_tmp_index
);
6467 if (emit
->key
.fs
.write_color0_to_n_cbufs
> 1 ||
6468 emit
->key
.fs
.white_fragments
) {
6469 emit_broadcast_color_instructions(emit
, fs_color_tmp_index
);
6478 * Translate the TGSI tokens into VGPU10 tokens.
6481 emit_vgpu10_instructions(struct svga_shader_emitter_v10
*emit
,
6482 const struct tgsi_token
*tokens
)
6484 struct tgsi_parse_context parse
;
6486 boolean pre_helpers_emitted
= FALSE
;
6487 unsigned inst_number
= 0;
6489 tgsi_parse_init(&parse
, tokens
);
6491 while (!tgsi_parse_end_of_tokens(&parse
)) {
6492 tgsi_parse_token(&parse
);
6494 switch (parse
.FullToken
.Token
.Type
) {
6495 case TGSI_TOKEN_TYPE_IMMEDIATE
:
6496 ret
= emit_vgpu10_immediate(emit
, &parse
.FullToken
.FullImmediate
);
6501 case TGSI_TOKEN_TYPE_DECLARATION
:
6502 ret
= emit_vgpu10_declaration(emit
, &parse
.FullToken
.FullDeclaration
);
6507 case TGSI_TOKEN_TYPE_INSTRUCTION
:
6508 if (!pre_helpers_emitted
) {
6509 ret
= emit_pre_helpers(emit
);
6512 pre_helpers_emitted
= TRUE
;
6514 ret
= emit_vgpu10_instruction(emit
, inst_number
++,
6515 &parse
.FullToken
.FullInstruction
);
6520 case TGSI_TOKEN_TYPE_PROPERTY
:
6521 ret
= emit_vgpu10_property(emit
, &parse
.FullToken
.FullProperty
);
6532 tgsi_parse_free(&parse
);
6538 * Emit the first VGPU10 shader tokens.
6541 emit_vgpu10_header(struct svga_shader_emitter_v10
*emit
)
6543 VGPU10ProgramToken ptoken
;
6545 /* First token: VGPU10ProgramToken (version info, program type (VS,GS,PS)) */
6546 ptoken
.majorVersion
= 4;
6547 ptoken
.minorVersion
= 0;
6548 ptoken
.programType
= translate_shader_type(emit
->unit
);
6549 if (!emit_dword(emit
, ptoken
.value
))
6552 /* Second token: total length of shader, in tokens. We can't fill this
6553 * in until we're all done. Emit zero for now.
6555 return emit_dword(emit
, 0);
6560 emit_vgpu10_tail(struct svga_shader_emitter_v10
*emit
)
6562 VGPU10ProgramToken
*tokens
;
6564 /* Replace the second token with total shader length */
6565 tokens
= (VGPU10ProgramToken
*) emit
->buf
;
6566 tokens
[1].value
= emit_get_num_tokens(emit
);
6573 * Modify the FS to read the BCOLORs and use the FACE register
6574 * to choose between the front/back colors.
6576 static const struct tgsi_token
*
6577 transform_fs_twoside(const struct tgsi_token
*tokens
)
6580 debug_printf("Before tgsi_add_two_side ------------------\n");
6581 tgsi_dump(tokens
,0);
6583 tokens
= tgsi_add_two_side(tokens
);
6585 debug_printf("After tgsi_add_two_side ------------------\n");
6586 tgsi_dump(tokens
, 0);
6593 * Modify the FS to do polygon stipple.
6595 static const struct tgsi_token
*
6596 transform_fs_pstipple(struct svga_shader_emitter_v10
*emit
,
6597 const struct tgsi_token
*tokens
)
6599 const struct tgsi_token
*new_tokens
;
6603 debug_printf("Before pstipple ------------------\n");
6604 tgsi_dump(tokens
,0);
6607 new_tokens
= util_pstipple_create_fragment_shader(tokens
, &unit
, 0);
6609 emit
->fs
.pstipple_sampler_unit
= unit
;
6611 /* Setup texture state for stipple */
6612 emit
->key
.tex
[unit
].texture_target
= PIPE_TEXTURE_2D
;
6613 emit
->key
.tex
[unit
].swizzle_r
= TGSI_SWIZZLE_X
;
6614 emit
->key
.tex
[unit
].swizzle_g
= TGSI_SWIZZLE_Y
;
6615 emit
->key
.tex
[unit
].swizzle_b
= TGSI_SWIZZLE_Z
;
6616 emit
->key
.tex
[unit
].swizzle_a
= TGSI_SWIZZLE_W
;
6619 debug_printf("After pstipple ------------------\n");
6620 tgsi_dump(new_tokens
, 0);
6627 * Modify the FS to support anti-aliasing point.
6629 static const struct tgsi_token
*
6630 transform_fs_aapoint(const struct tgsi_token
*tokens
,
6634 debug_printf("Before tgsi_add_aa_point ------------------\n");
6635 tgsi_dump(tokens
,0);
6637 tokens
= tgsi_add_aa_point(tokens
, aa_coord_index
);
6639 debug_printf("After tgsi_add_aa_point ------------------\n");
6640 tgsi_dump(tokens
, 0);
6646 * This is the main entrypoint for the TGSI -> VPGU10 translator.
6648 struct svga_shader_variant
*
6649 svga_tgsi_vgpu10_translate(struct svga_context
*svga
,
6650 const struct svga_shader
*shader
,
6651 const struct svga_compile_key
*key
,
6654 struct svga_shader_variant
*variant
= NULL
;
6655 struct svga_shader_emitter_v10
*emit
;
6656 const struct tgsi_token
*tokens
= shader
->tokens
;
6657 struct svga_vertex_shader
*vs
= svga
->curr
.vs
;
6658 struct svga_geometry_shader
*gs
= svga
->curr
.gs
;
6660 assert(unit
== PIPE_SHADER_VERTEX
||
6661 unit
== PIPE_SHADER_GEOMETRY
||
6662 unit
== PIPE_SHADER_FRAGMENT
);
6664 /* These two flags cannot be used together */
6665 assert(key
->vs
.need_prescale
+ key
->vs
.undo_viewport
<= 1);
6668 * Setup the code emitter
6670 emit
= alloc_emitter();
6677 emit
->vposition
.need_prescale
= (emit
->key
.vs
.need_prescale
||
6678 emit
->key
.gs
.need_prescale
);
6679 emit
->vposition
.tmp_index
= INVALID_INDEX
;
6680 emit
->vposition
.so_index
= INVALID_INDEX
;
6681 emit
->vposition
.out_index
= INVALID_INDEX
;
6683 emit
->fs
.color_tmp_index
= INVALID_INDEX
;
6684 emit
->fs
.face_input_index
= INVALID_INDEX
;
6685 emit
->fs
.fragcoord_input_index
= INVALID_INDEX
;
6687 emit
->gs
.prim_id_index
= INVALID_INDEX
;
6689 emit
->clip_dist_out_index
= INVALID_INDEX
;
6690 emit
->clip_dist_tmp_index
= INVALID_INDEX
;
6691 emit
->clip_dist_so_index
= INVALID_INDEX
;
6692 emit
->clip_vertex_out_index
= INVALID_INDEX
;
6694 if (emit
->key
.fs
.alpha_func
== SVGA3D_CMP_INVALID
) {
6695 emit
->key
.fs
.alpha_func
= SVGA3D_CMP_ALWAYS
;
6698 if (unit
== PIPE_SHADER_FRAGMENT
) {
6699 if (key
->fs
.light_twoside
) {
6700 tokens
= transform_fs_twoside(tokens
);
6702 if (key
->fs
.pstipple
) {
6703 const struct tgsi_token
*new_tokens
=
6704 transform_fs_pstipple(emit
, tokens
);
6705 if (tokens
!= shader
->tokens
) {
6706 /* free the two-sided shader tokens */
6707 tgsi_free_tokens(tokens
);
6709 tokens
= new_tokens
;
6711 if (key
->fs
.aa_point
) {
6712 tokens
= transform_fs_aapoint(tokens
, key
->fs
.aa_point_coord_index
);
6716 if (SVGA_DEBUG
& DEBUG_TGSI
) {
6717 debug_printf("#####################################\n");
6718 debug_printf("### TGSI Shader %u\n", shader
->id
);
6719 tgsi_dump(tokens
, 0);
6723 * Rescan the header if the token string is different from the one
6724 * included in the shader; otherwise, the header info is already up-to-date
6726 if (tokens
!= shader
->tokens
) {
6727 tgsi_scan_shader(tokens
, &emit
->info
);
6729 emit
->info
= shader
->info
;
6732 emit
->num_outputs
= emit
->info
.num_outputs
;
6734 if (unit
== PIPE_SHADER_FRAGMENT
) {
6735 /* Compute FS input remapping to match the output from VS/GS */
6737 svga_link_shaders(&gs
->base
.info
, &emit
->info
, &emit
->linkage
);
6740 svga_link_shaders(&vs
->base
.info
, &emit
->info
, &emit
->linkage
);
6742 } else if (unit
== PIPE_SHADER_GEOMETRY
) {
6744 svga_link_shaders(&vs
->base
.info
, &emit
->info
, &emit
->linkage
);
6747 determine_clipping_mode(emit
);
6749 if (unit
== PIPE_SHADER_GEOMETRY
|| unit
== PIPE_SHADER_VERTEX
) {
6750 if (shader
->stream_output
!= NULL
|| emit
->clip_mode
== CLIP_DISTANCE
) {
6751 /* if there is stream output declarations associated
6752 * with this shader or the shader writes to ClipDistance
6753 * then reserve extra registers for the non-adjusted vertex position
6754 * and the ClipDistance shadow copy
6756 emit
->vposition
.so_index
= emit
->num_outputs
++;
6758 if (emit
->clip_mode
== CLIP_DISTANCE
) {
6759 emit
->clip_dist_so_index
= emit
->num_outputs
++;
6760 if (emit
->info
.num_written_clipdistance
> 4)
6761 emit
->num_outputs
++;
6767 * Do actual shader translation.
6769 if (!emit_vgpu10_header(emit
)) {
6770 debug_printf("svga: emit VGPU10 header failed\n");
6774 if (!emit_vgpu10_instructions(emit
, tokens
)) {
6775 debug_printf("svga: emit VGPU10 instructions failed\n");
6779 if (!emit_vgpu10_tail(emit
)) {
6780 debug_printf("svga: emit VGPU10 tail failed\n");
6784 if (emit
->register_overflow
) {
6789 * Create, initialize the 'variant' object.
6791 variant
= svga_new_shader_variant(svga
);
6795 variant
->shader
= shader
;
6796 variant
->nr_tokens
= emit_get_num_tokens(emit
);
6797 variant
->tokens
= (const unsigned *)emit
->buf
;
6798 emit
->buf
= NULL
; /* buffer is no longer owed by emitter context */
6799 memcpy(&variant
->key
, key
, sizeof(*key
));
6800 variant
->id
= UTIL_BITMASK_INVALID_INDEX
;
6802 /* The extra constant starting offset starts with the number of
6803 * shader constants declared in the shader.
6805 variant
->extra_const_start
= emit
->num_shader_consts
[0];
6806 if (key
->gs
.wide_point
) {
6808 * The extra constant added in the transformed shader
6809 * for inverse viewport scale is to be supplied by the driver.
6810 * So the extra constant starting offset needs to be reduced by 1.
6812 assert(variant
->extra_const_start
> 0);
6813 variant
->extra_const_start
--;
6816 variant
->pstipple_sampler_unit
= emit
->fs
.pstipple_sampler_unit
;
6818 /* If there was exactly one write to a fragment shader output register
6819 * and it came from a constant buffer, we know all fragments will have
6820 * the same color (except for blending).
6822 variant
->constant_color_output
=
6823 emit
->constant_color_output
&& emit
->num_output_writes
== 1;
6825 /** keep track in the variant if flat interpolation is used
6826 * for any of the varyings.
6828 variant
->uses_flat_interp
= emit
->uses_flat_interp
;
6830 if (tokens
!= shader
->tokens
) {
6831 tgsi_free_tokens(tokens
);