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
;
101 unsigned version
; /**< Either 40 or 41 at this time */
103 unsigned inst_start_token
;
104 boolean discard_instruction
; /**< throw away current instruction? */
106 union tgsi_immediate_data immediates
[MAX_IMMEDIATE_COUNT
][4];
107 unsigned num_immediates
; /**< Number of immediates emitted */
108 unsigned common_immediate_pos
[8]; /**< literals for common immediates */
109 unsigned num_common_immediates
;
110 boolean immediates_emitted
;
112 unsigned num_outputs
; /**< include any extra outputs */
113 /** The first extra output is reserved for
114 * non-adjusted vertex position for
115 * stream output purpose
118 /* Temporary Registers */
119 unsigned num_shader_temps
; /**< num of temps used by original shader */
120 unsigned internal_temp_count
; /**< currently allocated internal temps */
122 unsigned start
, size
;
123 } temp_arrays
[MAX_TEMP_ARRAYS
];
124 unsigned num_temp_arrays
;
126 /** Map TGSI temp registers to VGPU10 temp array IDs and indexes */
128 unsigned arrayId
, index
;
129 } temp_map
[VGPU10_MAX_TEMPS
]; /**< arrayId, element */
131 /** Number of constants used by original shader for each constant buffer.
132 * The size should probably always match with that of svga_state.constbufs.
134 unsigned num_shader_consts
[SVGA_MAX_CONST_BUFS
];
137 unsigned num_samplers
;
138 boolean sampler_view
[PIPE_MAX_SAMPLERS
]; /**< True if sampler view exists*/
139 ubyte sampler_target
[PIPE_MAX_SAMPLERS
]; /**< TGSI_TEXTURE_x */
140 ubyte sampler_return_type
[PIPE_MAX_SAMPLERS
]; /**< TGSI_RETURN_TYPE_x */
142 /* Address regs (really implemented with temps) */
143 unsigned num_address_regs
;
144 unsigned address_reg_index
[MAX_VGPU10_ADDR_REGS
];
146 /* Output register usage masks */
147 ubyte output_usage_mask
[PIPE_MAX_SHADER_OUTPUTS
];
149 /* To map TGSI system value index to VGPU shader input indexes */
150 ubyte system_value_indexes
[MAX_SYSTEM_VALUES
];
153 /* vertex position scale/translation */
154 unsigned out_index
; /**< the real position output reg */
155 unsigned tmp_index
; /**< the fake/temp position output reg */
156 unsigned so_index
; /**< the non-adjusted position output reg */
157 unsigned prescale_scale_index
, prescale_trans_index
;
158 boolean need_prescale
;
161 /* For vertex shaders only */
163 /* viewport constant */
164 unsigned viewport_index
;
166 /* temp index of adjusted vertex attributes */
167 unsigned adjusted_input
[PIPE_MAX_SHADER_INPUTS
];
170 /* For fragment shaders only */
172 unsigned color_out_index
[PIPE_MAX_COLOR_BUFS
]; /**< the real color output regs */
173 unsigned num_color_outputs
;
174 unsigned color_tmp_index
; /**< fake/temp color output reg */
175 unsigned alpha_ref_index
; /**< immediate constant for alpha ref */
178 unsigned face_input_index
; /**< real fragment shader face reg (bool) */
179 unsigned face_tmp_index
; /**< temp face reg converted to -1 / +1 */
181 unsigned pstipple_sampler_unit
;
183 unsigned fragcoord_input_index
; /**< real fragment position input reg */
184 unsigned fragcoord_tmp_index
; /**< 1/w modified position temp reg */
186 /** Which texture units are doing shadow comparison in the FS code */
187 unsigned shadow_compare_units
;
189 unsigned sample_id_sys_index
; /**< TGSI index of sample id sys value */
191 unsigned sample_pos_sys_index
; /**< TGSI index of sample pos sys value */
192 unsigned sample_pos_tmp_index
; /**< which temp reg has the sample pos */
195 /* For geometry shaders only */
197 VGPU10_PRIMITIVE prim_type
;/**< VGPU10 primitive type */
198 VGPU10_PRIMITIVE_TOPOLOGY prim_topology
; /**< VGPU10 primitive topology */
199 unsigned input_size
; /**< size of input arrays */
200 unsigned prim_id_index
; /**< primitive id register index */
201 unsigned max_out_vertices
; /**< maximum number of output vertices */
204 /* For vertex or geometry shaders */
205 enum clipping_mode clip_mode
;
206 unsigned clip_dist_out_index
; /**< clip distance output register index */
207 unsigned clip_dist_tmp_index
; /**< clip distance temporary register */
208 unsigned clip_dist_so_index
; /**< clip distance shadow copy */
210 /** Index of temporary holding the clipvertex coordinate */
211 unsigned clip_vertex_out_index
; /**< clip vertex output register index */
212 unsigned clip_vertex_tmp_index
; /**< clip vertex temporary index */
214 /* user clip plane constant slot indexes */
215 unsigned clip_plane_const
[PIPE_MAX_CLIP_PLANES
];
217 unsigned num_output_writes
;
218 boolean constant_color_output
;
220 boolean uses_flat_interp
;
222 /* For all shaders: const reg index for RECT coord scaling */
223 unsigned texcoord_scale_index
[PIPE_MAX_SAMPLERS
];
225 /* For all shaders: const reg index for texture buffer size */
226 unsigned texture_buffer_size_index
[PIPE_MAX_SAMPLERS
];
228 /* VS/GS/FS Linkage info */
229 struct shader_linkage linkage
;
231 bool register_overflow
; /**< Set if we exceed a VGPU10 register limit */
236 emit_post_helpers(struct svga_shader_emitter_v10
*emit
);
239 emit_vertex(struct svga_shader_emitter_v10
*emit
,
240 const struct tgsi_full_instruction
*inst
);
242 static char err_buf
[128];
245 expand(struct svga_shader_emitter_v10
*emit
)
248 unsigned newsize
= emit
->size
* 2;
250 if (emit
->buf
!= err_buf
)
251 new_buf
= REALLOC(emit
->buf
, emit
->size
, newsize
);
258 emit
->size
= sizeof(err_buf
);
262 emit
->size
= newsize
;
263 emit
->ptr
= new_buf
+ (emit
->ptr
- emit
->buf
);
269 * Create and initialize a new svga_shader_emitter_v10 object.
271 static struct svga_shader_emitter_v10
*
274 struct svga_shader_emitter_v10
*emit
= CALLOC(1, sizeof(*emit
));
279 /* to initialize the output buffer */
289 * Free an svga_shader_emitter_v10 object.
292 free_emitter(struct svga_shader_emitter_v10
*emit
)
295 FREE(emit
->buf
); /* will be NULL if translation succeeded */
299 static inline boolean
300 reserve(struct svga_shader_emitter_v10
*emit
,
303 while (emit
->ptr
- emit
->buf
+ nr_dwords
* sizeof(uint32
) >= emit
->size
) {
312 emit_dword(struct svga_shader_emitter_v10
*emit
, uint32 dword
)
314 if (!reserve(emit
, 1))
317 *(uint32
*)emit
->ptr
= dword
;
318 emit
->ptr
+= sizeof dword
;
323 emit_dwords(struct svga_shader_emitter_v10
*emit
,
324 const uint32
*dwords
,
327 if (!reserve(emit
, nr
))
330 memcpy(emit
->ptr
, dwords
, nr
* sizeof *dwords
);
331 emit
->ptr
+= nr
* sizeof *dwords
;
335 /** Return the number of tokens in the emitter's buffer */
337 emit_get_num_tokens(const struct svga_shader_emitter_v10
*emit
)
339 return (emit
->ptr
- emit
->buf
) / sizeof(unsigned);
344 * Check for register overflow. If we overflow we'll set an
345 * error flag. This function can be called for register declarations
346 * or use as src/dst instruction operands.
347 * \param type register type. One of VGPU10_OPERAND_TYPE_x
348 or VGPU10_OPCODE_DCL_x
349 * \param index the register index
352 check_register_index(struct svga_shader_emitter_v10
*emit
,
353 unsigned operandType
, unsigned index
)
355 bool overflow_before
= emit
->register_overflow
;
357 switch (operandType
) {
358 case VGPU10_OPERAND_TYPE_TEMP
:
359 case VGPU10_OPERAND_TYPE_INDEXABLE_TEMP
:
360 case VGPU10_OPCODE_DCL_TEMPS
:
361 if (index
>= VGPU10_MAX_TEMPS
) {
362 emit
->register_overflow
= TRUE
;
365 case VGPU10_OPERAND_TYPE_CONSTANT_BUFFER
:
366 case VGPU10_OPCODE_DCL_CONSTANT_BUFFER
:
367 if (index
>= VGPU10_MAX_CONSTANT_BUFFER_ELEMENT_COUNT
) {
368 emit
->register_overflow
= TRUE
;
371 case VGPU10_OPERAND_TYPE_INPUT
:
372 case VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID
:
373 case VGPU10_OPCODE_DCL_INPUT
:
374 case VGPU10_OPCODE_DCL_INPUT_SGV
:
375 case VGPU10_OPCODE_DCL_INPUT_SIV
:
376 case VGPU10_OPCODE_DCL_INPUT_PS
:
377 case VGPU10_OPCODE_DCL_INPUT_PS_SGV
:
378 case VGPU10_OPCODE_DCL_INPUT_PS_SIV
:
379 if ((emit
->unit
== PIPE_SHADER_VERTEX
&&
380 index
>= VGPU10_MAX_VS_INPUTS
) ||
381 (emit
->unit
== PIPE_SHADER_GEOMETRY
&&
382 index
>= VGPU10_MAX_GS_INPUTS
) ||
383 (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
384 index
>= VGPU10_MAX_FS_INPUTS
)) {
385 emit
->register_overflow
= TRUE
;
388 case VGPU10_OPERAND_TYPE_OUTPUT
:
389 case VGPU10_OPCODE_DCL_OUTPUT
:
390 case VGPU10_OPCODE_DCL_OUTPUT_SGV
:
391 case VGPU10_OPCODE_DCL_OUTPUT_SIV
:
392 if ((emit
->unit
== PIPE_SHADER_VERTEX
&&
393 index
>= VGPU10_MAX_VS_OUTPUTS
) ||
394 (emit
->unit
== PIPE_SHADER_GEOMETRY
&&
395 index
>= VGPU10_MAX_GS_OUTPUTS
) ||
396 (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
397 index
>= VGPU10_MAX_FS_OUTPUTS
)) {
398 emit
->register_overflow
= TRUE
;
401 case VGPU10_OPERAND_TYPE_SAMPLER
:
402 case VGPU10_OPCODE_DCL_SAMPLER
:
403 if (index
>= VGPU10_MAX_SAMPLERS
) {
404 emit
->register_overflow
= TRUE
;
407 case VGPU10_OPERAND_TYPE_RESOURCE
:
408 case VGPU10_OPCODE_DCL_RESOURCE
:
409 if (index
>= VGPU10_MAX_RESOURCES
) {
410 emit
->register_overflow
= TRUE
;
413 case VGPU10_OPERAND_TYPE_IMMEDIATE_CONSTANT_BUFFER
:
414 if (index
>= MAX_IMMEDIATE_COUNT
) {
415 emit
->register_overflow
= TRUE
;
418 case VGPU10_OPERAND_TYPE_OUTPUT_COVERAGE_MASK
:
426 if (emit
->register_overflow
&& !overflow_before
) {
427 debug_printf("svga: vgpu10 register overflow (reg %u, index %u)\n",
434 * Examine misc state to determine the clipping mode.
437 determine_clipping_mode(struct svga_shader_emitter_v10
*emit
)
439 if (emit
->info
.num_written_clipdistance
> 0) {
440 emit
->clip_mode
= CLIP_DISTANCE
;
442 else if (emit
->info
.writes_clipvertex
) {
443 emit
->clip_mode
= CLIP_VERTEX
;
445 else if (emit
->key
.clip_plane_enable
) {
446 emit
->clip_mode
= CLIP_LEGACY
;
449 emit
->clip_mode
= CLIP_NONE
;
455 * For clip distance register declarations and clip distance register
456 * writes we need to mask the declaration usage or instruction writemask
457 * (respectively) against the set of the really-enabled clipping planes.
459 * The piglit test spec/glsl-1.30/execution/clipping/vs-clip-distance-enables
460 * has a VS that writes to all 8 clip distance registers, but the plane enable
461 * flags are a subset of that.
463 * This function is used to apply the plane enable flags to the register
464 * declaration or instruction writemask.
466 * \param writemask the declaration usage mask or instruction writemask
467 * \param clip_reg_index which clip plane register is being declared/written.
468 * The legal values are 0 and 1 (two clip planes per
469 * register, for a total of 8 clip planes)
472 apply_clip_plane_mask(struct svga_shader_emitter_v10
*emit
,
473 unsigned writemask
, unsigned clip_reg_index
)
477 assert(clip_reg_index
< 2);
479 /* four clip planes per clip register: */
480 shift
= clip_reg_index
* 4;
481 writemask
&= ((emit
->key
.clip_plane_enable
>> shift
) & 0xf);
488 * Translate gallium shader type into VGPU10 type.
490 static VGPU10_PROGRAM_TYPE
491 translate_shader_type(unsigned type
)
494 case PIPE_SHADER_VERTEX
:
495 return VGPU10_VERTEX_SHADER
;
496 case PIPE_SHADER_GEOMETRY
:
497 return VGPU10_GEOMETRY_SHADER
;
498 case PIPE_SHADER_FRAGMENT
:
499 return VGPU10_PIXEL_SHADER
;
501 assert(!"Unexpected shader type");
502 return VGPU10_VERTEX_SHADER
;
508 * Translate a TGSI_OPCODE_x into a VGPU10_OPCODE_x
509 * Note: we only need to translate the opcodes for "simple" instructions,
510 * as seen below. All other opcodes are handled/translated specially.
512 static VGPU10_OPCODE_TYPE
513 translate_opcode(enum tgsi_opcode opcode
)
516 case TGSI_OPCODE_MOV
:
517 return VGPU10_OPCODE_MOV
;
518 case TGSI_OPCODE_MUL
:
519 return VGPU10_OPCODE_MUL
;
520 case TGSI_OPCODE_ADD
:
521 return VGPU10_OPCODE_ADD
;
522 case TGSI_OPCODE_DP3
:
523 return VGPU10_OPCODE_DP3
;
524 case TGSI_OPCODE_DP4
:
525 return VGPU10_OPCODE_DP4
;
526 case TGSI_OPCODE_MIN
:
527 return VGPU10_OPCODE_MIN
;
528 case TGSI_OPCODE_MAX
:
529 return VGPU10_OPCODE_MAX
;
530 case TGSI_OPCODE_MAD
:
531 return VGPU10_OPCODE_MAD
;
532 case TGSI_OPCODE_SQRT
:
533 return VGPU10_OPCODE_SQRT
;
534 case TGSI_OPCODE_FRC
:
535 return VGPU10_OPCODE_FRC
;
536 case TGSI_OPCODE_FLR
:
537 return VGPU10_OPCODE_ROUND_NI
;
538 case TGSI_OPCODE_FSEQ
:
539 return VGPU10_OPCODE_EQ
;
540 case TGSI_OPCODE_FSGE
:
541 return VGPU10_OPCODE_GE
;
542 case TGSI_OPCODE_FSNE
:
543 return VGPU10_OPCODE_NE
;
544 case TGSI_OPCODE_DDX
:
545 return VGPU10_OPCODE_DERIV_RTX
;
546 case TGSI_OPCODE_DDY
:
547 return VGPU10_OPCODE_DERIV_RTY
;
548 case TGSI_OPCODE_RET
:
549 return VGPU10_OPCODE_RET
;
550 case TGSI_OPCODE_DIV
:
551 return VGPU10_OPCODE_DIV
;
552 case TGSI_OPCODE_IDIV
:
553 return VGPU10_OPCODE_IDIV
;
554 case TGSI_OPCODE_DP2
:
555 return VGPU10_OPCODE_DP2
;
556 case TGSI_OPCODE_BRK
:
557 return VGPU10_OPCODE_BREAK
;
559 return VGPU10_OPCODE_IF
;
560 case TGSI_OPCODE_ELSE
:
561 return VGPU10_OPCODE_ELSE
;
562 case TGSI_OPCODE_ENDIF
:
563 return VGPU10_OPCODE_ENDIF
;
564 case TGSI_OPCODE_CEIL
:
565 return VGPU10_OPCODE_ROUND_PI
;
566 case TGSI_OPCODE_I2F
:
567 return VGPU10_OPCODE_ITOF
;
568 case TGSI_OPCODE_NOT
:
569 return VGPU10_OPCODE_NOT
;
570 case TGSI_OPCODE_TRUNC
:
571 return VGPU10_OPCODE_ROUND_Z
;
572 case TGSI_OPCODE_SHL
:
573 return VGPU10_OPCODE_ISHL
;
574 case TGSI_OPCODE_AND
:
575 return VGPU10_OPCODE_AND
;
577 return VGPU10_OPCODE_OR
;
578 case TGSI_OPCODE_XOR
:
579 return VGPU10_OPCODE_XOR
;
580 case TGSI_OPCODE_CONT
:
581 return VGPU10_OPCODE_CONTINUE
;
582 case TGSI_OPCODE_EMIT
:
583 return VGPU10_OPCODE_EMIT
;
584 case TGSI_OPCODE_ENDPRIM
:
585 return VGPU10_OPCODE_CUT
;
586 case TGSI_OPCODE_BGNLOOP
:
587 return VGPU10_OPCODE_LOOP
;
588 case TGSI_OPCODE_ENDLOOP
:
589 return VGPU10_OPCODE_ENDLOOP
;
590 case TGSI_OPCODE_ENDSUB
:
591 return VGPU10_OPCODE_RET
;
592 case TGSI_OPCODE_NOP
:
593 return VGPU10_OPCODE_NOP
;
594 case TGSI_OPCODE_END
:
595 return VGPU10_OPCODE_RET
;
596 case TGSI_OPCODE_F2I
:
597 return VGPU10_OPCODE_FTOI
;
598 case TGSI_OPCODE_IMAX
:
599 return VGPU10_OPCODE_IMAX
;
600 case TGSI_OPCODE_IMIN
:
601 return VGPU10_OPCODE_IMIN
;
602 case TGSI_OPCODE_UDIV
:
603 case TGSI_OPCODE_UMOD
:
604 case TGSI_OPCODE_MOD
:
605 return VGPU10_OPCODE_UDIV
;
606 case TGSI_OPCODE_IMUL_HI
:
607 return VGPU10_OPCODE_IMUL
;
608 case TGSI_OPCODE_INEG
:
609 return VGPU10_OPCODE_INEG
;
610 case TGSI_OPCODE_ISHR
:
611 return VGPU10_OPCODE_ISHR
;
612 case TGSI_OPCODE_ISGE
:
613 return VGPU10_OPCODE_IGE
;
614 case TGSI_OPCODE_ISLT
:
615 return VGPU10_OPCODE_ILT
;
616 case TGSI_OPCODE_F2U
:
617 return VGPU10_OPCODE_FTOU
;
618 case TGSI_OPCODE_UADD
:
619 return VGPU10_OPCODE_IADD
;
620 case TGSI_OPCODE_U2F
:
621 return VGPU10_OPCODE_UTOF
;
622 case TGSI_OPCODE_UCMP
:
623 return VGPU10_OPCODE_MOVC
;
624 case TGSI_OPCODE_UMAD
:
625 return VGPU10_OPCODE_UMAD
;
626 case TGSI_OPCODE_UMAX
:
627 return VGPU10_OPCODE_UMAX
;
628 case TGSI_OPCODE_UMIN
:
629 return VGPU10_OPCODE_UMIN
;
630 case TGSI_OPCODE_UMUL
:
631 case TGSI_OPCODE_UMUL_HI
:
632 return VGPU10_OPCODE_UMUL
;
633 case TGSI_OPCODE_USEQ
:
634 return VGPU10_OPCODE_IEQ
;
635 case TGSI_OPCODE_USGE
:
636 return VGPU10_OPCODE_UGE
;
637 case TGSI_OPCODE_USHR
:
638 return VGPU10_OPCODE_USHR
;
639 case TGSI_OPCODE_USLT
:
640 return VGPU10_OPCODE_ULT
;
641 case TGSI_OPCODE_USNE
:
642 return VGPU10_OPCODE_INE
;
643 case TGSI_OPCODE_SWITCH
:
644 return VGPU10_OPCODE_SWITCH
;
645 case TGSI_OPCODE_CASE
:
646 return VGPU10_OPCODE_CASE
;
647 case TGSI_OPCODE_DEFAULT
:
648 return VGPU10_OPCODE_DEFAULT
;
649 case TGSI_OPCODE_ENDSWITCH
:
650 return VGPU10_OPCODE_ENDSWITCH
;
651 case TGSI_OPCODE_FSLT
:
652 return VGPU10_OPCODE_LT
;
653 case TGSI_OPCODE_ROUND
:
654 return VGPU10_OPCODE_ROUND_NE
;
655 case TGSI_OPCODE_SAMPLE_POS
:
656 /* Note: we never actually get this opcode because there's no GLSL
657 * function to query multisample resource sample positions. There's
658 * only the TGSI_SEMANTIC_SAMPLEPOS system value which contains the
659 * position of the current sample in the render target.
662 case TGSI_OPCODE_SAMPLE_INFO
:
663 /* NOTE: we never actually get this opcode because the GLSL compiler
664 * implements the gl_NumSamples variable with a simple constant in the
669 assert(!"Unexpected TGSI opcode in translate_opcode()");
670 return VGPU10_OPCODE_NOP
;
676 * Translate a TGSI register file type into a VGPU10 operand type.
677 * \param array is the TGSI_FILE_TEMPORARY register an array?
679 static VGPU10_OPERAND_TYPE
680 translate_register_file(enum tgsi_file_type file
, boolean array
)
683 case TGSI_FILE_CONSTANT
:
684 return VGPU10_OPERAND_TYPE_CONSTANT_BUFFER
;
685 case TGSI_FILE_INPUT
:
686 return VGPU10_OPERAND_TYPE_INPUT
;
687 case TGSI_FILE_OUTPUT
:
688 return VGPU10_OPERAND_TYPE_OUTPUT
;
689 case TGSI_FILE_TEMPORARY
:
690 return array
? VGPU10_OPERAND_TYPE_INDEXABLE_TEMP
691 : VGPU10_OPERAND_TYPE_TEMP
;
692 case TGSI_FILE_IMMEDIATE
:
693 /* all immediates are 32-bit values at this time so
694 * VGPU10_OPERAND_TYPE_IMMEDIATE64 is not possible at this time.
696 return VGPU10_OPERAND_TYPE_IMMEDIATE_CONSTANT_BUFFER
;
697 case TGSI_FILE_SAMPLER
:
698 return VGPU10_OPERAND_TYPE_SAMPLER
;
699 case TGSI_FILE_SYSTEM_VALUE
:
700 return VGPU10_OPERAND_TYPE_INPUT
;
702 /* XXX TODO more cases to finish */
705 assert(!"Bad tgsi register file!");
706 return VGPU10_OPERAND_TYPE_NULL
;
712 * Emit a null dst register
715 emit_null_dst_register(struct svga_shader_emitter_v10
*emit
)
717 VGPU10OperandToken0 operand
;
720 operand
.operandType
= VGPU10_OPERAND_TYPE_NULL
;
721 operand
.numComponents
= VGPU10_OPERAND_0_COMPONENT
;
723 emit_dword(emit
, operand
.value
);
728 * If the given register is a temporary, return the array ID.
732 get_temp_array_id(const struct svga_shader_emitter_v10
*emit
,
733 enum tgsi_file_type file
, unsigned index
)
735 if (file
== TGSI_FILE_TEMPORARY
) {
736 return emit
->temp_map
[index
].arrayId
;
745 * If the given register is a temporary, convert the index from a TGSI
746 * TEMPORARY index to a VGPU10 temp index.
749 remap_temp_index(const struct svga_shader_emitter_v10
*emit
,
750 enum tgsi_file_type file
, unsigned index
)
752 if (file
== TGSI_FILE_TEMPORARY
) {
753 return emit
->temp_map
[index
].index
;
762 * Setup the operand0 fields related to indexing (1D, 2D, relative, etc).
763 * Note: the operandType field must already be initialized.
765 static VGPU10OperandToken0
766 setup_operand0_indexing(struct svga_shader_emitter_v10
*emit
,
767 VGPU10OperandToken0 operand0
,
768 enum tgsi_file_type file
,
769 boolean indirect
, boolean index2D
,
770 unsigned tempArrayID
)
772 unsigned indexDim
, index0Rep
, index1Rep
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
775 * Compute index dimensions
777 if (operand0
.operandType
== VGPU10_OPERAND_TYPE_IMMEDIATE32
||
778 operand0
.operandType
== VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID
) {
779 /* there's no swizzle for in-line immediates */
780 indexDim
= VGPU10_OPERAND_INDEX_0D
;
781 assert(operand0
.selectionMode
== 0);
786 operand0
.operandType
== VGPU10_OPERAND_TYPE_CONSTANT_BUFFER
) {
787 indexDim
= VGPU10_OPERAND_INDEX_2D
;
790 indexDim
= VGPU10_OPERAND_INDEX_1D
;
795 * Compute index representations (immediate, relative, etc).
797 if (tempArrayID
> 0) {
798 assert(file
== TGSI_FILE_TEMPORARY
);
799 /* First index is the array ID, second index is the array element */
800 index0Rep
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
802 index1Rep
= VGPU10_OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE
;
805 index1Rep
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
809 if (file
== TGSI_FILE_CONSTANT
) {
810 /* index[0] indicates which constant buffer while index[1] indicates
811 * the position in the constant buffer.
813 index0Rep
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
814 index1Rep
= VGPU10_OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE
;
817 /* All other register files are 1-dimensional */
818 index0Rep
= VGPU10_OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE
;
822 index0Rep
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
823 index1Rep
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
826 operand0
.indexDimension
= indexDim
;
827 operand0
.index0Representation
= index0Rep
;
828 operand0
.index1Representation
= index1Rep
;
835 * Emit the operand for expressing an address register for indirect indexing.
836 * Note that the address register is really just a temp register.
837 * \param addr_reg_index which address register to use
840 emit_indirect_register(struct svga_shader_emitter_v10
*emit
,
841 unsigned addr_reg_index
)
843 unsigned tmp_reg_index
;
844 VGPU10OperandToken0 operand0
;
846 assert(addr_reg_index
< MAX_VGPU10_ADDR_REGS
);
848 tmp_reg_index
= emit
->address_reg_index
[addr_reg_index
];
850 /* operand0 is a simple temporary register, selecting one component */
852 operand0
.operandType
= VGPU10_OPERAND_TYPE_TEMP
;
853 operand0
.numComponents
= VGPU10_OPERAND_4_COMPONENT
;
854 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_1D
;
855 operand0
.index0Representation
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
856 operand0
.selectionMode
= VGPU10_OPERAND_4_COMPONENT_SELECT_1_MODE
;
857 operand0
.swizzleX
= 0;
858 operand0
.swizzleY
= 1;
859 operand0
.swizzleZ
= 2;
860 operand0
.swizzleW
= 3;
862 emit_dword(emit
, operand0
.value
);
863 emit_dword(emit
, remap_temp_index(emit
, TGSI_FILE_TEMPORARY
, tmp_reg_index
));
868 * Translate the dst register of a TGSI instruction and emit VGPU10 tokens.
869 * \param emit the emitter context
870 * \param reg the TGSI dst register to translate
873 emit_dst_register(struct svga_shader_emitter_v10
*emit
,
874 const struct tgsi_full_dst_register
*reg
)
876 enum tgsi_file_type file
= reg
->Register
.File
;
877 unsigned index
= reg
->Register
.Index
;
878 const enum tgsi_semantic sem_name
= emit
->info
.output_semantic_name
[index
];
879 const unsigned sem_index
= emit
->info
.output_semantic_index
[index
];
880 unsigned writemask
= reg
->Register
.WriteMask
;
881 const boolean indirect
= reg
->Register
.Indirect
;
882 const unsigned tempArrayId
= get_temp_array_id(emit
, file
, index
);
883 const boolean index2d
= reg
->Register
.Dimension
;
884 VGPU10OperandToken0 operand0
;
886 if (file
== TGSI_FILE_OUTPUT
) {
887 if (emit
->unit
== PIPE_SHADER_VERTEX
||
888 emit
->unit
== PIPE_SHADER_GEOMETRY
) {
889 if (index
== emit
->vposition
.out_index
&&
890 emit
->vposition
.tmp_index
!= INVALID_INDEX
) {
891 /* replace OUTPUT[POS] with TEMP[POS]. We need to store the
892 * vertex position result in a temporary so that we can modify
893 * it in the post_helper() code.
895 file
= TGSI_FILE_TEMPORARY
;
896 index
= emit
->vposition
.tmp_index
;
898 else if (sem_name
== TGSI_SEMANTIC_CLIPDIST
&&
899 emit
->clip_dist_tmp_index
!= INVALID_INDEX
) {
900 /* replace OUTPUT[CLIPDIST] with TEMP[CLIPDIST].
901 * We store the clip distance in a temporary first, then
902 * we'll copy it to the shadow copy and to CLIPDIST with the
903 * enabled planes mask in emit_clip_distance_instructions().
905 file
= TGSI_FILE_TEMPORARY
;
906 index
= emit
->clip_dist_tmp_index
+ sem_index
;
908 else if (sem_name
== TGSI_SEMANTIC_CLIPVERTEX
&&
909 emit
->clip_vertex_tmp_index
!= INVALID_INDEX
) {
910 /* replace the CLIPVERTEX output register with a temporary */
911 assert(emit
->clip_mode
== CLIP_VERTEX
);
912 assert(sem_index
== 0);
913 file
= TGSI_FILE_TEMPORARY
;
914 index
= emit
->clip_vertex_tmp_index
;
917 else if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
918 if (sem_name
== TGSI_SEMANTIC_POSITION
) {
919 /* Fragment depth output register */
921 operand0
.operandType
= VGPU10_OPERAND_TYPE_OUTPUT_DEPTH
;
922 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_0D
;
923 operand0
.numComponents
= VGPU10_OPERAND_1_COMPONENT
;
924 emit_dword(emit
, operand0
.value
);
927 else if (sem_name
== TGSI_SEMANTIC_SAMPLEMASK
) {
928 /* Fragment sample mask output */
930 operand0
.operandType
= VGPU10_OPERAND_TYPE_OUTPUT_COVERAGE_MASK
;
931 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_0D
;
932 operand0
.numComponents
= VGPU10_OPERAND_1_COMPONENT
;
933 emit_dword(emit
, operand0
.value
);
936 else if (index
== emit
->fs
.color_out_index
[0] &&
937 emit
->fs
.color_tmp_index
!= INVALID_INDEX
) {
938 /* replace OUTPUT[COLOR] with TEMP[COLOR]. We need to store the
939 * fragment color result in a temporary so that we can read it
940 * it in the post_helper() code.
942 file
= TGSI_FILE_TEMPORARY
;
943 index
= emit
->fs
.color_tmp_index
;
946 /* Typically, for fragment shaders, the output register index
947 * matches the color semantic index. But not when we write to
948 * the fragment depth register. In that case, OUT[0] will be
949 * fragdepth and OUT[1] will be the 0th color output. We need
950 * to use the semantic index for color outputs.
952 assert(sem_name
== TGSI_SEMANTIC_COLOR
);
953 index
= emit
->info
.output_semantic_index
[index
];
955 emit
->num_output_writes
++;
960 /* init operand tokens to all zero */
963 operand0
.numComponents
= VGPU10_OPERAND_4_COMPONENT
;
965 /* the operand has a writemask */
966 operand0
.selectionMode
= VGPU10_OPERAND_4_COMPONENT_MASK_MODE
;
968 /* Which of the four dest components to write to. Note that we can use a
969 * simple assignment here since TGSI writemasks match VGPU10 writemasks.
971 STATIC_ASSERT(TGSI_WRITEMASK_X
== VGPU10_OPERAND_4_COMPONENT_MASK_X
);
972 operand0
.mask
= writemask
;
974 /* translate TGSI register file type to VGPU10 operand type */
975 operand0
.operandType
= translate_register_file(file
, tempArrayId
> 0);
977 check_register_index(emit
, operand0
.operandType
, index
);
979 operand0
= setup_operand0_indexing(emit
, operand0
, file
, indirect
,
980 index2d
, tempArrayId
);
983 emit_dword(emit
, operand0
.value
);
984 if (tempArrayId
> 0) {
985 emit_dword(emit
, tempArrayId
);
988 emit_dword(emit
, remap_temp_index(emit
, file
, index
));
991 emit_indirect_register(emit
, reg
->Indirect
.Index
);
997 * Translate a src register of a TGSI instruction and emit VGPU10 tokens.
998 * In quite a few cases, we do register substitution. For example, if
999 * the TGSI register is the front/back-face register, we replace that with
1000 * a temp register containing a value we computed earlier.
1003 emit_src_register(struct svga_shader_emitter_v10
*emit
,
1004 const struct tgsi_full_src_register
*reg
)
1006 enum tgsi_file_type file
= reg
->Register
.File
;
1007 unsigned index
= reg
->Register
.Index
;
1008 const boolean indirect
= reg
->Register
.Indirect
;
1009 const unsigned tempArrayId
= get_temp_array_id(emit
, file
, index
);
1010 const boolean index2d
= reg
->Register
.Dimension
;
1011 const unsigned swizzleX
= reg
->Register
.SwizzleX
;
1012 const unsigned swizzleY
= reg
->Register
.SwizzleY
;
1013 const unsigned swizzleZ
= reg
->Register
.SwizzleZ
;
1014 const unsigned swizzleW
= reg
->Register
.SwizzleW
;
1015 const boolean absolute
= reg
->Register
.Absolute
;
1016 const boolean negate
= reg
->Register
.Negate
;
1017 bool is_prim_id
= FALSE
;
1019 VGPU10OperandToken0 operand0
;
1020 VGPU10OperandToken1 operand1
;
1022 if (emit
->unit
== PIPE_SHADER_FRAGMENT
){
1023 if (file
== TGSI_FILE_INPUT
) {
1024 if (index
== emit
->fs
.face_input_index
) {
1025 /* Replace INPUT[FACE] with TEMP[FACE] */
1026 file
= TGSI_FILE_TEMPORARY
;
1027 index
= emit
->fs
.face_tmp_index
;
1029 else if (index
== emit
->fs
.fragcoord_input_index
) {
1030 /* Replace INPUT[POSITION] with TEMP[POSITION] */
1031 file
= TGSI_FILE_TEMPORARY
;
1032 index
= emit
->fs
.fragcoord_tmp_index
;
1035 /* We remap fragment shader inputs to that FS input indexes
1036 * match up with VS/GS output indexes.
1038 index
= emit
->linkage
.input_map
[index
];
1041 else if (file
== TGSI_FILE_SYSTEM_VALUE
) {
1042 if (index
== emit
->fs
.sample_pos_sys_index
) {
1043 assert(emit
->version
>= 41);
1044 /* Current sample position is in a temp register */
1045 file
= TGSI_FILE_TEMPORARY
;
1046 index
= emit
->fs
.sample_pos_tmp_index
;
1049 /* Map the TGSI system value to a VGPU10 input register */
1050 assert(index
< ARRAY_SIZE(emit
->system_value_indexes
));
1051 file
= TGSI_FILE_INPUT
;
1052 index
= emit
->system_value_indexes
[index
];
1056 else if (emit
->unit
== PIPE_SHADER_GEOMETRY
) {
1057 if (file
== TGSI_FILE_INPUT
) {
1058 is_prim_id
= (index
== emit
->gs
.prim_id_index
);
1059 index
= emit
->linkage
.input_map
[index
];
1062 else if (emit
->unit
== PIPE_SHADER_VERTEX
) {
1063 if (file
== TGSI_FILE_INPUT
) {
1064 /* if input is adjusted... */
1065 if ((emit
->key
.vs
.adjust_attrib_w_1
|
1066 emit
->key
.vs
.adjust_attrib_itof
|
1067 emit
->key
.vs
.adjust_attrib_utof
|
1068 emit
->key
.vs
.attrib_is_bgra
|
1069 emit
->key
.vs
.attrib_puint_to_snorm
|
1070 emit
->key
.vs
.attrib_puint_to_uscaled
|
1071 emit
->key
.vs
.attrib_puint_to_sscaled
) & (1 << index
)) {
1072 file
= TGSI_FILE_TEMPORARY
;
1073 index
= emit
->vs
.adjusted_input
[index
];
1076 else if (file
== TGSI_FILE_SYSTEM_VALUE
) {
1077 /* Map the TGSI system value to a VGPU10 input register */
1078 assert(index
< ARRAY_SIZE(emit
->system_value_indexes
));
1079 file
= TGSI_FILE_INPUT
;
1080 index
= emit
->system_value_indexes
[index
];
1084 operand0
.value
= operand1
.value
= 0;
1087 /* NOTE: we should be using VGPU10_OPERAND_1_COMPONENT here, but
1088 * our virtual GPU accepts this as-is.
1090 operand0
.numComponents
= VGPU10_OPERAND_0_COMPONENT
;
1091 operand0
.operandType
= VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID
;
1094 operand0
.numComponents
= VGPU10_OPERAND_4_COMPONENT
;
1095 operand0
.operandType
= translate_register_file(file
, tempArrayId
> 0);
1098 operand0
= setup_operand0_indexing(emit
, operand0
, file
, indirect
,
1099 index2d
, tempArrayId
);
1101 if (operand0
.operandType
!= VGPU10_OPERAND_TYPE_IMMEDIATE32
&&
1102 operand0
.operandType
!= VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID
) {
1103 /* there's no swizzle for in-line immediates */
1104 if (swizzleX
== swizzleY
&&
1105 swizzleX
== swizzleZ
&&
1106 swizzleX
== swizzleW
) {
1107 operand0
.selectionMode
= VGPU10_OPERAND_4_COMPONENT_SELECT_1_MODE
;
1110 operand0
.selectionMode
= VGPU10_OPERAND_4_COMPONENT_SWIZZLE_MODE
;
1113 operand0
.swizzleX
= swizzleX
;
1114 operand0
.swizzleY
= swizzleY
;
1115 operand0
.swizzleZ
= swizzleZ
;
1116 operand0
.swizzleW
= swizzleW
;
1118 if (absolute
|| negate
) {
1119 operand0
.extended
= 1;
1120 operand1
.extendedOperandType
= VGPU10_EXTENDED_OPERAND_MODIFIER
;
1121 if (absolute
&& !negate
)
1122 operand1
.operandModifier
= VGPU10_OPERAND_MODIFIER_ABS
;
1123 if (!absolute
&& negate
)
1124 operand1
.operandModifier
= VGPU10_OPERAND_MODIFIER_NEG
;
1125 if (absolute
&& negate
)
1126 operand1
.operandModifier
= VGPU10_OPERAND_MODIFIER_ABSNEG
;
1130 /* Emit the operand tokens */
1131 emit_dword(emit
, operand0
.value
);
1132 if (operand0
.extended
)
1133 emit_dword(emit
, operand1
.value
);
1135 if (operand0
.operandType
== VGPU10_OPERAND_TYPE_IMMEDIATE32
) {
1136 /* Emit the four float/int in-line immediate values */
1138 assert(index
< ARRAY_SIZE(emit
->immediates
));
1139 assert(file
== TGSI_FILE_IMMEDIATE
);
1140 assert(swizzleX
< 4);
1141 assert(swizzleY
< 4);
1142 assert(swizzleZ
< 4);
1143 assert(swizzleW
< 4);
1144 c
= (unsigned *) emit
->immediates
[index
];
1145 emit_dword(emit
, c
[swizzleX
]);
1146 emit_dword(emit
, c
[swizzleY
]);
1147 emit_dword(emit
, c
[swizzleZ
]);
1148 emit_dword(emit
, c
[swizzleW
]);
1150 else if (operand0
.indexDimension
>= VGPU10_OPERAND_INDEX_1D
) {
1151 /* Emit the register index(es) */
1153 operand0
.operandType
== VGPU10_OPERAND_TYPE_CONSTANT_BUFFER
) {
1154 emit_dword(emit
, reg
->Dimension
.Index
);
1157 if (tempArrayId
> 0) {
1158 emit_dword(emit
, tempArrayId
);
1161 emit_dword(emit
, remap_temp_index(emit
, file
, index
));
1164 emit_indirect_register(emit
, reg
->Indirect
.Index
);
1171 * Emit a resource operand (for use with a SAMPLE instruction).
1174 emit_resource_register(struct svga_shader_emitter_v10
*emit
,
1175 unsigned resource_number
)
1177 VGPU10OperandToken0 operand0
;
1179 check_register_index(emit
, VGPU10_OPERAND_TYPE_RESOURCE
, resource_number
);
1184 operand0
.operandType
= VGPU10_OPERAND_TYPE_RESOURCE
;
1185 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_1D
;
1186 operand0
.numComponents
= VGPU10_OPERAND_4_COMPONENT
;
1187 operand0
.selectionMode
= VGPU10_OPERAND_4_COMPONENT_SWIZZLE_MODE
;
1188 operand0
.swizzleX
= VGPU10_COMPONENT_X
;
1189 operand0
.swizzleY
= VGPU10_COMPONENT_Y
;
1190 operand0
.swizzleZ
= VGPU10_COMPONENT_Z
;
1191 operand0
.swizzleW
= VGPU10_COMPONENT_W
;
1193 emit_dword(emit
, operand0
.value
);
1194 emit_dword(emit
, resource_number
);
1199 * Emit a sampler operand (for use with a SAMPLE instruction).
1202 emit_sampler_register(struct svga_shader_emitter_v10
*emit
,
1203 unsigned sampler_number
)
1205 VGPU10OperandToken0 operand0
;
1207 check_register_index(emit
, VGPU10_OPERAND_TYPE_SAMPLER
, sampler_number
);
1212 operand0
.operandType
= VGPU10_OPERAND_TYPE_SAMPLER
;
1213 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_1D
;
1215 emit_dword(emit
, operand0
.value
);
1216 emit_dword(emit
, sampler_number
);
1221 * Emit an operand which reads the IS_FRONT_FACING register.
1224 emit_face_register(struct svga_shader_emitter_v10
*emit
)
1226 VGPU10OperandToken0 operand0
;
1227 unsigned index
= emit
->linkage
.input_map
[emit
->fs
.face_input_index
];
1232 operand0
.operandType
= VGPU10_OPERAND_TYPE_INPUT
;
1233 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_1D
;
1234 operand0
.selectionMode
= VGPU10_OPERAND_4_COMPONENT_SELECT_1_MODE
;
1235 operand0
.numComponents
= VGPU10_OPERAND_4_COMPONENT
;
1237 operand0
.swizzleX
= VGPU10_COMPONENT_X
;
1238 operand0
.swizzleY
= VGPU10_COMPONENT_X
;
1239 operand0
.swizzleZ
= VGPU10_COMPONENT_X
;
1240 operand0
.swizzleW
= VGPU10_COMPONENT_X
;
1242 emit_dword(emit
, operand0
.value
);
1243 emit_dword(emit
, index
);
1248 * Emit tokens for the "rasterizer" register used by the SAMPLE_POS
1252 emit_rasterizer_register(struct svga_shader_emitter_v10
*emit
)
1254 VGPU10OperandToken0 operand0
;
1259 /* No register index for rasterizer index (there's only one) */
1260 operand0
.operandType
= VGPU10_OPERAND_TYPE_RASTERIZER
;
1261 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_0D
;
1262 operand0
.numComponents
= VGPU10_OPERAND_4_COMPONENT
;
1263 operand0
.selectionMode
= VGPU10_OPERAND_4_COMPONENT_SWIZZLE_MODE
;
1264 operand0
.swizzleX
= VGPU10_COMPONENT_X
;
1265 operand0
.swizzleY
= VGPU10_COMPONENT_Y
;
1266 operand0
.swizzleZ
= VGPU10_COMPONENT_Z
;
1267 operand0
.swizzleW
= VGPU10_COMPONENT_W
;
1269 emit_dword(emit
, operand0
.value
);
1274 * Emit the token for a VGPU10 opcode.
1275 * \param saturate clamp result to [0,1]?
1278 emit_opcode(struct svga_shader_emitter_v10
*emit
,
1279 VGPU10_OPCODE_TYPE vgpu10_opcode
, boolean saturate
)
1281 VGPU10OpcodeToken0 token0
;
1283 token0
.value
= 0; /* init all fields to zero */
1284 token0
.opcodeType
= vgpu10_opcode
;
1285 token0
.instructionLength
= 0; /* Filled in by end_emit_instruction() */
1286 token0
.saturate
= saturate
;
1288 emit_dword(emit
, token0
.value
);
1293 * Emit the token for a VGPU10 resinfo instruction.
1294 * \param modifier return type modifier, _uint or _rcpFloat.
1295 * TODO: We may want to remove this parameter if it will
1296 * only ever be used as _uint.
1299 emit_opcode_resinfo(struct svga_shader_emitter_v10
*emit
,
1300 VGPU10_RESINFO_RETURN_TYPE modifier
)
1302 VGPU10OpcodeToken0 token0
;
1304 token0
.value
= 0; /* init all fields to zero */
1305 token0
.opcodeType
= VGPU10_OPCODE_RESINFO
;
1306 token0
.instructionLength
= 0; /* Filled in by end_emit_instruction() */
1307 token0
.resinfoReturnType
= modifier
;
1309 emit_dword(emit
, token0
.value
);
1314 * Emit opcode tokens for a texture sample instruction. Texture instructions
1315 * can be rather complicated (texel offsets, etc) so we have this specialized
1319 emit_sample_opcode(struct svga_shader_emitter_v10
*emit
,
1320 unsigned vgpu10_opcode
, boolean saturate
,
1321 const int offsets
[3])
1323 VGPU10OpcodeToken0 token0
;
1324 VGPU10OpcodeToken1 token1
;
1326 token0
.value
= 0; /* init all fields to zero */
1327 token0
.opcodeType
= vgpu10_opcode
;
1328 token0
.instructionLength
= 0; /* Filled in by end_emit_instruction() */
1329 token0
.saturate
= saturate
;
1331 if (offsets
[0] || offsets
[1] || offsets
[2]) {
1332 assert(offsets
[0] >= VGPU10_MIN_TEXEL_FETCH_OFFSET
);
1333 assert(offsets
[1] >= VGPU10_MIN_TEXEL_FETCH_OFFSET
);
1334 assert(offsets
[2] >= VGPU10_MIN_TEXEL_FETCH_OFFSET
);
1335 assert(offsets
[0] <= VGPU10_MAX_TEXEL_FETCH_OFFSET
);
1336 assert(offsets
[1] <= VGPU10_MAX_TEXEL_FETCH_OFFSET
);
1337 assert(offsets
[2] <= VGPU10_MAX_TEXEL_FETCH_OFFSET
);
1339 token0
.extended
= 1;
1341 token1
.opcodeType
= VGPU10_EXTENDED_OPCODE_SAMPLE_CONTROLS
;
1342 token1
.offsetU
= offsets
[0];
1343 token1
.offsetV
= offsets
[1];
1344 token1
.offsetW
= offsets
[2];
1347 emit_dword(emit
, token0
.value
);
1348 if (token0
.extended
) {
1349 emit_dword(emit
, token1
.value
);
1355 * Emit a DISCARD opcode token.
1356 * If nonzero is set, we'll discard the fragment if the X component is not 0.
1357 * Otherwise, we'll discard the fragment if the X component is 0.
1360 emit_discard_opcode(struct svga_shader_emitter_v10
*emit
, boolean nonzero
)
1362 VGPU10OpcodeToken0 opcode0
;
1365 opcode0
.opcodeType
= VGPU10_OPCODE_DISCARD
;
1367 opcode0
.testBoolean
= VGPU10_INSTRUCTION_TEST_NONZERO
;
1369 emit_dword(emit
, opcode0
.value
);
1374 * We need to call this before we begin emitting a VGPU10 instruction.
1377 begin_emit_instruction(struct svga_shader_emitter_v10
*emit
)
1379 assert(emit
->inst_start_token
== 0);
1380 /* Save location of the instruction's VGPU10OpcodeToken0 token.
1381 * Note, we can't save a pointer because it would become invalid if
1382 * we have to realloc the output buffer.
1384 emit
->inst_start_token
= emit_get_num_tokens(emit
);
1389 * We need to call this after we emit the last token of a VGPU10 instruction.
1390 * This function patches in the opcode token's instructionLength field.
1393 end_emit_instruction(struct svga_shader_emitter_v10
*emit
)
1395 VGPU10OpcodeToken0
*tokens
= (VGPU10OpcodeToken0
*) emit
->buf
;
1396 unsigned inst_length
;
1398 assert(emit
->inst_start_token
> 0);
1400 if (emit
->discard_instruction
) {
1401 /* Back up the emit->ptr to where this instruction started so
1402 * that we discard the current instruction.
1404 emit
->ptr
= (char *) (tokens
+ emit
->inst_start_token
);
1407 /* Compute instruction length and patch that into the start of
1410 inst_length
= emit_get_num_tokens(emit
) - emit
->inst_start_token
;
1412 assert(inst_length
> 0);
1414 tokens
[emit
->inst_start_token
].instructionLength
= inst_length
;
1417 emit
->inst_start_token
= 0; /* reset to zero for error checking */
1418 emit
->discard_instruction
= FALSE
;
1423 * Return index for a free temporary register.
1426 get_temp_index(struct svga_shader_emitter_v10
*emit
)
1428 assert(emit
->internal_temp_count
< MAX_INTERNAL_TEMPS
);
1429 return emit
->num_shader_temps
+ emit
->internal_temp_count
++;
1434 * Release the temporaries which were generated by get_temp_index().
1437 free_temp_indexes(struct svga_shader_emitter_v10
*emit
)
1439 emit
->internal_temp_count
= 0;
1444 * Create a tgsi_full_src_register.
1446 static struct tgsi_full_src_register
1447 make_src_reg(enum tgsi_file_type file
, unsigned index
)
1449 struct tgsi_full_src_register reg
;
1451 memset(®
, 0, sizeof(reg
));
1452 reg
.Register
.File
= file
;
1453 reg
.Register
.Index
= index
;
1454 reg
.Register
.SwizzleX
= TGSI_SWIZZLE_X
;
1455 reg
.Register
.SwizzleY
= TGSI_SWIZZLE_Y
;
1456 reg
.Register
.SwizzleZ
= TGSI_SWIZZLE_Z
;
1457 reg
.Register
.SwizzleW
= TGSI_SWIZZLE_W
;
1463 * Create a tgsi_full_src_register with a swizzle such that all four
1464 * vector components have the same scalar value.
1466 static struct tgsi_full_src_register
1467 make_src_scalar_reg(enum tgsi_file_type file
, unsigned index
, unsigned component
)
1469 struct tgsi_full_src_register reg
;
1471 assert(component
>= TGSI_SWIZZLE_X
);
1472 assert(component
<= TGSI_SWIZZLE_W
);
1474 memset(®
, 0, sizeof(reg
));
1475 reg
.Register
.File
= file
;
1476 reg
.Register
.Index
= index
;
1477 reg
.Register
.SwizzleX
=
1478 reg
.Register
.SwizzleY
=
1479 reg
.Register
.SwizzleZ
=
1480 reg
.Register
.SwizzleW
= component
;
1486 * Create a tgsi_full_src_register for a temporary.
1488 static struct tgsi_full_src_register
1489 make_src_temp_reg(unsigned index
)
1491 return make_src_reg(TGSI_FILE_TEMPORARY
, index
);
1496 * Create a tgsi_full_src_register for a constant.
1498 static struct tgsi_full_src_register
1499 make_src_const_reg(unsigned index
)
1501 return make_src_reg(TGSI_FILE_CONSTANT
, index
);
1506 * Create a tgsi_full_src_register for an immediate constant.
1508 static struct tgsi_full_src_register
1509 make_src_immediate_reg(unsigned index
)
1511 return make_src_reg(TGSI_FILE_IMMEDIATE
, index
);
1516 * Create a tgsi_full_dst_register.
1518 static struct tgsi_full_dst_register
1519 make_dst_reg(enum tgsi_file_type file
, unsigned index
)
1521 struct tgsi_full_dst_register reg
;
1523 memset(®
, 0, sizeof(reg
));
1524 reg
.Register
.File
= file
;
1525 reg
.Register
.Index
= index
;
1526 reg
.Register
.WriteMask
= TGSI_WRITEMASK_XYZW
;
1532 * Create a tgsi_full_dst_register for a temporary.
1534 static struct tgsi_full_dst_register
1535 make_dst_temp_reg(unsigned index
)
1537 return make_dst_reg(TGSI_FILE_TEMPORARY
, index
);
1542 * Create a tgsi_full_dst_register for an output.
1544 static struct tgsi_full_dst_register
1545 make_dst_output_reg(unsigned index
)
1547 return make_dst_reg(TGSI_FILE_OUTPUT
, index
);
1552 * Create negated tgsi_full_src_register.
1554 static struct tgsi_full_src_register
1555 negate_src(const struct tgsi_full_src_register
*reg
)
1557 struct tgsi_full_src_register neg
= *reg
;
1558 neg
.Register
.Negate
= !reg
->Register
.Negate
;
1563 * Create absolute value of a tgsi_full_src_register.
1565 static struct tgsi_full_src_register
1566 absolute_src(const struct tgsi_full_src_register
*reg
)
1568 struct tgsi_full_src_register absolute
= *reg
;
1569 absolute
.Register
.Absolute
= 1;
1574 /** Return the named swizzle term from the src register */
1575 static inline unsigned
1576 get_swizzle(const struct tgsi_full_src_register
*reg
, enum tgsi_swizzle term
)
1579 case TGSI_SWIZZLE_X
:
1580 return reg
->Register
.SwizzleX
;
1581 case TGSI_SWIZZLE_Y
:
1582 return reg
->Register
.SwizzleY
;
1583 case TGSI_SWIZZLE_Z
:
1584 return reg
->Register
.SwizzleZ
;
1585 case TGSI_SWIZZLE_W
:
1586 return reg
->Register
.SwizzleW
;
1588 assert(!"Bad swizzle");
1589 return TGSI_SWIZZLE_X
;
1595 * Create swizzled tgsi_full_src_register.
1597 static struct tgsi_full_src_register
1598 swizzle_src(const struct tgsi_full_src_register
*reg
,
1599 enum tgsi_swizzle swizzleX
, enum tgsi_swizzle swizzleY
,
1600 enum tgsi_swizzle swizzleZ
, enum tgsi_swizzle swizzleW
)
1602 struct tgsi_full_src_register swizzled
= *reg
;
1603 /* Note: we swizzle the current swizzle */
1604 swizzled
.Register
.SwizzleX
= get_swizzle(reg
, swizzleX
);
1605 swizzled
.Register
.SwizzleY
= get_swizzle(reg
, swizzleY
);
1606 swizzled
.Register
.SwizzleZ
= get_swizzle(reg
, swizzleZ
);
1607 swizzled
.Register
.SwizzleW
= get_swizzle(reg
, swizzleW
);
1613 * Create swizzled tgsi_full_src_register where all the swizzle
1614 * terms are the same.
1616 static struct tgsi_full_src_register
1617 scalar_src(const struct tgsi_full_src_register
*reg
, enum tgsi_swizzle swizzle
)
1619 struct tgsi_full_src_register swizzled
= *reg
;
1620 /* Note: we swizzle the current swizzle */
1621 swizzled
.Register
.SwizzleX
=
1622 swizzled
.Register
.SwizzleY
=
1623 swizzled
.Register
.SwizzleZ
=
1624 swizzled
.Register
.SwizzleW
= get_swizzle(reg
, swizzle
);
1630 * Create new tgsi_full_dst_register with writemask.
1631 * \param mask bitmask of TGSI_WRITEMASK_[XYZW]
1633 static struct tgsi_full_dst_register
1634 writemask_dst(const struct tgsi_full_dst_register
*reg
, unsigned mask
)
1636 struct tgsi_full_dst_register masked
= *reg
;
1637 masked
.Register
.WriteMask
= mask
;
1643 * Check if the register's swizzle is XXXX, YYYY, ZZZZ, or WWWW.
1646 same_swizzle_terms(const struct tgsi_full_src_register
*reg
)
1648 return (reg
->Register
.SwizzleX
== reg
->Register
.SwizzleY
&&
1649 reg
->Register
.SwizzleY
== reg
->Register
.SwizzleZ
&&
1650 reg
->Register
.SwizzleZ
== reg
->Register
.SwizzleW
);
1655 * Search the vector for the value 'x' and return its position.
1658 find_imm_in_vec4(const union tgsi_immediate_data vec
[4],
1659 union tgsi_immediate_data x
)
1662 for (i
= 0; i
< 4; i
++) {
1663 if (vec
[i
].Int
== x
.Int
)
1671 * Helper used by make_immediate_reg(), make_immediate_reg_4().
1674 find_immediate(struct svga_shader_emitter_v10
*emit
,
1675 union tgsi_immediate_data x
, unsigned startIndex
)
1677 const unsigned endIndex
= emit
->num_immediates
;
1680 assert(emit
->immediates_emitted
);
1682 /* Search immediates for x, y, z, w */
1683 for (i
= startIndex
; i
< endIndex
; i
++) {
1684 if (x
.Int
== emit
->immediates
[i
][0].Int
||
1685 x
.Int
== emit
->immediates
[i
][1].Int
||
1686 x
.Int
== emit
->immediates
[i
][2].Int
||
1687 x
.Int
== emit
->immediates
[i
][3].Int
) {
1691 /* Should never try to use an immediate value that wasn't pre-declared */
1692 assert(!"find_immediate() failed!");
1698 * Return a tgsi_full_src_register for an immediate/literal
1699 * union tgsi_immediate_data[4] value.
1700 * Note: the values must have been previously declared/allocated in
1701 * emit_pre_helpers(). And, all of x,y,z,w must be located in the same
1704 static struct tgsi_full_src_register
1705 make_immediate_reg_4(struct svga_shader_emitter_v10
*emit
,
1706 const union tgsi_immediate_data imm
[4])
1708 struct tgsi_full_src_register reg
;
1711 for (i
= 0; i
< emit
->num_common_immediates
; i
++) {
1712 /* search for first component value */
1713 int immpos
= find_immediate(emit
, imm
[0], i
);
1716 assert(immpos
>= 0);
1718 /* find remaining components within the immediate vector */
1719 x
= find_imm_in_vec4(emit
->immediates
[immpos
], imm
[0]);
1720 y
= find_imm_in_vec4(emit
->immediates
[immpos
], imm
[1]);
1721 z
= find_imm_in_vec4(emit
->immediates
[immpos
], imm
[2]);
1722 w
= find_imm_in_vec4(emit
->immediates
[immpos
], imm
[3]);
1724 if (x
>=0 && y
>= 0 && z
>= 0 && w
>= 0) {
1725 /* found them all */
1726 memset(®
, 0, sizeof(reg
));
1727 reg
.Register
.File
= TGSI_FILE_IMMEDIATE
;
1728 reg
.Register
.Index
= immpos
;
1729 reg
.Register
.SwizzleX
= x
;
1730 reg
.Register
.SwizzleY
= y
;
1731 reg
.Register
.SwizzleZ
= z
;
1732 reg
.Register
.SwizzleW
= w
;
1735 /* else, keep searching */
1738 assert(!"Failed to find immediate register!");
1740 /* Just return IMM[0].xxxx */
1741 memset(®
, 0, sizeof(reg
));
1742 reg
.Register
.File
= TGSI_FILE_IMMEDIATE
;
1748 * Return a tgsi_full_src_register for an immediate/literal
1749 * union tgsi_immediate_data value of the form {value, value, value, value}.
1750 * \sa make_immediate_reg_4() regarding allowed values.
1752 static struct tgsi_full_src_register
1753 make_immediate_reg(struct svga_shader_emitter_v10
*emit
,
1754 union tgsi_immediate_data value
)
1756 struct tgsi_full_src_register reg
;
1757 int immpos
= find_immediate(emit
, value
, 0);
1759 assert(immpos
>= 0);
1761 memset(®
, 0, sizeof(reg
));
1762 reg
.Register
.File
= TGSI_FILE_IMMEDIATE
;
1763 reg
.Register
.Index
= immpos
;
1764 reg
.Register
.SwizzleX
=
1765 reg
.Register
.SwizzleY
=
1766 reg
.Register
.SwizzleZ
=
1767 reg
.Register
.SwizzleW
= find_imm_in_vec4(emit
->immediates
[immpos
], value
);
1774 * Return a tgsi_full_src_register for an immediate/literal float[4] value.
1775 * \sa make_immediate_reg_4() regarding allowed values.
1777 static struct tgsi_full_src_register
1778 make_immediate_reg_float4(struct svga_shader_emitter_v10
*emit
,
1779 float x
, float y
, float z
, float w
)
1781 union tgsi_immediate_data imm
[4];
1786 return make_immediate_reg_4(emit
, imm
);
1791 * Return a tgsi_full_src_register for an immediate/literal float value
1792 * of the form {value, value, value, value}.
1793 * \sa make_immediate_reg_4() regarding allowed values.
1795 static struct tgsi_full_src_register
1796 make_immediate_reg_float(struct svga_shader_emitter_v10
*emit
, float value
)
1798 union tgsi_immediate_data imm
;
1800 return make_immediate_reg(emit
, imm
);
1805 * Return a tgsi_full_src_register for an immediate/literal int[4] vector.
1807 static struct tgsi_full_src_register
1808 make_immediate_reg_int4(struct svga_shader_emitter_v10
*emit
,
1809 int x
, int y
, int z
, int w
)
1811 union tgsi_immediate_data imm
[4];
1816 return make_immediate_reg_4(emit
, imm
);
1821 * Return a tgsi_full_src_register for an immediate/literal int value
1822 * of the form {value, value, value, value}.
1823 * \sa make_immediate_reg_4() regarding allowed values.
1825 static struct tgsi_full_src_register
1826 make_immediate_reg_int(struct svga_shader_emitter_v10
*emit
, int value
)
1828 union tgsi_immediate_data imm
;
1830 return make_immediate_reg(emit
, imm
);
1835 * Allocate space for a union tgsi_immediate_data[4] immediate.
1836 * \return the index/position of the immediate.
1839 alloc_immediate_4(struct svga_shader_emitter_v10
*emit
,
1840 const union tgsi_immediate_data imm
[4])
1842 unsigned n
= emit
->num_immediates
++;
1843 assert(!emit
->immediates_emitted
);
1844 assert(n
< ARRAY_SIZE(emit
->immediates
));
1845 emit
->immediates
[n
][0] = imm
[0];
1846 emit
->immediates
[n
][1] = imm
[1];
1847 emit
->immediates
[n
][2] = imm
[2];
1848 emit
->immediates
[n
][3] = imm
[3];
1854 * Allocate space for a float[4] immediate.
1855 * \return the index/position of the immediate.
1858 alloc_immediate_float4(struct svga_shader_emitter_v10
*emit
,
1859 float x
, float y
, float z
, float w
)
1861 union tgsi_immediate_data imm
[4];
1866 return alloc_immediate_4(emit
, imm
);
1871 * Allocate space for an int[4] immediate.
1872 * \return the index/position of the immediate.
1875 alloc_immediate_int4(struct svga_shader_emitter_v10
*emit
,
1876 int x
, int y
, int z
, int w
)
1878 union tgsi_immediate_data imm
[4];
1883 return alloc_immediate_4(emit
, imm
);
1888 * Allocate a shader input to store a system value.
1891 alloc_system_value_index(struct svga_shader_emitter_v10
*emit
, unsigned index
)
1893 const unsigned n
= emit
->linkage
.input_map_max
+ 1 + index
;
1894 assert(index
< ARRAY_SIZE(emit
->system_value_indexes
));
1895 emit
->system_value_indexes
[index
] = n
;
1901 * Translate a TGSI immediate value (union tgsi_immediate_data[4]) to VGPU10.
1904 emit_vgpu10_immediate(struct svga_shader_emitter_v10
*emit
,
1905 const struct tgsi_full_immediate
*imm
)
1907 /* We don't actually emit any code here. We just save the
1908 * immediate values and emit them later.
1910 alloc_immediate_4(emit
, imm
->u
);
1916 * Emit a VGPU10_CUSTOMDATA_DCL_IMMEDIATE_CONSTANT_BUFFER block
1917 * containing all the immediate values previously allocated
1918 * with alloc_immediate_4().
1921 emit_vgpu10_immediates_block(struct svga_shader_emitter_v10
*emit
)
1923 VGPU10OpcodeToken0 token
;
1925 assert(!emit
->immediates_emitted
);
1928 token
.opcodeType
= VGPU10_OPCODE_CUSTOMDATA
;
1929 token
.customDataClass
= VGPU10_CUSTOMDATA_DCL_IMMEDIATE_CONSTANT_BUFFER
;
1931 /* Note: no begin/end_emit_instruction() calls */
1932 emit_dword(emit
, token
.value
);
1933 emit_dword(emit
, 2 + 4 * emit
->num_immediates
);
1934 emit_dwords(emit
, (unsigned *) emit
->immediates
, 4 * emit
->num_immediates
);
1936 emit
->immediates_emitted
= TRUE
;
1943 * Translate a fragment shader's TGSI_INTERPOLATE_x mode to a vgpu10
1944 * interpolation mode.
1945 * \return a VGPU10_INTERPOLATION_x value
1948 translate_interpolation(const struct svga_shader_emitter_v10
*emit
,
1949 enum tgsi_interpolate_mode interp
,
1950 enum tgsi_interpolate_loc interpolate_loc
)
1952 if (interp
== TGSI_INTERPOLATE_COLOR
) {
1953 interp
= emit
->key
.fs
.flatshade
?
1954 TGSI_INTERPOLATE_CONSTANT
: TGSI_INTERPOLATE_PERSPECTIVE
;
1958 case TGSI_INTERPOLATE_CONSTANT
:
1959 return VGPU10_INTERPOLATION_CONSTANT
;
1960 case TGSI_INTERPOLATE_LINEAR
:
1961 if (interpolate_loc
== TGSI_INTERPOLATE_LOC_CENTROID
) {
1962 return VGPU10_INTERPOLATION_LINEAR_NOPERSPECTIVE_CENTROID
;
1963 } else if (interpolate_loc
== TGSI_INTERPOLATE_LOC_SAMPLE
&&
1964 emit
->version
>= 41) {
1965 return VGPU10_INTERPOLATION_LINEAR_NOPERSPECTIVE_SAMPLE
;
1967 return VGPU10_INTERPOLATION_LINEAR_NOPERSPECTIVE
;
1970 case TGSI_INTERPOLATE_PERSPECTIVE
:
1971 if (interpolate_loc
== TGSI_INTERPOLATE_LOC_CENTROID
) {
1972 return VGPU10_INTERPOLATION_LINEAR_CENTROID
;
1973 } else if (interpolate_loc
== TGSI_INTERPOLATE_LOC_SAMPLE
&&
1974 emit
->version
>= 41) {
1975 return VGPU10_INTERPOLATION_LINEAR_SAMPLE
;
1977 return VGPU10_INTERPOLATION_LINEAR
;
1981 assert(!"Unexpected interpolation mode");
1982 return VGPU10_INTERPOLATION_CONSTANT
;
1988 * Translate a TGSI property to VGPU10.
1989 * Don't emit any instructions yet, only need to gather the primitive property
1990 * information. The output primitive topology might be changed later. The
1991 * final property instructions will be emitted as part of the pre-helper code.
1994 emit_vgpu10_property(struct svga_shader_emitter_v10
*emit
,
1995 const struct tgsi_full_property
*prop
)
1997 static const VGPU10_PRIMITIVE primType
[] = {
1998 VGPU10_PRIMITIVE_POINT
, /* PIPE_PRIM_POINTS */
1999 VGPU10_PRIMITIVE_LINE
, /* PIPE_PRIM_LINES */
2000 VGPU10_PRIMITIVE_LINE
, /* PIPE_PRIM_LINE_LOOP */
2001 VGPU10_PRIMITIVE_LINE
, /* PIPE_PRIM_LINE_STRIP */
2002 VGPU10_PRIMITIVE_TRIANGLE
, /* PIPE_PRIM_TRIANGLES */
2003 VGPU10_PRIMITIVE_TRIANGLE
, /* PIPE_PRIM_TRIANGLE_STRIP */
2004 VGPU10_PRIMITIVE_TRIANGLE
, /* PIPE_PRIM_TRIANGLE_FAN */
2005 VGPU10_PRIMITIVE_UNDEFINED
, /* PIPE_PRIM_QUADS */
2006 VGPU10_PRIMITIVE_UNDEFINED
, /* PIPE_PRIM_QUAD_STRIP */
2007 VGPU10_PRIMITIVE_UNDEFINED
, /* PIPE_PRIM_POLYGON */
2008 VGPU10_PRIMITIVE_LINE_ADJ
, /* PIPE_PRIM_LINES_ADJACENCY */
2009 VGPU10_PRIMITIVE_LINE_ADJ
, /* PIPE_PRIM_LINE_STRIP_ADJACENCY */
2010 VGPU10_PRIMITIVE_TRIANGLE_ADJ
, /* PIPE_PRIM_TRIANGLES_ADJACENCY */
2011 VGPU10_PRIMITIVE_TRIANGLE_ADJ
/* PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY */
2014 static const VGPU10_PRIMITIVE_TOPOLOGY primTopology
[] = {
2015 VGPU10_PRIMITIVE_TOPOLOGY_POINTLIST
, /* PIPE_PRIM_POINTS */
2016 VGPU10_PRIMITIVE_TOPOLOGY_LINELIST
, /* PIPE_PRIM_LINES */
2017 VGPU10_PRIMITIVE_TOPOLOGY_LINELIST
, /* PIPE_PRIM_LINE_LOOP */
2018 VGPU10_PRIMITIVE_TOPOLOGY_LINESTRIP
, /* PIPE_PRIM_LINE_STRIP */
2019 VGPU10_PRIMITIVE_TOPOLOGY_TRIANGLELIST
, /* PIPE_PRIM_TRIANGLES */
2020 VGPU10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP
, /* PIPE_PRIM_TRIANGLE_STRIP */
2021 VGPU10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP
, /* PIPE_PRIM_TRIANGLE_FAN */
2022 VGPU10_PRIMITIVE_TOPOLOGY_UNDEFINED
, /* PIPE_PRIM_QUADS */
2023 VGPU10_PRIMITIVE_TOPOLOGY_UNDEFINED
, /* PIPE_PRIM_QUAD_STRIP */
2024 VGPU10_PRIMITIVE_TOPOLOGY_UNDEFINED
, /* PIPE_PRIM_POLYGON */
2025 VGPU10_PRIMITIVE_TOPOLOGY_LINELIST_ADJ
, /* PIPE_PRIM_LINES_ADJACENCY */
2026 VGPU10_PRIMITIVE_TOPOLOGY_LINELIST_ADJ
, /* PIPE_PRIM_LINE_STRIP_ADJACENCY */
2027 VGPU10_PRIMITIVE_TOPOLOGY_TRIANGLELIST_ADJ
, /* PIPE_PRIM_TRIANGLES_ADJACENCY */
2028 VGPU10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP_ADJ
/* PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY */
2031 static const unsigned inputArraySize
[] = {
2032 0, /* VGPU10_PRIMITIVE_UNDEFINED */
2033 1, /* VGPU10_PRIMITIVE_POINT */
2034 2, /* VGPU10_PRIMITIVE_LINE */
2035 3, /* VGPU10_PRIMITIVE_TRIANGLE */
2038 4, /* VGPU10_PRIMITIVE_LINE_ADJ */
2039 6 /* VGPU10_PRIMITIVE_TRIANGLE_ADJ */
2042 switch (prop
->Property
.PropertyName
) {
2043 case TGSI_PROPERTY_GS_INPUT_PRIM
:
2044 assert(prop
->u
[0].Data
< ARRAY_SIZE(primType
));
2045 emit
->gs
.prim_type
= primType
[prop
->u
[0].Data
];
2046 assert(emit
->gs
.prim_type
!= VGPU10_PRIMITIVE_UNDEFINED
);
2047 emit
->gs
.input_size
= inputArraySize
[emit
->gs
.prim_type
];
2050 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
2051 assert(prop
->u
[0].Data
< ARRAY_SIZE(primTopology
));
2052 emit
->gs
.prim_topology
= primTopology
[prop
->u
[0].Data
];
2053 assert(emit
->gs
.prim_topology
!= VGPU10_PRIMITIVE_TOPOLOGY_UNDEFINED
);
2056 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
:
2057 emit
->gs
.max_out_vertices
= prop
->u
[0].Data
;
2069 emit_property_instruction(struct svga_shader_emitter_v10
*emit
,
2070 VGPU10OpcodeToken0 opcode0
, unsigned nData
,
2073 begin_emit_instruction(emit
);
2074 emit_dword(emit
, opcode0
.value
);
2076 emit_dword(emit
, data
);
2077 end_emit_instruction(emit
);
2082 * Emit property instructions
2085 emit_property_instructions(struct svga_shader_emitter_v10
*emit
)
2087 VGPU10OpcodeToken0 opcode0
;
2089 assert(emit
->unit
== PIPE_SHADER_GEOMETRY
);
2091 /* emit input primitive type declaration */
2093 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_GS_INPUT_PRIMITIVE
;
2094 opcode0
.primitive
= emit
->gs
.prim_type
;
2095 emit_property_instruction(emit
, opcode0
, 0, 0);
2097 /* emit output primitive topology declaration */
2099 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_GS_OUTPUT_PRIMITIVE_TOPOLOGY
;
2100 opcode0
.primitiveTopology
= emit
->gs
.prim_topology
;
2101 emit_property_instruction(emit
, opcode0
, 0, 0);
2103 /* emit max output vertices */
2105 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_MAX_OUTPUT_VERTEX_COUNT
;
2106 emit_property_instruction(emit
, opcode0
, 1, emit
->gs
.max_out_vertices
);
2111 * Emit a vgpu10 declaration "instruction".
2112 * \param index the register index
2113 * \param size array size of the operand. In most cases, it is 1,
2114 * but for inputs to geometry shader, the array size varies
2115 * depending on the primitive type.
2118 emit_decl_instruction(struct svga_shader_emitter_v10
*emit
,
2119 VGPU10OpcodeToken0 opcode0
,
2120 VGPU10OperandToken0 operand0
,
2121 VGPU10NameToken name_token
,
2122 unsigned index
, unsigned size
)
2124 assert(opcode0
.opcodeType
);
2125 assert(operand0
.mask
||
2126 (operand0
.operandType
== VGPU10_OPERAND_TYPE_OUTPUT_DEPTH
) ||
2127 (operand0
.operandType
== VGPU10_OPERAND_TYPE_OUTPUT_COVERAGE_MASK
));
2129 begin_emit_instruction(emit
);
2130 emit_dword(emit
, opcode0
.value
);
2132 emit_dword(emit
, operand0
.value
);
2134 if (operand0
.indexDimension
== VGPU10_OPERAND_INDEX_1D
) {
2135 /* Next token is the index of the register to declare */
2136 emit_dword(emit
, index
);
2138 else if (operand0
.indexDimension
>= VGPU10_OPERAND_INDEX_2D
) {
2139 /* Next token is the size of the register */
2140 emit_dword(emit
, size
);
2142 /* Followed by the index of the register */
2143 emit_dword(emit
, index
);
2146 if (name_token
.value
) {
2147 emit_dword(emit
, name_token
.value
);
2150 end_emit_instruction(emit
);
2155 * Emit the declaration for a shader input.
2156 * \param opcodeType opcode type, one of VGPU10_OPCODE_DCL_INPUTx
2157 * \param operandType operand type, one of VGPU10_OPERAND_TYPE_INPUT_x
2158 * \param dim index dimension
2159 * \param index the input register index
2160 * \param size array size of the operand. In most cases, it is 1,
2161 * but for inputs to geometry shader, the array size varies
2162 * depending on the primitive type.
2163 * \param name one of VGPU10_NAME_x
2164 * \parma numComp number of components
2165 * \param selMode component selection mode
2166 * \param usageMask bitfield of VGPU10_OPERAND_4_COMPONENT_MASK_x values
2167 * \param interpMode interpolation mode
2170 emit_input_declaration(struct svga_shader_emitter_v10
*emit
,
2171 VGPU10_OPCODE_TYPE opcodeType
,
2172 VGPU10_OPERAND_TYPE operandType
,
2173 VGPU10_OPERAND_INDEX_DIMENSION dim
,
2174 unsigned index
, unsigned size
,
2175 VGPU10_SYSTEM_NAME name
,
2176 VGPU10_OPERAND_NUM_COMPONENTS numComp
,
2177 VGPU10_OPERAND_4_COMPONENT_SELECTION_MODE selMode
,
2179 VGPU10_INTERPOLATION_MODE interpMode
)
2181 VGPU10OpcodeToken0 opcode0
;
2182 VGPU10OperandToken0 operand0
;
2183 VGPU10NameToken name_token
;
2185 assert(usageMask
<= VGPU10_OPERAND_4_COMPONENT_MASK_ALL
);
2186 assert(opcodeType
== VGPU10_OPCODE_DCL_INPUT
||
2187 opcodeType
== VGPU10_OPCODE_DCL_INPUT_SIV
||
2188 opcodeType
== VGPU10_OPCODE_DCL_INPUT_PS
||
2189 opcodeType
== VGPU10_OPCODE_DCL_INPUT_PS_SIV
||
2190 opcodeType
== VGPU10_OPCODE_DCL_INPUT_PS_SGV
);
2191 assert(operandType
== VGPU10_OPERAND_TYPE_INPUT
||
2192 operandType
== VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID
);
2193 assert(numComp
<= VGPU10_OPERAND_4_COMPONENT
);
2194 assert(selMode
<= VGPU10_OPERAND_4_COMPONENT_MASK_MODE
);
2195 assert(dim
<= VGPU10_OPERAND_INDEX_3D
);
2196 assert(name
== VGPU10_NAME_UNDEFINED
||
2197 name
== VGPU10_NAME_POSITION
||
2198 name
== VGPU10_NAME_INSTANCE_ID
||
2199 name
== VGPU10_NAME_VERTEX_ID
||
2200 name
== VGPU10_NAME_PRIMITIVE_ID
||
2201 name
== VGPU10_NAME_IS_FRONT_FACE
||
2202 name
== VGPU10_NAME_SAMPLE_INDEX
);
2204 assert(interpMode
== VGPU10_INTERPOLATION_UNDEFINED
||
2205 interpMode
== VGPU10_INTERPOLATION_CONSTANT
||
2206 interpMode
== VGPU10_INTERPOLATION_LINEAR
||
2207 interpMode
== VGPU10_INTERPOLATION_LINEAR_CENTROID
||
2208 interpMode
== VGPU10_INTERPOLATION_LINEAR_NOPERSPECTIVE
||
2209 interpMode
== VGPU10_INTERPOLATION_LINEAR_NOPERSPECTIVE_CENTROID
||
2210 interpMode
== VGPU10_INTERPOLATION_LINEAR_SAMPLE
||
2211 interpMode
== VGPU10_INTERPOLATION_LINEAR_NOPERSPECTIVE_SAMPLE
);
2213 check_register_index(emit
, opcodeType
, index
);
2215 opcode0
.value
= operand0
.value
= name_token
.value
= 0;
2217 opcode0
.opcodeType
= opcodeType
;
2218 opcode0
.interpolationMode
= interpMode
;
2220 operand0
.operandType
= operandType
;
2221 operand0
.numComponents
= numComp
;
2222 operand0
.selectionMode
= selMode
;
2223 operand0
.mask
= usageMask
;
2224 operand0
.indexDimension
= dim
;
2225 operand0
.index0Representation
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
2226 if (dim
== VGPU10_OPERAND_INDEX_2D
)
2227 operand0
.index1Representation
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
2229 name_token
.name
= name
;
2231 emit_decl_instruction(emit
, opcode0
, operand0
, name_token
, index
, size
);
2236 * Emit the declaration for a shader output.
2237 * \param type one of VGPU10_OPCODE_DCL_OUTPUTx
2238 * \param index the output register index
2239 * \param name one of VGPU10_NAME_x
2240 * \param usageMask bitfield of VGPU10_OPERAND_4_COMPONENT_MASK_x values
2243 emit_output_declaration(struct svga_shader_emitter_v10
*emit
,
2244 VGPU10_OPCODE_TYPE type
, unsigned index
,
2245 VGPU10_SYSTEM_NAME name
,
2248 VGPU10OpcodeToken0 opcode0
;
2249 VGPU10OperandToken0 operand0
;
2250 VGPU10NameToken name_token
;
2252 assert(usageMask
<= VGPU10_OPERAND_4_COMPONENT_MASK_ALL
);
2253 assert(type
== VGPU10_OPCODE_DCL_OUTPUT
||
2254 type
== VGPU10_OPCODE_DCL_OUTPUT_SGV
||
2255 type
== VGPU10_OPCODE_DCL_OUTPUT_SIV
);
2256 assert(name
== VGPU10_NAME_UNDEFINED
||
2257 name
== VGPU10_NAME_POSITION
||
2258 name
== VGPU10_NAME_PRIMITIVE_ID
||
2259 name
== VGPU10_NAME_RENDER_TARGET_ARRAY_INDEX
||
2260 name
== VGPU10_NAME_CLIP_DISTANCE
);
2262 check_register_index(emit
, type
, index
);
2264 opcode0
.value
= operand0
.value
= name_token
.value
= 0;
2266 opcode0
.opcodeType
= type
;
2267 operand0
.operandType
= VGPU10_OPERAND_TYPE_OUTPUT
;
2268 operand0
.numComponents
= VGPU10_OPERAND_4_COMPONENT
;
2269 operand0
.selectionMode
= VGPU10_OPERAND_4_COMPONENT_MASK_MODE
;
2270 operand0
.mask
= usageMask
;
2271 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_1D
;
2272 operand0
.index0Representation
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
2274 name_token
.name
= name
;
2276 emit_decl_instruction(emit
, opcode0
, operand0
, name_token
, index
, 1);
2281 * Emit the declaration for the fragment depth output.
2284 emit_fragdepth_output_declaration(struct svga_shader_emitter_v10
*emit
)
2286 VGPU10OpcodeToken0 opcode0
;
2287 VGPU10OperandToken0 operand0
;
2288 VGPU10NameToken name_token
;
2290 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
2292 opcode0
.value
= operand0
.value
= name_token
.value
= 0;
2294 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_OUTPUT
;
2295 operand0
.operandType
= VGPU10_OPERAND_TYPE_OUTPUT_DEPTH
;
2296 operand0
.numComponents
= VGPU10_OPERAND_1_COMPONENT
;
2297 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_0D
;
2300 emit_decl_instruction(emit
, opcode0
, operand0
, name_token
, 0, 1);
2305 * Emit the declaration for the fragment sample mask/coverage output.
2308 emit_samplemask_output_declaration(struct svga_shader_emitter_v10
*emit
)
2310 VGPU10OpcodeToken0 opcode0
;
2311 VGPU10OperandToken0 operand0
;
2312 VGPU10NameToken name_token
;
2314 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
2315 assert(emit
->version
>= 41);
2317 opcode0
.value
= operand0
.value
= name_token
.value
= 0;
2319 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_OUTPUT
;
2320 operand0
.operandType
= VGPU10_OPERAND_TYPE_OUTPUT_COVERAGE_MASK
;
2321 operand0
.numComponents
= VGPU10_OPERAND_0_COMPONENT
;
2322 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_0D
;
2325 emit_decl_instruction(emit
, opcode0
, operand0
, name_token
, 0, 1);
2330 * Emit the declaration for a system value input/output.
2333 emit_system_value_declaration(struct svga_shader_emitter_v10
*emit
,
2334 enum tgsi_semantic semantic_name
, unsigned index
)
2336 switch (semantic_name
) {
2337 case TGSI_SEMANTIC_INSTANCEID
:
2338 index
= alloc_system_value_index(emit
, index
);
2339 emit_input_declaration(emit
, VGPU10_OPCODE_DCL_INPUT_SIV
,
2340 VGPU10_OPERAND_TYPE_INPUT
,
2341 VGPU10_OPERAND_INDEX_1D
,
2343 VGPU10_NAME_INSTANCE_ID
,
2344 VGPU10_OPERAND_4_COMPONENT
,
2345 VGPU10_OPERAND_4_COMPONENT_MASK_MODE
,
2346 VGPU10_OPERAND_4_COMPONENT_MASK_X
,
2347 VGPU10_INTERPOLATION_UNDEFINED
);
2349 case TGSI_SEMANTIC_VERTEXID
:
2350 index
= alloc_system_value_index(emit
, index
);
2351 emit_input_declaration(emit
, VGPU10_OPCODE_DCL_INPUT_SIV
,
2352 VGPU10_OPERAND_TYPE_INPUT
,
2353 VGPU10_OPERAND_INDEX_1D
,
2355 VGPU10_NAME_VERTEX_ID
,
2356 VGPU10_OPERAND_4_COMPONENT
,
2357 VGPU10_OPERAND_4_COMPONENT_MASK_MODE
,
2358 VGPU10_OPERAND_4_COMPONENT_MASK_X
,
2359 VGPU10_INTERPOLATION_UNDEFINED
);
2361 case TGSI_SEMANTIC_SAMPLEID
:
2362 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
2363 emit
->fs
.sample_id_sys_index
= index
;
2364 index
= alloc_system_value_index(emit
, index
);
2365 emit_input_declaration(emit
, VGPU10_OPCODE_DCL_INPUT_PS_SIV
,
2366 VGPU10_OPERAND_TYPE_INPUT
,
2367 VGPU10_OPERAND_INDEX_1D
,
2369 VGPU10_NAME_SAMPLE_INDEX
,
2370 VGPU10_OPERAND_4_COMPONENT
,
2371 VGPU10_OPERAND_4_COMPONENT_MASK_MODE
,
2372 VGPU10_OPERAND_4_COMPONENT_MASK_X
,
2373 VGPU10_INTERPOLATION_CONSTANT
);
2375 case TGSI_SEMANTIC_SAMPLEPOS
:
2376 /* This system value contains the position of the current sample
2377 * when using per-sample shading. We implement this by calling
2378 * the VGPU10_OPCODE_SAMPLE_POS instruction with the current sample
2379 * index as the argument. See emit_sample_position_instructions().
2381 assert(emit
->version
>= 41);
2382 emit
->fs
.sample_pos_sys_index
= index
;
2383 index
= alloc_system_value_index(emit
, index
);
2386 debug_printf("unexpected sytem value semantic index %u\n",
2392 * Translate a TGSI declaration to VGPU10.
2395 emit_vgpu10_declaration(struct svga_shader_emitter_v10
*emit
,
2396 const struct tgsi_full_declaration
*decl
)
2398 switch (decl
->Declaration
.File
) {
2399 case TGSI_FILE_INPUT
:
2400 /* do nothing - see emit_input_declarations() */
2403 case TGSI_FILE_OUTPUT
:
2404 assert(decl
->Range
.First
== decl
->Range
.Last
);
2405 emit
->output_usage_mask
[decl
->Range
.First
] = decl
->Declaration
.UsageMask
;
2408 case TGSI_FILE_TEMPORARY
:
2409 /* Don't declare the temps here. Just keep track of how many
2410 * and emit the declaration later.
2412 if (decl
->Declaration
.Array
) {
2413 /* Indexed temporary array. Save the start index of the array
2414 * and the size of the array.
2416 const unsigned arrayID
= MIN2(decl
->Array
.ArrayID
, MAX_TEMP_ARRAYS
);
2419 assert(arrayID
< ARRAY_SIZE(emit
->temp_arrays
));
2421 /* Save this array so we can emit the declaration for it later */
2422 emit
->temp_arrays
[arrayID
].start
= decl
->Range
.First
;
2423 emit
->temp_arrays
[arrayID
].size
=
2424 decl
->Range
.Last
- decl
->Range
.First
+ 1;
2426 emit
->num_temp_arrays
= MAX2(emit
->num_temp_arrays
, arrayID
+ 1);
2427 assert(emit
->num_temp_arrays
<= MAX_TEMP_ARRAYS
);
2428 emit
->num_temp_arrays
= MIN2(emit
->num_temp_arrays
, MAX_TEMP_ARRAYS
);
2430 /* Fill in the temp_map entries for this array */
2431 for (i
= decl
->Range
.First
; i
<= decl
->Range
.Last
; i
++) {
2432 emit
->temp_map
[i
].arrayId
= arrayID
;
2433 emit
->temp_map
[i
].index
= i
- decl
->Range
.First
;
2437 /* for all temps, indexed or not, keep track of highest index */
2438 emit
->num_shader_temps
= MAX2(emit
->num_shader_temps
,
2439 decl
->Range
.Last
+ 1);
2442 case TGSI_FILE_CONSTANT
:
2443 /* Don't declare constants here. Just keep track and emit later. */
2445 unsigned constbuf
= 0, num_consts
;
2446 if (decl
->Declaration
.Dimension
) {
2447 constbuf
= decl
->Dim
.Index2D
;
2449 /* We throw an assertion here when, in fact, the shader should never
2450 * have linked due to constbuf index out of bounds, so we shouldn't
2451 * have reached here.
2453 assert(constbuf
< ARRAY_SIZE(emit
->num_shader_consts
));
2455 num_consts
= MAX2(emit
->num_shader_consts
[constbuf
],
2456 decl
->Range
.Last
+ 1);
2458 if (num_consts
> VGPU10_MAX_CONSTANT_BUFFER_ELEMENT_COUNT
) {
2459 debug_printf("Warning: constant buffer is declared to size [%u]"
2460 " but [%u] is the limit.\n",
2462 VGPU10_MAX_CONSTANT_BUFFER_ELEMENT_COUNT
);
2464 /* The linker doesn't enforce the max UBO size so we clamp here */
2465 emit
->num_shader_consts
[constbuf
] =
2466 MIN2(num_consts
, VGPU10_MAX_CONSTANT_BUFFER_ELEMENT_COUNT
);
2470 case TGSI_FILE_IMMEDIATE
:
2471 assert(!"TGSI_FILE_IMMEDIATE not handled yet!");
2474 case TGSI_FILE_SYSTEM_VALUE
:
2475 emit_system_value_declaration(emit
, decl
->Semantic
.Name
,
2479 case TGSI_FILE_SAMPLER
:
2480 /* Don't declare samplers here. Just keep track and emit later. */
2481 emit
->num_samplers
= MAX2(emit
->num_samplers
, decl
->Range
.Last
+ 1);
2485 case TGSI_FILE_RESOURCE
:
2486 /*opcode0.opcodeType = VGPU10_OPCODE_DCL_RESOURCE;*/
2487 /* XXX more, VGPU10_RETURN_TYPE_FLOAT */
2488 assert(!"TGSI_FILE_RESOURCE not handled yet");
2492 case TGSI_FILE_ADDRESS
:
2493 emit
->num_address_regs
= MAX2(emit
->num_address_regs
,
2494 decl
->Range
.Last
+ 1);
2497 case TGSI_FILE_SAMPLER_VIEW
:
2499 unsigned unit
= decl
->Range
.First
;
2500 assert(decl
->Range
.First
== decl
->Range
.Last
);
2501 emit
->sampler_target
[unit
] = decl
->SamplerView
.Resource
;
2502 /* Note: we can ignore YZW return types for now */
2503 emit
->sampler_return_type
[unit
] = decl
->SamplerView
.ReturnTypeX
;
2504 emit
->sampler_view
[unit
] = TRUE
;
2509 assert(!"Unexpected type of declaration");
2517 * Emit all input declarations.
2520 emit_input_declarations(struct svga_shader_emitter_v10
*emit
)
2524 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
2526 for (i
= 0; i
< emit
->linkage
.num_inputs
; i
++) {
2527 enum tgsi_semantic semantic_name
= emit
->info
.input_semantic_name
[i
];
2528 unsigned usage_mask
= emit
->info
.input_usage_mask
[i
];
2529 unsigned index
= emit
->linkage
.input_map
[i
];
2530 VGPU10_OPCODE_TYPE type
;
2531 VGPU10_INTERPOLATION_MODE interpolationMode
;
2532 VGPU10_SYSTEM_NAME name
;
2534 if (usage_mask
== 0)
2535 continue; /* register is not actually used */
2537 if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
2538 /* fragment position input */
2539 type
= VGPU10_OPCODE_DCL_INPUT_PS_SGV
;
2540 interpolationMode
= VGPU10_INTERPOLATION_LINEAR
;
2541 name
= VGPU10_NAME_POSITION
;
2542 if (usage_mask
& TGSI_WRITEMASK_W
) {
2543 /* we need to replace use of 'w' with '1/w' */
2544 emit
->fs
.fragcoord_input_index
= i
;
2547 else if (semantic_name
== TGSI_SEMANTIC_FACE
) {
2548 /* fragment front-facing input */
2549 type
= VGPU10_OPCODE_DCL_INPUT_PS_SGV
;
2550 interpolationMode
= VGPU10_INTERPOLATION_CONSTANT
;
2551 name
= VGPU10_NAME_IS_FRONT_FACE
;
2552 emit
->fs
.face_input_index
= i
;
2554 else if (semantic_name
== TGSI_SEMANTIC_PRIMID
) {
2556 type
= VGPU10_OPCODE_DCL_INPUT_PS_SGV
;
2557 interpolationMode
= VGPU10_INTERPOLATION_CONSTANT
;
2558 name
= VGPU10_NAME_PRIMITIVE_ID
;
2560 else if (semantic_name
== TGSI_SEMANTIC_SAMPLEID
) {
2561 /* sample index / ID */
2562 type
= VGPU10_OPCODE_DCL_INPUT_PS_SGV
;
2563 interpolationMode
= VGPU10_INTERPOLATION_CONSTANT
;
2564 name
= VGPU10_NAME_SAMPLE_INDEX
;
2567 /* general fragment input */
2568 type
= VGPU10_OPCODE_DCL_INPUT_PS
;
2570 translate_interpolation(emit
,
2571 emit
->info
.input_interpolate
[i
],
2572 emit
->info
.input_interpolate_loc
[i
]);
2574 /* keeps track if flat interpolation mode is being used */
2575 emit
->uses_flat_interp
|=
2576 (interpolationMode
== VGPU10_INTERPOLATION_CONSTANT
);
2578 name
= VGPU10_NAME_UNDEFINED
;
2581 emit_input_declaration(emit
, type
,
2582 VGPU10_OPERAND_TYPE_INPUT
,
2583 VGPU10_OPERAND_INDEX_1D
, index
, 1,
2585 VGPU10_OPERAND_4_COMPONENT
,
2586 VGPU10_OPERAND_4_COMPONENT_MASK_MODE
,
2587 VGPU10_OPERAND_4_COMPONENT_MASK_ALL
,
2591 else if (emit
->unit
== PIPE_SHADER_GEOMETRY
) {
2593 for (i
= 0; i
< emit
->info
.num_inputs
; i
++) {
2594 enum tgsi_semantic semantic_name
= emit
->info
.input_semantic_name
[i
];
2595 unsigned usage_mask
= emit
->info
.input_usage_mask
[i
];
2596 unsigned index
= emit
->linkage
.input_map
[i
];
2597 VGPU10_OPCODE_TYPE opcodeType
, operandType
;
2598 VGPU10_OPERAND_NUM_COMPONENTS numComp
;
2599 VGPU10_OPERAND_4_COMPONENT_SELECTION_MODE selMode
;
2600 VGPU10_SYSTEM_NAME name
;
2601 VGPU10_OPERAND_INDEX_DIMENSION dim
;
2603 if (usage_mask
== 0)
2604 continue; /* register is not actually used */
2606 opcodeType
= VGPU10_OPCODE_DCL_INPUT
;
2607 operandType
= VGPU10_OPERAND_TYPE_INPUT
;
2608 numComp
= VGPU10_OPERAND_4_COMPONENT
;
2609 selMode
= VGPU10_OPERAND_4_COMPONENT_MASK_MODE
;
2610 name
= VGPU10_NAME_UNDEFINED
;
2612 /* all geometry shader inputs are two dimensional except
2615 dim
= VGPU10_OPERAND_INDEX_2D
;
2617 if (semantic_name
== TGSI_SEMANTIC_PRIMID
) {
2619 operandType
= VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID
;
2620 dim
= VGPU10_OPERAND_INDEX_0D
;
2621 numComp
= VGPU10_OPERAND_0_COMPONENT
;
2624 /* also save the register index so we can check for
2625 * primitive id when emit src register. We need to modify the
2626 * operand type, index dimension when emit primitive id src reg.
2628 emit
->gs
.prim_id_index
= i
;
2630 else if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
2631 /* vertex position input */
2632 opcodeType
= VGPU10_OPCODE_DCL_INPUT_SIV
;
2633 name
= VGPU10_NAME_POSITION
;
2636 emit_input_declaration(emit
, opcodeType
, operandType
,
2638 emit
->gs
.input_size
,
2641 VGPU10_OPERAND_4_COMPONENT_MASK_ALL
,
2642 VGPU10_INTERPOLATION_UNDEFINED
);
2646 assert(emit
->unit
== PIPE_SHADER_VERTEX
);
2648 for (i
= 0; i
< emit
->info
.file_max
[TGSI_FILE_INPUT
] + 1; i
++) {
2649 unsigned usage_mask
= emit
->info
.input_usage_mask
[i
];
2652 if (usage_mask
== 0)
2653 continue; /* register is not actually used */
2655 emit_input_declaration(emit
, VGPU10_OPCODE_DCL_INPUT
,
2656 VGPU10_OPERAND_TYPE_INPUT
,
2657 VGPU10_OPERAND_INDEX_1D
, index
, 1,
2658 VGPU10_NAME_UNDEFINED
,
2659 VGPU10_OPERAND_4_COMPONENT
,
2660 VGPU10_OPERAND_4_COMPONENT_MASK_MODE
,
2661 VGPU10_OPERAND_4_COMPONENT_MASK_ALL
,
2662 VGPU10_INTERPOLATION_UNDEFINED
);
2671 * Emit all output declarations.
2674 emit_output_declarations(struct svga_shader_emitter_v10
*emit
)
2678 for (i
= 0; i
< emit
->info
.num_outputs
; i
++) {
2679 /*const unsigned usage_mask = emit->info.output_usage_mask[i];*/
2680 const enum tgsi_semantic semantic_name
=
2681 emit
->info
.output_semantic_name
[i
];
2682 const unsigned semantic_index
= emit
->info
.output_semantic_index
[i
];
2685 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
2686 if (semantic_name
== TGSI_SEMANTIC_COLOR
) {
2687 assert(semantic_index
< ARRAY_SIZE(emit
->fs
.color_out_index
));
2689 emit
->fs
.color_out_index
[semantic_index
] = index
;
2691 emit
->fs
.num_color_outputs
= MAX2(emit
->fs
.num_color_outputs
,
2694 /* The semantic index is the shader's color output/buffer index */
2695 emit_output_declaration(emit
,
2696 VGPU10_OPCODE_DCL_OUTPUT
, semantic_index
,
2697 VGPU10_NAME_UNDEFINED
,
2698 VGPU10_OPERAND_4_COMPONENT_MASK_ALL
);
2700 if (semantic_index
== 0) {
2701 if (emit
->key
.fs
.write_color0_to_n_cbufs
> 1) {
2702 /* Emit declarations for the additional color outputs
2706 for (j
= 1; j
< emit
->key
.fs
.write_color0_to_n_cbufs
; j
++) {
2707 /* Allocate a new output index */
2708 unsigned idx
= emit
->info
.num_outputs
+ j
- 1;
2709 emit
->fs
.color_out_index
[j
] = idx
;
2710 emit_output_declaration(emit
,
2711 VGPU10_OPCODE_DCL_OUTPUT
, idx
,
2712 VGPU10_NAME_UNDEFINED
,
2713 VGPU10_OPERAND_4_COMPONENT_MASK_ALL
);
2714 emit
->info
.output_semantic_index
[idx
] = j
;
2717 emit
->fs
.num_color_outputs
=
2718 emit
->key
.fs
.write_color0_to_n_cbufs
;
2722 assert(!emit
->key
.fs
.write_color0_to_n_cbufs
);
2725 else if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
2726 /* Fragment depth output */
2727 emit_fragdepth_output_declaration(emit
);
2729 else if (semantic_name
== TGSI_SEMANTIC_SAMPLEMASK
) {
2730 /* Fragment depth output */
2731 emit_samplemask_output_declaration(emit
);
2734 assert(!"Bad output semantic name");
2739 VGPU10_COMPONENT_NAME name
;
2740 VGPU10_OPCODE_TYPE type
;
2741 unsigned writemask
= VGPU10_OPERAND_4_COMPONENT_MASK_ALL
;
2743 switch (semantic_name
) {
2744 case TGSI_SEMANTIC_POSITION
:
2745 assert(emit
->unit
!= PIPE_SHADER_FRAGMENT
);
2746 type
= VGPU10_OPCODE_DCL_OUTPUT_SIV
;
2747 name
= VGPU10_NAME_POSITION
;
2748 /* Save the index of the vertex position output register */
2749 emit
->vposition
.out_index
= index
;
2751 case TGSI_SEMANTIC_CLIPDIST
:
2752 type
= VGPU10_OPCODE_DCL_OUTPUT_SIV
;
2753 name
= VGPU10_NAME_CLIP_DISTANCE
;
2754 /* save the starting index of the clip distance output register */
2755 if (semantic_index
== 0)
2756 emit
->clip_dist_out_index
= index
;
2757 writemask
= emit
->output_usage_mask
[index
];
2758 writemask
= apply_clip_plane_mask(emit
, writemask
, semantic_index
);
2759 if (writemask
== 0x0) {
2760 continue; /* discard this do-nothing declaration */
2763 case TGSI_SEMANTIC_PRIMID
:
2764 assert(emit
->unit
== PIPE_SHADER_GEOMETRY
);
2765 type
= VGPU10_OPCODE_DCL_OUTPUT_SGV
;
2766 name
= VGPU10_NAME_PRIMITIVE_ID
;
2768 case TGSI_SEMANTIC_LAYER
:
2769 assert(emit
->unit
== PIPE_SHADER_GEOMETRY
);
2770 type
= VGPU10_OPCODE_DCL_OUTPUT_SGV
;
2771 name
= VGPU10_NAME_RENDER_TARGET_ARRAY_INDEX
;
2773 case TGSI_SEMANTIC_CLIPVERTEX
:
2774 type
= VGPU10_OPCODE_DCL_OUTPUT
;
2775 name
= VGPU10_NAME_UNDEFINED
;
2776 emit
->clip_vertex_out_index
= index
;
2779 /* generic output */
2780 type
= VGPU10_OPCODE_DCL_OUTPUT
;
2781 name
= VGPU10_NAME_UNDEFINED
;
2784 emit_output_declaration(emit
, type
, index
, name
, writemask
);
2788 if (emit
->vposition
.so_index
!= INVALID_INDEX
&&
2789 emit
->vposition
.out_index
!= INVALID_INDEX
) {
2791 assert(emit
->unit
!= PIPE_SHADER_FRAGMENT
);
2793 /* Emit the declaration for the non-adjusted vertex position
2794 * for stream output purpose
2796 emit_output_declaration(emit
, VGPU10_OPCODE_DCL_OUTPUT
,
2797 emit
->vposition
.so_index
,
2798 VGPU10_NAME_UNDEFINED
,
2799 VGPU10_OPERAND_4_COMPONENT_MASK_ALL
);
2802 if (emit
->clip_dist_so_index
!= INVALID_INDEX
&&
2803 emit
->clip_dist_out_index
!= INVALID_INDEX
) {
2805 assert(emit
->unit
!= PIPE_SHADER_FRAGMENT
);
2807 /* Emit the declaration for the clip distance shadow copy which
2808 * will be used for stream output purpose and for clip distance
2811 emit_output_declaration(emit
, VGPU10_OPCODE_DCL_OUTPUT
,
2812 emit
->clip_dist_so_index
,
2813 VGPU10_NAME_UNDEFINED
,
2814 emit
->output_usage_mask
[emit
->clip_dist_out_index
]);
2816 if (emit
->info
.num_written_clipdistance
> 4) {
2817 /* for the second clip distance register, each handles 4 planes */
2818 emit_output_declaration(emit
, VGPU10_OPCODE_DCL_OUTPUT
,
2819 emit
->clip_dist_so_index
+ 1,
2820 VGPU10_NAME_UNDEFINED
,
2821 emit
->output_usage_mask
[emit
->clip_dist_out_index
+1]);
2830 * Emit the declaration for the temporary registers.
2833 emit_temporaries_declaration(struct svga_shader_emitter_v10
*emit
)
2835 unsigned total_temps
, reg
, i
;
2837 total_temps
= emit
->num_shader_temps
;
2839 /* If there is indirect access to non-indexable temps in the shader,
2840 * convert those temps to indexable temps. This works around a bug
2841 * in the GLSL->TGSI translator exposed in piglit test
2842 * glsl-1.20/execution/fs-const-array-of-struct-of-array.shader_test.
2843 * Internal temps added by the driver remain as non-indexable temps.
2845 if ((emit
->info
.indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) &&
2846 emit
->num_temp_arrays
== 0) {
2850 emit
->num_temp_arrays
= arrayID
+ 1;
2851 emit
->temp_arrays
[arrayID
].start
= 0;
2852 emit
->temp_arrays
[arrayID
].size
= total_temps
;
2854 /* Fill in the temp_map entries for this temp array */
2855 for (i
= 0; i
< total_temps
; i
++) {
2856 emit
->temp_map
[i
].arrayId
= arrayID
;
2857 emit
->temp_map
[i
].index
= i
;
2861 /* Allocate extra temps for specially-implemented instructions,
2864 total_temps
+= MAX_INTERNAL_TEMPS
;
2866 if (emit
->unit
== PIPE_SHADER_VERTEX
|| emit
->unit
== PIPE_SHADER_GEOMETRY
) {
2867 if (emit
->vposition
.need_prescale
|| emit
->key
.vs
.undo_viewport
||
2868 emit
->key
.clip_plane_enable
||
2869 emit
->vposition
.so_index
!= INVALID_INDEX
) {
2870 emit
->vposition
.tmp_index
= total_temps
;
2874 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
2875 unsigned attrib_mask
= (emit
->key
.vs
.adjust_attrib_w_1
|
2876 emit
->key
.vs
.adjust_attrib_itof
|
2877 emit
->key
.vs
.adjust_attrib_utof
|
2878 emit
->key
.vs
.attrib_is_bgra
|
2879 emit
->key
.vs
.attrib_puint_to_snorm
|
2880 emit
->key
.vs
.attrib_puint_to_uscaled
|
2881 emit
->key
.vs
.attrib_puint_to_sscaled
);
2882 while (attrib_mask
) {
2883 unsigned index
= u_bit_scan(&attrib_mask
);
2884 emit
->vs
.adjusted_input
[index
] = total_temps
++;
2888 if (emit
->clip_mode
== CLIP_DISTANCE
) {
2889 /* We need to write the clip distance to a temporary register
2890 * first. Then it will be copied to the shadow copy for
2891 * the clip distance varying variable and stream output purpose.
2892 * It will also be copied to the actual CLIPDIST register
2893 * according to the enabled clip planes
2895 emit
->clip_dist_tmp_index
= total_temps
++;
2896 if (emit
->info
.num_written_clipdistance
> 4)
2897 total_temps
++; /* second clip register */
2899 else if (emit
->clip_mode
== CLIP_VERTEX
) {
2900 /* We need to convert the TGSI CLIPVERTEX output to one or more
2901 * clip distances. Allocate a temp reg for the clipvertex here.
2903 assert(emit
->info
.writes_clipvertex
> 0);
2904 emit
->clip_vertex_tmp_index
= total_temps
;
2908 else if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
2909 if (emit
->key
.fs
.alpha_func
!= SVGA3D_CMP_ALWAYS
||
2910 emit
->key
.fs
.write_color0_to_n_cbufs
> 1) {
2911 /* Allocate a temp to hold the output color */
2912 emit
->fs
.color_tmp_index
= total_temps
;
2916 if (emit
->fs
.face_input_index
!= INVALID_INDEX
) {
2917 /* Allocate a temp for the +/-1 face register */
2918 emit
->fs
.face_tmp_index
= total_temps
;
2922 if (emit
->fs
.fragcoord_input_index
!= INVALID_INDEX
) {
2923 /* Allocate a temp for modified fragment position register */
2924 emit
->fs
.fragcoord_tmp_index
= total_temps
;
2928 if (emit
->fs
.sample_pos_sys_index
!= INVALID_INDEX
) {
2929 /* Allocate a temp for the sample position */
2930 emit
->fs
.sample_pos_tmp_index
= total_temps
++;
2934 for (i
= 0; i
< emit
->num_address_regs
; i
++) {
2935 emit
->address_reg_index
[i
] = total_temps
++;
2938 /* Initialize the temp_map array which maps TGSI temp indexes to VGPU10
2939 * temp indexes. Basically, we compact all the non-array temp register
2940 * indexes into a consecutive series.
2942 * Before, we may have some TGSI declarations like:
2943 * DCL TEMP[0..1], LOCAL
2944 * DCL TEMP[2..4], ARRAY(1), LOCAL
2945 * DCL TEMP[5..7], ARRAY(2), LOCAL
2946 * plus, some extra temps, like TEMP[8], TEMP[9] for misc things
2948 * After, we'll have a map like this:
2949 * temp_map[0] = { array 0, index 0 }
2950 * temp_map[1] = { array 0, index 1 }
2951 * temp_map[2] = { array 1, index 0 }
2952 * temp_map[3] = { array 1, index 1 }
2953 * temp_map[4] = { array 1, index 2 }
2954 * temp_map[5] = { array 2, index 0 }
2955 * temp_map[6] = { array 2, index 1 }
2956 * temp_map[7] = { array 2, index 2 }
2957 * temp_map[8] = { array 0, index 2 }
2958 * temp_map[9] = { array 0, index 3 }
2960 * We'll declare two arrays of 3 elements, plus a set of four non-indexed
2961 * temps numbered 0..3
2963 * Any time we emit a temporary register index, we'll have to use the
2964 * temp_map[] table to convert the TGSI index to the VGPU10 index.
2966 * Finally, we recompute the total_temps value here.
2969 for (i
= 0; i
< total_temps
; i
++) {
2970 if (emit
->temp_map
[i
].arrayId
== 0) {
2971 emit
->temp_map
[i
].index
= reg
++;
2976 debug_printf("total_temps %u\n", total_temps
);
2977 for (i
= 0; i
< total_temps
; i
++) {
2978 debug_printf("temp %u -> array %u index %u\n",
2979 i
, emit
->temp_map
[i
].arrayId
, emit
->temp_map
[i
].index
);
2985 /* Emit declaration of ordinary temp registers */
2986 if (total_temps
> 0) {
2987 VGPU10OpcodeToken0 opcode0
;
2990 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_TEMPS
;
2992 begin_emit_instruction(emit
);
2993 emit_dword(emit
, opcode0
.value
);
2994 emit_dword(emit
, total_temps
);
2995 end_emit_instruction(emit
);
2998 /* Emit declarations for indexable temp arrays. Skip 0th entry since
3001 for (i
= 1; i
< emit
->num_temp_arrays
; i
++) {
3002 unsigned num_temps
= emit
->temp_arrays
[i
].size
;
3004 if (num_temps
> 0) {
3005 VGPU10OpcodeToken0 opcode0
;
3008 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_INDEXABLE_TEMP
;
3010 begin_emit_instruction(emit
);
3011 emit_dword(emit
, opcode0
.value
);
3012 emit_dword(emit
, i
); /* which array */
3013 emit_dword(emit
, num_temps
);
3014 emit_dword(emit
, 4); /* num components */
3015 end_emit_instruction(emit
);
3017 total_temps
+= num_temps
;
3021 /* Check that the grand total of all regular and indexed temps is
3024 check_register_index(emit
, VGPU10_OPCODE_DCL_TEMPS
, total_temps
- 1);
3031 emit_constant_declaration(struct svga_shader_emitter_v10
*emit
)
3033 VGPU10OpcodeToken0 opcode0
;
3034 VGPU10OperandToken0 operand0
;
3035 unsigned total_consts
, i
;
3038 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_CONSTANT_BUFFER
;
3039 opcode0
.accessPattern
= VGPU10_CB_IMMEDIATE_INDEXED
;
3040 /* XXX or, access pattern = VGPU10_CB_DYNAMIC_INDEXED */
3043 operand0
.numComponents
= VGPU10_OPERAND_4_COMPONENT
;
3044 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_2D
;
3045 operand0
.index0Representation
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
3046 operand0
.index1Representation
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
3047 operand0
.operandType
= VGPU10_OPERAND_TYPE_CONSTANT_BUFFER
;
3048 operand0
.selectionMode
= VGPU10_OPERAND_4_COMPONENT_SWIZZLE_MODE
;
3049 operand0
.swizzleX
= 0;
3050 operand0
.swizzleY
= 1;
3051 operand0
.swizzleZ
= 2;
3052 operand0
.swizzleW
= 3;
3055 * Emit declaration for constant buffer [0]. We also allocate
3056 * room for the extra constants here.
3058 total_consts
= emit
->num_shader_consts
[0];
3060 /* Now, allocate constant slots for the "extra" constants.
3061 * Note: it's critical that these extra constant locations
3062 * exactly match what's emitted by the "extra" constants code
3063 * in svga_state_constants.c
3066 /* Vertex position scale/translation */
3067 if (emit
->vposition
.need_prescale
) {
3068 emit
->vposition
.prescale_scale_index
= total_consts
++;
3069 emit
->vposition
.prescale_trans_index
= total_consts
++;
3072 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
3073 if (emit
->key
.vs
.undo_viewport
) {
3074 emit
->vs
.viewport_index
= total_consts
++;
3078 /* user-defined clip planes */
3079 if (emit
->key
.clip_plane_enable
) {
3080 unsigned n
= util_bitcount(emit
->key
.clip_plane_enable
);
3081 assert(emit
->unit
== PIPE_SHADER_VERTEX
||
3082 emit
->unit
== PIPE_SHADER_GEOMETRY
);
3083 for (i
= 0; i
< n
; i
++) {
3084 emit
->clip_plane_const
[i
] = total_consts
++;
3088 for (i
= 0; i
< emit
->num_samplers
; i
++) {
3090 if (emit
->sampler_view
[i
]) {
3092 /* Texcoord scale factors for RECT textures */
3093 if (emit
->key
.tex
[i
].unnormalized
) {
3094 emit
->texcoord_scale_index
[i
] = total_consts
++;
3097 /* Texture buffer sizes */
3098 if (emit
->sampler_target
[i
] == TGSI_TEXTURE_BUFFER
) {
3099 emit
->texture_buffer_size_index
[i
] = total_consts
++;
3104 if (total_consts
> 0) {
3105 begin_emit_instruction(emit
);
3106 emit_dword(emit
, opcode0
.value
);
3107 emit_dword(emit
, operand0
.value
);
3108 emit_dword(emit
, 0); /* which const buffer slot */
3109 emit_dword(emit
, total_consts
);
3110 end_emit_instruction(emit
);
3113 /* Declare remaining constant buffers (UBOs) */
3114 for (i
= 1; i
< ARRAY_SIZE(emit
->num_shader_consts
); i
++) {
3115 if (emit
->num_shader_consts
[i
] > 0) {
3116 begin_emit_instruction(emit
);
3117 emit_dword(emit
, opcode0
.value
);
3118 emit_dword(emit
, operand0
.value
);
3119 emit_dword(emit
, i
); /* which const buffer slot */
3120 emit_dword(emit
, emit
->num_shader_consts
[i
]);
3121 end_emit_instruction(emit
);
3130 * Emit declarations for samplers.
3133 emit_sampler_declarations(struct svga_shader_emitter_v10
*emit
)
3137 for (i
= 0; i
< emit
->num_samplers
; i
++) {
3138 VGPU10OpcodeToken0 opcode0
;
3139 VGPU10OperandToken0 operand0
;
3142 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_SAMPLER
;
3143 opcode0
.samplerMode
= VGPU10_SAMPLER_MODE_DEFAULT
;
3146 operand0
.numComponents
= VGPU10_OPERAND_0_COMPONENT
;
3147 operand0
.operandType
= VGPU10_OPERAND_TYPE_SAMPLER
;
3148 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_1D
;
3149 operand0
.index0Representation
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
3151 begin_emit_instruction(emit
);
3152 emit_dword(emit
, opcode0
.value
);
3153 emit_dword(emit
, operand0
.value
);
3154 emit_dword(emit
, i
);
3155 end_emit_instruction(emit
);
3163 * Translate TGSI_TEXTURE_x to VGAPU10_RESOURCE_DIMENSION_x.
3166 tgsi_texture_to_resource_dimension(enum tgsi_texture_type target
,
3170 case TGSI_TEXTURE_BUFFER
:
3171 return VGPU10_RESOURCE_DIMENSION_BUFFER
;
3172 case TGSI_TEXTURE_1D
:
3173 return VGPU10_RESOURCE_DIMENSION_TEXTURE1D
;
3174 case TGSI_TEXTURE_2D
:
3175 case TGSI_TEXTURE_RECT
:
3176 return VGPU10_RESOURCE_DIMENSION_TEXTURE2D
;
3177 case TGSI_TEXTURE_3D
:
3178 return VGPU10_RESOURCE_DIMENSION_TEXTURE3D
;
3179 case TGSI_TEXTURE_CUBE
:
3180 case TGSI_TEXTURE_SHADOWCUBE
:
3181 return VGPU10_RESOURCE_DIMENSION_TEXTURECUBE
;
3182 case TGSI_TEXTURE_SHADOW1D
:
3183 return VGPU10_RESOURCE_DIMENSION_TEXTURE1D
;
3184 case TGSI_TEXTURE_SHADOW2D
:
3185 case TGSI_TEXTURE_SHADOWRECT
:
3186 return VGPU10_RESOURCE_DIMENSION_TEXTURE2D
;
3187 case TGSI_TEXTURE_1D_ARRAY
:
3188 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
3189 return is_array
? VGPU10_RESOURCE_DIMENSION_TEXTURE1DARRAY
3190 : VGPU10_RESOURCE_DIMENSION_TEXTURE1D
;
3191 case TGSI_TEXTURE_2D_ARRAY
:
3192 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
3193 return is_array
? VGPU10_RESOURCE_DIMENSION_TEXTURE2DARRAY
3194 : VGPU10_RESOURCE_DIMENSION_TEXTURE2D
;
3195 case TGSI_TEXTURE_2D_MSAA
:
3196 return VGPU10_RESOURCE_DIMENSION_TEXTURE2DMS
;
3197 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
3198 return is_array
? VGPU10_RESOURCE_DIMENSION_TEXTURE2DMSARRAY
3199 : VGPU10_RESOURCE_DIMENSION_TEXTURE2DMS
;
3200 case TGSI_TEXTURE_CUBE_ARRAY
:
3201 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
3202 return is_array
? VGPU10_RESOURCE_DIMENSION_TEXTURECUBEARRAY
3203 : VGPU10_RESOURCE_DIMENSION_TEXTURECUBE
;
3205 assert(!"Unexpected resource type");
3206 return VGPU10_RESOURCE_DIMENSION_TEXTURE2D
;
3212 * Given a tgsi_return_type, return true iff it is an integer type.
3215 is_integer_type(enum tgsi_return_type type
)
3218 case TGSI_RETURN_TYPE_SINT
:
3219 case TGSI_RETURN_TYPE_UINT
:
3221 case TGSI_RETURN_TYPE_FLOAT
:
3222 case TGSI_RETURN_TYPE_UNORM
:
3223 case TGSI_RETURN_TYPE_SNORM
:
3225 case TGSI_RETURN_TYPE_COUNT
:
3227 assert(!"is_integer_type: Unknown tgsi_return_type");
3234 * Emit declarations for resources.
3235 * XXX When we're sure that all TGSI shaders will be generated with
3236 * sampler view declarations (Ex: DCL SVIEW[n], 2D, UINT) we may
3240 emit_resource_declarations(struct svga_shader_emitter_v10
*emit
)
3244 /* Emit resource decl for each sampler */
3245 for (i
= 0; i
< emit
->num_samplers
; i
++) {
3246 VGPU10OpcodeToken0 opcode0
;
3247 VGPU10OperandToken0 operand0
;
3248 VGPU10ResourceReturnTypeToken return_type
;
3249 VGPU10_RESOURCE_RETURN_TYPE rt
;
3252 opcode0
.opcodeType
= VGPU10_OPCODE_DCL_RESOURCE
;
3253 opcode0
.resourceDimension
=
3254 tgsi_texture_to_resource_dimension(emit
->sampler_target
[i
],
3255 emit
->key
.tex
[i
].is_array
);
3256 opcode0
.sampleCount
= emit
->key
.tex
[i
].num_samples
;
3258 operand0
.numComponents
= VGPU10_OPERAND_0_COMPONENT
;
3259 operand0
.operandType
= VGPU10_OPERAND_TYPE_RESOURCE
;
3260 operand0
.indexDimension
= VGPU10_OPERAND_INDEX_1D
;
3261 operand0
.index0Representation
= VGPU10_OPERAND_INDEX_IMMEDIATE32
;
3264 /* convert TGSI_RETURN_TYPE_x to VGPU10_RETURN_TYPE_x */
3265 STATIC_ASSERT(VGPU10_RETURN_TYPE_UNORM
== TGSI_RETURN_TYPE_UNORM
+ 1);
3266 STATIC_ASSERT(VGPU10_RETURN_TYPE_SNORM
== TGSI_RETURN_TYPE_SNORM
+ 1);
3267 STATIC_ASSERT(VGPU10_RETURN_TYPE_SINT
== TGSI_RETURN_TYPE_SINT
+ 1);
3268 STATIC_ASSERT(VGPU10_RETURN_TYPE_UINT
== TGSI_RETURN_TYPE_UINT
+ 1);
3269 STATIC_ASSERT(VGPU10_RETURN_TYPE_FLOAT
== TGSI_RETURN_TYPE_FLOAT
+ 1);
3270 assert(emit
->sampler_return_type
[i
] <= TGSI_RETURN_TYPE_FLOAT
);
3271 rt
= emit
->sampler_return_type
[i
] + 1;
3273 switch (emit
->sampler_return_type
[i
]) {
3274 case TGSI_RETURN_TYPE_UNORM
: rt
= VGPU10_RETURN_TYPE_UNORM
; break;
3275 case TGSI_RETURN_TYPE_SNORM
: rt
= VGPU10_RETURN_TYPE_SNORM
; break;
3276 case TGSI_RETURN_TYPE_SINT
: rt
= VGPU10_RETURN_TYPE_SINT
; break;
3277 case TGSI_RETURN_TYPE_UINT
: rt
= VGPU10_RETURN_TYPE_UINT
; break;
3278 case TGSI_RETURN_TYPE_FLOAT
: rt
= VGPU10_RETURN_TYPE_FLOAT
; break;
3279 case TGSI_RETURN_TYPE_COUNT
:
3281 rt
= VGPU10_RETURN_TYPE_FLOAT
;
3282 assert(!"emit_resource_declarations: Unknown tgsi_return_type");
3286 return_type
.value
= 0;
3287 return_type
.component0
= rt
;
3288 return_type
.component1
= rt
;
3289 return_type
.component2
= rt
;
3290 return_type
.component3
= rt
;
3292 begin_emit_instruction(emit
);
3293 emit_dword(emit
, opcode0
.value
);
3294 emit_dword(emit
, operand0
.value
);
3295 emit_dword(emit
, i
);
3296 emit_dword(emit
, return_type
.value
);
3297 end_emit_instruction(emit
);
3304 emit_instruction_op1(struct svga_shader_emitter_v10
*emit
,
3305 VGPU10_OPCODE_TYPE opcode
,
3306 const struct tgsi_full_dst_register
*dst
,
3307 const struct tgsi_full_src_register
*src
,
3310 begin_emit_instruction(emit
);
3311 emit_opcode(emit
, opcode
, saturate
);
3312 emit_dst_register(emit
, dst
);
3313 emit_src_register(emit
, src
);
3314 end_emit_instruction(emit
);
3318 emit_instruction_op2(struct svga_shader_emitter_v10
*emit
,
3319 VGPU10_OPCODE_TYPE opcode
,
3320 const struct tgsi_full_dst_register
*dst
,
3321 const struct tgsi_full_src_register
*src1
,
3322 const struct tgsi_full_src_register
*src2
,
3325 begin_emit_instruction(emit
);
3326 emit_opcode(emit
, opcode
, saturate
);
3327 emit_dst_register(emit
, dst
);
3328 emit_src_register(emit
, src1
);
3329 emit_src_register(emit
, src2
);
3330 end_emit_instruction(emit
);
3334 emit_instruction_op3(struct svga_shader_emitter_v10
*emit
,
3335 VGPU10_OPCODE_TYPE opcode
,
3336 const struct tgsi_full_dst_register
*dst
,
3337 const struct tgsi_full_src_register
*src1
,
3338 const struct tgsi_full_src_register
*src2
,
3339 const struct tgsi_full_src_register
*src3
,
3342 begin_emit_instruction(emit
);
3343 emit_opcode(emit
, opcode
, saturate
);
3344 emit_dst_register(emit
, dst
);
3345 emit_src_register(emit
, src1
);
3346 emit_src_register(emit
, src2
);
3347 emit_src_register(emit
, src3
);
3348 end_emit_instruction(emit
);
3352 * Emit the actual clip distance instructions to be used for clipping
3353 * by copying the clip distance from the temporary registers to the
3354 * CLIPDIST registers written with the enabled planes mask.
3355 * Also copy the clip distance from the temporary to the clip distance
3356 * shadow copy register which will be referenced by the input shader
3359 emit_clip_distance_instructions(struct svga_shader_emitter_v10
*emit
)
3361 struct tgsi_full_src_register tmp_clip_dist_src
;
3362 struct tgsi_full_dst_register clip_dist_dst
;
3365 unsigned clip_plane_enable
= emit
->key
.clip_plane_enable
;
3366 unsigned clip_dist_tmp_index
= emit
->clip_dist_tmp_index
;
3367 int num_written_clipdist
= emit
->info
.num_written_clipdistance
;
3369 assert(emit
->clip_dist_out_index
!= INVALID_INDEX
);
3370 assert(emit
->clip_dist_tmp_index
!= INVALID_INDEX
);
3373 * Temporary reset the temporary clip dist register index so
3374 * that the copy to the real clip dist register will not
3375 * attempt to copy to the temporary register again
3377 emit
->clip_dist_tmp_index
= INVALID_INDEX
;
3379 for (i
= 0; i
< 2 && num_written_clipdist
> 0; i
++, num_written_clipdist
-=4) {
3381 tmp_clip_dist_src
= make_src_temp_reg(clip_dist_tmp_index
+ i
);
3384 * copy to the shadow copy for use by varying variable and
3385 * stream output. All clip distances
3386 * will be written regardless of the enabled clipping planes.
3388 clip_dist_dst
= make_dst_reg(TGSI_FILE_OUTPUT
,
3389 emit
->clip_dist_so_index
+ i
);
3391 /* MOV clip_dist_so, tmp_clip_dist */
3392 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &clip_dist_dst
,
3393 &tmp_clip_dist_src
, FALSE
);
3396 * copy those clip distances to enabled clipping planes
3397 * to CLIPDIST registers for clipping
3399 if (clip_plane_enable
& 0xf) {
3400 clip_dist_dst
= make_dst_reg(TGSI_FILE_OUTPUT
,
3401 emit
->clip_dist_out_index
+ i
);
3402 clip_dist_dst
= writemask_dst(&clip_dist_dst
, clip_plane_enable
& 0xf);
3404 /* MOV CLIPDIST, tmp_clip_dist */
3405 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &clip_dist_dst
,
3406 &tmp_clip_dist_src
, FALSE
);
3408 /* four clip planes per clip register */
3409 clip_plane_enable
>>= 4;
3412 * set the temporary clip dist register index back to the
3413 * temporary index for the next vertex
3415 emit
->clip_dist_tmp_index
= clip_dist_tmp_index
;
3418 /* Declare clip distance output registers for user-defined clip planes
3419 * or the TGSI_CLIPVERTEX output.
3422 emit_clip_distance_declarations(struct svga_shader_emitter_v10
*emit
)
3424 unsigned num_clip_planes
= util_bitcount(emit
->key
.clip_plane_enable
);
3425 unsigned index
= emit
->num_outputs
;
3426 unsigned plane_mask
;
3428 assert(emit
->unit
== PIPE_SHADER_VERTEX
||
3429 emit
->unit
== PIPE_SHADER_GEOMETRY
);
3430 assert(num_clip_planes
<= 8);
3432 if (emit
->clip_mode
!= CLIP_LEGACY
&&
3433 emit
->clip_mode
!= CLIP_VERTEX
) {
3437 if (num_clip_planes
== 0)
3440 /* Declare one or two clip output registers. The number of components
3441 * in the mask reflects the number of clip planes. For example, if 5
3442 * clip planes are needed, we'll declare outputs similar to:
3443 * dcl_output_siv o2.xyzw, clip_distance
3444 * dcl_output_siv o3.x, clip_distance
3446 emit
->clip_dist_out_index
= index
; /* save the starting clip dist reg index */
3448 plane_mask
= (1 << num_clip_planes
) - 1;
3449 if (plane_mask
& 0xf) {
3450 unsigned cmask
= plane_mask
& VGPU10_OPERAND_4_COMPONENT_MASK_ALL
;
3451 emit_output_declaration(emit
, VGPU10_OPCODE_DCL_OUTPUT_SIV
, index
,
3452 VGPU10_NAME_CLIP_DISTANCE
, cmask
);
3453 emit
->num_outputs
++;
3455 if (plane_mask
& 0xf0) {
3456 unsigned cmask
= (plane_mask
>> 4) & VGPU10_OPERAND_4_COMPONENT_MASK_ALL
;
3457 emit_output_declaration(emit
, VGPU10_OPCODE_DCL_OUTPUT_SIV
, index
+ 1,
3458 VGPU10_NAME_CLIP_DISTANCE
, cmask
);
3459 emit
->num_outputs
++;
3465 * Emit the instructions for writing to the clip distance registers
3466 * to handle legacy/automatic clip planes.
3467 * For each clip plane, the distance is the dot product of the vertex
3468 * position (found in TEMP[vpos_tmp_index]) and the clip plane coefficients.
3469 * This is not used when the shader has an explicit CLIPVERTEX or CLIPDISTANCE
3470 * output registers already declared.
3473 emit_clip_distance_from_vpos(struct svga_shader_emitter_v10
*emit
,
3474 unsigned vpos_tmp_index
)
3476 unsigned i
, num_clip_planes
= util_bitcount(emit
->key
.clip_plane_enable
);
3478 assert(emit
->clip_mode
== CLIP_LEGACY
);
3479 assert(num_clip_planes
<= 8);
3481 assert(emit
->unit
== PIPE_SHADER_VERTEX
||
3482 emit
->unit
== PIPE_SHADER_GEOMETRY
);
3484 for (i
= 0; i
< num_clip_planes
; i
++) {
3485 struct tgsi_full_dst_register dst
;
3486 struct tgsi_full_src_register plane_src
, vpos_src
;
3487 unsigned reg_index
= emit
->clip_dist_out_index
+ i
/ 4;
3488 unsigned comp
= i
% 4;
3489 unsigned writemask
= VGPU10_OPERAND_4_COMPONENT_MASK_X
<< comp
;
3491 /* create dst, src regs */
3492 dst
= make_dst_reg(TGSI_FILE_OUTPUT
, reg_index
);
3493 dst
= writemask_dst(&dst
, writemask
);
3495 plane_src
= make_src_const_reg(emit
->clip_plane_const
[i
]);
3496 vpos_src
= make_src_temp_reg(vpos_tmp_index
);
3498 /* DP4 clip_dist, plane, vpos */
3499 emit_instruction_op2(emit
, VGPU10_OPCODE_DP4
, &dst
,
3500 &plane_src
, &vpos_src
, FALSE
);
3506 * Emit the instructions for computing the clip distance results from
3507 * the clip vertex temporary.
3508 * For each clip plane, the distance is the dot product of the clip vertex
3509 * position (found in a temp reg) and the clip plane coefficients.
3512 emit_clip_vertex_instructions(struct svga_shader_emitter_v10
*emit
)
3514 const unsigned num_clip
= util_bitcount(emit
->key
.clip_plane_enable
);
3516 struct tgsi_full_dst_register dst
;
3517 struct tgsi_full_src_register clipvert_src
;
3518 const unsigned clip_vertex_tmp
= emit
->clip_vertex_tmp_index
;
3520 assert(emit
->unit
== PIPE_SHADER_VERTEX
||
3521 emit
->unit
== PIPE_SHADER_GEOMETRY
);
3523 assert(emit
->clip_mode
== CLIP_VERTEX
);
3525 clipvert_src
= make_src_temp_reg(clip_vertex_tmp
);
3527 for (i
= 0; i
< num_clip
; i
++) {
3528 struct tgsi_full_src_register plane_src
;
3529 unsigned reg_index
= emit
->clip_dist_out_index
+ i
/ 4;
3530 unsigned comp
= i
% 4;
3531 unsigned writemask
= VGPU10_OPERAND_4_COMPONENT_MASK_X
<< comp
;
3533 /* create dst, src regs */
3534 dst
= make_dst_reg(TGSI_FILE_OUTPUT
, reg_index
);
3535 dst
= writemask_dst(&dst
, writemask
);
3537 plane_src
= make_src_const_reg(emit
->clip_plane_const
[i
]);
3539 /* DP4 clip_dist, plane, vpos */
3540 emit_instruction_op2(emit
, VGPU10_OPCODE_DP4
, &dst
,
3541 &plane_src
, &clipvert_src
, FALSE
);
3544 /* copy temporary clip vertex register to the clip vertex register */
3546 assert(emit
->clip_vertex_out_index
!= INVALID_INDEX
);
3549 * temporary reset the temporary clip vertex register index so
3550 * that copy to the clip vertex register will not attempt
3551 * to copy to the temporary register again
3553 emit
->clip_vertex_tmp_index
= INVALID_INDEX
;
3555 /* MOV clip_vertex, clip_vertex_tmp */
3556 dst
= make_dst_reg(TGSI_FILE_OUTPUT
, emit
->clip_vertex_out_index
);
3557 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
,
3558 &dst
, &clipvert_src
, FALSE
);
3561 * set the temporary clip vertex register index back to the
3562 * temporary index for the next vertex
3564 emit
->clip_vertex_tmp_index
= clip_vertex_tmp
;
3568 * Emit code to convert RGBA to BGRA
3571 emit_swap_r_b(struct svga_shader_emitter_v10
*emit
,
3572 const struct tgsi_full_dst_register
*dst
,
3573 const struct tgsi_full_src_register
*src
)
3575 struct tgsi_full_src_register bgra_src
=
3576 swizzle_src(src
, TGSI_SWIZZLE_Z
, TGSI_SWIZZLE_Y
, TGSI_SWIZZLE_X
, TGSI_SWIZZLE_W
);
3578 begin_emit_instruction(emit
);
3579 emit_opcode(emit
, VGPU10_OPCODE_MOV
, FALSE
);
3580 emit_dst_register(emit
, dst
);
3581 emit_src_register(emit
, &bgra_src
);
3582 end_emit_instruction(emit
);
3586 /** Convert from 10_10_10_2 normalized to 10_10_10_2_snorm */
3588 emit_puint_to_snorm(struct svga_shader_emitter_v10
*emit
,
3589 const struct tgsi_full_dst_register
*dst
,
3590 const struct tgsi_full_src_register
*src
)
3592 struct tgsi_full_src_register half
= make_immediate_reg_float(emit
, 0.5f
);
3593 struct tgsi_full_src_register two
=
3594 make_immediate_reg_float4(emit
, 2.0f
, 2.0f
, 2.0f
, 3.0f
);
3595 struct tgsi_full_src_register neg_two
=
3596 make_immediate_reg_float4(emit
, -2.0f
, -2.0f
, -2.0f
, -1.66666f
);
3598 unsigned val_tmp
= get_temp_index(emit
);
3599 struct tgsi_full_dst_register val_dst
= make_dst_temp_reg(val_tmp
);
3600 struct tgsi_full_src_register val_src
= make_src_temp_reg(val_tmp
);
3602 unsigned bias_tmp
= get_temp_index(emit
);
3603 struct tgsi_full_dst_register bias_dst
= make_dst_temp_reg(bias_tmp
);
3604 struct tgsi_full_src_register bias_src
= make_src_temp_reg(bias_tmp
);
3606 /* val = src * 2.0 */
3607 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &val_dst
,
3610 /* bias = src > 0.5 */
3611 emit_instruction_op2(emit
, VGPU10_OPCODE_GE
, &bias_dst
,
3614 /* bias = bias & -2.0 */
3615 emit_instruction_op2(emit
, VGPU10_OPCODE_AND
, &bias_dst
,
3616 &bias_src
, &neg_two
, FALSE
);
3618 /* dst = val + bias */
3619 emit_instruction_op2(emit
, VGPU10_OPCODE_ADD
, dst
,
3620 &val_src
, &bias_src
, FALSE
);
3622 free_temp_indexes(emit
);
3626 /** Convert from 10_10_10_2_unorm to 10_10_10_2_uscaled */
3628 emit_puint_to_uscaled(struct svga_shader_emitter_v10
*emit
,
3629 const struct tgsi_full_dst_register
*dst
,
3630 const struct tgsi_full_src_register
*src
)
3632 struct tgsi_full_src_register scale
=
3633 make_immediate_reg_float4(emit
, 1023.0f
, 1023.0f
, 1023.0f
, 3.0f
);
3635 /* dst = src * scale */
3636 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, dst
, src
, &scale
, FALSE
);
3640 /** Convert from R32_UINT to 10_10_10_2_sscaled */
3642 emit_puint_to_sscaled(struct svga_shader_emitter_v10
*emit
,
3643 const struct tgsi_full_dst_register
*dst
,
3644 const struct tgsi_full_src_register
*src
)
3646 struct tgsi_full_src_register lshift
=
3647 make_immediate_reg_int4(emit
, 22, 12, 2, 0);
3648 struct tgsi_full_src_register rshift
=
3649 make_immediate_reg_int4(emit
, 22, 22, 22, 30);
3651 struct tgsi_full_src_register src_xxxx
= scalar_src(src
, TGSI_SWIZZLE_X
);
3653 unsigned tmp
= get_temp_index(emit
);
3654 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
3655 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
3658 * r = (pixel << 22) >> 22; # signed int in [511, -512]
3659 * g = (pixel << 12) >> 22; # signed int in [511, -512]
3660 * b = (pixel << 2) >> 22; # signed int in [511, -512]
3661 * a = (pixel << 0) >> 30; # signed int in [1, -2]
3662 * dst = i_to_f(r,g,b,a); # convert to float
3664 emit_instruction_op2(emit
, VGPU10_OPCODE_ISHL
, &tmp_dst
,
3665 &src_xxxx
, &lshift
, FALSE
);
3666 emit_instruction_op2(emit
, VGPU10_OPCODE_ISHR
, &tmp_dst
,
3667 &tmp_src
, &rshift
, FALSE
);
3668 emit_instruction_op1(emit
, VGPU10_OPCODE_ITOF
, dst
, &tmp_src
, FALSE
);
3670 free_temp_indexes(emit
);
3675 * Emit code for TGSI_OPCODE_ARL or TGSI_OPCODE_UARL instruction.
3678 emit_arl_uarl(struct svga_shader_emitter_v10
*emit
,
3679 const struct tgsi_full_instruction
*inst
)
3681 unsigned index
= inst
->Dst
[0].Register
.Index
;
3682 struct tgsi_full_dst_register dst
;
3683 VGPU10_OPCODE_TYPE opcode
;
3685 assert(index
< MAX_VGPU10_ADDR_REGS
);
3686 dst
= make_dst_temp_reg(emit
->address_reg_index
[index
]);
3690 * FTOI address_tmp, s0
3694 * MOV address_tmp, s0
3696 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ARL
)
3697 opcode
= VGPU10_OPCODE_FTOI
;
3699 opcode
= VGPU10_OPCODE_MOV
;
3701 emit_instruction_op1(emit
, opcode
, &dst
, &inst
->Src
[0], FALSE
);
3708 * Emit code for TGSI_OPCODE_CAL instruction.
3711 emit_cal(struct svga_shader_emitter_v10
*emit
,
3712 const struct tgsi_full_instruction
*inst
)
3714 unsigned label
= inst
->Label
.Label
;
3715 VGPU10OperandToken0 operand
;
3717 operand
.operandType
= VGPU10_OPERAND_TYPE_LABEL
;
3719 begin_emit_instruction(emit
);
3720 emit_dword(emit
, operand
.value
);
3721 emit_dword(emit
, label
);
3722 end_emit_instruction(emit
);
3729 * Emit code for TGSI_OPCODE_IABS instruction.
3732 emit_iabs(struct svga_shader_emitter_v10
*emit
,
3733 const struct tgsi_full_instruction
*inst
)
3735 /* dst.x = (src0.x < 0) ? -src0.x : src0.x
3736 * dst.y = (src0.y < 0) ? -src0.y : src0.y
3737 * dst.z = (src0.z < 0) ? -src0.z : src0.z
3738 * dst.w = (src0.w < 0) ? -src0.w : src0.w
3741 * IMAX dst, src, neg(src)
3743 struct tgsi_full_src_register neg_src
= negate_src(&inst
->Src
[0]);
3744 emit_instruction_op2(emit
, VGPU10_OPCODE_IMAX
, &inst
->Dst
[0],
3745 &inst
->Src
[0], &neg_src
, FALSE
);
3752 * Emit code for TGSI_OPCODE_CMP instruction.
3755 emit_cmp(struct svga_shader_emitter_v10
*emit
,
3756 const struct tgsi_full_instruction
*inst
)
3758 /* dst.x = (src0.x < 0) ? src1.x : src2.x
3759 * dst.y = (src0.y < 0) ? src1.y : src2.y
3760 * dst.z = (src0.z < 0) ? src1.z : src2.z
3761 * dst.w = (src0.w < 0) ? src1.w : src2.w
3765 * MOVC dst, tmp, src1, src2
3767 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
3768 unsigned tmp
= get_temp_index(emit
);
3769 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
3770 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
3772 emit_instruction_op2(emit
, VGPU10_OPCODE_LT
, &tmp_dst
,
3773 &inst
->Src
[0], &zero
, FALSE
);
3774 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &inst
->Dst
[0],
3775 &tmp_src
, &inst
->Src
[1], &inst
->Src
[2],
3776 inst
->Instruction
.Saturate
);
3778 free_temp_indexes(emit
);
3785 * Emit code for TGSI_OPCODE_DST instruction.
3788 emit_dst(struct svga_shader_emitter_v10
*emit
,
3789 const struct tgsi_full_instruction
*inst
)
3793 * dst.y = src0.y * src1.y
3798 struct tgsi_full_src_register s0_yyyy
=
3799 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_Y
);
3800 struct tgsi_full_src_register s0_zzzz
=
3801 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_Z
);
3802 struct tgsi_full_src_register s1_yyyy
=
3803 scalar_src(&inst
->Src
[1], TGSI_SWIZZLE_Y
);
3804 struct tgsi_full_src_register s1_wwww
=
3805 scalar_src(&inst
->Src
[1], TGSI_SWIZZLE_W
);
3808 * If dst and either src0 and src1 are the same we need
3809 * to create a temporary for it and insert a extra move.
3811 unsigned tmp_move
= get_temp_index(emit
);
3812 struct tgsi_full_src_register move_src
= make_src_temp_reg(tmp_move
);
3813 struct tgsi_full_dst_register move_dst
= make_dst_temp_reg(tmp_move
);
3815 /* MOV dst.x, 1.0 */
3816 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_X
) {
3817 struct tgsi_full_dst_register dst_x
=
3818 writemask_dst(&move_dst
, TGSI_WRITEMASK_X
);
3819 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
3821 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_x
, &one
, FALSE
);
3824 /* MUL dst.y, s0.y, s1.y */
3825 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Y
) {
3826 struct tgsi_full_dst_register dst_y
=
3827 writemask_dst(&move_dst
, TGSI_WRITEMASK_Y
);
3829 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &dst_y
, &s0_yyyy
,
3830 &s1_yyyy
, inst
->Instruction
.Saturate
);
3833 /* MOV dst.z, s0.z */
3834 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Z
) {
3835 struct tgsi_full_dst_register dst_z
=
3836 writemask_dst(&move_dst
, TGSI_WRITEMASK_Z
);
3838 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_z
, &s0_zzzz
,
3839 inst
->Instruction
.Saturate
);
3842 /* MOV dst.w, s1.w */
3843 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_W
) {
3844 struct tgsi_full_dst_register dst_w
=
3845 writemask_dst(&move_dst
, TGSI_WRITEMASK_W
);
3847 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_w
, &s1_wwww
,
3848 inst
->Instruction
.Saturate
);
3851 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &inst
->Dst
[0], &move_src
,
3853 free_temp_indexes(emit
);
3861 * Emit code for TGSI_OPCODE_ENDPRIM (GS only)
3864 emit_endprim(struct svga_shader_emitter_v10
*emit
,
3865 const struct tgsi_full_instruction
*inst
)
3867 assert(emit
->unit
== PIPE_SHADER_GEOMETRY
);
3869 /* We can't use emit_simple() because the TGSI instruction has one
3870 * operand (vertex stream number) which we must ignore for VGPU10.
3872 begin_emit_instruction(emit
);
3873 emit_opcode(emit
, VGPU10_OPCODE_CUT
, FALSE
);
3874 end_emit_instruction(emit
);
3880 * Emit code for TGSI_OPCODE_EX2 (2^x) instruction.
3883 emit_ex2(struct svga_shader_emitter_v10
*emit
,
3884 const struct tgsi_full_instruction
*inst
)
3886 /* Note that TGSI_OPCODE_EX2 computes only one value from src.x
3887 * while VGPU10 computes four values.
3890 * dst.xyzw = 2.0 ^ src.x
3893 struct tgsi_full_src_register src_xxxx
=
3894 swizzle_src(&inst
->Src
[0], TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
,
3895 TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
);
3897 /* EXP tmp, s0.xxxx */
3898 emit_instruction_op1(emit
, VGPU10_OPCODE_EXP
, &inst
->Dst
[0], &src_xxxx
,
3899 inst
->Instruction
.Saturate
);
3906 * Emit code for TGSI_OPCODE_EXP instruction.
3909 emit_exp(struct svga_shader_emitter_v10
*emit
,
3910 const struct tgsi_full_instruction
*inst
)
3913 * dst.x = 2 ^ floor(s0.x)
3914 * dst.y = s0.x - floor(s0.x)
3919 struct tgsi_full_src_register src_xxxx
=
3920 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_X
);
3921 unsigned tmp
= get_temp_index(emit
);
3922 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
3923 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
3926 * If dst and src are the same we need to create
3927 * a temporary for it and insert a extra move.
3929 unsigned tmp_move
= get_temp_index(emit
);
3930 struct tgsi_full_src_register move_src
= make_src_temp_reg(tmp_move
);
3931 struct tgsi_full_dst_register move_dst
= make_dst_temp_reg(tmp_move
);
3933 /* only use X component of temp reg */
3934 tmp_dst
= writemask_dst(&tmp_dst
, TGSI_WRITEMASK_X
);
3935 tmp_src
= scalar_src(&tmp_src
, TGSI_SWIZZLE_X
);
3937 /* ROUND_NI tmp.x, s0.x */
3938 emit_instruction_op1(emit
, VGPU10_OPCODE_ROUND_NI
, &tmp_dst
,
3939 &src_xxxx
, FALSE
); /* round to -infinity */
3941 /* EXP dst.x, tmp.x */
3942 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_X
) {
3943 struct tgsi_full_dst_register dst_x
=
3944 writemask_dst(&move_dst
, TGSI_WRITEMASK_X
);
3946 emit_instruction_op1(emit
, VGPU10_OPCODE_EXP
, &dst_x
, &tmp_src
,
3947 inst
->Instruction
.Saturate
);
3950 /* ADD dst.y, s0.x, -tmp */
3951 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Y
) {
3952 struct tgsi_full_dst_register dst_y
=
3953 writemask_dst(&move_dst
, TGSI_WRITEMASK_Y
);
3954 struct tgsi_full_src_register neg_tmp_src
= negate_src(&tmp_src
);
3956 emit_instruction_op2(emit
, VGPU10_OPCODE_ADD
, &dst_y
, &src_xxxx
,
3957 &neg_tmp_src
, inst
->Instruction
.Saturate
);
3960 /* EXP dst.z, s0.x */
3961 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Z
) {
3962 struct tgsi_full_dst_register dst_z
=
3963 writemask_dst(&move_dst
, TGSI_WRITEMASK_Z
);
3965 emit_instruction_op1(emit
, VGPU10_OPCODE_EXP
, &dst_z
, &src_xxxx
,
3966 inst
->Instruction
.Saturate
);
3969 /* MOV dst.w, 1.0 */
3970 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_W
) {
3971 struct tgsi_full_dst_register dst_w
=
3972 writemask_dst(&move_dst
, TGSI_WRITEMASK_W
);
3973 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
3975 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_w
, &one
,
3979 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &inst
->Dst
[0], &move_src
,
3982 free_temp_indexes(emit
);
3989 * Emit code for TGSI_OPCODE_IF instruction.
3992 emit_if(struct svga_shader_emitter_v10
*emit
,
3993 const struct tgsi_full_instruction
*inst
)
3995 VGPU10OpcodeToken0 opcode0
;
3997 /* The src register should be a scalar */
3998 assert(inst
->Src
[0].Register
.SwizzleX
== inst
->Src
[0].Register
.SwizzleY
&&
3999 inst
->Src
[0].Register
.SwizzleX
== inst
->Src
[0].Register
.SwizzleZ
&&
4000 inst
->Src
[0].Register
.SwizzleX
== inst
->Src
[0].Register
.SwizzleW
);
4002 /* The only special thing here is that we need to set the
4003 * VGPU10_INSTRUCTION_TEST_NONZERO flag since we want to test if
4004 * src.x is non-zero.
4007 opcode0
.opcodeType
= VGPU10_OPCODE_IF
;
4008 opcode0
.testBoolean
= VGPU10_INSTRUCTION_TEST_NONZERO
;
4010 begin_emit_instruction(emit
);
4011 emit_dword(emit
, opcode0
.value
);
4012 emit_src_register(emit
, &inst
->Src
[0]);
4013 end_emit_instruction(emit
);
4020 * Emit code for TGSI_OPCODE_KILL_IF instruction (kill fragment if any of
4021 * the register components are negative).
4024 emit_kill_if(struct svga_shader_emitter_v10
*emit
,
4025 const struct tgsi_full_instruction
*inst
)
4027 unsigned tmp
= get_temp_index(emit
);
4028 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4029 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4031 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
4033 struct tgsi_full_dst_register tmp_dst_x
=
4034 writemask_dst(&tmp_dst
, TGSI_WRITEMASK_X
);
4035 struct tgsi_full_src_register tmp_src_xxxx
=
4036 scalar_src(&tmp_src
, TGSI_SWIZZLE_X
);
4038 /* tmp = src[0] < 0.0 */
4039 emit_instruction_op2(emit
, VGPU10_OPCODE_LT
, &tmp_dst
, &inst
->Src
[0],
4042 if (!same_swizzle_terms(&inst
->Src
[0])) {
4043 /* If the swizzle is not XXXX, YYYY, ZZZZ or WWWW we need to
4044 * logically OR the swizzle terms. Most uses of KILL_IF only
4045 * test one channel so it's good to avoid these extra steps.
4047 struct tgsi_full_src_register tmp_src_yyyy
=
4048 scalar_src(&tmp_src
, TGSI_SWIZZLE_Y
);
4049 struct tgsi_full_src_register tmp_src_zzzz
=
4050 scalar_src(&tmp_src
, TGSI_SWIZZLE_Z
);
4051 struct tgsi_full_src_register tmp_src_wwww
=
4052 scalar_src(&tmp_src
, TGSI_SWIZZLE_W
);
4054 emit_instruction_op2(emit
, VGPU10_OPCODE_OR
, &tmp_dst_x
, &tmp_src_xxxx
,
4055 &tmp_src_yyyy
, FALSE
);
4056 emit_instruction_op2(emit
, VGPU10_OPCODE_OR
, &tmp_dst_x
, &tmp_src_xxxx
,
4057 &tmp_src_zzzz
, FALSE
);
4058 emit_instruction_op2(emit
, VGPU10_OPCODE_OR
, &tmp_dst_x
, &tmp_src_xxxx
,
4059 &tmp_src_wwww
, FALSE
);
4062 begin_emit_instruction(emit
);
4063 emit_discard_opcode(emit
, TRUE
); /* discard if src0.x is non-zero */
4064 emit_src_register(emit
, &tmp_src_xxxx
);
4065 end_emit_instruction(emit
);
4067 free_temp_indexes(emit
);
4074 * Emit code for TGSI_OPCODE_KILL instruction (unconditional discard).
4077 emit_kill(struct svga_shader_emitter_v10
*emit
,
4078 const struct tgsi_full_instruction
*inst
)
4080 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
4082 /* DISCARD if 0.0 is zero */
4083 begin_emit_instruction(emit
);
4084 emit_discard_opcode(emit
, FALSE
);
4085 emit_src_register(emit
, &zero
);
4086 end_emit_instruction(emit
);
4093 * Emit code for TGSI_OPCODE_LG2 instruction.
4096 emit_lg2(struct svga_shader_emitter_v10
*emit
,
4097 const struct tgsi_full_instruction
*inst
)
4099 /* Note that TGSI_OPCODE_LG2 computes only one value from src.x
4100 * while VGPU10 computes four values.
4103 * dst.xyzw = log2(src.x)
4106 struct tgsi_full_src_register src_xxxx
=
4107 swizzle_src(&inst
->Src
[0], TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
,
4108 TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
);
4110 /* LOG tmp, s0.xxxx */
4111 emit_instruction_op1(emit
, VGPU10_OPCODE_LOG
, &inst
->Dst
[0], &src_xxxx
,
4112 inst
->Instruction
.Saturate
);
4119 * Emit code for TGSI_OPCODE_LIT instruction.
4122 emit_lit(struct svga_shader_emitter_v10
*emit
,
4123 const struct tgsi_full_instruction
*inst
)
4125 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
4128 * If dst and src are the same we need to create
4129 * a temporary for it and insert a extra move.
4131 unsigned tmp_move
= get_temp_index(emit
);
4132 struct tgsi_full_src_register move_src
= make_src_temp_reg(tmp_move
);
4133 struct tgsi_full_dst_register move_dst
= make_dst_temp_reg(tmp_move
);
4137 * dst.y = max(src.x, 0)
4138 * dst.z = (src.x > 0) ? max(src.y, 0)^{clamp(src.w, -128, 128))} : 0
4142 /* MOV dst.x, 1.0 */
4143 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_X
) {
4144 struct tgsi_full_dst_register dst_x
=
4145 writemask_dst(&move_dst
, TGSI_WRITEMASK_X
);
4146 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_x
, &one
, FALSE
);
4149 /* MOV dst.w, 1.0 */
4150 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_W
) {
4151 struct tgsi_full_dst_register dst_w
=
4152 writemask_dst(&move_dst
, TGSI_WRITEMASK_W
);
4153 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_w
, &one
, FALSE
);
4156 /* MAX dst.y, src.x, 0.0 */
4157 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Y
) {
4158 struct tgsi_full_dst_register dst_y
=
4159 writemask_dst(&move_dst
, TGSI_WRITEMASK_Y
);
4160 struct tgsi_full_src_register zero
=
4161 make_immediate_reg_float(emit
, 0.0f
);
4162 struct tgsi_full_src_register src_xxxx
=
4163 swizzle_src(&inst
->Src
[0], TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
,
4164 TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
);
4166 emit_instruction_op2(emit
, VGPU10_OPCODE_MAX
, &dst_y
, &src_xxxx
,
4167 &zero
, inst
->Instruction
.Saturate
);
4171 * tmp1 = clamp(src.w, -128, 128);
4172 * MAX tmp1, src.w, -128
4173 * MIN tmp1, tmp1, 128
4175 * tmp2 = max(tmp2, 0);
4176 * MAX tmp2, src.y, 0
4178 * tmp1 = pow(tmp2, tmp1);
4180 * MUL tmp1, tmp2, tmp1
4183 * tmp1 = (src.w == 0) ? 1 : tmp1;
4185 * MOVC tmp1, tmp2, 1.0, tmp1
4187 * dst.z = (0 < src.x) ? tmp1 : 0;
4189 * MOVC dst.z, tmp2, tmp1, 0.0
4191 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Z
) {
4192 struct tgsi_full_dst_register dst_z
=
4193 writemask_dst(&move_dst
, TGSI_WRITEMASK_Z
);
4195 unsigned tmp1
= get_temp_index(emit
);
4196 struct tgsi_full_src_register tmp1_src
= make_src_temp_reg(tmp1
);
4197 struct tgsi_full_dst_register tmp1_dst
= make_dst_temp_reg(tmp1
);
4198 unsigned tmp2
= get_temp_index(emit
);
4199 struct tgsi_full_src_register tmp2_src
= make_src_temp_reg(tmp2
);
4200 struct tgsi_full_dst_register tmp2_dst
= make_dst_temp_reg(tmp2
);
4202 struct tgsi_full_src_register src_xxxx
=
4203 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_X
);
4204 struct tgsi_full_src_register src_yyyy
=
4205 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_Y
);
4206 struct tgsi_full_src_register src_wwww
=
4207 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_W
);
4209 struct tgsi_full_src_register zero
=
4210 make_immediate_reg_float(emit
, 0.0f
);
4211 struct tgsi_full_src_register lowerbound
=
4212 make_immediate_reg_float(emit
, -128.0f
);
4213 struct tgsi_full_src_register upperbound
=
4214 make_immediate_reg_float(emit
, 128.0f
);
4216 emit_instruction_op2(emit
, VGPU10_OPCODE_MAX
, &tmp1_dst
, &src_wwww
,
4217 &lowerbound
, FALSE
);
4218 emit_instruction_op2(emit
, VGPU10_OPCODE_MIN
, &tmp1_dst
, &tmp1_src
,
4219 &upperbound
, FALSE
);
4220 emit_instruction_op2(emit
, VGPU10_OPCODE_MAX
, &tmp2_dst
, &src_yyyy
,
4223 /* POW tmp1, tmp2, tmp1 */
4224 /* LOG tmp2, tmp2 */
4225 emit_instruction_op1(emit
, VGPU10_OPCODE_LOG
, &tmp2_dst
, &tmp2_src
,
4228 /* MUL tmp1, tmp2, tmp1 */
4229 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &tmp1_dst
, &tmp2_src
,
4232 /* EXP tmp1, tmp1 */
4233 emit_instruction_op1(emit
, VGPU10_OPCODE_EXP
, &tmp1_dst
, &tmp1_src
,
4236 /* EQ tmp2, 0, src.w */
4237 emit_instruction_op2(emit
, VGPU10_OPCODE_EQ
, &tmp2_dst
, &zero
,
4239 /* MOVC tmp1.z, tmp2, tmp1, 1.0 */
4240 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &tmp1_dst
,
4241 &tmp2_src
, &one
, &tmp1_src
, FALSE
);
4243 /* LT tmp2, 0, src.x */
4244 emit_instruction_op2(emit
, VGPU10_OPCODE_LT
, &tmp2_dst
, &zero
,
4246 /* MOVC dst.z, tmp2, tmp1, 0.0 */
4247 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &dst_z
,
4248 &tmp2_src
, &tmp1_src
, &zero
, FALSE
);
4251 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &inst
->Dst
[0], &move_src
,
4253 free_temp_indexes(emit
);
4260 * Emit Level Of Detail Query (LODQ) instruction.
4263 emit_lodq(struct svga_shader_emitter_v10
*emit
,
4264 const struct tgsi_full_instruction
*inst
)
4266 const uint unit
= inst
->Src
[1].Register
.Index
;
4268 assert(emit
->version
>= 41);
4270 /* LOD dst, coord, resource, sampler */
4271 begin_emit_instruction(emit
);
4272 emit_opcode(emit
, VGPU10_OPCODE_LOD
, FALSE
);
4273 emit_dst_register(emit
, &inst
->Dst
[0]);
4274 emit_src_register(emit
, &inst
->Src
[0]); /* coord */
4275 emit_resource_register(emit
, unit
);
4276 emit_sampler_register(emit
, unit
);
4277 end_emit_instruction(emit
);
4284 * Emit code for TGSI_OPCODE_LOG instruction.
4287 emit_log(struct svga_shader_emitter_v10
*emit
,
4288 const struct tgsi_full_instruction
*inst
)
4291 * dst.x = floor(lg2(abs(s0.x)))
4292 * dst.y = abs(s0.x) / (2 ^ floor(lg2(abs(s0.x))))
4293 * dst.z = lg2(abs(s0.x))
4297 struct tgsi_full_src_register src_xxxx
=
4298 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_X
);
4299 unsigned tmp
= get_temp_index(emit
);
4300 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4301 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4302 struct tgsi_full_src_register abs_src_xxxx
= absolute_src(&src_xxxx
);
4304 /* only use X component of temp reg */
4305 tmp_dst
= writemask_dst(&tmp_dst
, TGSI_WRITEMASK_X
);
4306 tmp_src
= scalar_src(&tmp_src
, TGSI_SWIZZLE_X
);
4308 /* LOG tmp.x, abs(s0.x) */
4309 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_XYZ
) {
4310 emit_instruction_op1(emit
, VGPU10_OPCODE_LOG
, &tmp_dst
,
4311 &abs_src_xxxx
, FALSE
);
4314 /* MOV dst.z, tmp.x */
4315 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Z
) {
4316 struct tgsi_full_dst_register dst_z
=
4317 writemask_dst(&inst
->Dst
[0], TGSI_WRITEMASK_Z
);
4319 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_z
,
4320 &tmp_src
, inst
->Instruction
.Saturate
);
4323 /* FLR tmp.x, tmp.x */
4324 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_XY
) {
4325 emit_instruction_op1(emit
, VGPU10_OPCODE_ROUND_NI
, &tmp_dst
,
4329 /* MOV dst.x, tmp.x */
4330 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_X
) {
4331 struct tgsi_full_dst_register dst_x
=
4332 writemask_dst(&inst
->Dst
[0], TGSI_WRITEMASK_X
);
4334 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_x
, &tmp_src
,
4335 inst
->Instruction
.Saturate
);
4338 /* EXP tmp.x, tmp.x */
4339 /* DIV dst.y, abs(s0.x), tmp.x */
4340 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_Y
) {
4341 struct tgsi_full_dst_register dst_y
=
4342 writemask_dst(&inst
->Dst
[0], TGSI_WRITEMASK_Y
);
4344 emit_instruction_op1(emit
, VGPU10_OPCODE_EXP
, &tmp_dst
, &tmp_src
,
4346 emit_instruction_op2(emit
, VGPU10_OPCODE_DIV
, &dst_y
, &abs_src_xxxx
,
4347 &tmp_src
, inst
->Instruction
.Saturate
);
4350 /* MOV dst.w, 1.0 */
4351 if (inst
->Dst
[0].Register
.WriteMask
& TGSI_WRITEMASK_W
) {
4352 struct tgsi_full_dst_register dst_w
=
4353 writemask_dst(&inst
->Dst
[0], TGSI_WRITEMASK_W
);
4354 struct tgsi_full_src_register one
=
4355 make_immediate_reg_float(emit
, 1.0f
);
4357 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst_w
, &one
, FALSE
);
4360 free_temp_indexes(emit
);
4367 * Emit code for TGSI_OPCODE_LRP instruction.
4370 emit_lrp(struct svga_shader_emitter_v10
*emit
,
4371 const struct tgsi_full_instruction
*inst
)
4373 /* dst = LRP(s0, s1, s2):
4374 * dst = s0 * (s1 - s2) + s2
4376 * SUB tmp, s1, s2; tmp = s1 - s2
4377 * MAD dst, s0, tmp, s2; dst = s0 * t1 + s2
4379 unsigned tmp
= get_temp_index(emit
);
4380 struct tgsi_full_src_register src_tmp
= make_src_temp_reg(tmp
);
4381 struct tgsi_full_dst_register dst_tmp
= make_dst_temp_reg(tmp
);
4382 struct tgsi_full_src_register neg_src2
= negate_src(&inst
->Src
[2]);
4384 /* ADD tmp, s1, -s2 */
4385 emit_instruction_op2(emit
, VGPU10_OPCODE_ADD
, &dst_tmp
,
4386 &inst
->Src
[1], &neg_src2
, FALSE
);
4388 /* MAD dst, s1, tmp, s3 */
4389 emit_instruction_op3(emit
, VGPU10_OPCODE_MAD
, &inst
->Dst
[0],
4390 &inst
->Src
[0], &src_tmp
, &inst
->Src
[2],
4391 inst
->Instruction
.Saturate
);
4393 free_temp_indexes(emit
);
4400 * Emit code for TGSI_OPCODE_POW instruction.
4403 emit_pow(struct svga_shader_emitter_v10
*emit
,
4404 const struct tgsi_full_instruction
*inst
)
4406 /* Note that TGSI_OPCODE_POW computes only one value from src0.x and
4407 * src1.x while VGPU10 computes four values.
4409 * dst = POW(src0, src1):
4410 * dst.xyzw = src0.x ^ src1.x
4412 unsigned tmp
= get_temp_index(emit
);
4413 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4414 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4415 struct tgsi_full_src_register src0_xxxx
=
4416 swizzle_src(&inst
->Src
[0], TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
,
4417 TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
);
4418 struct tgsi_full_src_register src1_xxxx
=
4419 swizzle_src(&inst
->Src
[1], TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
,
4420 TGSI_SWIZZLE_X
, TGSI_SWIZZLE_X
);
4422 /* LOG tmp, s0.xxxx */
4423 emit_instruction_op1(emit
, VGPU10_OPCODE_LOG
, &tmp_dst
, &src0_xxxx
,
4426 /* MUL tmp, tmp, s1.xxxx */
4427 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &tmp_dst
, &tmp_src
,
4430 /* EXP tmp, s0.xxxx */
4431 emit_instruction_op1(emit
, VGPU10_OPCODE_EXP
, &inst
->Dst
[0],
4432 &tmp_src
, inst
->Instruction
.Saturate
);
4435 free_temp_indexes(emit
);
4442 * Emit code for TGSI_OPCODE_RCP (reciprocal) instruction.
4445 emit_rcp(struct svga_shader_emitter_v10
*emit
,
4446 const struct tgsi_full_instruction
*inst
)
4448 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
4450 unsigned tmp
= get_temp_index(emit
);
4451 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4452 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4454 struct tgsi_full_dst_register tmp_dst_x
=
4455 writemask_dst(&tmp_dst
, TGSI_WRITEMASK_X
);
4456 struct tgsi_full_src_register tmp_src_xxxx
=
4457 scalar_src(&tmp_src
, TGSI_SWIZZLE_X
);
4459 /* DIV tmp.x, 1.0, s0 */
4460 emit_instruction_op2(emit
, VGPU10_OPCODE_DIV
, &tmp_dst_x
, &one
,
4461 &inst
->Src
[0], FALSE
);
4463 /* MOV dst, tmp.xxxx */
4464 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &inst
->Dst
[0],
4465 &tmp_src_xxxx
, inst
->Instruction
.Saturate
);
4467 free_temp_indexes(emit
);
4474 * Emit code for TGSI_OPCODE_RSQ instruction.
4477 emit_rsq(struct svga_shader_emitter_v10
*emit
,
4478 const struct tgsi_full_instruction
*inst
)
4481 * dst.xyzw = 1 / sqrt(src.x)
4487 unsigned tmp
= get_temp_index(emit
);
4488 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4489 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4491 struct tgsi_full_dst_register tmp_dst_x
=
4492 writemask_dst(&tmp_dst
, TGSI_WRITEMASK_X
);
4493 struct tgsi_full_src_register tmp_src_xxxx
=
4494 scalar_src(&tmp_src
, TGSI_SWIZZLE_X
);
4496 /* RSQ tmp, src.x */
4497 emit_instruction_op1(emit
, VGPU10_OPCODE_RSQ
, &tmp_dst_x
,
4498 &inst
->Src
[0], FALSE
);
4500 /* MOV dst, tmp.xxxx */
4501 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &inst
->Dst
[0],
4502 &tmp_src_xxxx
, inst
->Instruction
.Saturate
);
4505 free_temp_indexes(emit
);
4512 * Emit code for TGSI_OPCODE_SEQ (Set Equal) instruction.
4515 emit_seq(struct svga_shader_emitter_v10
*emit
,
4516 const struct tgsi_full_instruction
*inst
)
4518 /* dst = SEQ(s0, s1):
4519 * dst = s0 == s1 ? 1.0 : 0.0 (per component)
4521 * EQ tmp, s0, s1; tmp = s0 == s1 : 0xffffffff : 0 (per comp)
4522 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
4524 unsigned tmp
= get_temp_index(emit
);
4525 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4526 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4527 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
4528 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
4530 /* EQ tmp, s0, s1 */
4531 emit_instruction_op2(emit
, VGPU10_OPCODE_EQ
, &tmp_dst
, &inst
->Src
[0],
4532 &inst
->Src
[1], FALSE
);
4534 /* MOVC dst, tmp, one, zero */
4535 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &inst
->Dst
[0], &tmp_src
,
4536 &one
, &zero
, FALSE
);
4538 free_temp_indexes(emit
);
4545 * Emit code for TGSI_OPCODE_SGE (Set Greater than or Equal) instruction.
4548 emit_sge(struct svga_shader_emitter_v10
*emit
,
4549 const struct tgsi_full_instruction
*inst
)
4551 /* dst = SGE(s0, s1):
4552 * dst = s0 >= s1 ? 1.0 : 0.0 (per component)
4554 * GE tmp, s0, s1; tmp = s0 >= s1 : 0xffffffff : 0 (per comp)
4555 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
4557 unsigned tmp
= get_temp_index(emit
);
4558 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4559 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4560 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
4561 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
4563 /* GE tmp, s0, s1 */
4564 emit_instruction_op2(emit
, VGPU10_OPCODE_GE
, &tmp_dst
, &inst
->Src
[0],
4565 &inst
->Src
[1], FALSE
);
4567 /* MOVC dst, tmp, one, zero */
4568 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &inst
->Dst
[0], &tmp_src
,
4569 &one
, &zero
, FALSE
);
4571 free_temp_indexes(emit
);
4578 * Emit code for TGSI_OPCODE_SGT (Set Greater than) instruction.
4581 emit_sgt(struct svga_shader_emitter_v10
*emit
,
4582 const struct tgsi_full_instruction
*inst
)
4584 /* dst = SGT(s0, s1):
4585 * dst = s0 > s1 ? 1.0 : 0.0 (per component)
4587 * LT tmp, s1, s0; tmp = s1 < s0 ? 0xffffffff : 0 (per comp)
4588 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
4590 unsigned tmp
= get_temp_index(emit
);
4591 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4592 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4593 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
4594 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
4596 /* LT tmp, s1, s0 */
4597 emit_instruction_op2(emit
, VGPU10_OPCODE_LT
, &tmp_dst
, &inst
->Src
[1],
4598 &inst
->Src
[0], FALSE
);
4600 /* MOVC dst, tmp, one, zero */
4601 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &inst
->Dst
[0], &tmp_src
,
4602 &one
, &zero
, FALSE
);
4604 free_temp_indexes(emit
);
4611 * Emit code for TGSI_OPCODE_SIN and TGSI_OPCODE_COS instructions.
4614 emit_sincos(struct svga_shader_emitter_v10
*emit
,
4615 const struct tgsi_full_instruction
*inst
)
4617 unsigned tmp
= get_temp_index(emit
);
4618 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4619 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4621 struct tgsi_full_src_register tmp_src_xxxx
=
4622 scalar_src(&tmp_src
, TGSI_SWIZZLE_X
);
4623 struct tgsi_full_dst_register tmp_dst_x
=
4624 writemask_dst(&tmp_dst
, TGSI_WRITEMASK_X
);
4626 begin_emit_instruction(emit
);
4627 emit_opcode(emit
, VGPU10_OPCODE_SINCOS
, FALSE
);
4629 if(inst
->Instruction
.Opcode
== TGSI_OPCODE_SIN
)
4631 emit_dst_register(emit
, &tmp_dst_x
); /* first destination register */
4632 emit_null_dst_register(emit
); /* second destination register */
4635 emit_null_dst_register(emit
);
4636 emit_dst_register(emit
, &tmp_dst_x
);
4639 emit_src_register(emit
, &inst
->Src
[0]);
4640 end_emit_instruction(emit
);
4642 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &inst
->Dst
[0],
4643 &tmp_src_xxxx
, inst
->Instruction
.Saturate
);
4645 free_temp_indexes(emit
);
4652 * Emit code for TGSI_OPCODE_SLE (Set Less than or Equal) instruction.
4655 emit_sle(struct svga_shader_emitter_v10
*emit
,
4656 const struct tgsi_full_instruction
*inst
)
4658 /* dst = SLE(s0, s1):
4659 * dst = s0 <= s1 ? 1.0 : 0.0 (per component)
4661 * GE tmp, s1, s0; tmp = s1 >= s0 : 0xffffffff : 0 (per comp)
4662 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
4664 unsigned tmp
= get_temp_index(emit
);
4665 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4666 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4667 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
4668 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
4670 /* GE tmp, s1, s0 */
4671 emit_instruction_op2(emit
, VGPU10_OPCODE_GE
, &tmp_dst
, &inst
->Src
[1],
4672 &inst
->Src
[0], FALSE
);
4674 /* MOVC dst, tmp, one, zero */
4675 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &inst
->Dst
[0], &tmp_src
,
4676 &one
, &zero
, FALSE
);
4678 free_temp_indexes(emit
);
4685 * Emit code for TGSI_OPCODE_SLT (Set Less than) instruction.
4688 emit_slt(struct svga_shader_emitter_v10
*emit
,
4689 const struct tgsi_full_instruction
*inst
)
4691 /* dst = SLT(s0, s1):
4692 * dst = s0 < s1 ? 1.0 : 0.0 (per component)
4694 * LT tmp, s0, s1; tmp = s0 < s1 ? 0xffffffff : 0 (per comp)
4695 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
4697 unsigned tmp
= get_temp_index(emit
);
4698 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4699 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4700 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
4701 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
4703 /* LT tmp, s0, s1 */
4704 emit_instruction_op2(emit
, VGPU10_OPCODE_LT
, &tmp_dst
, &inst
->Src
[0],
4705 &inst
->Src
[1], FALSE
);
4707 /* MOVC dst, tmp, one, zero */
4708 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &inst
->Dst
[0], &tmp_src
,
4709 &one
, &zero
, FALSE
);
4711 free_temp_indexes(emit
);
4718 * Emit code for TGSI_OPCODE_SNE (Set Not Equal) instruction.
4721 emit_sne(struct svga_shader_emitter_v10
*emit
,
4722 const struct tgsi_full_instruction
*inst
)
4724 /* dst = SNE(s0, s1):
4725 * dst = s0 != s1 ? 1.0 : 0.0 (per component)
4727 * EQ tmp, s0, s1; tmp = s0 == s1 : 0xffffffff : 0 (per comp)
4728 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
4730 unsigned tmp
= get_temp_index(emit
);
4731 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4732 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4733 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
4734 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
4736 /* NE tmp, s0, s1 */
4737 emit_instruction_op2(emit
, VGPU10_OPCODE_NE
, &tmp_dst
, &inst
->Src
[0],
4738 &inst
->Src
[1], FALSE
);
4740 /* MOVC dst, tmp, one, zero */
4741 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &inst
->Dst
[0], &tmp_src
,
4742 &one
, &zero
, FALSE
);
4744 free_temp_indexes(emit
);
4751 * Emit code for TGSI_OPCODE_SSG (Set Sign) instruction.
4754 emit_ssg(struct svga_shader_emitter_v10
*emit
,
4755 const struct tgsi_full_instruction
*inst
)
4757 /* dst.x = (src.x > 0.0) ? 1.0 : (src.x < 0.0) ? -1.0 : 0.0
4758 * dst.y = (src.y > 0.0) ? 1.0 : (src.y < 0.0) ? -1.0 : 0.0
4759 * dst.z = (src.z > 0.0) ? 1.0 : (src.z < 0.0) ? -1.0 : 0.0
4760 * dst.w = (src.w > 0.0) ? 1.0 : (src.w < 0.0) ? -1.0 : 0.0
4762 * LT tmp1, src, zero; tmp1 = src < zero ? 0xffffffff : 0 (per comp)
4763 * MOVC tmp2, tmp1, -1.0, 0.0; tmp2 = tmp1 ? -1.0 : 0.0 (per component)
4764 * LT tmp1, zero, src; tmp1 = zero < src ? 0xffffffff : 0 (per comp)
4765 * MOVC dst, tmp1, 1.0, tmp2; dst = tmp1 ? 1.0 : tmp2 (per component)
4767 struct tgsi_full_src_register zero
=
4768 make_immediate_reg_float(emit
, 0.0f
);
4769 struct tgsi_full_src_register one
=
4770 make_immediate_reg_float(emit
, 1.0f
);
4771 struct tgsi_full_src_register neg_one
=
4772 make_immediate_reg_float(emit
, -1.0f
);
4774 unsigned tmp1
= get_temp_index(emit
);
4775 struct tgsi_full_src_register tmp1_src
= make_src_temp_reg(tmp1
);
4776 struct tgsi_full_dst_register tmp1_dst
= make_dst_temp_reg(tmp1
);
4778 unsigned tmp2
= get_temp_index(emit
);
4779 struct tgsi_full_src_register tmp2_src
= make_src_temp_reg(tmp2
);
4780 struct tgsi_full_dst_register tmp2_dst
= make_dst_temp_reg(tmp2
);
4782 emit_instruction_op2(emit
, VGPU10_OPCODE_LT
, &tmp1_dst
, &inst
->Src
[0],
4784 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &tmp2_dst
, &tmp1_src
,
4785 &neg_one
, &zero
, FALSE
);
4786 emit_instruction_op2(emit
, VGPU10_OPCODE_LT
, &tmp1_dst
, &zero
,
4787 &inst
->Src
[0], FALSE
);
4788 emit_instruction_op3(emit
, VGPU10_OPCODE_MOVC
, &inst
->Dst
[0], &tmp1_src
,
4789 &one
, &tmp2_src
, FALSE
);
4791 free_temp_indexes(emit
);
4798 * Emit code for TGSI_OPCODE_ISSG (Integer Set Sign) instruction.
4801 emit_issg(struct svga_shader_emitter_v10
*emit
,
4802 const struct tgsi_full_instruction
*inst
)
4804 /* dst.x = (src.x > 0) ? 1 : (src.x < 0) ? -1 : 0
4805 * dst.y = (src.y > 0) ? 1 : (src.y < 0) ? -1 : 0
4806 * dst.z = (src.z > 0) ? 1 : (src.z < 0) ? -1 : 0
4807 * dst.w = (src.w > 0) ? 1 : (src.w < 0) ? -1 : 0
4809 * ILT tmp1, src, 0 tmp1 = src < 0 ? -1 : 0 (per component)
4810 * ILT tmp2, 0, src tmp2 = 0 < src ? -1 : 0 (per component)
4811 * IADD dst, tmp1, neg(tmp2) dst = tmp1 - tmp2 (per component)
4813 struct tgsi_full_src_register zero
= make_immediate_reg_float(emit
, 0.0f
);
4815 unsigned tmp1
= get_temp_index(emit
);
4816 struct tgsi_full_src_register tmp1_src
= make_src_temp_reg(tmp1
);
4817 struct tgsi_full_dst_register tmp1_dst
= make_dst_temp_reg(tmp1
);
4819 unsigned tmp2
= get_temp_index(emit
);
4820 struct tgsi_full_src_register tmp2_src
= make_src_temp_reg(tmp2
);
4821 struct tgsi_full_dst_register tmp2_dst
= make_dst_temp_reg(tmp2
);
4823 struct tgsi_full_src_register neg_tmp2
= negate_src(&tmp2_src
);
4825 emit_instruction_op2(emit
, VGPU10_OPCODE_ILT
, &tmp1_dst
,
4826 &inst
->Src
[0], &zero
, FALSE
);
4827 emit_instruction_op2(emit
, VGPU10_OPCODE_ILT
, &tmp2_dst
,
4828 &zero
, &inst
->Src
[0], FALSE
);
4829 emit_instruction_op2(emit
, VGPU10_OPCODE_IADD
, &inst
->Dst
[0],
4830 &tmp1_src
, &neg_tmp2
, FALSE
);
4832 free_temp_indexes(emit
);
4839 * Emit a comparison instruction. The dest register will get
4840 * 0 or ~0 values depending on the outcome of comparing src0 to src1.
4843 emit_comparison(struct svga_shader_emitter_v10
*emit
,
4845 const struct tgsi_full_dst_register
*dst
,
4846 const struct tgsi_full_src_register
*src0
,
4847 const struct tgsi_full_src_register
*src1
)
4849 struct tgsi_full_src_register immediate
;
4850 VGPU10OpcodeToken0 opcode0
;
4851 boolean swapSrc
= FALSE
;
4853 /* Sanity checks for svga vs. gallium enums */
4854 STATIC_ASSERT(SVGA3D_CMP_LESS
== (PIPE_FUNC_LESS
+ 1));
4855 STATIC_ASSERT(SVGA3D_CMP_GREATEREQUAL
== (PIPE_FUNC_GEQUAL
+ 1));
4860 case SVGA3D_CMP_NEVER
:
4861 immediate
= make_immediate_reg_int(emit
, 0);
4863 begin_emit_instruction(emit
);
4864 emit_dword(emit
, VGPU10_OPCODE_MOV
);
4865 emit_dst_register(emit
, dst
);
4866 emit_src_register(emit
, &immediate
);
4867 end_emit_instruction(emit
);
4869 case SVGA3D_CMP_ALWAYS
:
4870 immediate
= make_immediate_reg_int(emit
, -1);
4872 begin_emit_instruction(emit
);
4873 emit_dword(emit
, VGPU10_OPCODE_MOV
);
4874 emit_dst_register(emit
, dst
);
4875 emit_src_register(emit
, &immediate
);
4876 end_emit_instruction(emit
);
4878 case SVGA3D_CMP_LESS
:
4879 opcode0
.opcodeType
= VGPU10_OPCODE_LT
;
4881 case SVGA3D_CMP_EQUAL
:
4882 opcode0
.opcodeType
= VGPU10_OPCODE_EQ
;
4884 case SVGA3D_CMP_LESSEQUAL
:
4885 opcode0
.opcodeType
= VGPU10_OPCODE_GE
;
4888 case SVGA3D_CMP_GREATER
:
4889 opcode0
.opcodeType
= VGPU10_OPCODE_LT
;
4892 case SVGA3D_CMP_NOTEQUAL
:
4893 opcode0
.opcodeType
= VGPU10_OPCODE_NE
;
4895 case SVGA3D_CMP_GREATEREQUAL
:
4896 opcode0
.opcodeType
= VGPU10_OPCODE_GE
;
4899 assert(!"Unexpected comparison mode");
4900 opcode0
.opcodeType
= VGPU10_OPCODE_EQ
;
4903 begin_emit_instruction(emit
);
4904 emit_dword(emit
, opcode0
.value
);
4905 emit_dst_register(emit
, dst
);
4907 emit_src_register(emit
, src1
);
4908 emit_src_register(emit
, src0
);
4911 emit_src_register(emit
, src0
);
4912 emit_src_register(emit
, src1
);
4914 end_emit_instruction(emit
);
4919 * Get texel/address offsets for a texture instruction.
4922 get_texel_offsets(const struct svga_shader_emitter_v10
*emit
,
4923 const struct tgsi_full_instruction
*inst
, int offsets
[3])
4925 if (inst
->Texture
.NumOffsets
== 1) {
4926 /* According to OpenGL Shader Language spec the offsets are only
4927 * fetched from a previously-declared immediate/literal.
4929 const struct tgsi_texture_offset
*off
= inst
->TexOffsets
;
4930 const unsigned index
= off
[0].Index
;
4931 const unsigned swizzleX
= off
[0].SwizzleX
;
4932 const unsigned swizzleY
= off
[0].SwizzleY
;
4933 const unsigned swizzleZ
= off
[0].SwizzleZ
;
4934 const union tgsi_immediate_data
*imm
= emit
->immediates
[index
];
4936 assert(inst
->TexOffsets
[0].File
== TGSI_FILE_IMMEDIATE
);
4938 offsets
[0] = imm
[swizzleX
].Int
;
4939 offsets
[1] = imm
[swizzleY
].Int
;
4940 offsets
[2] = imm
[swizzleZ
].Int
;
4943 offsets
[0] = offsets
[1] = offsets
[2] = 0;
4949 * Set up the coordinate register for texture sampling.
4950 * When we're sampling from a RECT texture we have to scale the
4951 * unnormalized coordinate to a normalized coordinate.
4952 * We do that by multiplying the coordinate by an "extra" constant.
4953 * An alternative would be to use the RESINFO instruction to query the
4956 static struct tgsi_full_src_register
4957 setup_texcoord(struct svga_shader_emitter_v10
*emit
,
4959 const struct tgsi_full_src_register
*coord
)
4961 if (emit
->key
.tex
[unit
].unnormalized
) {
4962 unsigned scale_index
= emit
->texcoord_scale_index
[unit
];
4963 unsigned tmp
= get_temp_index(emit
);
4964 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
4965 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
4966 struct tgsi_full_src_register scale_src
= make_src_const_reg(scale_index
);
4968 if (emit
->key
.tex
[unit
].texel_bias
) {
4969 /* to fix texture coordinate rounding issue, 0.0001 offset is
4970 * been added. This fixes piglit test fbo-blit-scaled-linear. */
4971 struct tgsi_full_src_register offset
=
4972 make_immediate_reg_float(emit
, 0.0001f
);
4974 /* ADD tmp, coord, offset */
4975 emit_instruction_op2(emit
, VGPU10_OPCODE_ADD
, &tmp_dst
,
4976 coord
, &offset
, FALSE
);
4977 /* MUL tmp, tmp, scale */
4978 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &tmp_dst
,
4979 &tmp_src
, &scale_src
, FALSE
);
4982 /* MUL tmp, coord, const[] */
4983 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &tmp_dst
,
4984 coord
, &scale_src
, FALSE
);
4989 /* use texcoord as-is */
4996 * For SAMPLE_C instructions, emit the extra src register which indicates
4997 * the reference/comparision value.
5000 emit_tex_compare_refcoord(struct svga_shader_emitter_v10
*emit
,
5001 enum tgsi_texture_type target
,
5002 const struct tgsi_full_src_register
*coord
)
5004 struct tgsi_full_src_register coord_src_ref
;
5007 assert(tgsi_is_shadow_target(target
));
5009 component
= tgsi_util_get_shadow_ref_src_index(target
) % 4;
5010 assert(component
>= 0);
5012 coord_src_ref
= scalar_src(coord
, component
);
5014 emit_src_register(emit
, &coord_src_ref
);
5019 * Info for implementing texture swizzles.
5020 * The begin_tex_swizzle(), get_tex_swizzle_dst() and end_tex_swizzle()
5021 * functions use this to encapsulate the extra steps needed to perform
5022 * a texture swizzle, or shadow/depth comparisons.
5023 * The shadow/depth comparison is only done here if for the cases where
5024 * there's no VGPU10 opcode (like texture bias lookup w/ shadow compare).
5026 struct tex_swizzle_info
5029 boolean shadow_compare
;
5031 enum tgsi_texture_type texture_target
; /**< TGSI_TEXTURE_x */
5032 struct tgsi_full_src_register tmp_src
;
5033 struct tgsi_full_dst_register tmp_dst
;
5034 const struct tgsi_full_dst_register
*inst_dst
;
5035 const struct tgsi_full_src_register
*coord_src
;
5040 * Do setup for handling texture swizzles or shadow compares.
5041 * \param unit the texture unit
5042 * \param inst the TGSI texture instruction
5043 * \param shadow_compare do shadow/depth comparison?
5044 * \param swz returns the swizzle info
5047 begin_tex_swizzle(struct svga_shader_emitter_v10
*emit
,
5049 const struct tgsi_full_instruction
*inst
,
5050 boolean shadow_compare
,
5051 struct tex_swizzle_info
*swz
)
5053 swz
->swizzled
= (emit
->key
.tex
[unit
].swizzle_r
!= TGSI_SWIZZLE_X
||
5054 emit
->key
.tex
[unit
].swizzle_g
!= TGSI_SWIZZLE_Y
||
5055 emit
->key
.tex
[unit
].swizzle_b
!= TGSI_SWIZZLE_Z
||
5056 emit
->key
.tex
[unit
].swizzle_a
!= TGSI_SWIZZLE_W
);
5058 swz
->shadow_compare
= shadow_compare
;
5059 swz
->texture_target
= inst
->Texture
.Texture
;
5061 if (swz
->swizzled
|| shadow_compare
) {
5062 /* Allocate temp register for the result of the SAMPLE instruction
5063 * and the source of the MOV/compare/swizzle instructions.
5065 unsigned tmp
= get_temp_index(emit
);
5066 swz
->tmp_src
= make_src_temp_reg(tmp
);
5067 swz
->tmp_dst
= make_dst_temp_reg(tmp
);
5071 swz
->inst_dst
= &inst
->Dst
[0];
5072 swz
->coord_src
= &inst
->Src
[0];
5074 emit
->fs
.shadow_compare_units
|= shadow_compare
<< unit
;
5079 * Returns the register to put the SAMPLE instruction results into.
5080 * This will either be the original instruction dst reg (if no swizzle
5081 * and no shadow comparison) or a temporary reg if there is a swizzle.
5083 static const struct tgsi_full_dst_register
*
5084 get_tex_swizzle_dst(const struct tex_swizzle_info
*swz
)
5086 return (swz
->swizzled
|| swz
->shadow_compare
)
5087 ? &swz
->tmp_dst
: swz
->inst_dst
;
5092 * This emits the MOV instruction that actually implements a texture swizzle
5093 * and/or shadow comparison.
5096 end_tex_swizzle(struct svga_shader_emitter_v10
*emit
,
5097 const struct tex_swizzle_info
*swz
)
5099 if (swz
->shadow_compare
) {
5100 /* Emit extra instructions to compare the fetched texel value against
5101 * a texture coordinate component. The result of the comparison
5104 struct tgsi_full_src_register coord_src
;
5105 struct tgsi_full_src_register texel_src
=
5106 scalar_src(&swz
->tmp_src
, TGSI_SWIZZLE_X
);
5107 struct tgsi_full_src_register one
=
5108 make_immediate_reg_float(emit
, 1.0f
);
5109 /* convert gallium comparison func to SVGA comparison func */
5110 SVGA3dCmpFunc compare_func
= emit
->key
.tex
[swz
->unit
].compare_func
+ 1;
5112 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
5115 tgsi_util_get_shadow_ref_src_index(swz
->texture_target
) % 4;
5116 assert(component
>= 0);
5117 coord_src
= scalar_src(swz
->coord_src
, component
);
5119 /* COMPARE tmp, coord, texel */
5120 emit_comparison(emit
, compare_func
,
5121 &swz
->tmp_dst
, &coord_src
, &texel_src
);
5123 /* AND dest, tmp, {1.0} */
5124 begin_emit_instruction(emit
);
5125 emit_opcode(emit
, VGPU10_OPCODE_AND
, FALSE
);
5126 if (swz
->swizzled
) {
5127 emit_dst_register(emit
, &swz
->tmp_dst
);
5130 emit_dst_register(emit
, swz
->inst_dst
);
5132 emit_src_register(emit
, &swz
->tmp_src
);
5133 emit_src_register(emit
, &one
);
5134 end_emit_instruction(emit
);
5137 if (swz
->swizzled
) {
5138 unsigned swz_r
= emit
->key
.tex
[swz
->unit
].swizzle_r
;
5139 unsigned swz_g
= emit
->key
.tex
[swz
->unit
].swizzle_g
;
5140 unsigned swz_b
= emit
->key
.tex
[swz
->unit
].swizzle_b
;
5141 unsigned swz_a
= emit
->key
.tex
[swz
->unit
].swizzle_a
;
5142 unsigned writemask_0
= 0, writemask_1
= 0;
5143 boolean int_tex
= is_integer_type(emit
->sampler_return_type
[swz
->unit
]);
5145 /* Swizzle w/out zero/one terms */
5146 struct tgsi_full_src_register src_swizzled
=
5147 swizzle_src(&swz
->tmp_src
,
5148 swz_r
< PIPE_SWIZZLE_0
? swz_r
: PIPE_SWIZZLE_X
,
5149 swz_g
< PIPE_SWIZZLE_0
? swz_g
: PIPE_SWIZZLE_Y
,
5150 swz_b
< PIPE_SWIZZLE_0
? swz_b
: PIPE_SWIZZLE_Z
,
5151 swz_a
< PIPE_SWIZZLE_0
? swz_a
: PIPE_SWIZZLE_W
);
5153 /* MOV dst, color(tmp).<swizzle> */
5154 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
,
5155 swz
->inst_dst
, &src_swizzled
, FALSE
);
5157 /* handle swizzle zero terms */
5158 writemask_0
= (((swz_r
== PIPE_SWIZZLE_0
) << 0) |
5159 ((swz_g
== PIPE_SWIZZLE_0
) << 1) |
5160 ((swz_b
== PIPE_SWIZZLE_0
) << 2) |
5161 ((swz_a
== PIPE_SWIZZLE_0
) << 3));
5162 writemask_0
&= swz
->inst_dst
->Register
.WriteMask
;
5165 struct tgsi_full_src_register zero
= int_tex
?
5166 make_immediate_reg_int(emit
, 0) :
5167 make_immediate_reg_float(emit
, 0.0f
);
5168 struct tgsi_full_dst_register dst
=
5169 writemask_dst(swz
->inst_dst
, writemask_0
);
5171 /* MOV dst.writemask_0, {0,0,0,0} */
5172 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
,
5173 &dst
, &zero
, FALSE
);
5176 /* handle swizzle one terms */
5177 writemask_1
= (((swz_r
== PIPE_SWIZZLE_1
) << 0) |
5178 ((swz_g
== PIPE_SWIZZLE_1
) << 1) |
5179 ((swz_b
== PIPE_SWIZZLE_1
) << 2) |
5180 ((swz_a
== PIPE_SWIZZLE_1
) << 3));
5181 writemask_1
&= swz
->inst_dst
->Register
.WriteMask
;
5184 struct tgsi_full_src_register one
= int_tex
?
5185 make_immediate_reg_int(emit
, 1) :
5186 make_immediate_reg_float(emit
, 1.0f
);
5187 struct tgsi_full_dst_register dst
=
5188 writemask_dst(swz
->inst_dst
, writemask_1
);
5190 /* MOV dst.writemask_1, {1,1,1,1} */
5191 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &dst
, &one
, FALSE
);
5198 * Emit code for TGSI_OPCODE_SAMPLE instruction.
5201 emit_sample(struct svga_shader_emitter_v10
*emit
,
5202 const struct tgsi_full_instruction
*inst
)
5204 const unsigned resource_unit
= inst
->Src
[1].Register
.Index
;
5205 const unsigned sampler_unit
= inst
->Src
[2].Register
.Index
;
5206 struct tgsi_full_src_register coord
;
5208 struct tex_swizzle_info swz_info
;
5210 begin_tex_swizzle(emit
, sampler_unit
, inst
, FALSE
, &swz_info
);
5212 get_texel_offsets(emit
, inst
, offsets
);
5214 coord
= setup_texcoord(emit
, resource_unit
, &inst
->Src
[0]);
5216 /* SAMPLE dst, coord(s0), resource, sampler */
5217 begin_emit_instruction(emit
);
5219 /* NOTE: for non-fragment shaders, we should use VGPU10_OPCODE_SAMPLE_L
5220 * with LOD=0. But our virtual GPU accepts this as-is.
5222 emit_sample_opcode(emit
, VGPU10_OPCODE_SAMPLE
,
5223 inst
->Instruction
.Saturate
, offsets
);
5224 emit_dst_register(emit
, get_tex_swizzle_dst(&swz_info
));
5225 emit_src_register(emit
, &coord
);
5226 emit_resource_register(emit
, resource_unit
);
5227 emit_sampler_register(emit
, sampler_unit
);
5228 end_emit_instruction(emit
);
5230 end_tex_swizzle(emit
, &swz_info
);
5232 free_temp_indexes(emit
);
5239 * Check if a texture instruction is valid.
5240 * An example of an invalid texture instruction is doing shadow comparison
5241 * with an integer-valued texture.
5242 * If we detect an invalid texture instruction, we replace it with:
5243 * MOV dst, {1,1,1,1};
5244 * \return TRUE if valid, FALSE if invalid.
5247 is_valid_tex_instruction(struct svga_shader_emitter_v10
*emit
,
5248 const struct tgsi_full_instruction
*inst
)
5250 const unsigned unit
= inst
->Src
[1].Register
.Index
;
5251 const enum tgsi_texture_type target
= inst
->Texture
.Texture
;
5252 boolean valid
= TRUE
;
5254 if (tgsi_is_shadow_target(target
) &&
5255 is_integer_type(emit
->sampler_return_type
[unit
])) {
5256 debug_printf("Invalid SAMPLE_C with an integer texture!\n");
5259 /* XXX might check for other conditions in the future here */
5262 /* emit a MOV dst, {1,1,1,1} instruction. */
5263 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
5264 begin_emit_instruction(emit
);
5265 emit_opcode(emit
, VGPU10_OPCODE_MOV
, FALSE
);
5266 emit_dst_register(emit
, &inst
->Dst
[0]);
5267 emit_src_register(emit
, &one
);
5268 end_emit_instruction(emit
);
5276 * Emit code for TGSI_OPCODE_TEX (simple texture lookup)
5279 emit_tex(struct svga_shader_emitter_v10
*emit
,
5280 const struct tgsi_full_instruction
*inst
)
5282 const uint unit
= inst
->Src
[1].Register
.Index
;
5283 const enum tgsi_texture_type target
= inst
->Texture
.Texture
;
5284 VGPU10_OPCODE_TYPE opcode
;
5285 struct tgsi_full_src_register coord
;
5287 struct tex_swizzle_info swz_info
;
5289 /* check that the sampler returns a float */
5290 if (!is_valid_tex_instruction(emit
, inst
))
5293 begin_tex_swizzle(emit
, unit
, inst
, FALSE
, &swz_info
);
5295 get_texel_offsets(emit
, inst
, offsets
);
5297 coord
= setup_texcoord(emit
, unit
, &inst
->Src
[0]);
5299 /* SAMPLE dst, coord(s0), resource, sampler */
5300 begin_emit_instruction(emit
);
5302 if (tgsi_is_shadow_target(target
))
5303 opcode
= VGPU10_OPCODE_SAMPLE_C
;
5305 opcode
= VGPU10_OPCODE_SAMPLE
;
5307 emit_sample_opcode(emit
, opcode
, inst
->Instruction
.Saturate
, offsets
);
5308 emit_dst_register(emit
, get_tex_swizzle_dst(&swz_info
));
5309 emit_src_register(emit
, &coord
);
5310 emit_resource_register(emit
, unit
);
5311 emit_sampler_register(emit
, unit
);
5312 if (opcode
== VGPU10_OPCODE_SAMPLE_C
) {
5313 emit_tex_compare_refcoord(emit
, target
, &coord
);
5315 end_emit_instruction(emit
);
5317 end_tex_swizzle(emit
, &swz_info
);
5319 free_temp_indexes(emit
);
5325 * Emit code for TGSI_OPCODE_TG4 (texture lookup for texture gather)
5328 emit_tg4(struct svga_shader_emitter_v10
*emit
,
5329 const struct tgsi_full_instruction
*inst
)
5331 const uint unit
= inst
->Src
[2].Register
.Index
;
5332 struct tgsi_full_src_register src
;
5335 /* check that the sampler returns a float */
5336 if (!is_valid_tex_instruction(emit
, inst
))
5339 /* Only a single channel is supported in SM4_1 and we report
5340 * PIPE_CAP_MAX_TEXTURE_GATHER_COMPONENTS = 1.
5341 * Only the 0th component will be gathered.
5343 switch (emit
->key
.tex
[unit
].swizzle_r
) {
5344 case PIPE_SWIZZLE_X
:
5345 get_texel_offsets(emit
, inst
, offsets
);
5346 src
= setup_texcoord(emit
, unit
, &inst
->Src
[0]);
5348 /* Gather dst, coord, resource, sampler */
5349 begin_emit_instruction(emit
);
5350 emit_sample_opcode(emit
, VGPU10_OPCODE_GATHER4
,
5351 inst
->Instruction
.Saturate
, offsets
);
5352 emit_dst_register(emit
, &inst
->Dst
[0]);
5353 emit_src_register(emit
, &src
);
5354 emit_resource_register(emit
, unit
);
5355 emit_sampler_register(emit
, unit
);
5356 end_emit_instruction(emit
);
5358 case PIPE_SWIZZLE_W
:
5359 case PIPE_SWIZZLE_1
:
5360 src
= make_immediate_reg_float(emit
, 1.0);
5361 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
,
5362 &inst
->Dst
[0], &src
, FALSE
);
5364 case PIPE_SWIZZLE_Y
:
5365 case PIPE_SWIZZLE_Z
:
5366 case PIPE_SWIZZLE_0
:
5368 src
= make_immediate_reg_float(emit
, 0.0);
5369 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
,
5370 &inst
->Dst
[0], &src
, FALSE
);
5380 * Emit code for TGSI_OPCODE_TEX2 (texture lookup for shadow cube map arrays)
5383 emit_tex2(struct svga_shader_emitter_v10
*emit
,
5384 const struct tgsi_full_instruction
*inst
)
5386 const uint unit
= inst
->Src
[2].Register
.Index
;
5387 unsigned target
= inst
->Texture
.Texture
;
5388 struct tgsi_full_src_register coord
, ref
;
5390 struct tex_swizzle_info swz_info
;
5392 /* check that the sampler returns a float */
5393 if (!is_valid_tex_instruction(emit
, inst
))
5396 begin_tex_swizzle(emit
, unit
, inst
, FALSE
, &swz_info
);
5398 get_texel_offsets(emit
, inst
, offsets
);
5400 coord
= setup_texcoord(emit
, unit
, &inst
->Src
[0]);
5401 ref
= scalar_src(&inst
->Src
[1], TGSI_SWIZZLE_X
);
5403 /* SAMPLE_C dst, coord, resource, sampler, ref */
5404 begin_emit_instruction(emit
);
5405 emit_sample_opcode(emit
, VGPU10_OPCODE_SAMPLE_C
,
5406 inst
->Instruction
.Saturate
, offsets
);
5407 emit_dst_register(emit
, get_tex_swizzle_dst(&swz_info
));
5408 emit_src_register(emit
, &coord
);
5409 emit_resource_register(emit
, unit
);
5410 emit_sampler_register(emit
, unit
);
5411 emit_tex_compare_refcoord(emit
, target
, &ref
);
5412 end_emit_instruction(emit
);
5414 end_tex_swizzle(emit
, &swz_info
);
5416 free_temp_indexes(emit
);
5423 * Emit code for TGSI_OPCODE_TXP (projective texture)
5426 emit_txp(struct svga_shader_emitter_v10
*emit
,
5427 const struct tgsi_full_instruction
*inst
)
5429 const uint unit
= inst
->Src
[1].Register
.Index
;
5430 const enum tgsi_texture_type target
= inst
->Texture
.Texture
;
5431 VGPU10_OPCODE_TYPE opcode
;
5433 unsigned tmp
= get_temp_index(emit
);
5434 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
5435 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
5436 struct tgsi_full_src_register src0_wwww
=
5437 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_W
);
5438 struct tgsi_full_src_register coord
;
5439 struct tex_swizzle_info swz_info
;
5441 /* check that the sampler returns a float */
5442 if (!is_valid_tex_instruction(emit
, inst
))
5445 begin_tex_swizzle(emit
, unit
, inst
, FALSE
, &swz_info
);
5447 get_texel_offsets(emit
, inst
, offsets
);
5449 coord
= setup_texcoord(emit
, unit
, &inst
->Src
[0]);
5451 /* DIV tmp, coord, coord.wwww */
5452 emit_instruction_op2(emit
, VGPU10_OPCODE_DIV
, &tmp_dst
,
5453 &coord
, &src0_wwww
, FALSE
);
5455 /* SAMPLE dst, coord(tmp), resource, sampler */
5456 begin_emit_instruction(emit
);
5458 if (tgsi_is_shadow_target(target
))
5459 /* NOTE: for non-fragment shaders, we should use
5460 * VGPU10_OPCODE_SAMPLE_C_LZ, but our virtual GPU accepts this as-is.
5462 opcode
= VGPU10_OPCODE_SAMPLE_C
;
5464 opcode
= VGPU10_OPCODE_SAMPLE
;
5466 emit_sample_opcode(emit
, opcode
, inst
->Instruction
.Saturate
, offsets
);
5467 emit_dst_register(emit
, get_tex_swizzle_dst(&swz_info
));
5468 emit_src_register(emit
, &tmp_src
); /* projected coord */
5469 emit_resource_register(emit
, unit
);
5470 emit_sampler_register(emit
, unit
);
5471 if (opcode
== VGPU10_OPCODE_SAMPLE_C
) {
5472 emit_tex_compare_refcoord(emit
, target
, &tmp_src
);
5474 end_emit_instruction(emit
);
5476 end_tex_swizzle(emit
, &swz_info
);
5478 free_temp_indexes(emit
);
5485 * Emit code for TGSI_OPCODE_TXD (explicit derivatives)
5488 emit_txd(struct svga_shader_emitter_v10
*emit
,
5489 const struct tgsi_full_instruction
*inst
)
5491 const uint unit
= inst
->Src
[3].Register
.Index
;
5492 const enum tgsi_texture_type target
= inst
->Texture
.Texture
;
5494 struct tgsi_full_src_register coord
;
5495 struct tex_swizzle_info swz_info
;
5497 begin_tex_swizzle(emit
, unit
, inst
, tgsi_is_shadow_target(target
),
5500 get_texel_offsets(emit
, inst
, offsets
);
5502 coord
= setup_texcoord(emit
, unit
, &inst
->Src
[0]);
5504 /* SAMPLE_D dst, coord(s0), resource, sampler, Xderiv(s1), Yderiv(s2) */
5505 begin_emit_instruction(emit
);
5506 emit_sample_opcode(emit
, VGPU10_OPCODE_SAMPLE_D
,
5507 inst
->Instruction
.Saturate
, offsets
);
5508 emit_dst_register(emit
, get_tex_swizzle_dst(&swz_info
));
5509 emit_src_register(emit
, &coord
);
5510 emit_resource_register(emit
, unit
);
5511 emit_sampler_register(emit
, unit
);
5512 emit_src_register(emit
, &inst
->Src
[1]); /* Xderiv */
5513 emit_src_register(emit
, &inst
->Src
[2]); /* Yderiv */
5514 end_emit_instruction(emit
);
5516 end_tex_swizzle(emit
, &swz_info
);
5518 free_temp_indexes(emit
);
5525 * Emit code for TGSI_OPCODE_TXF (texel fetch)
5528 emit_txf(struct svga_shader_emitter_v10
*emit
,
5529 const struct tgsi_full_instruction
*inst
)
5531 const uint unit
= inst
->Src
[1].Register
.Index
;
5532 const boolean msaa
= tgsi_is_msaa_target(inst
->Texture
.Texture
);
5534 struct tex_swizzle_info swz_info
;
5536 begin_tex_swizzle(emit
, unit
, inst
, FALSE
, &swz_info
);
5538 get_texel_offsets(emit
, inst
, offsets
);
5541 /* Fetch one sample from an MSAA texture */
5542 struct tgsi_full_src_register sampleIndex
=
5543 scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_W
);
5544 /* LD_MS dst, coord(s0), resource, sampleIndex */
5545 begin_emit_instruction(emit
);
5546 emit_sample_opcode(emit
, VGPU10_OPCODE_LD_MS
,
5547 inst
->Instruction
.Saturate
, offsets
);
5548 emit_dst_register(emit
, get_tex_swizzle_dst(&swz_info
));
5549 emit_src_register(emit
, &inst
->Src
[0]);
5550 emit_resource_register(emit
, unit
);
5551 emit_src_register(emit
, &sampleIndex
);
5552 end_emit_instruction(emit
);
5555 /* Fetch one texel specified by integer coordinate */
5556 /* LD dst, coord(s0), resource */
5557 begin_emit_instruction(emit
);
5558 emit_sample_opcode(emit
, VGPU10_OPCODE_LD
,
5559 inst
->Instruction
.Saturate
, offsets
);
5560 emit_dst_register(emit
, get_tex_swizzle_dst(&swz_info
));
5561 emit_src_register(emit
, &inst
->Src
[0]);
5562 emit_resource_register(emit
, unit
);
5563 end_emit_instruction(emit
);
5566 end_tex_swizzle(emit
, &swz_info
);
5568 free_temp_indexes(emit
);
5575 * Emit code for TGSI_OPCODE_TXL (explicit LOD) or TGSI_OPCODE_TXB (LOD bias)
5576 * or TGSI_OPCODE_TXB2 (for cube shadow maps).
5579 emit_txl_txb(struct svga_shader_emitter_v10
*emit
,
5580 const struct tgsi_full_instruction
*inst
)
5582 const enum tgsi_texture_type target
= inst
->Texture
.Texture
;
5583 VGPU10_OPCODE_TYPE opcode
;
5586 struct tgsi_full_src_register coord
, lod_bias
;
5587 struct tex_swizzle_info swz_info
;
5589 assert(inst
->Instruction
.Opcode
== TGSI_OPCODE_TXL
||
5590 inst
->Instruction
.Opcode
== TGSI_OPCODE_TXB
||
5591 inst
->Instruction
.Opcode
== TGSI_OPCODE_TXB2
);
5593 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_TXB2
) {
5594 lod_bias
= scalar_src(&inst
->Src
[1], TGSI_SWIZZLE_X
);
5595 unit
= inst
->Src
[2].Register
.Index
;
5598 lod_bias
= scalar_src(&inst
->Src
[0], TGSI_SWIZZLE_W
);
5599 unit
= inst
->Src
[1].Register
.Index
;
5602 begin_tex_swizzle(emit
, unit
, inst
, tgsi_is_shadow_target(target
),
5605 get_texel_offsets(emit
, inst
, offsets
);
5607 coord
= setup_texcoord(emit
, unit
, &inst
->Src
[0]);
5609 /* SAMPLE_L/B dst, coord(s0), resource, sampler, lod(s3) */
5610 begin_emit_instruction(emit
);
5611 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_TXL
) {
5612 opcode
= VGPU10_OPCODE_SAMPLE_L
;
5615 opcode
= VGPU10_OPCODE_SAMPLE_B
;
5617 emit_sample_opcode(emit
, opcode
, inst
->Instruction
.Saturate
, offsets
);
5618 emit_dst_register(emit
, get_tex_swizzle_dst(&swz_info
));
5619 emit_src_register(emit
, &coord
);
5620 emit_resource_register(emit
, unit
);
5621 emit_sampler_register(emit
, unit
);
5622 emit_src_register(emit
, &lod_bias
);
5623 end_emit_instruction(emit
);
5625 end_tex_swizzle(emit
, &swz_info
);
5627 free_temp_indexes(emit
);
5634 * Emit code for TGSI_OPCODE_TXL2 (explicit LOD) for cubemap array.
5637 emit_txl2(struct svga_shader_emitter_v10
*emit
,
5638 const struct tgsi_full_instruction
*inst
)
5640 unsigned target
= inst
->Texture
.Texture
;
5641 unsigned opcode
, unit
;
5643 struct tgsi_full_src_register coord
, lod
;
5644 struct tex_swizzle_info swz_info
;
5646 assert(inst
->Instruction
.Opcode
== TGSI_OPCODE_TXL2
);
5648 lod
= scalar_src(&inst
->Src
[1], TGSI_SWIZZLE_X
);
5649 unit
= inst
->Src
[2].Register
.Index
;
5651 begin_tex_swizzle(emit
, unit
, inst
, tgsi_is_shadow_target(target
),
5654 get_texel_offsets(emit
, inst
, offsets
);
5656 coord
= setup_texcoord(emit
, unit
, &inst
->Src
[0]);
5658 /* SAMPLE_L dst, coord(s0), resource, sampler, lod(s3) */
5659 begin_emit_instruction(emit
);
5660 opcode
= VGPU10_OPCODE_SAMPLE_L
;
5661 emit_sample_opcode(emit
, opcode
, inst
->Instruction
.Saturate
, offsets
);
5662 emit_dst_register(emit
, get_tex_swizzle_dst(&swz_info
));
5663 emit_src_register(emit
, &coord
);
5664 emit_resource_register(emit
, unit
);
5665 emit_sampler_register(emit
, unit
);
5666 emit_src_register(emit
, &lod
);
5667 end_emit_instruction(emit
);
5669 end_tex_swizzle(emit
, &swz_info
);
5671 free_temp_indexes(emit
);
5678 * Emit code for TGSI_OPCODE_TXQ (texture query) instruction.
5681 emit_txq(struct svga_shader_emitter_v10
*emit
,
5682 const struct tgsi_full_instruction
*inst
)
5684 const uint unit
= inst
->Src
[1].Register
.Index
;
5686 if (emit
->sampler_target
[unit
] == TGSI_TEXTURE_BUFFER
) {
5687 /* RESINFO does not support querying texture buffers, so we instead
5688 * store texture buffer sizes in shader constants, then copy them to
5689 * implement TXQ instead of emitting RESINFO.
5690 * MOV dst, const[texture_buffer_size_index[unit]]
5692 struct tgsi_full_src_register size_src
=
5693 make_src_const_reg(emit
->texture_buffer_size_index
[unit
]);
5694 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &inst
->Dst
[0], &size_src
,
5697 /* RESINFO dst, srcMipLevel, resource */
5698 begin_emit_instruction(emit
);
5699 emit_opcode_resinfo(emit
, VGPU10_RESINFO_RETURN_UINT
);
5700 emit_dst_register(emit
, &inst
->Dst
[0]);
5701 emit_src_register(emit
, &inst
->Src
[0]);
5702 emit_resource_register(emit
, unit
);
5703 end_emit_instruction(emit
);
5706 free_temp_indexes(emit
);
5713 * Emit a simple instruction (like ADD, MUL, MIN, etc).
5716 emit_simple(struct svga_shader_emitter_v10
*emit
,
5717 const struct tgsi_full_instruction
*inst
)
5719 const enum tgsi_opcode opcode
= inst
->Instruction
.Opcode
;
5720 const struct tgsi_opcode_info
*op
= tgsi_get_opcode_info(opcode
);
5723 begin_emit_instruction(emit
);
5724 emit_opcode(emit
, translate_opcode(opcode
), inst
->Instruction
.Saturate
);
5725 for (i
= 0; i
< op
->num_dst
; i
++) {
5726 emit_dst_register(emit
, &inst
->Dst
[i
]);
5728 for (i
= 0; i
< op
->num_src
; i
++) {
5729 emit_src_register(emit
, &inst
->Src
[i
]);
5731 end_emit_instruction(emit
);
5738 * We only special case the MOV instruction to try to detect constant
5739 * color writes in the fragment shader.
5742 emit_mov(struct svga_shader_emitter_v10
*emit
,
5743 const struct tgsi_full_instruction
*inst
)
5745 const struct tgsi_full_src_register
*src
= &inst
->Src
[0];
5746 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[0];
5748 if (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
5749 dst
->Register
.File
== TGSI_FILE_OUTPUT
&&
5750 dst
->Register
.Index
== 0 &&
5751 src
->Register
.File
== TGSI_FILE_CONSTANT
&&
5752 !src
->Register
.Indirect
) {
5753 emit
->constant_color_output
= TRUE
;
5756 return emit_simple(emit
, inst
);
5761 * Emit a simple VGPU10 instruction which writes to multiple dest registers,
5762 * where TGSI only uses one dest register.
5765 emit_simple_1dst(struct svga_shader_emitter_v10
*emit
,
5766 const struct tgsi_full_instruction
*inst
,
5770 const enum tgsi_opcode opcode
= inst
->Instruction
.Opcode
;
5771 const struct tgsi_opcode_info
*op
= tgsi_get_opcode_info(opcode
);
5774 begin_emit_instruction(emit
);
5775 emit_opcode(emit
, translate_opcode(opcode
), inst
->Instruction
.Saturate
);
5777 for (i
= 0; i
< dst_count
; i
++) {
5778 if (i
== dst_index
) {
5779 emit_dst_register(emit
, &inst
->Dst
[0]);
5781 emit_null_dst_register(emit
);
5785 for (i
= 0; i
< op
->num_src
; i
++) {
5786 emit_src_register(emit
, &inst
->Src
[i
]);
5788 end_emit_instruction(emit
);
5795 * Translate a single TGSI instruction to VGPU10.
5798 emit_vgpu10_instruction(struct svga_shader_emitter_v10
*emit
,
5799 unsigned inst_number
,
5800 const struct tgsi_full_instruction
*inst
)
5802 const enum tgsi_opcode opcode
= inst
->Instruction
.Opcode
;
5805 case TGSI_OPCODE_ADD
:
5806 case TGSI_OPCODE_AND
:
5807 case TGSI_OPCODE_BGNLOOP
:
5808 case TGSI_OPCODE_BRK
:
5809 case TGSI_OPCODE_CEIL
:
5810 case TGSI_OPCODE_CONT
:
5811 case TGSI_OPCODE_DDX
:
5812 case TGSI_OPCODE_DDY
:
5813 case TGSI_OPCODE_DIV
:
5814 case TGSI_OPCODE_DP2
:
5815 case TGSI_OPCODE_DP3
:
5816 case TGSI_OPCODE_DP4
:
5817 case TGSI_OPCODE_ELSE
:
5818 case TGSI_OPCODE_ENDIF
:
5819 case TGSI_OPCODE_ENDLOOP
:
5820 case TGSI_OPCODE_ENDSUB
:
5821 case TGSI_OPCODE_F2I
:
5822 case TGSI_OPCODE_F2U
:
5823 case TGSI_OPCODE_FLR
:
5824 case TGSI_OPCODE_FRC
:
5825 case TGSI_OPCODE_FSEQ
:
5826 case TGSI_OPCODE_FSGE
:
5827 case TGSI_OPCODE_FSLT
:
5828 case TGSI_OPCODE_FSNE
:
5829 case TGSI_OPCODE_I2F
:
5830 case TGSI_OPCODE_IMAX
:
5831 case TGSI_OPCODE_IMIN
:
5832 case TGSI_OPCODE_INEG
:
5833 case TGSI_OPCODE_ISGE
:
5834 case TGSI_OPCODE_ISHR
:
5835 case TGSI_OPCODE_ISLT
:
5836 case TGSI_OPCODE_MAD
:
5837 case TGSI_OPCODE_MAX
:
5838 case TGSI_OPCODE_MIN
:
5839 case TGSI_OPCODE_MUL
:
5840 case TGSI_OPCODE_NOP
:
5841 case TGSI_OPCODE_NOT
:
5842 case TGSI_OPCODE_OR
:
5843 case TGSI_OPCODE_RET
:
5844 case TGSI_OPCODE_UADD
:
5845 case TGSI_OPCODE_USEQ
:
5846 case TGSI_OPCODE_USGE
:
5847 case TGSI_OPCODE_USLT
:
5848 case TGSI_OPCODE_UMIN
:
5849 case TGSI_OPCODE_UMAD
:
5850 case TGSI_OPCODE_UMAX
:
5851 case TGSI_OPCODE_ROUND
:
5852 case TGSI_OPCODE_SQRT
:
5853 case TGSI_OPCODE_SHL
:
5854 case TGSI_OPCODE_TRUNC
:
5855 case TGSI_OPCODE_U2F
:
5856 case TGSI_OPCODE_UCMP
:
5857 case TGSI_OPCODE_USHR
:
5858 case TGSI_OPCODE_USNE
:
5859 case TGSI_OPCODE_XOR
:
5860 /* simple instructions */
5861 return emit_simple(emit
, inst
);
5863 case TGSI_OPCODE_MOV
:
5864 return emit_mov(emit
, inst
);
5865 case TGSI_OPCODE_EMIT
:
5866 return emit_vertex(emit
, inst
);
5867 case TGSI_OPCODE_ENDPRIM
:
5868 return emit_endprim(emit
, inst
);
5869 case TGSI_OPCODE_IABS
:
5870 return emit_iabs(emit
, inst
);
5871 case TGSI_OPCODE_ARL
:
5873 case TGSI_OPCODE_UARL
:
5874 return emit_arl_uarl(emit
, inst
);
5875 case TGSI_OPCODE_BGNSUB
:
5878 case TGSI_OPCODE_CAL
:
5879 return emit_cal(emit
, inst
);
5880 case TGSI_OPCODE_CMP
:
5881 return emit_cmp(emit
, inst
);
5882 case TGSI_OPCODE_COS
:
5883 return emit_sincos(emit
, inst
);
5884 case TGSI_OPCODE_DST
:
5885 return emit_dst(emit
, inst
);
5886 case TGSI_OPCODE_EX2
:
5887 return emit_ex2(emit
, inst
);
5888 case TGSI_OPCODE_EXP
:
5889 return emit_exp(emit
, inst
);
5890 case TGSI_OPCODE_IF
:
5891 return emit_if(emit
, inst
);
5892 case TGSI_OPCODE_KILL
:
5893 return emit_kill(emit
, inst
);
5894 case TGSI_OPCODE_KILL_IF
:
5895 return emit_kill_if(emit
, inst
);
5896 case TGSI_OPCODE_LG2
:
5897 return emit_lg2(emit
, inst
);
5898 case TGSI_OPCODE_LIT
:
5899 return emit_lit(emit
, inst
);
5900 case TGSI_OPCODE_LODQ
:
5901 return emit_lodq(emit
, inst
);
5902 case TGSI_OPCODE_LOG
:
5903 return emit_log(emit
, inst
);
5904 case TGSI_OPCODE_LRP
:
5905 return emit_lrp(emit
, inst
);
5906 case TGSI_OPCODE_POW
:
5907 return emit_pow(emit
, inst
);
5908 case TGSI_OPCODE_RCP
:
5909 return emit_rcp(emit
, inst
);
5910 case TGSI_OPCODE_RSQ
:
5911 return emit_rsq(emit
, inst
);
5912 case TGSI_OPCODE_SAMPLE
:
5913 return emit_sample(emit
, inst
);
5914 case TGSI_OPCODE_SEQ
:
5915 return emit_seq(emit
, inst
);
5916 case TGSI_OPCODE_SGE
:
5917 return emit_sge(emit
, inst
);
5918 case TGSI_OPCODE_SGT
:
5919 return emit_sgt(emit
, inst
);
5920 case TGSI_OPCODE_SIN
:
5921 return emit_sincos(emit
, inst
);
5922 case TGSI_OPCODE_SLE
:
5923 return emit_sle(emit
, inst
);
5924 case TGSI_OPCODE_SLT
:
5925 return emit_slt(emit
, inst
);
5926 case TGSI_OPCODE_SNE
:
5927 return emit_sne(emit
, inst
);
5928 case TGSI_OPCODE_SSG
:
5929 return emit_ssg(emit
, inst
);
5930 case TGSI_OPCODE_ISSG
:
5931 return emit_issg(emit
, inst
);
5932 case TGSI_OPCODE_TEX
:
5933 return emit_tex(emit
, inst
);
5934 case TGSI_OPCODE_TG4
:
5935 return emit_tg4(emit
, inst
);
5936 case TGSI_OPCODE_TEX2
:
5937 return emit_tex2(emit
, inst
);
5938 case TGSI_OPCODE_TXP
:
5939 return emit_txp(emit
, inst
);
5940 case TGSI_OPCODE_TXB
:
5941 case TGSI_OPCODE_TXB2
:
5942 case TGSI_OPCODE_TXL
:
5943 return emit_txl_txb(emit
, inst
);
5944 case TGSI_OPCODE_TXD
:
5945 return emit_txd(emit
, inst
);
5946 case TGSI_OPCODE_TXF
:
5947 return emit_txf(emit
, inst
);
5948 case TGSI_OPCODE_TXL2
:
5949 return emit_txl2(emit
, inst
);
5950 case TGSI_OPCODE_TXQ
:
5951 return emit_txq(emit
, inst
);
5952 case TGSI_OPCODE_UIF
:
5953 return emit_if(emit
, inst
);
5954 case TGSI_OPCODE_UMUL_HI
:
5955 case TGSI_OPCODE_IMUL_HI
:
5956 case TGSI_OPCODE_UDIV
:
5957 case TGSI_OPCODE_IDIV
:
5958 /* These cases use only the FIRST of two destination registers */
5959 return emit_simple_1dst(emit
, inst
, 2, 0);
5960 case TGSI_OPCODE_UMUL
:
5961 case TGSI_OPCODE_UMOD
:
5962 case TGSI_OPCODE_MOD
:
5963 /* These cases use only the SECOND of two destination registers */
5964 return emit_simple_1dst(emit
, inst
, 2, 1);
5965 case TGSI_OPCODE_END
:
5966 if (!emit_post_helpers(emit
))
5968 return emit_simple(emit
, inst
);
5971 debug_printf("Unimplemented tgsi instruction %s\n",
5972 tgsi_get_opcode_name(opcode
));
5981 * Emit the extra instructions to adjust the vertex position.
5982 * There are two possible adjustments:
5983 * 1. Converting from Gallium to VGPU10 coordinate space by applying the
5984 * "prescale" and "pretranslate" values.
5985 * 2. Undoing the viewport transformation when we use the swtnl/draw path.
5986 * \param vs_pos_tmp_index which temporary register contains the vertex pos.
5989 emit_vpos_instructions(struct svga_shader_emitter_v10
*emit
,
5990 unsigned vs_pos_tmp_index
)
5992 struct tgsi_full_src_register tmp_pos_src
;
5993 struct tgsi_full_dst_register pos_dst
;
5995 /* Don't bother to emit any extra vertex instructions if vertex position is
5998 if (emit
->vposition
.out_index
== INVALID_INDEX
)
6001 tmp_pos_src
= make_src_temp_reg(vs_pos_tmp_index
);
6002 pos_dst
= make_dst_output_reg(emit
->vposition
.out_index
);
6004 /* If non-adjusted vertex position register index
6005 * is valid, copy the vertex position from the temporary
6006 * vertex position register before it is modified by the
6007 * prescale computation.
6009 if (emit
->vposition
.so_index
!= INVALID_INDEX
) {
6010 struct tgsi_full_dst_register pos_so_dst
=
6011 make_dst_output_reg(emit
->vposition
.so_index
);
6013 /* MOV pos_so, tmp_pos */
6014 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &pos_so_dst
,
6015 &tmp_pos_src
, FALSE
);
6018 if (emit
->vposition
.need_prescale
) {
6019 /* This code adjusts the vertex position to match the VGPU10 convention.
6020 * If p is the position computed by the shader (usually by applying the
6021 * modelview and projection matrices), the new position q is computed by:
6023 * q.x = p.w * trans.x + p.x * scale.x
6024 * q.y = p.w * trans.y + p.y * scale.y
6025 * q.z = p.w * trans.z + p.z * scale.z;
6026 * q.w = p.w * trans.w + p.w;
6028 struct tgsi_full_src_register tmp_pos_src_w
=
6029 scalar_src(&tmp_pos_src
, TGSI_SWIZZLE_W
);
6030 struct tgsi_full_dst_register tmp_pos_dst
=
6031 make_dst_temp_reg(vs_pos_tmp_index
);
6032 struct tgsi_full_dst_register tmp_pos_dst_xyz
=
6033 writemask_dst(&tmp_pos_dst
, TGSI_WRITEMASK_XYZ
);
6035 struct tgsi_full_src_register prescale_scale
=
6036 make_src_const_reg(emit
->vposition
.prescale_scale_index
);
6037 struct tgsi_full_src_register prescale_trans
=
6038 make_src_const_reg(emit
->vposition
.prescale_trans_index
);
6040 /* MUL tmp_pos.xyz, tmp_pos, prescale.scale */
6041 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &tmp_pos_dst_xyz
,
6042 &tmp_pos_src
, &prescale_scale
, FALSE
);
6044 /* MAD pos, tmp_pos.wwww, prescale.trans, tmp_pos */
6045 emit_instruction_op3(emit
, VGPU10_OPCODE_MAD
, &pos_dst
, &tmp_pos_src_w
,
6046 &prescale_trans
, &tmp_pos_src
, FALSE
);
6048 else if (emit
->key
.vs
.undo_viewport
) {
6049 /* This code computes the final vertex position from the temporary
6050 * vertex position by undoing the viewport transformation and the
6051 * divide-by-W operation (we convert window coords back to clip coords).
6052 * This is needed when we use the 'draw' module for fallbacks.
6053 * If p is the temp pos in window coords, then the NDC coord q is:
6054 * q.x = (p.x - vp.x_trans) / vp.x_scale * p.w
6055 * q.y = (p.y - vp.y_trans) / vp.y_scale * p.w
6058 * CONST[vs_viewport_index] contains:
6059 * { 1/vp.x_scale, 1/vp.y_scale, -vp.x_trans, -vp.y_trans }
6061 struct tgsi_full_dst_register tmp_pos_dst
=
6062 make_dst_temp_reg(vs_pos_tmp_index
);
6063 struct tgsi_full_dst_register tmp_pos_dst_xy
=
6064 writemask_dst(&tmp_pos_dst
, TGSI_WRITEMASK_XY
);
6065 struct tgsi_full_src_register tmp_pos_src_wwww
=
6066 scalar_src(&tmp_pos_src
, TGSI_SWIZZLE_W
);
6068 struct tgsi_full_dst_register pos_dst_xyz
=
6069 writemask_dst(&pos_dst
, TGSI_WRITEMASK_XYZ
);
6070 struct tgsi_full_dst_register pos_dst_w
=
6071 writemask_dst(&pos_dst
, TGSI_WRITEMASK_W
);
6073 struct tgsi_full_src_register vp_xyzw
=
6074 make_src_const_reg(emit
->vs
.viewport_index
);
6075 struct tgsi_full_src_register vp_zwww
=
6076 swizzle_src(&vp_xyzw
, TGSI_SWIZZLE_Z
, TGSI_SWIZZLE_W
,
6077 TGSI_SWIZZLE_W
, TGSI_SWIZZLE_W
);
6079 /* ADD tmp_pos.xy, tmp_pos.xy, viewport.zwww */
6080 emit_instruction_op2(emit
, VGPU10_OPCODE_ADD
, &tmp_pos_dst_xy
,
6081 &tmp_pos_src
, &vp_zwww
, FALSE
);
6083 /* MUL tmp_pos.xy, tmp_pos.xyzw, viewport.xyzy */
6084 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &tmp_pos_dst_xy
,
6085 &tmp_pos_src
, &vp_xyzw
, FALSE
);
6087 /* MUL pos.xyz, tmp_pos.xyz, tmp_pos.www */
6088 emit_instruction_op2(emit
, VGPU10_OPCODE_MUL
, &pos_dst_xyz
,
6089 &tmp_pos_src
, &tmp_pos_src_wwww
, FALSE
);
6091 /* MOV pos.w, tmp_pos.w */
6092 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &pos_dst_w
,
6093 &tmp_pos_src
, FALSE
);
6095 else if (vs_pos_tmp_index
!= INVALID_INDEX
) {
6096 /* This code is to handle the case where the temporary vertex
6097 * position register is created when the vertex shader has stream
6098 * output and prescale is disabled because rasterization is to be
6101 struct tgsi_full_dst_register pos_dst
=
6102 make_dst_output_reg(emit
->vposition
.out_index
);
6104 /* MOV pos, tmp_pos */
6105 begin_emit_instruction(emit
);
6106 emit_opcode(emit
, VGPU10_OPCODE_MOV
, FALSE
);
6107 emit_dst_register(emit
, &pos_dst
);
6108 emit_src_register(emit
, &tmp_pos_src
);
6109 end_emit_instruction(emit
);
6114 emit_clipping_instructions(struct svga_shader_emitter_v10
*emit
)
6116 if (emit
->clip_mode
== CLIP_DISTANCE
) {
6117 /* Copy from copy distance temporary to CLIPDIST & the shadow copy */
6118 emit_clip_distance_instructions(emit
);
6120 } else if (emit
->clip_mode
== CLIP_VERTEX
) {
6121 /* Convert TGSI CLIPVERTEX to CLIPDIST */
6122 emit_clip_vertex_instructions(emit
);
6126 * Emit vertex position and take care of legacy user planes only if
6127 * there is a valid vertex position register index.
6128 * This is to take care of the case
6129 * where the shader doesn't output vertex position. Then in
6130 * this case, don't bother to emit more vertex instructions.
6132 if (emit
->vposition
.out_index
== INVALID_INDEX
)
6136 * Emit per-vertex clipping instructions for legacy user defined clip planes.
6137 * NOTE: we must emit the clip distance instructions before the
6138 * emit_vpos_instructions() call since the later function will change
6139 * the TEMP[vs_pos_tmp_index] value.
6141 if (emit
->clip_mode
== CLIP_LEGACY
) {
6142 /* Emit CLIPDIST for legacy user defined clip planes */
6143 emit_clip_distance_from_vpos(emit
, emit
->vposition
.tmp_index
);
6149 * Emit extra per-vertex instructions. This includes clip-coordinate
6150 * space conversion and computing clip distances. This is called for
6151 * each GS emit-vertex instruction and at the end of VS translation.
6154 emit_vertex_instructions(struct svga_shader_emitter_v10
*emit
)
6156 const unsigned vs_pos_tmp_index
= emit
->vposition
.tmp_index
;
6158 /* Emit clipping instructions based on clipping mode */
6159 emit_clipping_instructions(emit
);
6162 * Reset the temporary vertex position register index
6163 * so that emit_dst_register() will use the real vertex position output
6165 emit
->vposition
.tmp_index
= INVALID_INDEX
;
6167 /* Emit vertex position instructions */
6168 emit_vpos_instructions(emit
, vs_pos_tmp_index
);
6170 /* Restore original vposition.tmp_index value for the next GS vertex.
6171 * It doesn't matter for VS.
6173 emit
->vposition
.tmp_index
= vs_pos_tmp_index
;
6177 * Translate the TGSI_OPCODE_EMIT GS instruction.
6180 emit_vertex(struct svga_shader_emitter_v10
*emit
,
6181 const struct tgsi_full_instruction
*inst
)
6183 unsigned ret
= TRUE
;
6185 assert(emit
->unit
== PIPE_SHADER_GEOMETRY
);
6187 emit_vertex_instructions(emit
);
6189 /* We can't use emit_simple() because the TGSI instruction has one
6190 * operand (vertex stream number) which we must ignore for VGPU10.
6192 begin_emit_instruction(emit
);
6193 emit_opcode(emit
, VGPU10_OPCODE_EMIT
, FALSE
);
6194 end_emit_instruction(emit
);
6201 * Emit the extra code to convert from VGPU10's boolean front-face
6202 * register to TGSI's signed front-face register.
6204 * TODO: Make temporary front-face register a scalar.
6207 emit_frontface_instructions(struct svga_shader_emitter_v10
*emit
)
6209 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
6211 if (emit
->fs
.face_input_index
!= INVALID_INDEX
) {
6212 /* convert vgpu10 boolean face register to gallium +/-1 value */
6213 struct tgsi_full_dst_register tmp_dst
=
6214 make_dst_temp_reg(emit
->fs
.face_tmp_index
);
6215 struct tgsi_full_src_register one
=
6216 make_immediate_reg_float(emit
, 1.0f
);
6217 struct tgsi_full_src_register neg_one
=
6218 make_immediate_reg_float(emit
, -1.0f
);
6220 /* MOVC face_tmp, IS_FRONT_FACE.x, 1.0, -1.0 */
6221 begin_emit_instruction(emit
);
6222 emit_opcode(emit
, VGPU10_OPCODE_MOVC
, FALSE
);
6223 emit_dst_register(emit
, &tmp_dst
);
6224 emit_face_register(emit
);
6225 emit_src_register(emit
, &one
);
6226 emit_src_register(emit
, &neg_one
);
6227 end_emit_instruction(emit
);
6233 * Emit the extra code to convert from VGPU10's fragcoord.w value to 1/w.
6236 emit_fragcoord_instructions(struct svga_shader_emitter_v10
*emit
)
6238 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
6240 if (emit
->fs
.fragcoord_input_index
!= INVALID_INDEX
) {
6241 struct tgsi_full_dst_register tmp_dst
=
6242 make_dst_temp_reg(emit
->fs
.fragcoord_tmp_index
);
6243 struct tgsi_full_dst_register tmp_dst_xyz
=
6244 writemask_dst(&tmp_dst
, TGSI_WRITEMASK_XYZ
);
6245 struct tgsi_full_dst_register tmp_dst_w
=
6246 writemask_dst(&tmp_dst
, TGSI_WRITEMASK_W
);
6247 struct tgsi_full_src_register one
=
6248 make_immediate_reg_float(emit
, 1.0f
);
6249 struct tgsi_full_src_register fragcoord
=
6250 make_src_reg(TGSI_FILE_INPUT
, emit
->fs
.fragcoord_input_index
);
6252 /* save the input index */
6253 unsigned fragcoord_input_index
= emit
->fs
.fragcoord_input_index
;
6254 /* set to invalid to prevent substitution in emit_src_register() */
6255 emit
->fs
.fragcoord_input_index
= INVALID_INDEX
;
6257 /* MOV fragcoord_tmp.xyz, fragcoord.xyz */
6258 begin_emit_instruction(emit
);
6259 emit_opcode(emit
, VGPU10_OPCODE_MOV
, FALSE
);
6260 emit_dst_register(emit
, &tmp_dst_xyz
);
6261 emit_src_register(emit
, &fragcoord
);
6262 end_emit_instruction(emit
);
6264 /* DIV fragcoord_tmp.w, 1.0, fragcoord.w */
6265 begin_emit_instruction(emit
);
6266 emit_opcode(emit
, VGPU10_OPCODE_DIV
, FALSE
);
6267 emit_dst_register(emit
, &tmp_dst_w
);
6268 emit_src_register(emit
, &one
);
6269 emit_src_register(emit
, &fragcoord
);
6270 end_emit_instruction(emit
);
6272 /* restore saved value */
6273 emit
->fs
.fragcoord_input_index
= fragcoord_input_index
;
6279 * Emit the extra code to get the current sample position value and
6280 * put it into a temp register.
6283 emit_sample_position_instructions(struct svga_shader_emitter_v10
*emit
)
6285 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
6287 if (emit
->fs
.sample_pos_sys_index
!= INVALID_INDEX
) {
6288 assert(emit
->version
>= 41);
6290 struct tgsi_full_dst_register tmp_dst
=
6291 make_dst_temp_reg(emit
->fs
.sample_pos_tmp_index
);
6292 struct tgsi_full_src_register half
=
6293 make_immediate_reg_float4(emit
, 0.5, 0.5, 0.0, 0.0);
6295 struct tgsi_full_src_register tmp_src
=
6296 make_src_temp_reg(emit
->fs
.sample_pos_tmp_index
);
6297 struct tgsi_full_src_register sample_index_reg
=
6298 make_src_scalar_reg(TGSI_FILE_SYSTEM_VALUE
,
6299 emit
->fs
.sample_id_sys_index
, TGSI_SWIZZLE_X
);
6301 /* The first src register is a shader resource (if we want a
6302 * multisampled resource sample position) or the rasterizer register
6303 * (if we want the current sample position in the color buffer). We
6307 /* SAMPLE_POS dst, RASTERIZER, sampleIndex */
6308 begin_emit_instruction(emit
);
6309 emit_opcode(emit
, VGPU10_OPCODE_SAMPLE_POS
, FALSE
);
6310 emit_dst_register(emit
, &tmp_dst
);
6311 emit_rasterizer_register(emit
);
6312 emit_src_register(emit
, &sample_index_reg
);
6313 end_emit_instruction(emit
);
6315 /* Convert from D3D coords to GL coords by adding 0.5 bias */
6316 /* ADD dst, dst, half */
6317 begin_emit_instruction(emit
);
6318 emit_opcode(emit
, VGPU10_OPCODE_ADD
, FALSE
);
6319 emit_dst_register(emit
, &tmp_dst
);
6320 emit_src_register(emit
, &tmp_src
);
6321 emit_src_register(emit
, &half
);
6322 end_emit_instruction(emit
);
6328 * Emit extra instructions to adjust VS inputs/attributes. This can
6329 * mean casting a vertex attribute from int to float or setting the
6330 * W component to 1, or both.
6333 emit_vertex_attrib_instructions(struct svga_shader_emitter_v10
*emit
)
6335 const unsigned save_w_1_mask
= emit
->key
.vs
.adjust_attrib_w_1
;
6336 const unsigned save_itof_mask
= emit
->key
.vs
.adjust_attrib_itof
;
6337 const unsigned save_utof_mask
= emit
->key
.vs
.adjust_attrib_utof
;
6338 const unsigned save_is_bgra_mask
= emit
->key
.vs
.attrib_is_bgra
;
6339 const unsigned save_puint_to_snorm_mask
= emit
->key
.vs
.attrib_puint_to_snorm
;
6340 const unsigned save_puint_to_uscaled_mask
= emit
->key
.vs
.attrib_puint_to_uscaled
;
6341 const unsigned save_puint_to_sscaled_mask
= emit
->key
.vs
.attrib_puint_to_sscaled
;
6343 unsigned adjust_mask
= (save_w_1_mask
|
6347 save_puint_to_snorm_mask
|
6348 save_puint_to_uscaled_mask
|
6349 save_puint_to_sscaled_mask
);
6351 assert(emit
->unit
== PIPE_SHADER_VERTEX
);
6354 struct tgsi_full_src_register one
=
6355 make_immediate_reg_float(emit
, 1.0f
);
6357 struct tgsi_full_src_register one_int
=
6358 make_immediate_reg_int(emit
, 1);
6360 /* We need to turn off these bitmasks while emitting the
6361 * instructions below, then restore them afterward.
6363 emit
->key
.vs
.adjust_attrib_w_1
= 0;
6364 emit
->key
.vs
.adjust_attrib_itof
= 0;
6365 emit
->key
.vs
.adjust_attrib_utof
= 0;
6366 emit
->key
.vs
.attrib_is_bgra
= 0;
6367 emit
->key
.vs
.attrib_puint_to_snorm
= 0;
6368 emit
->key
.vs
.attrib_puint_to_uscaled
= 0;
6369 emit
->key
.vs
.attrib_puint_to_sscaled
= 0;
6371 while (adjust_mask
) {
6372 unsigned index
= u_bit_scan(&adjust_mask
);
6374 /* skip the instruction if this vertex attribute is not being used */
6375 if (emit
->info
.input_usage_mask
[index
] == 0)
6378 unsigned tmp
= emit
->vs
.adjusted_input
[index
];
6379 struct tgsi_full_src_register input_src
=
6380 make_src_reg(TGSI_FILE_INPUT
, index
);
6382 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
6383 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
6384 struct tgsi_full_dst_register tmp_dst_w
=
6385 writemask_dst(&tmp_dst
, TGSI_WRITEMASK_W
);
6387 /* ITOF/UTOF/MOV tmp, input[index] */
6388 if (save_itof_mask
& (1 << index
)) {
6389 emit_instruction_op1(emit
, VGPU10_OPCODE_ITOF
,
6390 &tmp_dst
, &input_src
, FALSE
);
6392 else if (save_utof_mask
& (1 << index
)) {
6393 emit_instruction_op1(emit
, VGPU10_OPCODE_UTOF
,
6394 &tmp_dst
, &input_src
, FALSE
);
6396 else if (save_puint_to_snorm_mask
& (1 << index
)) {
6397 emit_puint_to_snorm(emit
, &tmp_dst
, &input_src
);
6399 else if (save_puint_to_uscaled_mask
& (1 << index
)) {
6400 emit_puint_to_uscaled(emit
, &tmp_dst
, &input_src
);
6402 else if (save_puint_to_sscaled_mask
& (1 << index
)) {
6403 emit_puint_to_sscaled(emit
, &tmp_dst
, &input_src
);
6406 assert((save_w_1_mask
| save_is_bgra_mask
) & (1 << index
));
6407 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
,
6408 &tmp_dst
, &input_src
, FALSE
);
6411 if (save_is_bgra_mask
& (1 << index
)) {
6412 emit_swap_r_b(emit
, &tmp_dst
, &tmp_src
);
6415 if (save_w_1_mask
& (1 << index
)) {
6416 /* MOV tmp.w, 1.0 */
6417 if (emit
->key
.vs
.attrib_is_pure_int
& (1 << index
)) {
6418 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
,
6419 &tmp_dst_w
, &one_int
, FALSE
);
6422 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
,
6423 &tmp_dst_w
, &one
, FALSE
);
6428 emit
->key
.vs
.adjust_attrib_w_1
= save_w_1_mask
;
6429 emit
->key
.vs
.adjust_attrib_itof
= save_itof_mask
;
6430 emit
->key
.vs
.adjust_attrib_utof
= save_utof_mask
;
6431 emit
->key
.vs
.attrib_is_bgra
= save_is_bgra_mask
;
6432 emit
->key
.vs
.attrib_puint_to_snorm
= save_puint_to_snorm_mask
;
6433 emit
->key
.vs
.attrib_puint_to_uscaled
= save_puint_to_uscaled_mask
;
6434 emit
->key
.vs
.attrib_puint_to_sscaled
= save_puint_to_sscaled_mask
;
6440 * Some common values like 0.0, 1.0, 0.5, etc. are frequently needed
6441 * to implement some instructions. We pre-allocate those values here
6442 * in the immediate constant buffer.
6445 alloc_common_immediates(struct svga_shader_emitter_v10
*emit
)
6449 emit
->common_immediate_pos
[n
++] =
6450 alloc_immediate_float4(emit
, 0.0f
, 1.0f
, 0.5f
, -1.0f
);
6452 if (emit
->info
.opcode_count
[TGSI_OPCODE_LIT
] > 0) {
6453 emit
->common_immediate_pos
[n
++] =
6454 alloc_immediate_float4(emit
, 128.0f
, -128.0f
, 0.0f
, 0.0f
);
6457 emit
->common_immediate_pos
[n
++] =
6458 alloc_immediate_int4(emit
, 0, 1, 0, -1);
6460 if (emit
->key
.vs
.attrib_puint_to_snorm
) {
6461 emit
->common_immediate_pos
[n
++] =
6462 alloc_immediate_float4(emit
, -2.0f
, 2.0f
, 3.0f
, -1.66666f
);
6465 if (emit
->key
.vs
.attrib_puint_to_uscaled
) {
6466 emit
->common_immediate_pos
[n
++] =
6467 alloc_immediate_float4(emit
, 1023.0f
, 3.0f
, 0.0f
, 0.0f
);
6470 if (emit
->key
.vs
.attrib_puint_to_sscaled
) {
6471 emit
->common_immediate_pos
[n
++] =
6472 alloc_immediate_int4(emit
, 22, 12, 2, 0);
6474 emit
->common_immediate_pos
[n
++] =
6475 alloc_immediate_int4(emit
, 22, 30, 0, 0);
6480 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
6481 if (emit
->key
.tex
[i
].texel_bias
) {
6482 /* Replace 0.0f if more immediate float value is needed */
6483 emit
->common_immediate_pos
[n
++] =
6484 alloc_immediate_float4(emit
, 0.0001f
, 0.0f
, 0.0f
, 0.0f
);
6489 assert(n
<= ARRAY_SIZE(emit
->common_immediate_pos
));
6490 emit
->num_common_immediates
= n
;
6495 * Emit any extra/helper declarations/code that we might need between
6496 * the declaration section and code section.
6499 emit_pre_helpers(struct svga_shader_emitter_v10
*emit
)
6502 if (emit
->unit
== PIPE_SHADER_GEOMETRY
)
6503 emit_property_instructions(emit
);
6505 /* Declare inputs */
6506 if (!emit_input_declarations(emit
))
6509 /* Declare outputs */
6510 if (!emit_output_declarations(emit
))
6513 /* Declare temporary registers */
6514 emit_temporaries_declaration(emit
);
6516 /* Declare constant registers */
6517 emit_constant_declaration(emit
);
6519 /* Declare samplers and resources */
6520 emit_sampler_declarations(emit
);
6521 emit_resource_declarations(emit
);
6523 /* Declare clip distance output registers */
6524 if (emit
->unit
== PIPE_SHADER_VERTEX
||
6525 emit
->unit
== PIPE_SHADER_GEOMETRY
) {
6526 emit_clip_distance_declarations(emit
);
6529 alloc_common_immediates(emit
);
6531 if (emit
->unit
== PIPE_SHADER_FRAGMENT
&&
6532 emit
->key
.fs
.alpha_func
!= SVGA3D_CMP_ALWAYS
) {
6533 float alpha
= emit
->key
.fs
.alpha_ref
;
6534 emit
->fs
.alpha_ref_index
=
6535 alloc_immediate_float4(emit
, alpha
, alpha
, alpha
, alpha
);
6538 /* Now, emit the constant block containing all the immediates
6539 * declared by shader, as well as the extra ones seen above.
6541 emit_vgpu10_immediates_block(emit
);
6543 if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
6544 emit_frontface_instructions(emit
);
6545 emit_fragcoord_instructions(emit
);
6546 emit_sample_position_instructions(emit
);
6548 else if (emit
->unit
== PIPE_SHADER_VERTEX
) {
6549 emit_vertex_attrib_instructions(emit
);
6557 * The device has no direct support for the pipe_blend_state::alpha_to_one
6558 * option so we implement it here with shader code.
6560 * Note that this is kind of pointless, actually. Here we're clobbering
6561 * the alpha value with 1.0. So if alpha-to-coverage is enabled, we'll wind
6562 * up with 100% coverage. That's almost certainly not what the user wants.
6563 * The work-around is to add extra shader code to compute coverage from alpha
6564 * and write it to the coverage output register (if the user's shader doesn't
6565 * do so already). We'll probably do that in the future.
6568 emit_alpha_to_one_instructions(struct svga_shader_emitter_v10
*emit
,
6569 unsigned fs_color_tmp_index
)
6571 struct tgsi_full_src_register one
= make_immediate_reg_float(emit
, 1.0f
);
6574 /* Note: it's not 100% clear from the spec if we're supposed to clobber
6575 * the alpha for all render targets. But that's what NVIDIA does and
6576 * that's what Piglit tests.
6578 for (i
= 0; i
< emit
->fs
.num_color_outputs
; i
++) {
6579 struct tgsi_full_dst_register color_dst
;
6581 if (fs_color_tmp_index
!= INVALID_INDEX
&& i
== 0) {
6582 /* write to the temp color register */
6583 color_dst
= make_dst_temp_reg(fs_color_tmp_index
);
6586 /* write directly to the color[i] output */
6587 color_dst
= make_dst_output_reg(emit
->fs
.color_out_index
[i
]);
6590 color_dst
= writemask_dst(&color_dst
, TGSI_WRITEMASK_W
);
6592 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &color_dst
, &one
, FALSE
);
6598 * Emit alpha test code. This compares TEMP[fs_color_tmp_index].w
6599 * against the alpha reference value and discards the fragment if the
6603 emit_alpha_test_instructions(struct svga_shader_emitter_v10
*emit
,
6604 unsigned fs_color_tmp_index
)
6606 /* compare output color's alpha to alpha ref and kill */
6607 unsigned tmp
= get_temp_index(emit
);
6608 struct tgsi_full_src_register tmp_src
= make_src_temp_reg(tmp
);
6609 struct tgsi_full_src_register tmp_src_x
=
6610 scalar_src(&tmp_src
, TGSI_SWIZZLE_X
);
6611 struct tgsi_full_dst_register tmp_dst
= make_dst_temp_reg(tmp
);
6612 struct tgsi_full_src_register color_src
=
6613 make_src_temp_reg(fs_color_tmp_index
);
6614 struct tgsi_full_src_register color_src_w
=
6615 scalar_src(&color_src
, TGSI_SWIZZLE_W
);
6616 struct tgsi_full_src_register ref_src
=
6617 make_src_immediate_reg(emit
->fs
.alpha_ref_index
);
6618 struct tgsi_full_dst_register color_dst
=
6619 make_dst_output_reg(emit
->fs
.color_out_index
[0]);
6621 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
6623 /* dst = src0 'alpha_func' src1 */
6624 emit_comparison(emit
, emit
->key
.fs
.alpha_func
, &tmp_dst
,
6625 &color_src_w
, &ref_src
);
6627 /* DISCARD if dst.x == 0 */
6628 begin_emit_instruction(emit
);
6629 emit_discard_opcode(emit
, FALSE
); /* discard if src0.x is zero */
6630 emit_src_register(emit
, &tmp_src_x
);
6631 end_emit_instruction(emit
);
6633 /* If we don't need to broadcast the color below, emit the final color here.
6635 if (emit
->key
.fs
.write_color0_to_n_cbufs
<= 1) {
6636 /* MOV output.color, tempcolor */
6637 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &color_dst
,
6638 &color_src
, FALSE
); /* XXX saturate? */
6641 free_temp_indexes(emit
);
6646 * Emit instructions for writing a single color output to multiple
6648 * This is used when the TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS (or
6649 * when key.fs.white_fragments is true).
6650 * property is set and the number of render targets is greater than one.
6651 * \param fs_color_tmp_index index of the temp register that holds the
6652 * color to broadcast.
6655 emit_broadcast_color_instructions(struct svga_shader_emitter_v10
*emit
,
6656 unsigned fs_color_tmp_index
)
6658 const unsigned n
= emit
->key
.fs
.write_color0_to_n_cbufs
;
6660 struct tgsi_full_src_register color_src
;
6662 if (emit
->key
.fs
.white_fragments
) {
6663 /* set all color outputs to white */
6664 color_src
= make_immediate_reg_float(emit
, 1.0f
);
6667 /* set all color outputs to TEMP[fs_color_tmp_index] */
6668 assert(fs_color_tmp_index
!= INVALID_INDEX
);
6669 color_src
= make_src_temp_reg(fs_color_tmp_index
);
6672 assert(emit
->unit
== PIPE_SHADER_FRAGMENT
);
6674 for (i
= 0; i
< n
; i
++) {
6675 unsigned output_reg
= emit
->fs
.color_out_index
[i
];
6676 struct tgsi_full_dst_register color_dst
=
6677 make_dst_output_reg(output_reg
);
6679 /* Fill in this semantic here since we'll use it later in
6680 * emit_dst_register().
6682 emit
->info
.output_semantic_name
[output_reg
] = TGSI_SEMANTIC_COLOR
;
6684 /* MOV output.color[i], tempcolor */
6685 emit_instruction_op1(emit
, VGPU10_OPCODE_MOV
, &color_dst
,
6686 &color_src
, FALSE
); /* XXX saturate? */
6692 * Emit extra helper code after the original shader code, but before the
6693 * last END/RET instruction.
6694 * For vertex shaders this means emitting the extra code to apply the
6695 * prescale scale/translation.
6698 emit_post_helpers(struct svga_shader_emitter_v10
*emit
)
6700 if (emit
->unit
== PIPE_SHADER_VERTEX
) {
6701 emit_vertex_instructions(emit
);
6703 else if (emit
->unit
== PIPE_SHADER_FRAGMENT
) {
6704 const unsigned fs_color_tmp_index
= emit
->fs
.color_tmp_index
;
6706 assert(!(emit
->key
.fs
.white_fragments
&&
6707 emit
->key
.fs
.write_color0_to_n_cbufs
== 0));
6709 /* We no longer want emit_dst_register() to substitute the
6710 * temporary fragment color register for the real color output.
6712 emit
->fs
.color_tmp_index
= INVALID_INDEX
;
6714 if (emit
->key
.fs
.alpha_to_one
) {
6715 emit_alpha_to_one_instructions(emit
, fs_color_tmp_index
);
6717 if (emit
->key
.fs
.alpha_func
!= SVGA3D_CMP_ALWAYS
) {
6718 emit_alpha_test_instructions(emit
, fs_color_tmp_index
);
6720 if (emit
->key
.fs
.write_color0_to_n_cbufs
> 1 ||
6721 emit
->key
.fs
.white_fragments
) {
6722 emit_broadcast_color_instructions(emit
, fs_color_tmp_index
);
6731 * Translate the TGSI tokens into VGPU10 tokens.
6734 emit_vgpu10_instructions(struct svga_shader_emitter_v10
*emit
,
6735 const struct tgsi_token
*tokens
)
6737 struct tgsi_parse_context parse
;
6739 boolean pre_helpers_emitted
= FALSE
;
6740 unsigned inst_number
= 0;
6742 tgsi_parse_init(&parse
, tokens
);
6744 while (!tgsi_parse_end_of_tokens(&parse
)) {
6745 tgsi_parse_token(&parse
);
6747 switch (parse
.FullToken
.Token
.Type
) {
6748 case TGSI_TOKEN_TYPE_IMMEDIATE
:
6749 ret
= emit_vgpu10_immediate(emit
, &parse
.FullToken
.FullImmediate
);
6754 case TGSI_TOKEN_TYPE_DECLARATION
:
6755 ret
= emit_vgpu10_declaration(emit
, &parse
.FullToken
.FullDeclaration
);
6760 case TGSI_TOKEN_TYPE_INSTRUCTION
:
6761 if (!pre_helpers_emitted
) {
6762 ret
= emit_pre_helpers(emit
);
6765 pre_helpers_emitted
= TRUE
;
6767 ret
= emit_vgpu10_instruction(emit
, inst_number
++,
6768 &parse
.FullToken
.FullInstruction
);
6773 case TGSI_TOKEN_TYPE_PROPERTY
:
6774 ret
= emit_vgpu10_property(emit
, &parse
.FullToken
.FullProperty
);
6785 tgsi_parse_free(&parse
);
6791 * Emit the first VGPU10 shader tokens.
6794 emit_vgpu10_header(struct svga_shader_emitter_v10
*emit
)
6796 VGPU10ProgramToken ptoken
;
6798 /* First token: VGPU10ProgramToken (version info, program type (VS,GS,PS)) */
6799 ptoken
.majorVersion
= emit
->version
/ 10;
6800 ptoken
.minorVersion
= emit
->version
% 10;
6801 ptoken
.programType
= translate_shader_type(emit
->unit
);
6802 if (!emit_dword(emit
, ptoken
.value
))
6805 /* Second token: total length of shader, in tokens. We can't fill this
6806 * in until we're all done. Emit zero for now.
6808 return emit_dword(emit
, 0);
6813 emit_vgpu10_tail(struct svga_shader_emitter_v10
*emit
)
6815 VGPU10ProgramToken
*tokens
;
6817 /* Replace the second token with total shader length */
6818 tokens
= (VGPU10ProgramToken
*) emit
->buf
;
6819 tokens
[1].value
= emit_get_num_tokens(emit
);
6826 * Modify the FS to read the BCOLORs and use the FACE register
6827 * to choose between the front/back colors.
6829 static const struct tgsi_token
*
6830 transform_fs_twoside(const struct tgsi_token
*tokens
)
6833 debug_printf("Before tgsi_add_two_side ------------------\n");
6834 tgsi_dump(tokens
,0);
6836 tokens
= tgsi_add_two_side(tokens
);
6838 debug_printf("After tgsi_add_two_side ------------------\n");
6839 tgsi_dump(tokens
, 0);
6846 * Modify the FS to do polygon stipple.
6848 static const struct tgsi_token
*
6849 transform_fs_pstipple(struct svga_shader_emitter_v10
*emit
,
6850 const struct tgsi_token
*tokens
)
6852 const struct tgsi_token
*new_tokens
;
6856 debug_printf("Before pstipple ------------------\n");
6857 tgsi_dump(tokens
,0);
6860 new_tokens
= util_pstipple_create_fragment_shader(tokens
, &unit
, 0,
6863 emit
->fs
.pstipple_sampler_unit
= unit
;
6865 /* Setup texture state for stipple */
6866 emit
->sampler_target
[unit
] = TGSI_TEXTURE_2D
;
6867 emit
->key
.tex
[unit
].swizzle_r
= TGSI_SWIZZLE_X
;
6868 emit
->key
.tex
[unit
].swizzle_g
= TGSI_SWIZZLE_Y
;
6869 emit
->key
.tex
[unit
].swizzle_b
= TGSI_SWIZZLE_Z
;
6870 emit
->key
.tex
[unit
].swizzle_a
= TGSI_SWIZZLE_W
;
6873 debug_printf("After pstipple ------------------\n");
6874 tgsi_dump(new_tokens
, 0);
6881 * Modify the FS to support anti-aliasing point.
6883 static const struct tgsi_token
*
6884 transform_fs_aapoint(const struct tgsi_token
*tokens
,
6888 debug_printf("Before tgsi_add_aa_point ------------------\n");
6889 tgsi_dump(tokens
,0);
6891 tokens
= tgsi_add_aa_point(tokens
, aa_coord_index
);
6893 debug_printf("After tgsi_add_aa_point ------------------\n");
6894 tgsi_dump(tokens
, 0);
6900 * This is the main entrypoint for the TGSI -> VPGU10 translator.
6902 struct svga_shader_variant
*
6903 svga_tgsi_vgpu10_translate(struct svga_context
*svga
,
6904 const struct svga_shader
*shader
,
6905 const struct svga_compile_key
*key
,
6906 enum pipe_shader_type unit
)
6908 struct svga_shader_variant
*variant
= NULL
;
6909 struct svga_shader_emitter_v10
*emit
;
6910 const struct tgsi_token
*tokens
= shader
->tokens
;
6911 struct svga_vertex_shader
*vs
= svga
->curr
.vs
;
6912 struct svga_geometry_shader
*gs
= svga
->curr
.gs
;
6914 assert(unit
== PIPE_SHADER_VERTEX
||
6915 unit
== PIPE_SHADER_GEOMETRY
||
6916 unit
== PIPE_SHADER_FRAGMENT
);
6918 /* These two flags cannot be used together */
6919 assert(key
->vs
.need_prescale
+ key
->vs
.undo_viewport
<= 1);
6921 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_TGSIVGPU10TRANSLATE
);
6923 * Setup the code emitter
6925 emit
= alloc_emitter();
6930 emit
->version
= svga_have_sm4_1(svga
) ? 41 : 40;
6934 emit
->vposition
.need_prescale
= (emit
->key
.vs
.need_prescale
||
6935 emit
->key
.gs
.need_prescale
);
6936 emit
->vposition
.tmp_index
= INVALID_INDEX
;
6937 emit
->vposition
.so_index
= INVALID_INDEX
;
6938 emit
->vposition
.out_index
= INVALID_INDEX
;
6940 emit
->fs
.color_tmp_index
= INVALID_INDEX
;
6941 emit
->fs
.face_input_index
= INVALID_INDEX
;
6942 emit
->fs
.fragcoord_input_index
= INVALID_INDEX
;
6943 emit
->fs
.sample_id_sys_index
= INVALID_INDEX
;
6944 emit
->fs
.sample_pos_sys_index
= INVALID_INDEX
;
6946 emit
->gs
.prim_id_index
= INVALID_INDEX
;
6948 emit
->clip_dist_out_index
= INVALID_INDEX
;
6949 emit
->clip_dist_tmp_index
= INVALID_INDEX
;
6950 emit
->clip_dist_so_index
= INVALID_INDEX
;
6951 emit
->clip_vertex_out_index
= INVALID_INDEX
;
6953 if (emit
->key
.fs
.alpha_func
== SVGA3D_CMP_INVALID
) {
6954 emit
->key
.fs
.alpha_func
= SVGA3D_CMP_ALWAYS
;
6957 if (unit
== PIPE_SHADER_FRAGMENT
) {
6958 if (key
->fs
.light_twoside
) {
6959 tokens
= transform_fs_twoside(tokens
);
6961 if (key
->fs
.pstipple
) {
6962 const struct tgsi_token
*new_tokens
=
6963 transform_fs_pstipple(emit
, tokens
);
6964 if (tokens
!= shader
->tokens
) {
6965 /* free the two-sided shader tokens */
6966 tgsi_free_tokens(tokens
);
6968 tokens
= new_tokens
;
6970 if (key
->fs
.aa_point
) {
6971 tokens
= transform_fs_aapoint(tokens
, key
->fs
.aa_point_coord_index
);
6975 if (SVGA_DEBUG
& DEBUG_TGSI
) {
6976 debug_printf("#####################################\n");
6977 debug_printf("### TGSI Shader %u\n", shader
->id
);
6978 tgsi_dump(tokens
, 0);
6982 * Rescan the header if the token string is different from the one
6983 * included in the shader; otherwise, the header info is already up-to-date
6985 if (tokens
!= shader
->tokens
) {
6986 tgsi_scan_shader(tokens
, &emit
->info
);
6988 emit
->info
= shader
->info
;
6991 emit
->num_outputs
= emit
->info
.num_outputs
;
6993 if (unit
== PIPE_SHADER_FRAGMENT
) {
6994 /* Compute FS input remapping to match the output from VS/GS */
6996 svga_link_shaders(&gs
->base
.info
, &emit
->info
, &emit
->linkage
);
6999 svga_link_shaders(&vs
->base
.info
, &emit
->info
, &emit
->linkage
);
7001 } else if (unit
== PIPE_SHADER_GEOMETRY
) {
7003 svga_link_shaders(&vs
->base
.info
, &emit
->info
, &emit
->linkage
);
7006 /* Since vertex shader does not need to go through the linker to
7007 * establish the input map, we need to make sure the highest index
7008 * of input registers is set properly here.
7010 emit
->linkage
.input_map_max
= MAX2((int)emit
->linkage
.input_map_max
,
7011 emit
->info
.file_max
[TGSI_FILE_INPUT
]);
7013 determine_clipping_mode(emit
);
7015 if (unit
== PIPE_SHADER_GEOMETRY
|| unit
== PIPE_SHADER_VERTEX
) {
7016 if (shader
->stream_output
!= NULL
|| emit
->clip_mode
== CLIP_DISTANCE
) {
7017 /* if there is stream output declarations associated
7018 * with this shader or the shader writes to ClipDistance
7019 * then reserve extra registers for the non-adjusted vertex position
7020 * and the ClipDistance shadow copy
7022 emit
->vposition
.so_index
= emit
->num_outputs
++;
7024 if (emit
->clip_mode
== CLIP_DISTANCE
) {
7025 emit
->clip_dist_so_index
= emit
->num_outputs
++;
7026 if (emit
->info
.num_written_clipdistance
> 4)
7027 emit
->num_outputs
++;
7033 * Do actual shader translation.
7035 if (!emit_vgpu10_header(emit
)) {
7036 debug_printf("svga: emit VGPU10 header failed\n");
7040 if (!emit_vgpu10_instructions(emit
, tokens
)) {
7041 debug_printf("svga: emit VGPU10 instructions failed\n");
7045 if (!emit_vgpu10_tail(emit
)) {
7046 debug_printf("svga: emit VGPU10 tail failed\n");
7050 if (emit
->register_overflow
) {
7055 * Create, initialize the 'variant' object.
7057 variant
= svga_new_shader_variant(svga
);
7061 variant
->shader
= shader
;
7062 variant
->nr_tokens
= emit_get_num_tokens(emit
);
7063 variant
->tokens
= (const unsigned *)emit
->buf
;
7064 emit
->buf
= NULL
; /* buffer is no longer owed by emitter context */
7065 memcpy(&variant
->key
, key
, sizeof(*key
));
7066 variant
->id
= UTIL_BITMASK_INVALID_INDEX
;
7068 /* The extra constant starting offset starts with the number of
7069 * shader constants declared in the shader.
7071 variant
->extra_const_start
= emit
->num_shader_consts
[0];
7072 if (key
->gs
.wide_point
) {
7074 * The extra constant added in the transformed shader
7075 * for inverse viewport scale is to be supplied by the driver.
7076 * So the extra constant starting offset needs to be reduced by 1.
7078 assert(variant
->extra_const_start
> 0);
7079 variant
->extra_const_start
--;
7082 variant
->pstipple_sampler_unit
= emit
->fs
.pstipple_sampler_unit
;
7084 /* If there was exactly one write to a fragment shader output register
7085 * and it came from a constant buffer, we know all fragments will have
7086 * the same color (except for blending).
7088 variant
->constant_color_output
=
7089 emit
->constant_color_output
&& emit
->num_output_writes
== 1;
7091 /** keep track in the variant if flat interpolation is used
7092 * for any of the varyings.
7094 variant
->uses_flat_interp
= emit
->uses_flat_interp
;
7096 variant
->fs_shadow_compare_units
= emit
->fs
.shadow_compare_units
;
7098 if (tokens
!= shader
->tokens
) {
7099 tgsi_free_tokens(tokens
);
7106 SVGA_STATS_TIME_POP(svga_sws(svga
));