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 Dimension
: 1; /* BOOL */
53 unsigned Absolute
: 1; /* BOOL */
54 unsigned Negate
: 1; /* BOOL */
55 int Index
: 16; /* SINT */
56 unsigned IndirectFile
: 4; /* TGSI_FILE_ */
57 int IndirectIndex
: 16; /* SINT */
58 unsigned IndirectSwizzle
: 2; /* TGSI_SWIZZLE_ */
59 int DimensionIndex
: 16; /* SINT */
62 /* Very similar to a tgsi_dst_register, removing unsupported fields
63 * and adding a Saturate flag. It's easier to push saturate into the
64 * destination register than to try and create a _SAT varient of each
65 * instruction function.
69 unsigned File
: 4; /* TGSI_FILE_ */
70 unsigned WriteMask
: 4; /* TGSI_WRITEMASK_ */
71 unsigned Indirect
: 1; /* BOOL */
72 unsigned Saturate
: 1; /* BOOL */
73 unsigned Predicate
: 1;
74 unsigned PredNegate
: 1; /* BOOL */
75 unsigned PredSwizzleX
: 2; /* TGSI_SWIZZLE_ */
76 unsigned PredSwizzleY
: 2; /* TGSI_SWIZZLE_ */
77 unsigned PredSwizzleZ
: 2; /* TGSI_SWIZZLE_ */
78 unsigned PredSwizzleW
: 2; /* TGSI_SWIZZLE_ */
79 int Index
: 16; /* SINT */
80 int IndirectIndex
: 16; /* SINT */
81 int IndirectSwizzle
: 2; /* TGSI_SWIZZLE_ */
87 ureg_create( unsigned processor
);
89 const struct tgsi_token
*
90 ureg_finalize( struct ureg_program
* );
92 /* Create and return a shader:
95 ureg_create_shader( struct ureg_program
*,
96 struct pipe_context
*pipe
);
99 /* Alternately, return the built token stream and hand ownership of
100 * that memory to the caller:
102 const struct tgsi_token
*
103 ureg_get_tokens( struct ureg_program
*ureg
,
104 unsigned *nr_tokens
);
108 ureg_destroy( struct ureg_program
* );
111 /***********************************************************************
112 * Convenience routine:
115 ureg_create_shader_and_destroy( struct ureg_program
*p
,
116 struct pipe_context
*pipe
)
118 void *result
= ureg_create_shader( p
, pipe
);
124 /***********************************************************************
125 * Build shader properties:
129 ureg_property_gs_input_prim(struct ureg_program
*ureg
,
130 unsigned input_prim
);
133 ureg_property_gs_output_prim(struct ureg_program
*ureg
,
134 unsigned output_prim
);
137 ureg_property_gs_max_vertices(struct ureg_program
*ureg
,
138 unsigned max_vertices
);
141 ureg_property_fs_coord_origin(struct ureg_program
*ureg
,
142 unsigned fs_coord_origin
);
145 ureg_property_fs_coord_pixel_center(struct ureg_program
*ureg
,
146 unsigned fs_coord_pixel_center
);
148 /***********************************************************************
149 * Build shader declarations:
153 ureg_DECL_fs_input_cyl(struct ureg_program
*,
154 unsigned semantic_name
,
155 unsigned semantic_index
,
156 unsigned interp_mode
,
157 unsigned cylindrical_wrap
);
159 static INLINE
struct ureg_src
160 ureg_DECL_fs_input(struct ureg_program
*ureg
,
161 unsigned semantic_name
,
162 unsigned semantic_index
,
163 unsigned interp_mode
)
165 return ureg_DECL_fs_input_cyl(ureg
,
173 ureg_DECL_vs_input( struct ureg_program
*,
177 ureg_DECL_gs_input(struct ureg_program
*,
179 unsigned semantic_name
,
180 unsigned semantic_index
);
183 ureg_DECL_system_value(struct ureg_program
*,
185 unsigned semantic_name
,
186 unsigned semantic_index
);
189 ureg_DECL_output( struct ureg_program
*,
190 unsigned semantic_name
,
191 unsigned semantic_index
);
194 ureg_DECL_immediate( struct ureg_program
*,
199 ureg_DECL_immediate_uint( struct ureg_program
*,
204 ureg_DECL_immediate_block_uint( struct ureg_program
*,
209 ureg_DECL_immediate_int( struct ureg_program
*,
214 ureg_DECL_constant2D(struct ureg_program
*ureg
,
220 ureg_DECL_constant( struct ureg_program
*,
224 ureg_DECL_temporary( struct ureg_program
* );
227 ureg_release_temporary( struct ureg_program
*ureg
,
228 struct ureg_dst tmp
);
231 ureg_DECL_address( struct ureg_program
* );
234 ureg_DECL_loop( struct ureg_program
* );
237 ureg_DECL_predicate(struct ureg_program
*);
239 /* Supply an index to the sampler declaration as this is the hook to
240 * the external pipe_sampler state. Users of this function probably
241 * don't want just any sampler, but a specific one which they've set
242 * up state for in the context.
245 ureg_DECL_sampler( struct ureg_program
*,
249 static INLINE
struct ureg_src
250 ureg_imm4f( struct ureg_program
*ureg
,
259 return ureg_DECL_immediate( ureg
, v
, 4 );
262 static INLINE
struct ureg_src
263 ureg_imm3f( struct ureg_program
*ureg
,
271 return ureg_DECL_immediate( ureg
, v
, 3 );
274 static INLINE
struct ureg_src
275 ureg_imm2f( struct ureg_program
*ureg
,
281 return ureg_DECL_immediate( ureg
, v
, 2 );
284 static INLINE
struct ureg_src
285 ureg_imm1f( struct ureg_program
*ureg
,
290 return ureg_DECL_immediate( ureg
, v
, 1 );
293 static INLINE
struct ureg_src
294 ureg_imm4u( struct ureg_program
*ureg
,
295 unsigned a
, unsigned b
,
296 unsigned c
, unsigned d
)
303 return ureg_DECL_immediate_uint( ureg
, v
, 4 );
306 static INLINE
struct ureg_src
307 ureg_imm3u( struct ureg_program
*ureg
,
308 unsigned a
, unsigned b
,
315 return ureg_DECL_immediate_uint( ureg
, v
, 3 );
318 static INLINE
struct ureg_src
319 ureg_imm2u( struct ureg_program
*ureg
,
320 unsigned a
, unsigned b
)
325 return ureg_DECL_immediate_uint( ureg
, v
, 2 );
328 static INLINE
struct ureg_src
329 ureg_imm1u( struct ureg_program
*ureg
,
332 return ureg_DECL_immediate_uint( ureg
, &a
, 1 );
335 static INLINE
struct ureg_src
336 ureg_imm4i( struct ureg_program
*ureg
,
345 return ureg_DECL_immediate_int( ureg
, v
, 4 );
348 static INLINE
struct ureg_src
349 ureg_imm3i( struct ureg_program
*ureg
,
357 return ureg_DECL_immediate_int( ureg
, v
, 3 );
360 static INLINE
struct ureg_src
361 ureg_imm2i( struct ureg_program
*ureg
,
367 return ureg_DECL_immediate_int( ureg
, v
, 2 );
370 static INLINE
struct ureg_src
371 ureg_imm1i( struct ureg_program
*ureg
,
374 return ureg_DECL_immediate_int( ureg
, &a
, 1 );
377 /***********************************************************************
378 * Functions for patching up labels
382 /* Will return a number which can be used in a label to point to the
383 * next instruction to be emitted.
386 ureg_get_instruction_number( struct ureg_program
*ureg
);
389 /* Patch a given label (expressed as a token number) to point to a
390 * given instruction (expressed as an instruction number).
392 * Labels are obtained from instruction emitters, eg ureg_CAL().
393 * Instruction numbers are obtained from ureg_get_instruction_number(),
397 ureg_fixup_label(struct ureg_program
*ureg
,
398 unsigned label_token
,
399 unsigned instruction_number
);
402 /* Generic instruction emitter. Use if you need to pass the opcode as
403 * a parameter, rather than using the emit_OP() varients below.
406 ureg_insn(struct ureg_program
*ureg
,
408 const struct ureg_dst
*dst
,
410 const struct ureg_src
*src
,
415 ureg_tex_insn(struct ureg_program
*ureg
,
417 const struct ureg_dst
*dst
,
420 const struct ureg_src
*src
,
425 ureg_label_insn(struct ureg_program
*ureg
,
427 const struct ureg_src
*src
,
432 /***********************************************************************
433 * Internal instruction helpers, don't call these directly:
436 struct ureg_emit_insn_result
{
437 unsigned insn_token
; /*< Used to fixup insn size. */
438 unsigned extended_token
; /*< Used to set the Extended bit, usually the same as insn_token. */
441 struct ureg_emit_insn_result
442 ureg_emit_insn(struct ureg_program
*ureg
,
447 unsigned pred_swizzle_x
,
448 unsigned pred_swizzle_y
,
449 unsigned pred_swizzle_z
,
450 unsigned pred_swizzle_w
,
455 ureg_emit_label(struct ureg_program
*ureg
,
457 unsigned *label_token
);
460 ureg_emit_texture(struct ureg_program
*ureg
,
465 ureg_emit_dst( struct ureg_program
*ureg
,
466 struct ureg_dst dst
);
469 ureg_emit_src( struct ureg_program
*ureg
,
470 struct ureg_src src
);
473 ureg_fixup_insn_size(struct ureg_program
*ureg
,
478 static INLINE void ureg_##op( struct ureg_program *ureg ) \
480 unsigned opcode = TGSI_OPCODE_##op; \
481 unsigned insn = ureg_emit_insn(ureg, \
492 ureg_fixup_insn_size( ureg, insn ); \
496 static INLINE void ureg_##op( struct ureg_program *ureg, \
497 struct ureg_src src ) \
499 unsigned opcode = TGSI_OPCODE_##op; \
500 unsigned insn = ureg_emit_insn(ureg, \
511 ureg_emit_src( ureg, src ); \
512 ureg_fixup_insn_size( ureg, insn ); \
515 #define OP00_LBL( op ) \
516 static INLINE void ureg_##op( struct ureg_program *ureg, \
517 unsigned *label_token ) \
519 unsigned opcode = TGSI_OPCODE_##op; \
520 struct ureg_emit_insn_result insn; \
521 insn = ureg_emit_insn(ureg, \
532 ureg_emit_label( ureg, insn.extended_token, label_token ); \
533 ureg_fixup_insn_size( ureg, insn.insn_token ); \
536 #define OP01_LBL( op ) \
537 static INLINE void ureg_##op( struct ureg_program *ureg, \
538 struct ureg_src src, \
539 unsigned *label_token ) \
541 unsigned opcode = TGSI_OPCODE_##op; \
542 struct ureg_emit_insn_result insn; \
543 insn = ureg_emit_insn(ureg, \
554 ureg_emit_label( ureg, insn.extended_token, label_token ); \
555 ureg_emit_src( ureg, src ); \
556 ureg_fixup_insn_size( ureg, insn.insn_token ); \
560 static INLINE void ureg_##op( struct ureg_program *ureg, \
561 struct ureg_dst dst ) \
563 unsigned opcode = TGSI_OPCODE_##op; \
564 unsigned insn = ureg_emit_insn(ureg, \
575 ureg_emit_dst( ureg, dst ); \
576 ureg_fixup_insn_size( ureg, insn ); \
581 static INLINE void ureg_##op( struct ureg_program *ureg, \
582 struct ureg_dst dst, \
583 struct ureg_src src ) \
585 unsigned opcode = TGSI_OPCODE_##op; \
586 unsigned insn = ureg_emit_insn(ureg, \
597 ureg_emit_dst( ureg, dst ); \
598 ureg_emit_src( ureg, src ); \
599 ureg_fixup_insn_size( ureg, insn ); \
603 static INLINE void ureg_##op( struct ureg_program *ureg, \
604 struct ureg_dst dst, \
605 struct ureg_src src0, \
606 struct ureg_src src1 ) \
608 unsigned opcode = TGSI_OPCODE_##op; \
609 unsigned insn = ureg_emit_insn(ureg, \
620 ureg_emit_dst( ureg, dst ); \
621 ureg_emit_src( ureg, src0 ); \
622 ureg_emit_src( ureg, src1 ); \
623 ureg_fixup_insn_size( ureg, insn ); \
626 #define OP12_TEX( op ) \
627 static INLINE void ureg_##op( struct ureg_program *ureg, \
628 struct ureg_dst dst, \
630 struct ureg_src src0, \
631 struct ureg_src src1 ) \
633 unsigned opcode = TGSI_OPCODE_##op; \
634 struct ureg_emit_insn_result insn; \
635 insn = ureg_emit_insn(ureg, \
646 ureg_emit_texture( ureg, insn.extended_token, target ); \
647 ureg_emit_dst( ureg, dst ); \
648 ureg_emit_src( ureg, src0 ); \
649 ureg_emit_src( ureg, src1 ); \
650 ureg_fixup_insn_size( ureg, insn.insn_token ); \
654 static INLINE void ureg_##op( struct ureg_program *ureg, \
655 struct ureg_dst dst, \
656 struct ureg_src src0, \
657 struct ureg_src src1, \
658 struct ureg_src src2 ) \
660 unsigned opcode = TGSI_OPCODE_##op; \
661 unsigned insn = ureg_emit_insn(ureg, \
672 ureg_emit_dst( ureg, dst ); \
673 ureg_emit_src( ureg, src0 ); \
674 ureg_emit_src( ureg, src1 ); \
675 ureg_emit_src( ureg, src2 ); \
676 ureg_fixup_insn_size( ureg, insn ); \
679 #define OP14_TEX( op ) \
680 static INLINE void ureg_##op( struct ureg_program *ureg, \
681 struct ureg_dst dst, \
683 struct ureg_src src0, \
684 struct ureg_src src1, \
685 struct ureg_src src2, \
686 struct ureg_src src3 ) \
688 unsigned opcode = TGSI_OPCODE_##op; \
689 struct ureg_emit_insn_result insn; \
690 insn = ureg_emit_insn(ureg, \
701 ureg_emit_texture( ureg, insn.extended_token, target ); \
702 ureg_emit_dst( ureg, dst ); \
703 ureg_emit_src( ureg, src0 ); \
704 ureg_emit_src( ureg, src1 ); \
705 ureg_emit_src( ureg, src2 ); \
706 ureg_emit_src( ureg, src3 ); \
707 ureg_fixup_insn_size( ureg, insn.insn_token ); \
711 /* Use a template include to generate a correctly-typed ureg_OP()
712 * function for each TGSI opcode:
714 #include "tgsi_opcode_tmp.h"
717 /***********************************************************************
718 * Inline helpers for manipulating register structs:
720 static INLINE
struct ureg_src
721 ureg_negate( struct ureg_src reg
)
723 assert(reg
.File
!= TGSI_FILE_NULL
);
728 static INLINE
struct ureg_src
729 ureg_abs( struct ureg_src reg
)
731 assert(reg
.File
!= TGSI_FILE_NULL
);
737 static INLINE
struct ureg_src
738 ureg_swizzle( struct ureg_src reg
,
739 int x
, int y
, int z
, int w
)
741 unsigned swz
= ( (reg
.SwizzleX
<< 0) |
742 (reg
.SwizzleY
<< 2) |
743 (reg
.SwizzleZ
<< 4) |
744 (reg
.SwizzleW
<< 6));
746 assert(reg
.File
!= TGSI_FILE_NULL
);
752 reg
.SwizzleX
= (swz
>> (x
*2)) & 0x3;
753 reg
.SwizzleY
= (swz
>> (y
*2)) & 0x3;
754 reg
.SwizzleZ
= (swz
>> (z
*2)) & 0x3;
755 reg
.SwizzleW
= (swz
>> (w
*2)) & 0x3;
759 static INLINE
struct ureg_src
760 ureg_scalar( struct ureg_src reg
, int x
)
762 return ureg_swizzle(reg
, x
, x
, x
, x
);
765 static INLINE
struct ureg_dst
766 ureg_writemask( struct ureg_dst reg
,
769 assert(reg
.File
!= TGSI_FILE_NULL
);
770 reg
.WriteMask
&= writemask
;
774 static INLINE
struct ureg_dst
775 ureg_saturate( struct ureg_dst reg
)
777 assert(reg
.File
!= TGSI_FILE_NULL
);
782 static INLINE
struct ureg_dst
783 ureg_predicate(struct ureg_dst reg
,
790 assert(reg
.File
!= TGSI_FILE_NULL
);
792 reg
.PredNegate
= negate
;
793 reg
.PredSwizzleX
= swizzle_x
;
794 reg
.PredSwizzleY
= swizzle_y
;
795 reg
.PredSwizzleZ
= swizzle_z
;
796 reg
.PredSwizzleW
= swizzle_w
;
800 static INLINE
struct ureg_dst
801 ureg_dst_indirect( struct ureg_dst reg
, struct ureg_src addr
)
803 assert(reg
.File
!= TGSI_FILE_NULL
);
804 assert(addr
.File
== TGSI_FILE_ADDRESS
);
806 reg
.IndirectIndex
= addr
.Index
;
807 reg
.IndirectSwizzle
= addr
.SwizzleX
;
811 static INLINE
struct ureg_src
812 ureg_src_indirect( struct ureg_src reg
, struct ureg_src addr
)
814 assert(reg
.File
!= TGSI_FILE_NULL
);
815 assert(addr
.File
== TGSI_FILE_ADDRESS
|| addr
.File
== TGSI_FILE_TEMPORARY
);
817 reg
.IndirectFile
= addr
.File
;
818 reg
.IndirectIndex
= addr
.Index
;
819 reg
.IndirectSwizzle
= addr
.SwizzleX
;
823 static INLINE
struct ureg_src
824 ureg_src_dimension( struct ureg_src reg
, int index
)
826 assert(reg
.File
!= TGSI_FILE_NULL
);
828 reg
.DimensionIndex
= index
;
832 static INLINE
struct ureg_dst
833 ureg_dst( struct ureg_src src
)
837 assert(!src
.Indirect
|| src
.IndirectFile
== TGSI_FILE_ADDRESS
);
840 dst
.WriteMask
= TGSI_WRITEMASK_XYZW
;
841 dst
.Indirect
= src
.Indirect
;
842 dst
.IndirectIndex
= src
.IndirectIndex
;
843 dst
.IndirectSwizzle
= src
.IndirectSwizzle
;
847 dst
.PredSwizzleX
= TGSI_SWIZZLE_X
;
848 dst
.PredSwizzleY
= TGSI_SWIZZLE_Y
;
849 dst
.PredSwizzleZ
= TGSI_SWIZZLE_Z
;
850 dst
.PredSwizzleW
= TGSI_SWIZZLE_W
;
851 dst
.Index
= src
.Index
;
856 static INLINE
struct ureg_src
857 ureg_src_register(unsigned file
,
863 src
.SwizzleX
= TGSI_SWIZZLE_X
;
864 src
.SwizzleY
= TGSI_SWIZZLE_Y
;
865 src
.SwizzleZ
= TGSI_SWIZZLE_Z
;
866 src
.SwizzleW
= TGSI_SWIZZLE_W
;
868 src
.IndirectFile
= TGSI_FILE_NULL
;
869 src
.IndirectIndex
= 0;
870 src
.IndirectSwizzle
= 0;
875 src
.DimensionIndex
= 0;
880 static INLINE
struct ureg_src
881 ureg_src( struct ureg_dst dst
)
886 src
.SwizzleX
= TGSI_SWIZZLE_X
;
887 src
.SwizzleY
= TGSI_SWIZZLE_Y
;
888 src
.SwizzleZ
= TGSI_SWIZZLE_Z
;
889 src
.SwizzleW
= TGSI_SWIZZLE_W
;
890 src
.Indirect
= dst
.Indirect
;
891 src
.IndirectFile
= TGSI_FILE_ADDRESS
;
892 src
.IndirectIndex
= dst
.IndirectIndex
;
893 src
.IndirectSwizzle
= dst
.IndirectSwizzle
;
895 src
.Index
= dst
.Index
;
898 src
.DimensionIndex
= 0;
905 static INLINE
struct ureg_dst
906 ureg_dst_undef( void )
910 dst
.File
= TGSI_FILE_NULL
;
913 dst
.IndirectIndex
= 0;
914 dst
.IndirectSwizzle
= 0;
918 dst
.PredSwizzleX
= TGSI_SWIZZLE_X
;
919 dst
.PredSwizzleY
= TGSI_SWIZZLE_Y
;
920 dst
.PredSwizzleZ
= TGSI_SWIZZLE_Z
;
921 dst
.PredSwizzleW
= TGSI_SWIZZLE_W
;
927 static INLINE
struct ureg_src
928 ureg_src_undef( void )
932 src
.File
= TGSI_FILE_NULL
;
938 src
.IndirectFile
= TGSI_FILE_NULL
;
939 src
.IndirectIndex
= 0;
940 src
.IndirectSwizzle
= 0;
945 src
.DimensionIndex
= 0;
950 static INLINE boolean
951 ureg_src_is_undef( struct ureg_src src
)
953 return src
.File
== TGSI_FILE_NULL
;
956 static INLINE boolean
957 ureg_dst_is_undef( struct ureg_dst dst
)
959 return dst
.File
== TGSI_FILE_NULL
;