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"
40 /* Almost a tgsi_src_register, but we need to pull in the Absolute
41 * flag from the _ext token. Indirect flag always implies ADDR[0].
45 unsigned File
: 4; /* TGSI_FILE_ */
46 unsigned SwizzleX
: 2; /* TGSI_SWIZZLE_ */
47 unsigned SwizzleY
: 2; /* TGSI_SWIZZLE_ */
48 unsigned SwizzleZ
: 2; /* TGSI_SWIZZLE_ */
49 unsigned SwizzleW
: 2; /* TGSI_SWIZZLE_ */
50 unsigned Indirect
: 1; /* BOOL */
51 unsigned Dimension
: 1; /* BOOL */
52 unsigned Absolute
: 1; /* BOOL */
53 unsigned Negate
: 1; /* BOOL */
54 int Index
: 16; /* SINT */
55 unsigned IndirectFile
: 4; /* TGSI_FILE_ */
56 int IndirectIndex
: 16; /* SINT */
57 unsigned IndirectSwizzle
: 2; /* TGSI_SWIZZLE_ */
58 int DimensionIndex
: 16; /* SINT */
61 /* Very similar to a tgsi_dst_register, removing unsupported fields
62 * and adding a Saturate flag. It's easier to push saturate into the
63 * destination register than to try and create a _SAT varient of each
64 * instruction function.
68 unsigned File
: 4; /* TGSI_FILE_ */
69 unsigned WriteMask
: 4; /* TGSI_WRITEMASK_ */
70 unsigned Indirect
: 1; /* BOOL */
71 unsigned Saturate
: 1; /* BOOL */
72 unsigned Predicate
: 1;
73 unsigned PredNegate
: 1; /* BOOL */
74 unsigned PredSwizzleX
: 2; /* TGSI_SWIZZLE_ */
75 unsigned PredSwizzleY
: 2; /* TGSI_SWIZZLE_ */
76 unsigned PredSwizzleZ
: 2; /* TGSI_SWIZZLE_ */
77 unsigned PredSwizzleW
: 2; /* TGSI_SWIZZLE_ */
78 int Index
: 16; /* SINT */
79 int IndirectIndex
: 16; /* SINT */
80 int IndirectSwizzle
: 2; /* TGSI_SWIZZLE_ */
86 ureg_create( unsigned processor
);
88 const struct tgsi_token
*
89 ureg_finalize( struct ureg_program
* );
91 /* Create and return a shader:
94 ureg_create_shader( struct ureg_program
*,
95 struct pipe_context
*pipe
);
98 /* Alternately, return the built token stream and hand ownership of
99 * that memory to the caller:
101 const struct tgsi_token
*
102 ureg_get_tokens( struct ureg_program
*ureg
,
103 unsigned *nr_tokens
);
107 ureg_destroy( struct ureg_program
* );
110 /***********************************************************************
111 * Convenience routine:
114 ureg_create_shader_and_destroy( struct ureg_program
*p
,
115 struct pipe_context
*pipe
)
117 void *result
= ureg_create_shader( p
, pipe
);
123 /***********************************************************************
124 * Build shader properties:
128 ureg_property_gs_input_prim(struct ureg_program
*ureg
,
129 unsigned input_prim
);
132 ureg_property_gs_output_prim(struct ureg_program
*ureg
,
133 unsigned output_prim
);
136 ureg_property_gs_max_vertices(struct ureg_program
*ureg
,
137 unsigned max_vertices
);
140 ureg_property_fs_coord_origin(struct ureg_program
*ureg
,
141 unsigned fs_coord_origin
);
144 ureg_property_fs_coord_pixel_center(struct ureg_program
*ureg
,
145 unsigned fs_coord_pixel_center
);
147 /***********************************************************************
148 * Build shader declarations:
152 ureg_DECL_fs_input( struct ureg_program
*,
153 unsigned semantic_name
,
154 unsigned semantic_index
,
155 unsigned interp_mode
);
158 ureg_DECL_vs_input( struct ureg_program
*,
162 ureg_DECL_gs_input(struct ureg_program
*,
164 unsigned semantic_name
,
165 unsigned semantic_index
);
168 ureg_DECL_system_value(struct ureg_program
*,
170 unsigned semantic_name
,
171 unsigned semantic_index
);
174 ureg_DECL_output( struct ureg_program
*,
175 unsigned semantic_name
,
176 unsigned semantic_index
);
179 ureg_DECL_immediate( struct ureg_program
*,
184 ureg_DECL_immediate_uint( struct ureg_program
*,
189 ureg_DECL_immediate_block_uint( struct ureg_program
*,
194 ureg_DECL_immediate_int( struct ureg_program
*,
199 ureg_DECL_constant2D(struct ureg_program
*ureg
,
205 ureg_DECL_constant( struct ureg_program
*,
209 ureg_DECL_temporary( struct ureg_program
* );
212 ureg_release_temporary( struct ureg_program
*ureg
,
213 struct ureg_dst tmp
);
216 ureg_DECL_address( struct ureg_program
* );
219 ureg_DECL_loop( struct ureg_program
* );
222 ureg_DECL_predicate(struct ureg_program
*);
224 /* Supply an index to the sampler declaration as this is the hook to
225 * the external pipe_sampler state. Users of this function probably
226 * don't want just any sampler, but a specific one which they've set
227 * up state for in the context.
230 ureg_DECL_sampler( struct ureg_program
*,
234 static INLINE
struct ureg_src
235 ureg_imm4f( struct ureg_program
*ureg
,
244 return ureg_DECL_immediate( ureg
, v
, 4 );
247 static INLINE
struct ureg_src
248 ureg_imm3f( struct ureg_program
*ureg
,
256 return ureg_DECL_immediate( ureg
, v
, 3 );
259 static INLINE
struct ureg_src
260 ureg_imm2f( struct ureg_program
*ureg
,
266 return ureg_DECL_immediate( ureg
, v
, 2 );
269 static INLINE
struct ureg_src
270 ureg_imm1f( struct ureg_program
*ureg
,
275 return ureg_DECL_immediate( ureg
, v
, 1 );
278 static INLINE
struct ureg_src
279 ureg_imm4u( struct ureg_program
*ureg
,
280 unsigned a
, unsigned b
,
281 unsigned c
, unsigned d
)
288 return ureg_DECL_immediate_uint( ureg
, v
, 4 );
291 static INLINE
struct ureg_src
292 ureg_imm3u( struct ureg_program
*ureg
,
293 unsigned a
, unsigned b
,
300 return ureg_DECL_immediate_uint( ureg
, v
, 3 );
303 static INLINE
struct ureg_src
304 ureg_imm2u( struct ureg_program
*ureg
,
305 unsigned a
, unsigned b
)
310 return ureg_DECL_immediate_uint( ureg
, v
, 2 );
313 static INLINE
struct ureg_src
314 ureg_imm1u( struct ureg_program
*ureg
,
317 return ureg_DECL_immediate_uint( ureg
, &a
, 1 );
320 static INLINE
struct ureg_src
321 ureg_imm4i( struct ureg_program
*ureg
,
330 return ureg_DECL_immediate_int( ureg
, v
, 4 );
333 static INLINE
struct ureg_src
334 ureg_imm3i( struct ureg_program
*ureg
,
342 return ureg_DECL_immediate_int( ureg
, v
, 3 );
345 static INLINE
struct ureg_src
346 ureg_imm2i( struct ureg_program
*ureg
,
352 return ureg_DECL_immediate_int( ureg
, v
, 2 );
355 static INLINE
struct ureg_src
356 ureg_imm1i( struct ureg_program
*ureg
,
359 return ureg_DECL_immediate_int( ureg
, &a
, 1 );
362 /***********************************************************************
363 * Functions for patching up labels
367 /* Will return a number which can be used in a label to point to the
368 * next instruction to be emitted.
371 ureg_get_instruction_number( struct ureg_program
*ureg
);
374 /* Patch a given label (expressed as a token number) to point to a
375 * given instruction (expressed as an instruction number).
377 * Labels are obtained from instruction emitters, eg ureg_CAL().
378 * Instruction numbers are obtained from ureg_get_instruction_number(),
382 ureg_fixup_label(struct ureg_program
*ureg
,
383 unsigned label_token
,
384 unsigned instruction_number
);
387 /* Generic instruction emitter. Use if you need to pass the opcode as
388 * a parameter, rather than using the emit_OP() varients below.
391 ureg_insn(struct ureg_program
*ureg
,
393 const struct ureg_dst
*dst
,
395 const struct ureg_src
*src
,
400 ureg_tex_insn(struct ureg_program
*ureg
,
402 const struct ureg_dst
*dst
,
405 const struct ureg_src
*src
,
410 ureg_label_insn(struct ureg_program
*ureg
,
412 const struct ureg_src
*src
,
417 /***********************************************************************
418 * Internal instruction helpers, don't call these directly:
421 struct ureg_emit_insn_result
{
422 unsigned insn_token
; /*< Used to fixup insn size. */
423 unsigned extended_token
; /*< Used to set the Extended bit, usually the same as insn_token. */
426 struct ureg_emit_insn_result
427 ureg_emit_insn(struct ureg_program
*ureg
,
432 unsigned pred_swizzle_x
,
433 unsigned pred_swizzle_y
,
434 unsigned pred_swizzle_z
,
435 unsigned pred_swizzle_w
,
440 ureg_emit_label(struct ureg_program
*ureg
,
442 unsigned *label_token
);
445 ureg_emit_texture(struct ureg_program
*ureg
,
450 ureg_emit_dst( struct ureg_program
*ureg
,
451 struct ureg_dst dst
);
454 ureg_emit_src( struct ureg_program
*ureg
,
455 struct ureg_src src
);
458 ureg_fixup_insn_size(struct ureg_program
*ureg
,
463 static INLINE void ureg_##op( struct ureg_program *ureg ) \
465 unsigned opcode = TGSI_OPCODE_##op; \
466 unsigned insn = ureg_emit_insn(ureg, \
477 ureg_fixup_insn_size( ureg, insn ); \
481 static INLINE void ureg_##op( struct ureg_program *ureg, \
482 struct ureg_src src ) \
484 unsigned opcode = TGSI_OPCODE_##op; \
485 unsigned insn = ureg_emit_insn(ureg, \
496 ureg_emit_src( ureg, src ); \
497 ureg_fixup_insn_size( ureg, insn ); \
500 #define OP00_LBL( op ) \
501 static INLINE void ureg_##op( struct ureg_program *ureg, \
502 unsigned *label_token ) \
504 unsigned opcode = TGSI_OPCODE_##op; \
505 struct ureg_emit_insn_result insn; \
506 insn = ureg_emit_insn(ureg, \
517 ureg_emit_label( ureg, insn.extended_token, label_token ); \
518 ureg_fixup_insn_size( ureg, insn.insn_token ); \
521 #define OP01_LBL( op ) \
522 static INLINE void ureg_##op( struct ureg_program *ureg, \
523 struct ureg_src src, \
524 unsigned *label_token ) \
526 unsigned opcode = TGSI_OPCODE_##op; \
527 struct ureg_emit_insn_result insn; \
528 insn = ureg_emit_insn(ureg, \
539 ureg_emit_label( ureg, insn.extended_token, label_token ); \
540 ureg_emit_src( ureg, src ); \
541 ureg_fixup_insn_size( ureg, insn.insn_token ); \
545 static INLINE void ureg_##op( struct ureg_program *ureg, \
546 struct ureg_dst dst ) \
548 unsigned opcode = TGSI_OPCODE_##op; \
549 unsigned insn = ureg_emit_insn(ureg, \
560 ureg_emit_dst( ureg, dst ); \
561 ureg_fixup_insn_size( ureg, insn ); \
566 static INLINE void ureg_##op( struct ureg_program *ureg, \
567 struct ureg_dst dst, \
568 struct ureg_src src ) \
570 unsigned opcode = TGSI_OPCODE_##op; \
571 unsigned insn = ureg_emit_insn(ureg, \
582 ureg_emit_dst( ureg, dst ); \
583 ureg_emit_src( ureg, src ); \
584 ureg_fixup_insn_size( ureg, insn ); \
588 static INLINE void ureg_##op( struct ureg_program *ureg, \
589 struct ureg_dst dst, \
590 struct ureg_src src0, \
591 struct ureg_src src1 ) \
593 unsigned opcode = TGSI_OPCODE_##op; \
594 unsigned insn = ureg_emit_insn(ureg, \
605 ureg_emit_dst( ureg, dst ); \
606 ureg_emit_src( ureg, src0 ); \
607 ureg_emit_src( ureg, src1 ); \
608 ureg_fixup_insn_size( ureg, insn ); \
611 #define OP12_TEX( op ) \
612 static INLINE void ureg_##op( struct ureg_program *ureg, \
613 struct ureg_dst dst, \
615 struct ureg_src src0, \
616 struct ureg_src src1 ) \
618 unsigned opcode = TGSI_OPCODE_##op; \
619 struct ureg_emit_insn_result insn; \
620 insn = ureg_emit_insn(ureg, \
631 ureg_emit_texture( ureg, insn.extended_token, target ); \
632 ureg_emit_dst( ureg, dst ); \
633 ureg_emit_src( ureg, src0 ); \
634 ureg_emit_src( ureg, src1 ); \
635 ureg_fixup_insn_size( ureg, insn.insn_token ); \
639 static INLINE void ureg_##op( struct ureg_program *ureg, \
640 struct ureg_dst dst, \
641 struct ureg_src src0, \
642 struct ureg_src src1, \
643 struct ureg_src src2 ) \
645 unsigned opcode = TGSI_OPCODE_##op; \
646 unsigned insn = ureg_emit_insn(ureg, \
657 ureg_emit_dst( ureg, dst ); \
658 ureg_emit_src( ureg, src0 ); \
659 ureg_emit_src( ureg, src1 ); \
660 ureg_emit_src( ureg, src2 ); \
661 ureg_fixup_insn_size( ureg, insn ); \
664 #define OP14_TEX( op ) \
665 static INLINE void ureg_##op( struct ureg_program *ureg, \
666 struct ureg_dst dst, \
668 struct ureg_src src0, \
669 struct ureg_src src1, \
670 struct ureg_src src2, \
671 struct ureg_src src3 ) \
673 unsigned opcode = TGSI_OPCODE_##op; \
674 struct ureg_emit_insn_result insn; \
675 insn = ureg_emit_insn(ureg, \
686 ureg_emit_texture( ureg, insn.extended_token, target ); \
687 ureg_emit_dst( ureg, dst ); \
688 ureg_emit_src( ureg, src0 ); \
689 ureg_emit_src( ureg, src1 ); \
690 ureg_emit_src( ureg, src2 ); \
691 ureg_emit_src( ureg, src3 ); \
692 ureg_fixup_insn_size( ureg, insn.insn_token ); \
696 /* Use a template include to generate a correctly-typed ureg_OP()
697 * function for each TGSI opcode:
699 #include "tgsi_opcode_tmp.h"
702 /***********************************************************************
703 * Inline helpers for manipulating register structs:
705 static INLINE
struct ureg_src
706 ureg_negate( struct ureg_src reg
)
708 assert(reg
.File
!= TGSI_FILE_NULL
);
713 static INLINE
struct ureg_src
714 ureg_abs( struct ureg_src reg
)
716 assert(reg
.File
!= TGSI_FILE_NULL
);
722 static INLINE
struct ureg_src
723 ureg_swizzle( struct ureg_src reg
,
724 int x
, int y
, int z
, int w
)
726 unsigned swz
= ( (reg
.SwizzleX
<< 0) |
727 (reg
.SwizzleY
<< 2) |
728 (reg
.SwizzleZ
<< 4) |
729 (reg
.SwizzleW
<< 6));
731 assert(reg
.File
!= TGSI_FILE_NULL
);
737 reg
.SwizzleX
= (swz
>> (x
*2)) & 0x3;
738 reg
.SwizzleY
= (swz
>> (y
*2)) & 0x3;
739 reg
.SwizzleZ
= (swz
>> (z
*2)) & 0x3;
740 reg
.SwizzleW
= (swz
>> (w
*2)) & 0x3;
744 static INLINE
struct ureg_src
745 ureg_scalar( struct ureg_src reg
, int x
)
747 return ureg_swizzle(reg
, x
, x
, x
, x
);
750 static INLINE
struct ureg_dst
751 ureg_writemask( struct ureg_dst reg
,
754 assert(reg
.File
!= TGSI_FILE_NULL
);
755 reg
.WriteMask
&= writemask
;
759 static INLINE
struct ureg_dst
760 ureg_saturate( struct ureg_dst reg
)
762 assert(reg
.File
!= TGSI_FILE_NULL
);
767 static INLINE
struct ureg_dst
768 ureg_predicate(struct ureg_dst reg
,
775 assert(reg
.File
!= TGSI_FILE_NULL
);
777 reg
.PredNegate
= negate
;
778 reg
.PredSwizzleX
= swizzle_x
;
779 reg
.PredSwizzleY
= swizzle_y
;
780 reg
.PredSwizzleZ
= swizzle_z
;
781 reg
.PredSwizzleW
= swizzle_w
;
785 static INLINE
struct ureg_dst
786 ureg_dst_indirect( struct ureg_dst reg
, struct ureg_src addr
)
788 assert(reg
.File
!= TGSI_FILE_NULL
);
789 assert(addr
.File
== TGSI_FILE_ADDRESS
);
791 reg
.IndirectIndex
= addr
.Index
;
792 reg
.IndirectSwizzle
= addr
.SwizzleX
;
796 static INLINE
struct ureg_src
797 ureg_src_indirect( struct ureg_src reg
, struct ureg_src addr
)
799 assert(reg
.File
!= TGSI_FILE_NULL
);
800 assert(addr
.File
== TGSI_FILE_ADDRESS
|| addr
.File
== TGSI_FILE_TEMPORARY
);
802 reg
.IndirectFile
= addr
.File
;
803 reg
.IndirectIndex
= addr
.Index
;
804 reg
.IndirectSwizzle
= addr
.SwizzleX
;
808 static INLINE
struct ureg_src
809 ureg_src_dimension( struct ureg_src reg
, int index
)
811 assert(reg
.File
!= TGSI_FILE_NULL
);
813 reg
.DimensionIndex
= index
;
817 static INLINE
struct ureg_dst
818 ureg_dst( struct ureg_src src
)
822 assert(!src
.Indirect
|| src
.IndirectFile
== TGSI_FILE_ADDRESS
);
825 dst
.WriteMask
= TGSI_WRITEMASK_XYZW
;
826 dst
.Indirect
= src
.Indirect
;
827 dst
.IndirectIndex
= src
.IndirectIndex
;
828 dst
.IndirectSwizzle
= src
.IndirectSwizzle
;
832 dst
.PredSwizzleX
= TGSI_SWIZZLE_X
;
833 dst
.PredSwizzleY
= TGSI_SWIZZLE_Y
;
834 dst
.PredSwizzleZ
= TGSI_SWIZZLE_Z
;
835 dst
.PredSwizzleW
= TGSI_SWIZZLE_W
;
836 dst
.Index
= src
.Index
;
841 static INLINE
struct ureg_src
842 ureg_src_register(unsigned file
,
848 src
.SwizzleX
= TGSI_SWIZZLE_X
;
849 src
.SwizzleY
= TGSI_SWIZZLE_Y
;
850 src
.SwizzleZ
= TGSI_SWIZZLE_Z
;
851 src
.SwizzleW
= TGSI_SWIZZLE_W
;
853 src
.IndirectFile
= TGSI_FILE_NULL
;
854 src
.IndirectIndex
= 0;
855 src
.IndirectSwizzle
= 0;
860 src
.DimensionIndex
= 0;
865 static INLINE
struct ureg_src
866 ureg_src( struct ureg_dst dst
)
871 src
.SwizzleX
= TGSI_SWIZZLE_X
;
872 src
.SwizzleY
= TGSI_SWIZZLE_Y
;
873 src
.SwizzleZ
= TGSI_SWIZZLE_Z
;
874 src
.SwizzleW
= TGSI_SWIZZLE_W
;
875 src
.Indirect
= dst
.Indirect
;
876 src
.IndirectFile
= TGSI_FILE_ADDRESS
;
877 src
.IndirectIndex
= dst
.IndirectIndex
;
878 src
.IndirectSwizzle
= dst
.IndirectSwizzle
;
880 src
.Index
= dst
.Index
;
883 src
.DimensionIndex
= 0;
890 static INLINE
struct ureg_dst
891 ureg_dst_undef( void )
895 dst
.File
= TGSI_FILE_NULL
;
898 dst
.IndirectIndex
= 0;
899 dst
.IndirectSwizzle
= 0;
903 dst
.PredSwizzleX
= TGSI_SWIZZLE_X
;
904 dst
.PredSwizzleY
= TGSI_SWIZZLE_Y
;
905 dst
.PredSwizzleZ
= TGSI_SWIZZLE_Z
;
906 dst
.PredSwizzleW
= TGSI_SWIZZLE_W
;
912 static INLINE
struct ureg_src
913 ureg_src_undef( void )
917 src
.File
= TGSI_FILE_NULL
;
923 src
.IndirectFile
= TGSI_FILE_NULL
;
924 src
.IndirectIndex
= 0;
925 src
.IndirectSwizzle
= 0;
930 src
.DimensionIndex
= 0;
935 static INLINE boolean
936 ureg_src_is_undef( struct ureg_src src
)
938 return src
.File
== TGSI_FILE_NULL
;
941 static INLINE boolean
942 ureg_dst_is_undef( struct ureg_dst dst
)
944 return dst
.File
== TGSI_FILE_NULL
;