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 gs_input_prim
);
132 /***********************************************************************
133 * Build shader declarations:
137 ureg_DECL_fs_input( struct ureg_program
*,
138 unsigned semantic_name
,
139 unsigned semantic_index
,
140 unsigned interp_mode
);
143 ureg_DECL_vs_input( struct ureg_program
*,
147 ureg_DECL_gs_input(struct ureg_program
*,
151 ureg_DECL_system_value(struct ureg_program
*,
153 unsigned semantic_name
,
154 unsigned semantic_index
);
157 ureg_DECL_output( struct ureg_program
*,
158 unsigned semantic_name
,
159 unsigned semantic_index
);
162 ureg_DECL_immediate( struct ureg_program
*,
167 ureg_DECL_immediate_uint( struct ureg_program
*,
172 ureg_DECL_immediate_block_uint( struct ureg_program
*,
177 ureg_DECL_immediate_int( struct ureg_program
*,
182 ureg_DECL_constant( struct ureg_program
*,
186 ureg_DECL_temporary( struct ureg_program
* );
189 ureg_release_temporary( struct ureg_program
*ureg
,
190 struct ureg_dst tmp
);
193 ureg_DECL_address( struct ureg_program
* );
196 ureg_DECL_loop( struct ureg_program
* );
199 ureg_DECL_predicate(struct ureg_program
*);
201 /* Supply an index to the sampler declaration as this is the hook to
202 * the external pipe_sampler state. Users of this function probably
203 * don't want just any sampler, but a specific one which they've set
204 * up state for in the context.
207 ureg_DECL_sampler( struct ureg_program
*,
211 static INLINE
struct ureg_src
212 ureg_imm4f( struct ureg_program
*ureg
,
221 return ureg_DECL_immediate( ureg
, v
, 4 );
224 static INLINE
struct ureg_src
225 ureg_imm3f( struct ureg_program
*ureg
,
233 return ureg_DECL_immediate( ureg
, v
, 3 );
236 static INLINE
struct ureg_src
237 ureg_imm2f( struct ureg_program
*ureg
,
243 return ureg_DECL_immediate( ureg
, v
, 2 );
246 static INLINE
struct ureg_src
247 ureg_imm1f( struct ureg_program
*ureg
,
252 return ureg_DECL_immediate( ureg
, v
, 1 );
255 static INLINE
struct ureg_src
256 ureg_imm4u( struct ureg_program
*ureg
,
257 unsigned a
, unsigned b
,
258 unsigned c
, unsigned d
)
265 return ureg_DECL_immediate_uint( ureg
, v
, 4 );
268 static INLINE
struct ureg_src
269 ureg_imm3u( struct ureg_program
*ureg
,
270 unsigned a
, unsigned b
,
277 return ureg_DECL_immediate_uint( ureg
, v
, 3 );
280 static INLINE
struct ureg_src
281 ureg_imm2u( struct ureg_program
*ureg
,
282 unsigned a
, unsigned b
)
287 return ureg_DECL_immediate_uint( ureg
, v
, 2 );
290 static INLINE
struct ureg_src
291 ureg_imm1u( struct ureg_program
*ureg
,
294 return ureg_DECL_immediate_uint( ureg
, &a
, 1 );
297 static INLINE
struct ureg_src
298 ureg_imm4i( struct ureg_program
*ureg
,
307 return ureg_DECL_immediate_int( ureg
, v
, 4 );
310 static INLINE
struct ureg_src
311 ureg_imm3i( struct ureg_program
*ureg
,
319 return ureg_DECL_immediate_int( ureg
, v
, 3 );
322 static INLINE
struct ureg_src
323 ureg_imm2i( struct ureg_program
*ureg
,
329 return ureg_DECL_immediate_int( ureg
, v
, 2 );
332 static INLINE
struct ureg_src
333 ureg_imm1i( struct ureg_program
*ureg
,
336 return ureg_DECL_immediate_int( ureg
, &a
, 1 );
339 /***********************************************************************
340 * Functions for patching up labels
344 /* Will return a number which can be used in a label to point to the
345 * next instruction to be emitted.
348 ureg_get_instruction_number( struct ureg_program
*ureg
);
351 /* Patch a given label (expressed as a token number) to point to a
352 * given instruction (expressed as an instruction number).
354 * Labels are obtained from instruction emitters, eg ureg_CAL().
355 * Instruction numbers are obtained from ureg_get_instruction_number(),
359 ureg_fixup_label(struct ureg_program
*ureg
,
360 unsigned label_token
,
361 unsigned instruction_number
);
364 /* Generic instruction emitter. Use if you need to pass the opcode as
365 * a parameter, rather than using the emit_OP() varients below.
368 ureg_insn(struct ureg_program
*ureg
,
370 const struct ureg_dst
*dst
,
372 const struct ureg_src
*src
,
377 ureg_tex_insn(struct ureg_program
*ureg
,
379 const struct ureg_dst
*dst
,
382 const struct ureg_src
*src
,
387 ureg_label_insn(struct ureg_program
*ureg
,
389 const struct ureg_src
*src
,
394 /***********************************************************************
395 * Internal instruction helpers, don't call these directly:
398 struct ureg_emit_insn_result
{
399 unsigned insn_token
; /*< Used to fixup insn size. */
400 unsigned extended_token
; /*< Used to set the Extended bit, usually the same as insn_token. */
403 struct ureg_emit_insn_result
404 ureg_emit_insn(struct ureg_program
*ureg
,
409 unsigned pred_swizzle_x
,
410 unsigned pred_swizzle_y
,
411 unsigned pred_swizzle_z
,
412 unsigned pred_swizzle_w
,
417 ureg_emit_label(struct ureg_program
*ureg
,
419 unsigned *label_token
);
422 ureg_emit_texture(struct ureg_program
*ureg
,
427 ureg_emit_dst( struct ureg_program
*ureg
,
428 struct ureg_dst dst
);
431 ureg_emit_src( struct ureg_program
*ureg
,
432 struct ureg_src src
);
435 ureg_fixup_insn_size(struct ureg_program
*ureg
,
440 static INLINE void ureg_##op( struct ureg_program *ureg ) \
442 unsigned opcode = TGSI_OPCODE_##op; \
443 unsigned insn = ureg_emit_insn(ureg, \
454 ureg_fixup_insn_size( ureg, insn ); \
458 static INLINE void ureg_##op( struct ureg_program *ureg, \
459 struct ureg_src src ) \
461 unsigned opcode = TGSI_OPCODE_##op; \
462 unsigned insn = ureg_emit_insn(ureg, \
473 ureg_emit_src( ureg, src ); \
474 ureg_fixup_insn_size( ureg, insn ); \
477 #define OP00_LBL( op ) \
478 static INLINE void ureg_##op( struct ureg_program *ureg, \
479 unsigned *label_token ) \
481 unsigned opcode = TGSI_OPCODE_##op; \
482 struct ureg_emit_insn_result insn; \
483 insn = ureg_emit_insn(ureg, \
494 ureg_emit_label( ureg, insn.extended_token, label_token ); \
495 ureg_fixup_insn_size( ureg, insn.insn_token ); \
498 #define OP01_LBL( op ) \
499 static INLINE void ureg_##op( struct ureg_program *ureg, \
500 struct ureg_src src, \
501 unsigned *label_token ) \
503 unsigned opcode = TGSI_OPCODE_##op; \
504 struct ureg_emit_insn_result insn; \
505 insn = ureg_emit_insn(ureg, \
516 ureg_emit_label( ureg, insn.extended_token, label_token ); \
517 ureg_emit_src( ureg, src ); \
518 ureg_fixup_insn_size( ureg, insn.insn_token ); \
522 static INLINE void ureg_##op( struct ureg_program *ureg, \
523 struct ureg_dst dst ) \
525 unsigned opcode = TGSI_OPCODE_##op; \
526 unsigned insn = ureg_emit_insn(ureg, \
537 ureg_emit_dst( ureg, dst ); \
538 ureg_fixup_insn_size( ureg, insn ); \
543 static INLINE void ureg_##op( struct ureg_program *ureg, \
544 struct ureg_dst dst, \
545 struct ureg_src src ) \
547 unsigned opcode = TGSI_OPCODE_##op; \
548 unsigned insn = ureg_emit_insn(ureg, \
559 ureg_emit_dst( ureg, dst ); \
560 ureg_emit_src( ureg, src ); \
561 ureg_fixup_insn_size( ureg, insn ); \
565 static INLINE void ureg_##op( struct ureg_program *ureg, \
566 struct ureg_dst dst, \
567 struct ureg_src src0, \
568 struct ureg_src src1 ) \
570 unsigned opcode = TGSI_OPCODE_##op; \
571 unsigned insn = ureg_emit_insn(ureg, \
582 ureg_emit_dst( ureg, dst ); \
583 ureg_emit_src( ureg, src0 ); \
584 ureg_emit_src( ureg, src1 ); \
585 ureg_fixup_insn_size( ureg, insn ); \
588 #define OP12_TEX( op ) \
589 static INLINE void ureg_##op( struct ureg_program *ureg, \
590 struct ureg_dst dst, \
592 struct ureg_src src0, \
593 struct ureg_src src1 ) \
595 unsigned opcode = TGSI_OPCODE_##op; \
596 struct ureg_emit_insn_result insn; \
597 insn = ureg_emit_insn(ureg, \
608 ureg_emit_texture( ureg, insn.extended_token, target ); \
609 ureg_emit_dst( ureg, dst ); \
610 ureg_emit_src( ureg, src0 ); \
611 ureg_emit_src( ureg, src1 ); \
612 ureg_fixup_insn_size( ureg, insn.insn_token ); \
616 static INLINE void ureg_##op( struct ureg_program *ureg, \
617 struct ureg_dst dst, \
618 struct ureg_src src0, \
619 struct ureg_src src1, \
620 struct ureg_src src2 ) \
622 unsigned opcode = TGSI_OPCODE_##op; \
623 unsigned insn = ureg_emit_insn(ureg, \
634 ureg_emit_dst( ureg, dst ); \
635 ureg_emit_src( ureg, src0 ); \
636 ureg_emit_src( ureg, src1 ); \
637 ureg_emit_src( ureg, src2 ); \
638 ureg_fixup_insn_size( ureg, insn ); \
641 #define OP14_TEX( op ) \
642 static INLINE void ureg_##op( struct ureg_program *ureg, \
643 struct ureg_dst dst, \
645 struct ureg_src src0, \
646 struct ureg_src src1, \
647 struct ureg_src src2, \
648 struct ureg_src src3 ) \
650 unsigned opcode = TGSI_OPCODE_##op; \
651 struct ureg_emit_insn_result insn; \
652 insn = ureg_emit_insn(ureg, \
663 ureg_emit_texture( ureg, insn.extended_token, target ); \
664 ureg_emit_dst( ureg, dst ); \
665 ureg_emit_src( ureg, src0 ); \
666 ureg_emit_src( ureg, src1 ); \
667 ureg_emit_src( ureg, src2 ); \
668 ureg_emit_src( ureg, src3 ); \
669 ureg_fixup_insn_size( ureg, insn.insn_token ); \
673 /* Use a template include to generate a correctly-typed ureg_OP()
674 * function for each TGSI opcode:
676 #include "tgsi_opcode_tmp.h"
679 /***********************************************************************
680 * Inline helpers for manipulating register structs:
682 static INLINE
struct ureg_src
683 ureg_negate( struct ureg_src reg
)
685 assert(reg
.File
!= TGSI_FILE_NULL
);
690 static INLINE
struct ureg_src
691 ureg_abs( struct ureg_src reg
)
693 assert(reg
.File
!= TGSI_FILE_NULL
);
699 static INLINE
struct ureg_src
700 ureg_swizzle( struct ureg_src reg
,
701 int x
, int y
, int z
, int w
)
703 unsigned swz
= ( (reg
.SwizzleX
<< 0) |
704 (reg
.SwizzleY
<< 2) |
705 (reg
.SwizzleZ
<< 4) |
706 (reg
.SwizzleW
<< 6));
708 assert(reg
.File
!= TGSI_FILE_NULL
);
714 reg
.SwizzleX
= (swz
>> (x
*2)) & 0x3;
715 reg
.SwizzleY
= (swz
>> (y
*2)) & 0x3;
716 reg
.SwizzleZ
= (swz
>> (z
*2)) & 0x3;
717 reg
.SwizzleW
= (swz
>> (w
*2)) & 0x3;
721 static INLINE
struct ureg_src
722 ureg_scalar( struct ureg_src reg
, int x
)
724 return ureg_swizzle(reg
, x
, x
, x
, x
);
727 static INLINE
struct ureg_dst
728 ureg_writemask( struct ureg_dst reg
,
731 assert(reg
.File
!= TGSI_FILE_NULL
);
732 reg
.WriteMask
&= writemask
;
736 static INLINE
struct ureg_dst
737 ureg_saturate( struct ureg_dst reg
)
739 assert(reg
.File
!= TGSI_FILE_NULL
);
744 static INLINE
struct ureg_dst
745 ureg_predicate(struct ureg_dst reg
,
752 assert(reg
.File
!= TGSI_FILE_NULL
);
754 reg
.PredNegate
= negate
;
755 reg
.PredSwizzleX
= swizzle_x
;
756 reg
.PredSwizzleY
= swizzle_y
;
757 reg
.PredSwizzleZ
= swizzle_z
;
758 reg
.PredSwizzleW
= swizzle_w
;
762 static INLINE
struct ureg_dst
763 ureg_dst_indirect( struct ureg_dst reg
, struct ureg_src addr
)
765 assert(reg
.File
!= TGSI_FILE_NULL
);
766 assert(addr
.File
== TGSI_FILE_ADDRESS
);
768 reg
.IndirectIndex
= addr
.Index
;
769 reg
.IndirectSwizzle
= addr
.SwizzleX
;
773 static INLINE
struct ureg_src
774 ureg_src_indirect( struct ureg_src reg
, struct ureg_src addr
)
776 assert(reg
.File
!= TGSI_FILE_NULL
);
777 assert(addr
.File
== TGSI_FILE_ADDRESS
|| addr
.File
== TGSI_FILE_TEMPORARY
);
779 reg
.IndirectFile
= addr
.File
;
780 reg
.IndirectIndex
= addr
.Index
;
781 reg
.IndirectSwizzle
= addr
.SwizzleX
;
785 static INLINE
struct ureg_src
786 ureg_src_dimension( struct ureg_src reg
, int index
)
788 assert(reg
.File
!= TGSI_FILE_NULL
);
790 reg
.DimensionIndex
= index
;
794 static INLINE
struct ureg_dst
795 ureg_dst( struct ureg_src src
)
799 assert(!src
.Indirect
|| src
.IndirectFile
== TGSI_FILE_ADDRESS
);
802 dst
.WriteMask
= TGSI_WRITEMASK_XYZW
;
803 dst
.Indirect
= src
.Indirect
;
804 dst
.IndirectIndex
= src
.IndirectIndex
;
805 dst
.IndirectSwizzle
= src
.IndirectSwizzle
;
809 dst
.PredSwizzleX
= TGSI_SWIZZLE_X
;
810 dst
.PredSwizzleY
= TGSI_SWIZZLE_Y
;
811 dst
.PredSwizzleZ
= TGSI_SWIZZLE_Z
;
812 dst
.PredSwizzleW
= TGSI_SWIZZLE_W
;
813 dst
.Index
= src
.Index
;
818 static INLINE
struct ureg_src
819 ureg_src( struct ureg_dst dst
)
824 src
.SwizzleX
= TGSI_SWIZZLE_X
;
825 src
.SwizzleY
= TGSI_SWIZZLE_Y
;
826 src
.SwizzleZ
= TGSI_SWIZZLE_Z
;
827 src
.SwizzleW
= TGSI_SWIZZLE_W
;
828 src
.Indirect
= dst
.Indirect
;
829 src
.IndirectFile
= TGSI_FILE_ADDRESS
;
830 src
.IndirectIndex
= dst
.IndirectIndex
;
831 src
.IndirectSwizzle
= dst
.IndirectSwizzle
;
833 src
.Index
= dst
.Index
;
836 src
.DimensionIndex
= 0;
843 static INLINE
struct ureg_dst
844 ureg_dst_undef( void )
848 dst
.File
= TGSI_FILE_NULL
;
851 dst
.IndirectIndex
= 0;
852 dst
.IndirectSwizzle
= 0;
856 dst
.PredSwizzleX
= TGSI_SWIZZLE_X
;
857 dst
.PredSwizzleY
= TGSI_SWIZZLE_Y
;
858 dst
.PredSwizzleZ
= TGSI_SWIZZLE_Z
;
859 dst
.PredSwizzleW
= TGSI_SWIZZLE_W
;
865 static INLINE
struct ureg_src
866 ureg_src_undef( void )
870 src
.File
= TGSI_FILE_NULL
;
876 src
.IndirectFile
= TGSI_FILE_NULL
;
877 src
.IndirectIndex
= 0;
878 src
.IndirectSwizzle
= 0;
883 src
.DimensionIndex
= 0;
888 static INLINE boolean
889 ureg_src_is_undef( struct ureg_src src
)
891 return src
.File
== TGSI_FILE_NULL
;
894 static INLINE boolean
895 ureg_dst_is_undef( struct ureg_dst dst
)
897 return dst
.File
== TGSI_FILE_NULL
;