1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE, INC AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
31 #include "pipe/p_compiler.h"
32 #include "pipe/p_shader_tokens.h"
33 #include "util/u_debug.h"
41 /* Almost a tgsi_src_register, but we need to pull in the Absolute
42 * flag from the _ext token. Indirect flag always implies ADDR[0].
46 unsigned File
: 4; /* TGSI_FILE_ */
47 unsigned SwizzleX
: 2; /* TGSI_SWIZZLE_ */
48 unsigned SwizzleY
: 2; /* TGSI_SWIZZLE_ */
49 unsigned SwizzleZ
: 2; /* TGSI_SWIZZLE_ */
50 unsigned SwizzleW
: 2; /* TGSI_SWIZZLE_ */
51 unsigned Indirect
: 1; /* BOOL */
52 unsigned DimIndirect
: 1; /* BOOL */
53 unsigned Dimension
: 1; /* BOOL */
54 unsigned Absolute
: 1; /* BOOL */
55 unsigned Negate
: 1; /* BOOL */
56 int Index
: 16; /* SINT */
57 unsigned IndirectFile
: 4; /* TGSI_FILE_ */
58 int IndirectIndex
: 16; /* SINT */
59 unsigned IndirectSwizzle
: 2; /* TGSI_SWIZZLE_ */
60 int DimensionIndex
: 16; /* SINT */
61 unsigned DimIndFile
: 4; /* TGSI_FILE_ */
62 int DimIndIndex
: 16; /* SINT */
63 unsigned DimIndSwizzle
: 2; /* TGSI_SWIZZLE_ */
66 /* Very similar to a tgsi_dst_register, removing unsupported fields
67 * and adding a Saturate flag. It's easier to push saturate into the
68 * destination register than to try and create a _SAT varient of each
69 * instruction function.
73 unsigned File
: 4; /* TGSI_FILE_ */
74 unsigned WriteMask
: 4; /* TGSI_WRITEMASK_ */
75 unsigned Indirect
: 1; /* BOOL */
76 unsigned Saturate
: 1; /* BOOL */
77 unsigned Predicate
: 1;
78 unsigned PredNegate
: 1; /* BOOL */
79 unsigned PredSwizzleX
: 2; /* TGSI_SWIZZLE_ */
80 unsigned PredSwizzleY
: 2; /* TGSI_SWIZZLE_ */
81 unsigned PredSwizzleZ
: 2; /* TGSI_SWIZZLE_ */
82 unsigned PredSwizzleW
: 2; /* TGSI_SWIZZLE_ */
83 int Index
: 16; /* SINT */
84 int IndirectIndex
: 16; /* SINT */
85 int IndirectSwizzle
: 2; /* TGSI_SWIZZLE_ */
91 ureg_create( unsigned processor
);
93 const struct tgsi_token
*
94 ureg_finalize( struct ureg_program
* );
96 /* Create and return a shader:
99 ureg_create_shader( struct ureg_program
*,
100 struct pipe_context
*pipe
);
103 /* Alternately, return the built token stream and hand ownership of
104 * that memory to the caller:
106 const struct tgsi_token
*
107 ureg_get_tokens( struct ureg_program
*ureg
,
108 unsigned *nr_tokens
);
111 /* Free the tokens created by ureg_get_tokens() */
112 void ureg_free_tokens( const struct tgsi_token
*tokens
);
116 ureg_destroy( struct ureg_program
* );
119 /***********************************************************************
120 * Convenience routine:
123 ureg_create_shader_and_destroy( struct ureg_program
*p
,
124 struct pipe_context
*pipe
)
126 void *result
= ureg_create_shader( p
, pipe
);
132 /***********************************************************************
133 * Build shader properties:
137 ureg_property_gs_input_prim(struct ureg_program
*ureg
,
138 unsigned input_prim
);
141 ureg_property_gs_output_prim(struct ureg_program
*ureg
,
142 unsigned output_prim
);
145 ureg_property_gs_max_vertices(struct ureg_program
*ureg
,
146 unsigned max_vertices
);
149 ureg_property_fs_coord_origin(struct ureg_program
*ureg
,
150 unsigned fs_coord_origin
);
153 ureg_property_fs_coord_pixel_center(struct ureg_program
*ureg
,
154 unsigned fs_coord_pixel_center
);
156 /***********************************************************************
157 * Build shader declarations:
161 ureg_DECL_fs_input_cyl(struct ureg_program
*,
162 unsigned semantic_name
,
163 unsigned semantic_index
,
164 unsigned interp_mode
,
165 unsigned cylindrical_wrap
);
167 static INLINE
struct ureg_src
168 ureg_DECL_fs_input(struct ureg_program
*ureg
,
169 unsigned semantic_name
,
170 unsigned semantic_index
,
171 unsigned interp_mode
)
173 return ureg_DECL_fs_input_cyl(ureg
,
181 ureg_DECL_vs_input( struct ureg_program
*,
185 ureg_DECL_gs_input(struct ureg_program
*,
187 unsigned semantic_name
,
188 unsigned semantic_index
);
191 ureg_DECL_system_value(struct ureg_program
*,
193 unsigned semantic_name
,
194 unsigned semantic_index
);
197 ureg_DECL_output( struct ureg_program
*,
198 unsigned semantic_name
,
199 unsigned semantic_index
);
202 ureg_DECL_immediate( struct ureg_program
*,
207 ureg_DECL_immediate_uint( struct ureg_program
*,
212 ureg_DECL_immediate_block_uint( struct ureg_program
*,
217 ureg_DECL_immediate_int( struct ureg_program
*,
222 ureg_DECL_constant2D(struct ureg_program
*ureg
,
228 ureg_DECL_constant( struct ureg_program
*,
232 ureg_DECL_temporary( struct ureg_program
* );
235 ureg_release_temporary( struct ureg_program
*ureg
,
236 struct ureg_dst tmp
);
239 ureg_DECL_address( struct ureg_program
* );
242 ureg_DECL_predicate(struct ureg_program
*);
244 /* Supply an index to the sampler declaration as this is the hook to
245 * the external pipe_sampler state. Users of this function probably
246 * don't want just any sampler, but a specific one which they've set
247 * up state for in the context.
250 ureg_DECL_sampler( struct ureg_program
*,
254 static INLINE
struct ureg_src
255 ureg_imm4f( struct ureg_program
*ureg
,
264 return ureg_DECL_immediate( ureg
, v
, 4 );
267 static INLINE
struct ureg_src
268 ureg_imm3f( struct ureg_program
*ureg
,
276 return ureg_DECL_immediate( ureg
, v
, 3 );
279 static INLINE
struct ureg_src
280 ureg_imm2f( struct ureg_program
*ureg
,
286 return ureg_DECL_immediate( ureg
, v
, 2 );
289 static INLINE
struct ureg_src
290 ureg_imm1f( struct ureg_program
*ureg
,
295 return ureg_DECL_immediate( ureg
, v
, 1 );
298 static INLINE
struct ureg_src
299 ureg_imm4u( struct ureg_program
*ureg
,
300 unsigned a
, unsigned b
,
301 unsigned c
, unsigned d
)
308 return ureg_DECL_immediate_uint( ureg
, v
, 4 );
311 static INLINE
struct ureg_src
312 ureg_imm3u( struct ureg_program
*ureg
,
313 unsigned a
, unsigned b
,
320 return ureg_DECL_immediate_uint( ureg
, v
, 3 );
323 static INLINE
struct ureg_src
324 ureg_imm2u( struct ureg_program
*ureg
,
325 unsigned a
, unsigned b
)
330 return ureg_DECL_immediate_uint( ureg
, v
, 2 );
333 static INLINE
struct ureg_src
334 ureg_imm1u( struct ureg_program
*ureg
,
337 return ureg_DECL_immediate_uint( ureg
, &a
, 1 );
340 static INLINE
struct ureg_src
341 ureg_imm4i( struct ureg_program
*ureg
,
350 return ureg_DECL_immediate_int( ureg
, v
, 4 );
353 static INLINE
struct ureg_src
354 ureg_imm3i( struct ureg_program
*ureg
,
362 return ureg_DECL_immediate_int( ureg
, v
, 3 );
365 static INLINE
struct ureg_src
366 ureg_imm2i( struct ureg_program
*ureg
,
372 return ureg_DECL_immediate_int( ureg
, v
, 2 );
375 static INLINE
struct ureg_src
376 ureg_imm1i( struct ureg_program
*ureg
,
379 return ureg_DECL_immediate_int( ureg
, &a
, 1 );
382 /***********************************************************************
383 * Functions for patching up labels
387 /* Will return a number which can be used in a label to point to the
388 * next instruction to be emitted.
391 ureg_get_instruction_number( struct ureg_program
*ureg
);
394 /* Patch a given label (expressed as a token number) to point to a
395 * given instruction (expressed as an instruction number).
397 * Labels are obtained from instruction emitters, eg ureg_CAL().
398 * Instruction numbers are obtained from ureg_get_instruction_number(),
402 ureg_fixup_label(struct ureg_program
*ureg
,
403 unsigned label_token
,
404 unsigned instruction_number
);
407 /* Generic instruction emitter. Use if you need to pass the opcode as
408 * a parameter, rather than using the emit_OP() varients below.
411 ureg_insn(struct ureg_program
*ureg
,
413 const struct ureg_dst
*dst
,
415 const struct ureg_src
*src
,
420 ureg_tex_insn(struct ureg_program
*ureg
,
422 const struct ureg_dst
*dst
,
425 const struct ureg_src
*src
,
430 ureg_label_insn(struct ureg_program
*ureg
,
432 const struct ureg_src
*src
,
437 /***********************************************************************
438 * Internal instruction helpers, don't call these directly:
441 struct ureg_emit_insn_result
{
442 unsigned insn_token
; /*< Used to fixup insn size. */
443 unsigned extended_token
; /*< Used to set the Extended bit, usually the same as insn_token. */
446 struct ureg_emit_insn_result
447 ureg_emit_insn(struct ureg_program
*ureg
,
452 unsigned pred_swizzle_x
,
453 unsigned pred_swizzle_y
,
454 unsigned pred_swizzle_z
,
455 unsigned pred_swizzle_w
,
460 ureg_emit_label(struct ureg_program
*ureg
,
462 unsigned *label_token
);
465 ureg_emit_texture(struct ureg_program
*ureg
,
470 ureg_emit_dst( struct ureg_program
*ureg
,
471 struct ureg_dst dst
);
474 ureg_emit_src( struct ureg_program
*ureg
,
475 struct ureg_src src
);
478 ureg_fixup_insn_size(struct ureg_program
*ureg
,
483 static INLINE void ureg_##op( struct ureg_program *ureg ) \
485 unsigned opcode = TGSI_OPCODE_##op; \
486 unsigned insn = ureg_emit_insn(ureg, \
497 ureg_fixup_insn_size( ureg, insn ); \
501 static INLINE void ureg_##op( struct ureg_program *ureg, \
502 struct ureg_src src ) \
504 unsigned opcode = TGSI_OPCODE_##op; \
505 unsigned insn = ureg_emit_insn(ureg, \
516 ureg_emit_src( ureg, src ); \
517 ureg_fixup_insn_size( ureg, insn ); \
520 #define OP00_LBL( op ) \
521 static INLINE void ureg_##op( struct ureg_program *ureg, \
522 unsigned *label_token ) \
524 unsigned opcode = TGSI_OPCODE_##op; \
525 struct ureg_emit_insn_result insn; \
526 insn = ureg_emit_insn(ureg, \
537 ureg_emit_label( ureg, insn.extended_token, label_token ); \
538 ureg_fixup_insn_size( ureg, insn.insn_token ); \
541 #define OP01_LBL( op ) \
542 static INLINE void ureg_##op( struct ureg_program *ureg, \
543 struct ureg_src src, \
544 unsigned *label_token ) \
546 unsigned opcode = TGSI_OPCODE_##op; \
547 struct ureg_emit_insn_result insn; \
548 insn = ureg_emit_insn(ureg, \
559 ureg_emit_label( ureg, insn.extended_token, label_token ); \
560 ureg_emit_src( ureg, src ); \
561 ureg_fixup_insn_size( ureg, insn.insn_token ); \
565 static INLINE void ureg_##op( struct ureg_program *ureg, \
566 struct ureg_dst dst ) \
568 unsigned opcode = TGSI_OPCODE_##op; \
569 unsigned insn = ureg_emit_insn(ureg, \
580 ureg_emit_dst( ureg, dst ); \
581 ureg_fixup_insn_size( ureg, insn ); \
586 static INLINE void ureg_##op( struct ureg_program *ureg, \
587 struct ureg_dst dst, \
588 struct ureg_src src ) \
590 unsigned opcode = TGSI_OPCODE_##op; \
591 unsigned insn = ureg_emit_insn(ureg, \
602 ureg_emit_dst( ureg, dst ); \
603 ureg_emit_src( ureg, src ); \
604 ureg_fixup_insn_size( ureg, insn ); \
608 static INLINE void ureg_##op( struct ureg_program *ureg, \
609 struct ureg_dst dst, \
610 struct ureg_src src0, \
611 struct ureg_src src1 ) \
613 unsigned opcode = TGSI_OPCODE_##op; \
614 unsigned insn = ureg_emit_insn(ureg, \
625 ureg_emit_dst( ureg, dst ); \
626 ureg_emit_src( ureg, src0 ); \
627 ureg_emit_src( ureg, src1 ); \
628 ureg_fixup_insn_size( ureg, insn ); \
631 #define OP12_TEX( op ) \
632 static INLINE void ureg_##op( struct ureg_program *ureg, \
633 struct ureg_dst dst, \
635 struct ureg_src src0, \
636 struct ureg_src src1 ) \
638 unsigned opcode = TGSI_OPCODE_##op; \
639 struct ureg_emit_insn_result insn; \
640 insn = ureg_emit_insn(ureg, \
651 ureg_emit_texture( ureg, insn.extended_token, target ); \
652 ureg_emit_dst( ureg, dst ); \
653 ureg_emit_src( ureg, src0 ); \
654 ureg_emit_src( ureg, src1 ); \
655 ureg_fixup_insn_size( ureg, insn.insn_token ); \
659 static INLINE void ureg_##op( struct ureg_program *ureg, \
660 struct ureg_dst dst, \
661 struct ureg_src src0, \
662 struct ureg_src src1, \
663 struct ureg_src src2 ) \
665 unsigned opcode = TGSI_OPCODE_##op; \
666 unsigned insn = ureg_emit_insn(ureg, \
677 ureg_emit_dst( ureg, dst ); \
678 ureg_emit_src( ureg, src0 ); \
679 ureg_emit_src( ureg, src1 ); \
680 ureg_emit_src( ureg, src2 ); \
681 ureg_fixup_insn_size( ureg, insn ); \
684 #define OP14_TEX( op ) \
685 static INLINE void ureg_##op( struct ureg_program *ureg, \
686 struct ureg_dst dst, \
688 struct ureg_src src0, \
689 struct ureg_src src1, \
690 struct ureg_src src2, \
691 struct ureg_src src3 ) \
693 unsigned opcode = TGSI_OPCODE_##op; \
694 struct ureg_emit_insn_result insn; \
695 insn = ureg_emit_insn(ureg, \
706 ureg_emit_texture( ureg, insn.extended_token, target ); \
707 ureg_emit_dst( ureg, dst ); \
708 ureg_emit_src( ureg, src0 ); \
709 ureg_emit_src( ureg, src1 ); \
710 ureg_emit_src( ureg, src2 ); \
711 ureg_emit_src( ureg, src3 ); \
712 ureg_fixup_insn_size( ureg, insn.insn_token ); \
716 /* Use a template include to generate a correctly-typed ureg_OP()
717 * function for each TGSI opcode:
719 #include "tgsi_opcode_tmp.h"
722 /***********************************************************************
723 * Inline helpers for manipulating register structs:
725 static INLINE
struct ureg_src
726 ureg_negate( struct ureg_src reg
)
728 assert(reg
.File
!= TGSI_FILE_NULL
);
733 static INLINE
struct ureg_src
734 ureg_abs( struct ureg_src reg
)
736 assert(reg
.File
!= TGSI_FILE_NULL
);
742 static INLINE
struct ureg_src
743 ureg_swizzle( struct ureg_src reg
,
744 int x
, int y
, int z
, int w
)
746 unsigned swz
= ( (reg
.SwizzleX
<< 0) |
747 (reg
.SwizzleY
<< 2) |
748 (reg
.SwizzleZ
<< 4) |
749 (reg
.SwizzleW
<< 6));
751 assert(reg
.File
!= TGSI_FILE_NULL
);
757 reg
.SwizzleX
= (swz
>> (x
*2)) & 0x3;
758 reg
.SwizzleY
= (swz
>> (y
*2)) & 0x3;
759 reg
.SwizzleZ
= (swz
>> (z
*2)) & 0x3;
760 reg
.SwizzleW
= (swz
>> (w
*2)) & 0x3;
764 static INLINE
struct ureg_src
765 ureg_scalar( struct ureg_src reg
, int x
)
767 return ureg_swizzle(reg
, x
, x
, x
, x
);
770 static INLINE
struct ureg_dst
771 ureg_writemask( struct ureg_dst reg
,
774 assert(reg
.File
!= TGSI_FILE_NULL
);
775 reg
.WriteMask
&= writemask
;
779 static INLINE
struct ureg_dst
780 ureg_saturate( struct ureg_dst reg
)
782 assert(reg
.File
!= TGSI_FILE_NULL
);
787 static INLINE
struct ureg_dst
788 ureg_predicate(struct ureg_dst reg
,
795 assert(reg
.File
!= TGSI_FILE_NULL
);
797 reg
.PredNegate
= negate
;
798 reg
.PredSwizzleX
= swizzle_x
;
799 reg
.PredSwizzleY
= swizzle_y
;
800 reg
.PredSwizzleZ
= swizzle_z
;
801 reg
.PredSwizzleW
= swizzle_w
;
805 static INLINE
struct ureg_dst
806 ureg_dst_indirect( struct ureg_dst reg
, struct ureg_src addr
)
808 assert(reg
.File
!= TGSI_FILE_NULL
);
809 assert(addr
.File
== TGSI_FILE_ADDRESS
);
811 reg
.IndirectIndex
= addr
.Index
;
812 reg
.IndirectSwizzle
= addr
.SwizzleX
;
816 static INLINE
struct ureg_src
817 ureg_src_indirect( struct ureg_src reg
, struct ureg_src addr
)
819 assert(reg
.File
!= TGSI_FILE_NULL
);
820 assert(addr
.File
== TGSI_FILE_ADDRESS
|| addr
.File
== TGSI_FILE_TEMPORARY
);
822 reg
.IndirectFile
= addr
.File
;
823 reg
.IndirectIndex
= addr
.Index
;
824 reg
.IndirectSwizzle
= addr
.SwizzleX
;
828 static INLINE
struct ureg_src
829 ureg_src_dimension( struct ureg_src reg
, int index
)
831 assert(reg
.File
!= TGSI_FILE_NULL
);
834 reg
.DimensionIndex
= index
;
839 static INLINE
struct ureg_src
840 ureg_src_dimension_indirect( struct ureg_src reg
, struct ureg_src addr
,
843 assert(reg
.File
!= TGSI_FILE_NULL
);
846 reg
.DimensionIndex
= index
;
847 reg
.DimIndFile
= addr
.File
;
848 reg
.DimIndIndex
= addr
.Index
;
849 reg
.DimIndSwizzle
= addr
.SwizzleX
;
853 static INLINE
struct ureg_dst
854 ureg_dst( struct ureg_src src
)
858 assert(!src
.Indirect
|| src
.IndirectFile
== TGSI_FILE_ADDRESS
);
861 dst
.WriteMask
= TGSI_WRITEMASK_XYZW
;
862 dst
.Indirect
= src
.Indirect
;
863 dst
.IndirectIndex
= src
.IndirectIndex
;
864 dst
.IndirectSwizzle
= src
.IndirectSwizzle
;
868 dst
.PredSwizzleX
= TGSI_SWIZZLE_X
;
869 dst
.PredSwizzleY
= TGSI_SWIZZLE_Y
;
870 dst
.PredSwizzleZ
= TGSI_SWIZZLE_Z
;
871 dst
.PredSwizzleW
= TGSI_SWIZZLE_W
;
872 dst
.Index
= src
.Index
;
877 static INLINE
struct ureg_src
878 ureg_src_register(unsigned file
,
884 src
.SwizzleX
= TGSI_SWIZZLE_X
;
885 src
.SwizzleY
= TGSI_SWIZZLE_Y
;
886 src
.SwizzleZ
= TGSI_SWIZZLE_Z
;
887 src
.SwizzleW
= TGSI_SWIZZLE_W
;
889 src
.IndirectFile
= TGSI_FILE_NULL
;
890 src
.IndirectIndex
= 0;
891 src
.IndirectSwizzle
= 0;
896 src
.DimensionIndex
= 0;
898 src
.DimIndFile
= TGSI_FILE_NULL
;
900 src
.DimIndSwizzle
= 0;
905 static INLINE
struct ureg_src
906 ureg_src( struct ureg_dst dst
)
911 src
.SwizzleX
= TGSI_SWIZZLE_X
;
912 src
.SwizzleY
= TGSI_SWIZZLE_Y
;
913 src
.SwizzleZ
= TGSI_SWIZZLE_Z
;
914 src
.SwizzleW
= TGSI_SWIZZLE_W
;
915 src
.Indirect
= dst
.Indirect
;
916 src
.IndirectFile
= TGSI_FILE_ADDRESS
;
917 src
.IndirectIndex
= dst
.IndirectIndex
;
918 src
.IndirectSwizzle
= dst
.IndirectSwizzle
;
920 src
.Index
= dst
.Index
;
923 src
.DimensionIndex
= 0;
925 src
.DimIndFile
= TGSI_FILE_NULL
;
927 src
.DimIndSwizzle
= 0;
934 static INLINE
struct ureg_dst
935 ureg_dst_undef( void )
939 dst
.File
= TGSI_FILE_NULL
;
942 dst
.IndirectIndex
= 0;
943 dst
.IndirectSwizzle
= 0;
947 dst
.PredSwizzleX
= TGSI_SWIZZLE_X
;
948 dst
.PredSwizzleY
= TGSI_SWIZZLE_Y
;
949 dst
.PredSwizzleZ
= TGSI_SWIZZLE_Z
;
950 dst
.PredSwizzleW
= TGSI_SWIZZLE_W
;
956 static INLINE
struct ureg_src
957 ureg_src_undef( void )
961 src
.File
= TGSI_FILE_NULL
;
967 src
.IndirectFile
= TGSI_FILE_NULL
;
968 src
.IndirectIndex
= 0;
969 src
.IndirectSwizzle
= 0;
974 src
.DimensionIndex
= 0;
976 src
.DimIndFile
= TGSI_FILE_NULL
;
978 src
.DimIndSwizzle
= 0;
983 static INLINE boolean
984 ureg_src_is_undef( struct ureg_src src
)
986 return src
.File
== TGSI_FILE_NULL
;
989 static INLINE boolean
990 ureg_dst_is_undef( struct ureg_dst dst
)
992 return dst
.File
== TGSI_FILE_NULL
;