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 Pad
: 1; /* BOOL */
51 unsigned Indirect
: 1; /* BOOL */
52 unsigned Absolute
: 1; /* BOOL */
53 int Index
: 16; /* SINT */
54 unsigned Negate
: 1; /* BOOL */
55 int IndirectIndex
: 16; /* SINT */
56 int IndirectSwizzle
: 2; /* TGSI_SWIZZLE_ */
59 /* Very similar to a tgsi_dst_register, removing unsupported fields
60 * and adding a Saturate flag. It's easier to push saturate into the
61 * destination register than to try and create a _SAT varient of each
62 * instruction function.
66 unsigned File
: 4; /* TGSI_FILE_ */
67 unsigned WriteMask
: 4; /* TGSI_WRITEMASK_ */
68 unsigned Indirect
: 1; /* BOOL */
69 unsigned Saturate
: 1; /* BOOL */
70 unsigned Predicate
: 1;
71 unsigned PredNegate
: 1; /* BOOL */
72 unsigned PredSwizzleX
: 2; /* TGSI_SWIZZLE_ */
73 unsigned PredSwizzleY
: 2; /* TGSI_SWIZZLE_ */
74 unsigned PredSwizzleZ
: 2; /* TGSI_SWIZZLE_ */
75 unsigned PredSwizzleW
: 2; /* TGSI_SWIZZLE_ */
76 int Index
: 16; /* SINT */
77 int IndirectIndex
: 16; /* SINT */
78 int IndirectSwizzle
: 2; /* TGSI_SWIZZLE_ */
84 ureg_create( unsigned processor
);
86 const struct tgsi_token
*
87 ureg_finalize( struct ureg_program
* );
89 /* Create and return a shader:
92 ureg_create_shader( struct ureg_program
*,
93 struct pipe_context
*pipe
);
96 /* Alternately, return the built token stream and hand ownership of
97 * that memory to the caller:
99 const struct tgsi_token
*
100 ureg_get_tokens( struct ureg_program
*ureg
,
101 unsigned *nr_tokens
);
105 ureg_destroy( struct ureg_program
* );
108 /***********************************************************************
109 * Convenience routine:
112 ureg_create_shader_and_destroy( struct ureg_program
*p
,
113 struct pipe_context
*pipe
)
115 void *result
= ureg_create_shader( p
, pipe
);
122 /***********************************************************************
123 * Build shader declarations:
127 ureg_DECL_fs_input( struct ureg_program
*,
128 unsigned semantic_name
,
129 unsigned semantic_index
,
130 unsigned interp_mode
);
133 ureg_DECL_vs_input( struct ureg_program
*,
137 ureg_DECL_gs_input(struct ureg_program
*,
141 ureg_DECL_output( struct ureg_program
*,
142 unsigned semantic_name
,
143 unsigned semantic_index
);
146 ureg_DECL_immediate( struct ureg_program
*,
151 ureg_DECL_constant( struct ureg_program
*,
155 ureg_DECL_temporary( struct ureg_program
* );
158 ureg_release_temporary( struct ureg_program
*ureg
,
159 struct ureg_dst tmp
);
162 ureg_DECL_address( struct ureg_program
* );
165 ureg_DECL_loop( struct ureg_program
* );
168 ureg_DECL_predicate(struct ureg_program
*);
170 /* Supply an index to the sampler declaration as this is the hook to
171 * the external pipe_sampler state. Users of this function probably
172 * don't want just any sampler, but a specific one which they've set
173 * up state for in the context.
176 ureg_DECL_sampler( struct ureg_program
*,
180 static INLINE
struct ureg_src
181 ureg_imm4f( struct ureg_program
*ureg
,
190 return ureg_DECL_immediate( ureg
, v
, 4 );
193 static INLINE
struct ureg_src
194 ureg_imm3f( struct ureg_program
*ureg
,
202 return ureg_DECL_immediate( ureg
, v
, 3 );
205 static INLINE
struct ureg_src
206 ureg_imm2f( struct ureg_program
*ureg
,
212 return ureg_DECL_immediate( ureg
, v
, 2 );
215 static INLINE
struct ureg_src
216 ureg_imm1f( struct ureg_program
*ureg
,
221 return ureg_DECL_immediate( ureg
, v
, 1 );
224 /***********************************************************************
225 * Functions for patching up labels
229 /* Will return a number which can be used in a label to point to the
230 * next instruction to be emitted.
233 ureg_get_instruction_number( struct ureg_program
*ureg
);
236 /* Patch a given label (expressed as a token number) to point to a
237 * given instruction (expressed as an instruction number).
239 * Labels are obtained from instruction emitters, eg ureg_CAL().
240 * Instruction numbers are obtained from ureg_get_instruction_number(),
244 ureg_fixup_label(struct ureg_program
*ureg
,
245 unsigned label_token
,
246 unsigned instruction_number
);
249 /* Generic instruction emitter. Use if you need to pass the opcode as
250 * a parameter, rather than using the emit_OP() varients below.
253 ureg_insn(struct ureg_program
*ureg
,
255 const struct ureg_dst
*dst
,
257 const struct ureg_src
*src
,
262 ureg_tex_insn(struct ureg_program
*ureg
,
264 const struct ureg_dst
*dst
,
267 const struct ureg_src
*src
,
272 ureg_label_insn(struct ureg_program
*ureg
,
274 const struct ureg_src
*src
,
279 /***********************************************************************
280 * Internal instruction helpers, don't call these directly:
283 struct ureg_emit_insn_result
{
284 unsigned insn_token
; /*< Used to fixup insn size. */
285 unsigned extended_token
; /*< Used to set the Extended bit, usually the same as insn_token. */
288 struct ureg_emit_insn_result
289 ureg_emit_insn(struct ureg_program
*ureg
,
294 unsigned pred_swizzle_x
,
295 unsigned pred_swizzle_y
,
296 unsigned pred_swizzle_z
,
297 unsigned pred_swizzle_w
,
302 ureg_emit_label(struct ureg_program
*ureg
,
304 unsigned *label_token
);
307 ureg_emit_texture(struct ureg_program
*ureg
,
312 ureg_emit_dst( struct ureg_program
*ureg
,
313 struct ureg_dst dst
);
316 ureg_emit_src( struct ureg_program
*ureg
,
317 struct ureg_src src
);
320 ureg_fixup_insn_size(struct ureg_program
*ureg
,
325 static INLINE void ureg_##op( struct ureg_program *ureg ) \
327 unsigned opcode = TGSI_OPCODE_##op; \
328 unsigned insn = ureg_emit_insn(ureg, \
339 ureg_fixup_insn_size( ureg, insn ); \
343 static INLINE void ureg_##op( struct ureg_program *ureg, \
344 struct ureg_src src ) \
346 unsigned opcode = TGSI_OPCODE_##op; \
347 unsigned insn = ureg_emit_insn(ureg, \
358 ureg_emit_src( ureg, src ); \
359 ureg_fixup_insn_size( ureg, insn ); \
362 #define OP00_LBL( op ) \
363 static INLINE void ureg_##op( struct ureg_program *ureg, \
364 unsigned *label_token ) \
366 unsigned opcode = TGSI_OPCODE_##op; \
367 struct ureg_emit_insn_result insn; \
368 insn = ureg_emit_insn(ureg, \
379 ureg_emit_label( ureg, insn.extended_token, label_token ); \
380 ureg_fixup_insn_size( ureg, insn.insn_token ); \
383 #define OP01_LBL( op ) \
384 static INLINE void ureg_##op( struct ureg_program *ureg, \
385 struct ureg_src src, \
386 unsigned *label_token ) \
388 unsigned opcode = TGSI_OPCODE_##op; \
389 struct ureg_emit_insn_result insn; \
390 insn = ureg_emit_insn(ureg, \
401 ureg_emit_label( ureg, insn.extended_token, label_token ); \
402 ureg_emit_src( ureg, src ); \
403 ureg_fixup_insn_size( ureg, insn.insn_token ); \
407 static INLINE void ureg_##op( struct ureg_program *ureg, \
408 struct ureg_dst dst ) \
410 unsigned opcode = TGSI_OPCODE_##op; \
411 unsigned insn = ureg_emit_insn(ureg, \
422 ureg_emit_dst( ureg, dst ); \
423 ureg_fixup_insn_size( ureg, insn ); \
428 static INLINE void ureg_##op( struct ureg_program *ureg, \
429 struct ureg_dst dst, \
430 struct ureg_src src ) \
432 unsigned opcode = TGSI_OPCODE_##op; \
433 unsigned insn = ureg_emit_insn(ureg, \
444 ureg_emit_dst( ureg, dst ); \
445 ureg_emit_src( ureg, src ); \
446 ureg_fixup_insn_size( ureg, insn ); \
450 static INLINE void ureg_##op( struct ureg_program *ureg, \
451 struct ureg_dst dst, \
452 struct ureg_src src0, \
453 struct ureg_src src1 ) \
455 unsigned opcode = TGSI_OPCODE_##op; \
456 unsigned insn = ureg_emit_insn(ureg, \
467 ureg_emit_dst( ureg, dst ); \
468 ureg_emit_src( ureg, src0 ); \
469 ureg_emit_src( ureg, src1 ); \
470 ureg_fixup_insn_size( ureg, insn ); \
473 #define OP12_TEX( op ) \
474 static INLINE void ureg_##op( struct ureg_program *ureg, \
475 struct ureg_dst dst, \
477 struct ureg_src src0, \
478 struct ureg_src src1 ) \
480 unsigned opcode = TGSI_OPCODE_##op; \
481 struct ureg_emit_insn_result insn; \
482 insn = ureg_emit_insn(ureg, \
493 ureg_emit_texture( ureg, insn.extended_token, target ); \
494 ureg_emit_dst( ureg, dst ); \
495 ureg_emit_src( ureg, src0 ); \
496 ureg_emit_src( ureg, src1 ); \
497 ureg_fixup_insn_size( ureg, insn.insn_token ); \
501 static INLINE void ureg_##op( struct ureg_program *ureg, \
502 struct ureg_dst dst, \
503 struct ureg_src src0, \
504 struct ureg_src src1, \
505 struct ureg_src src2 ) \
507 unsigned opcode = TGSI_OPCODE_##op; \
508 unsigned insn = ureg_emit_insn(ureg, \
519 ureg_emit_dst( ureg, dst ); \
520 ureg_emit_src( ureg, src0 ); \
521 ureg_emit_src( ureg, src1 ); \
522 ureg_emit_src( ureg, src2 ); \
523 ureg_fixup_insn_size( ureg, insn ); \
526 #define OP14_TEX( op ) \
527 static INLINE void ureg_##op( struct ureg_program *ureg, \
528 struct ureg_dst dst, \
530 struct ureg_src src0, \
531 struct ureg_src src1, \
532 struct ureg_src src2, \
533 struct ureg_src src3 ) \
535 unsigned opcode = TGSI_OPCODE_##op; \
536 struct ureg_emit_insn_result insn; \
537 insn = ureg_emit_insn(ureg, \
548 ureg_emit_texture( ureg, insn.extended_token, target ); \
549 ureg_emit_dst( ureg, dst ); \
550 ureg_emit_src( ureg, src0 ); \
551 ureg_emit_src( ureg, src1 ); \
552 ureg_emit_src( ureg, src2 ); \
553 ureg_emit_src( ureg, src3 ); \
554 ureg_fixup_insn_size( ureg, insn.insn_token ); \
558 /* Use a template include to generate a correctly-typed ureg_OP()
559 * function for each TGSI opcode:
561 #include "tgsi_opcode_tmp.h"
564 /***********************************************************************
565 * Inline helpers for manipulating register structs:
567 static INLINE
struct ureg_src
568 ureg_negate( struct ureg_src reg
)
570 assert(reg
.File
!= TGSI_FILE_NULL
);
575 static INLINE
struct ureg_src
576 ureg_abs( struct ureg_src reg
)
578 assert(reg
.File
!= TGSI_FILE_NULL
);
584 static INLINE
struct ureg_src
585 ureg_swizzle( struct ureg_src reg
,
586 int x
, int y
, int z
, int w
)
588 unsigned swz
= ( (reg
.SwizzleX
<< 0) |
589 (reg
.SwizzleY
<< 2) |
590 (reg
.SwizzleZ
<< 4) |
591 (reg
.SwizzleW
<< 6));
593 assert(reg
.File
!= TGSI_FILE_NULL
);
599 reg
.SwizzleX
= (swz
>> (x
*2)) & 0x3;
600 reg
.SwizzleY
= (swz
>> (y
*2)) & 0x3;
601 reg
.SwizzleZ
= (swz
>> (z
*2)) & 0x3;
602 reg
.SwizzleW
= (swz
>> (w
*2)) & 0x3;
606 static INLINE
struct ureg_src
607 ureg_scalar( struct ureg_src reg
, int x
)
609 return ureg_swizzle(reg
, x
, x
, x
, x
);
612 static INLINE
struct ureg_dst
613 ureg_writemask( struct ureg_dst reg
,
616 assert(reg
.File
!= TGSI_FILE_NULL
);
617 reg
.WriteMask
&= writemask
;
621 static INLINE
struct ureg_dst
622 ureg_saturate( struct ureg_dst reg
)
624 assert(reg
.File
!= TGSI_FILE_NULL
);
629 static INLINE
struct ureg_dst
630 ureg_predicate(struct ureg_dst reg
,
637 assert(reg
.File
!= TGSI_FILE_NULL
);
639 reg
.PredNegate
= negate
;
640 reg
.PredSwizzleX
= swizzle_x
;
641 reg
.PredSwizzleY
= swizzle_y
;
642 reg
.PredSwizzleZ
= swizzle_z
;
643 reg
.PredSwizzleW
= swizzle_w
;
647 static INLINE
struct ureg_dst
648 ureg_dst_indirect( struct ureg_dst reg
, struct ureg_src addr
)
650 assert(reg
.File
!= TGSI_FILE_NULL
);
651 assert(addr
.File
== TGSI_FILE_ADDRESS
);
653 reg
.IndirectIndex
= addr
.Index
;
654 reg
.IndirectSwizzle
= addr
.SwizzleX
;
658 static INLINE
struct ureg_src
659 ureg_src_indirect( struct ureg_src reg
, struct ureg_src addr
)
661 assert(reg
.File
!= TGSI_FILE_NULL
);
662 assert(addr
.File
== TGSI_FILE_ADDRESS
);
664 reg
.IndirectIndex
= addr
.Index
;
665 reg
.IndirectSwizzle
= addr
.SwizzleX
;
669 static INLINE
struct ureg_dst
670 ureg_dst( struct ureg_src src
)
675 dst
.WriteMask
= TGSI_WRITEMASK_XYZW
;
676 dst
.Indirect
= src
.Indirect
;
677 dst
.IndirectIndex
= src
.IndirectIndex
;
678 dst
.IndirectSwizzle
= src
.IndirectSwizzle
;
682 dst
.PredSwizzleX
= TGSI_SWIZZLE_X
;
683 dst
.PredSwizzleY
= TGSI_SWIZZLE_Y
;
684 dst
.PredSwizzleZ
= TGSI_SWIZZLE_Z
;
685 dst
.PredSwizzleW
= TGSI_SWIZZLE_W
;
686 dst
.Index
= src
.Index
;
691 static INLINE
struct ureg_src
692 ureg_src( struct ureg_dst dst
)
697 src
.SwizzleX
= TGSI_SWIZZLE_X
;
698 src
.SwizzleY
= TGSI_SWIZZLE_Y
;
699 src
.SwizzleZ
= TGSI_SWIZZLE_Z
;
700 src
.SwizzleW
= TGSI_SWIZZLE_W
;
702 src
.Indirect
= dst
.Indirect
;
703 src
.IndirectIndex
= dst
.IndirectIndex
;
704 src
.IndirectSwizzle
= dst
.IndirectSwizzle
;
706 src
.Index
= dst
.Index
;
714 static INLINE
struct ureg_dst
715 ureg_dst_undef( void )
719 dst
.File
= TGSI_FILE_NULL
;
722 dst
.IndirectIndex
= 0;
723 dst
.IndirectSwizzle
= 0;
727 dst
.PredSwizzleX
= TGSI_SWIZZLE_X
;
728 dst
.PredSwizzleY
= TGSI_SWIZZLE_Y
;
729 dst
.PredSwizzleZ
= TGSI_SWIZZLE_Z
;
730 dst
.PredSwizzleW
= TGSI_SWIZZLE_W
;
736 static INLINE
struct ureg_src
737 ureg_src_undef( void )
741 src
.File
= TGSI_FILE_NULL
;
748 src
.IndirectIndex
= 0;
749 src
.IndirectSwizzle
= 0;
757 static INLINE boolean
758 ureg_src_is_undef( struct ureg_src src
)
760 return src
.File
== TGSI_FILE_NULL
;
763 static INLINE boolean
764 ureg_dst_is_undef( struct ureg_dst dst
)
766 return dst
.File
== TGSI_FILE_NULL
;