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 /***********************************************************************
141 * Build shader declarations:
145 ureg_DECL_fs_input( struct ureg_program
*,
146 unsigned semantic_name
,
147 unsigned semantic_index
,
148 unsigned interp_mode
);
151 ureg_DECL_vs_input( struct ureg_program
*,
155 ureg_DECL_gs_input(struct ureg_program
*,
159 ureg_DECL_system_value(struct ureg_program
*,
161 unsigned semantic_name
,
162 unsigned semantic_index
);
165 ureg_DECL_output( struct ureg_program
*,
166 unsigned semantic_name
,
167 unsigned semantic_index
);
170 ureg_DECL_immediate( struct ureg_program
*,
175 ureg_DECL_immediate_uint( struct ureg_program
*,
180 ureg_DECL_immediate_block_uint( struct ureg_program
*,
185 ureg_DECL_immediate_int( struct ureg_program
*,
190 ureg_DECL_constant2D(struct ureg_program
*ureg
,
196 ureg_DECL_constant( struct ureg_program
*,
200 ureg_DECL_temporary( struct ureg_program
* );
203 ureg_release_temporary( struct ureg_program
*ureg
,
204 struct ureg_dst tmp
);
207 ureg_DECL_address( struct ureg_program
* );
210 ureg_DECL_loop( struct ureg_program
* );
213 ureg_DECL_predicate(struct ureg_program
*);
215 /* Supply an index to the sampler declaration as this is the hook to
216 * the external pipe_sampler state. Users of this function probably
217 * don't want just any sampler, but a specific one which they've set
218 * up state for in the context.
221 ureg_DECL_sampler( struct ureg_program
*,
225 static INLINE
struct ureg_src
226 ureg_imm4f( struct ureg_program
*ureg
,
235 return ureg_DECL_immediate( ureg
, v
, 4 );
238 static INLINE
struct ureg_src
239 ureg_imm3f( struct ureg_program
*ureg
,
247 return ureg_DECL_immediate( ureg
, v
, 3 );
250 static INLINE
struct ureg_src
251 ureg_imm2f( struct ureg_program
*ureg
,
257 return ureg_DECL_immediate( ureg
, v
, 2 );
260 static INLINE
struct ureg_src
261 ureg_imm1f( struct ureg_program
*ureg
,
266 return ureg_DECL_immediate( ureg
, v
, 1 );
269 static INLINE
struct ureg_src
270 ureg_imm4u( struct ureg_program
*ureg
,
271 unsigned a
, unsigned b
,
272 unsigned c
, unsigned d
)
279 return ureg_DECL_immediate_uint( ureg
, v
, 4 );
282 static INLINE
struct ureg_src
283 ureg_imm3u( struct ureg_program
*ureg
,
284 unsigned a
, unsigned b
,
291 return ureg_DECL_immediate_uint( ureg
, v
, 3 );
294 static INLINE
struct ureg_src
295 ureg_imm2u( struct ureg_program
*ureg
,
296 unsigned a
, unsigned b
)
301 return ureg_DECL_immediate_uint( ureg
, v
, 2 );
304 static INLINE
struct ureg_src
305 ureg_imm1u( struct ureg_program
*ureg
,
308 return ureg_DECL_immediate_uint( ureg
, &a
, 1 );
311 static INLINE
struct ureg_src
312 ureg_imm4i( struct ureg_program
*ureg
,
321 return ureg_DECL_immediate_int( ureg
, v
, 4 );
324 static INLINE
struct ureg_src
325 ureg_imm3i( struct ureg_program
*ureg
,
333 return ureg_DECL_immediate_int( ureg
, v
, 3 );
336 static INLINE
struct ureg_src
337 ureg_imm2i( struct ureg_program
*ureg
,
343 return ureg_DECL_immediate_int( ureg
, v
, 2 );
346 static INLINE
struct ureg_src
347 ureg_imm1i( struct ureg_program
*ureg
,
350 return ureg_DECL_immediate_int( ureg
, &a
, 1 );
353 /***********************************************************************
354 * Functions for patching up labels
358 /* Will return a number which can be used in a label to point to the
359 * next instruction to be emitted.
362 ureg_get_instruction_number( struct ureg_program
*ureg
);
365 /* Patch a given label (expressed as a token number) to point to a
366 * given instruction (expressed as an instruction number).
368 * Labels are obtained from instruction emitters, eg ureg_CAL().
369 * Instruction numbers are obtained from ureg_get_instruction_number(),
373 ureg_fixup_label(struct ureg_program
*ureg
,
374 unsigned label_token
,
375 unsigned instruction_number
);
378 /* Generic instruction emitter. Use if you need to pass the opcode as
379 * a parameter, rather than using the emit_OP() varients below.
382 ureg_insn(struct ureg_program
*ureg
,
384 const struct ureg_dst
*dst
,
386 const struct ureg_src
*src
,
391 ureg_tex_insn(struct ureg_program
*ureg
,
393 const struct ureg_dst
*dst
,
396 const struct ureg_src
*src
,
401 ureg_label_insn(struct ureg_program
*ureg
,
403 const struct ureg_src
*src
,
408 /***********************************************************************
409 * Internal instruction helpers, don't call these directly:
412 struct ureg_emit_insn_result
{
413 unsigned insn_token
; /*< Used to fixup insn size. */
414 unsigned extended_token
; /*< Used to set the Extended bit, usually the same as insn_token. */
417 struct ureg_emit_insn_result
418 ureg_emit_insn(struct ureg_program
*ureg
,
423 unsigned pred_swizzle_x
,
424 unsigned pred_swizzle_y
,
425 unsigned pred_swizzle_z
,
426 unsigned pred_swizzle_w
,
431 ureg_emit_label(struct ureg_program
*ureg
,
433 unsigned *label_token
);
436 ureg_emit_texture(struct ureg_program
*ureg
,
441 ureg_emit_dst( struct ureg_program
*ureg
,
442 struct ureg_dst dst
);
445 ureg_emit_src( struct ureg_program
*ureg
,
446 struct ureg_src src
);
449 ureg_fixup_insn_size(struct ureg_program
*ureg
,
454 static INLINE void ureg_##op( struct ureg_program *ureg ) \
456 unsigned opcode = TGSI_OPCODE_##op; \
457 unsigned insn = ureg_emit_insn(ureg, \
468 ureg_fixup_insn_size( ureg, insn ); \
472 static INLINE void ureg_##op( struct ureg_program *ureg, \
473 struct ureg_src src ) \
475 unsigned opcode = TGSI_OPCODE_##op; \
476 unsigned insn = ureg_emit_insn(ureg, \
487 ureg_emit_src( ureg, src ); \
488 ureg_fixup_insn_size( ureg, insn ); \
491 #define OP00_LBL( op ) \
492 static INLINE void ureg_##op( struct ureg_program *ureg, \
493 unsigned *label_token ) \
495 unsigned opcode = TGSI_OPCODE_##op; \
496 struct ureg_emit_insn_result insn; \
497 insn = ureg_emit_insn(ureg, \
508 ureg_emit_label( ureg, insn.extended_token, label_token ); \
509 ureg_fixup_insn_size( ureg, insn.insn_token ); \
512 #define OP01_LBL( op ) \
513 static INLINE void ureg_##op( struct ureg_program *ureg, \
514 struct ureg_src src, \
515 unsigned *label_token ) \
517 unsigned opcode = TGSI_OPCODE_##op; \
518 struct ureg_emit_insn_result insn; \
519 insn = ureg_emit_insn(ureg, \
530 ureg_emit_label( ureg, insn.extended_token, label_token ); \
531 ureg_emit_src( ureg, src ); \
532 ureg_fixup_insn_size( ureg, insn.insn_token ); \
536 static INLINE void ureg_##op( struct ureg_program *ureg, \
537 struct ureg_dst dst ) \
539 unsigned opcode = TGSI_OPCODE_##op; \
540 unsigned insn = ureg_emit_insn(ureg, \
551 ureg_emit_dst( ureg, dst ); \
552 ureg_fixup_insn_size( ureg, insn ); \
557 static INLINE void ureg_##op( struct ureg_program *ureg, \
558 struct ureg_dst dst, \
559 struct ureg_src src ) \
561 unsigned opcode = TGSI_OPCODE_##op; \
562 unsigned insn = ureg_emit_insn(ureg, \
573 ureg_emit_dst( ureg, dst ); \
574 ureg_emit_src( ureg, src ); \
575 ureg_fixup_insn_size( ureg, insn ); \
579 static INLINE void ureg_##op( struct ureg_program *ureg, \
580 struct ureg_dst dst, \
581 struct ureg_src src0, \
582 struct ureg_src src1 ) \
584 unsigned opcode = TGSI_OPCODE_##op; \
585 unsigned insn = ureg_emit_insn(ureg, \
596 ureg_emit_dst( ureg, dst ); \
597 ureg_emit_src( ureg, src0 ); \
598 ureg_emit_src( ureg, src1 ); \
599 ureg_fixup_insn_size( ureg, insn ); \
602 #define OP12_TEX( op ) \
603 static INLINE void ureg_##op( struct ureg_program *ureg, \
604 struct ureg_dst dst, \
606 struct ureg_src src0, \
607 struct ureg_src src1 ) \
609 unsigned opcode = TGSI_OPCODE_##op; \
610 struct ureg_emit_insn_result insn; \
611 insn = ureg_emit_insn(ureg, \
622 ureg_emit_texture( ureg, insn.extended_token, target ); \
623 ureg_emit_dst( ureg, dst ); \
624 ureg_emit_src( ureg, src0 ); \
625 ureg_emit_src( ureg, src1 ); \
626 ureg_fixup_insn_size( ureg, insn.insn_token ); \
630 static INLINE void ureg_##op( struct ureg_program *ureg, \
631 struct ureg_dst dst, \
632 struct ureg_src src0, \
633 struct ureg_src src1, \
634 struct ureg_src src2 ) \
636 unsigned opcode = TGSI_OPCODE_##op; \
637 unsigned insn = ureg_emit_insn(ureg, \
648 ureg_emit_dst( ureg, dst ); \
649 ureg_emit_src( ureg, src0 ); \
650 ureg_emit_src( ureg, src1 ); \
651 ureg_emit_src( ureg, src2 ); \
652 ureg_fixup_insn_size( ureg, insn ); \
655 #define OP14_TEX( op ) \
656 static INLINE void ureg_##op( struct ureg_program *ureg, \
657 struct ureg_dst dst, \
659 struct ureg_src src0, \
660 struct ureg_src src1, \
661 struct ureg_src src2, \
662 struct ureg_src src3 ) \
664 unsigned opcode = TGSI_OPCODE_##op; \
665 struct ureg_emit_insn_result insn; \
666 insn = ureg_emit_insn(ureg, \
677 ureg_emit_texture( ureg, insn.extended_token, target ); \
678 ureg_emit_dst( ureg, dst ); \
679 ureg_emit_src( ureg, src0 ); \
680 ureg_emit_src( ureg, src1 ); \
681 ureg_emit_src( ureg, src2 ); \
682 ureg_emit_src( ureg, src3 ); \
683 ureg_fixup_insn_size( ureg, insn.insn_token ); \
687 /* Use a template include to generate a correctly-typed ureg_OP()
688 * function for each TGSI opcode:
690 #include "tgsi_opcode_tmp.h"
693 /***********************************************************************
694 * Inline helpers for manipulating register structs:
696 static INLINE
struct ureg_src
697 ureg_negate( struct ureg_src reg
)
699 assert(reg
.File
!= TGSI_FILE_NULL
);
704 static INLINE
struct ureg_src
705 ureg_abs( struct ureg_src reg
)
707 assert(reg
.File
!= TGSI_FILE_NULL
);
713 static INLINE
struct ureg_src
714 ureg_swizzle( struct ureg_src reg
,
715 int x
, int y
, int z
, int w
)
717 unsigned swz
= ( (reg
.SwizzleX
<< 0) |
718 (reg
.SwizzleY
<< 2) |
719 (reg
.SwizzleZ
<< 4) |
720 (reg
.SwizzleW
<< 6));
722 assert(reg
.File
!= TGSI_FILE_NULL
);
728 reg
.SwizzleX
= (swz
>> (x
*2)) & 0x3;
729 reg
.SwizzleY
= (swz
>> (y
*2)) & 0x3;
730 reg
.SwizzleZ
= (swz
>> (z
*2)) & 0x3;
731 reg
.SwizzleW
= (swz
>> (w
*2)) & 0x3;
735 static INLINE
struct ureg_src
736 ureg_scalar( struct ureg_src reg
, int x
)
738 return ureg_swizzle(reg
, x
, x
, x
, x
);
741 static INLINE
struct ureg_dst
742 ureg_writemask( struct ureg_dst reg
,
745 assert(reg
.File
!= TGSI_FILE_NULL
);
746 reg
.WriteMask
&= writemask
;
750 static INLINE
struct ureg_dst
751 ureg_saturate( struct ureg_dst reg
)
753 assert(reg
.File
!= TGSI_FILE_NULL
);
758 static INLINE
struct ureg_dst
759 ureg_predicate(struct ureg_dst reg
,
766 assert(reg
.File
!= TGSI_FILE_NULL
);
768 reg
.PredNegate
= negate
;
769 reg
.PredSwizzleX
= swizzle_x
;
770 reg
.PredSwizzleY
= swizzle_y
;
771 reg
.PredSwizzleZ
= swizzle_z
;
772 reg
.PredSwizzleW
= swizzle_w
;
776 static INLINE
struct ureg_dst
777 ureg_dst_indirect( struct ureg_dst reg
, struct ureg_src addr
)
779 assert(reg
.File
!= TGSI_FILE_NULL
);
780 assert(addr
.File
== TGSI_FILE_ADDRESS
);
782 reg
.IndirectIndex
= addr
.Index
;
783 reg
.IndirectSwizzle
= addr
.SwizzleX
;
787 static INLINE
struct ureg_src
788 ureg_src_indirect( struct ureg_src reg
, struct ureg_src addr
)
790 assert(reg
.File
!= TGSI_FILE_NULL
);
791 assert(addr
.File
== TGSI_FILE_ADDRESS
|| addr
.File
== TGSI_FILE_TEMPORARY
);
793 reg
.IndirectFile
= addr
.File
;
794 reg
.IndirectIndex
= addr
.Index
;
795 reg
.IndirectSwizzle
= addr
.SwizzleX
;
799 static INLINE
struct ureg_src
800 ureg_src_dimension( struct ureg_src reg
, int index
)
802 assert(reg
.File
!= TGSI_FILE_NULL
);
804 reg
.DimensionIndex
= index
;
808 static INLINE
struct ureg_dst
809 ureg_dst( struct ureg_src src
)
813 assert(!src
.Indirect
|| src
.IndirectFile
== TGSI_FILE_ADDRESS
);
816 dst
.WriteMask
= TGSI_WRITEMASK_XYZW
;
817 dst
.Indirect
= src
.Indirect
;
818 dst
.IndirectIndex
= src
.IndirectIndex
;
819 dst
.IndirectSwizzle
= src
.IndirectSwizzle
;
823 dst
.PredSwizzleX
= TGSI_SWIZZLE_X
;
824 dst
.PredSwizzleY
= TGSI_SWIZZLE_Y
;
825 dst
.PredSwizzleZ
= TGSI_SWIZZLE_Z
;
826 dst
.PredSwizzleW
= TGSI_SWIZZLE_W
;
827 dst
.Index
= src
.Index
;
832 static INLINE
struct ureg_src
833 ureg_src_register(unsigned file
,
839 src
.SwizzleX
= TGSI_SWIZZLE_X
;
840 src
.SwizzleY
= TGSI_SWIZZLE_Y
;
841 src
.SwizzleZ
= TGSI_SWIZZLE_Z
;
842 src
.SwizzleW
= TGSI_SWIZZLE_W
;
844 src
.IndirectFile
= TGSI_FILE_NULL
;
845 src
.IndirectIndex
= 0;
846 src
.IndirectSwizzle
= 0;
851 src
.DimensionIndex
= 0;
856 static INLINE
struct ureg_src
857 ureg_src( struct ureg_dst dst
)
862 src
.SwizzleX
= TGSI_SWIZZLE_X
;
863 src
.SwizzleY
= TGSI_SWIZZLE_Y
;
864 src
.SwizzleZ
= TGSI_SWIZZLE_Z
;
865 src
.SwizzleW
= TGSI_SWIZZLE_W
;
866 src
.Indirect
= dst
.Indirect
;
867 src
.IndirectFile
= TGSI_FILE_ADDRESS
;
868 src
.IndirectIndex
= dst
.IndirectIndex
;
869 src
.IndirectSwizzle
= dst
.IndirectSwizzle
;
871 src
.Index
= dst
.Index
;
874 src
.DimensionIndex
= 0;
881 static INLINE
struct ureg_dst
882 ureg_dst_undef( void )
886 dst
.File
= TGSI_FILE_NULL
;
889 dst
.IndirectIndex
= 0;
890 dst
.IndirectSwizzle
= 0;
894 dst
.PredSwizzleX
= TGSI_SWIZZLE_X
;
895 dst
.PredSwizzleY
= TGSI_SWIZZLE_Y
;
896 dst
.PredSwizzleZ
= TGSI_SWIZZLE_Z
;
897 dst
.PredSwizzleW
= TGSI_SWIZZLE_W
;
903 static INLINE
struct ureg_src
904 ureg_src_undef( void )
908 src
.File
= TGSI_FILE_NULL
;
914 src
.IndirectFile
= TGSI_FILE_NULL
;
915 src
.IndirectIndex
= 0;
916 src
.IndirectSwizzle
= 0;
921 src
.DimensionIndex
= 0;
926 static INLINE boolean
927 ureg_src_is_undef( struct ureg_src src
)
929 return src
.File
== TGSI_FILE_NULL
;
932 static INLINE boolean
933 ureg_dst_is_undef( struct ureg_dst dst
)
935 return dst
.File
== TGSI_FILE_NULL
;