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 **************************************************************************/
29 #include "pipe/p_context.h"
30 #include "pipe/p_state.h"
31 #include "tgsi/tgsi_ureg.h"
32 #include "tgsi/tgsi_build.h"
33 #include "tgsi/tgsi_info.h"
34 #include "tgsi/tgsi_dump.h"
35 #include "tgsi/tgsi_sanity.h"
36 #include "util/u_debug.h"
37 #include "util/u_memory.h"
38 #include "util/u_math.h"
40 union tgsi_any_token
{
41 struct tgsi_header header
;
42 struct tgsi_processor processor
;
43 struct tgsi_token token
;
44 struct tgsi_property prop
;
45 struct tgsi_property_data prop_data
;
46 struct tgsi_declaration decl
;
47 struct tgsi_declaration_range decl_range
;
48 struct tgsi_declaration_dimension decl_dim
;
49 struct tgsi_declaration_semantic decl_semantic
;
50 struct tgsi_immediate imm
;
51 union tgsi_immediate_data imm_data
;
52 struct tgsi_instruction insn
;
53 struct tgsi_instruction_predicate insn_predicate
;
54 struct tgsi_instruction_label insn_label
;
55 struct tgsi_instruction_texture insn_texture
;
56 struct tgsi_src_register src
;
57 struct tgsi_dimension dim
;
58 struct tgsi_dst_register dst
;
64 union tgsi_any_token
*tokens
;
70 #define UREG_MAX_INPUT PIPE_MAX_ATTRIBS
71 #define UREG_MAX_SYSTEM_VALUE PIPE_MAX_ATTRIBS
72 #define UREG_MAX_OUTPUT PIPE_MAX_ATTRIBS
73 #define UREG_MAX_CONSTANT_RANGE 32
74 #define UREG_MAX_IMMEDIATE 32
75 #define UREG_MAX_TEMP 256
76 #define UREG_MAX_ADDR 2
77 #define UREG_MAX_LOOP 1
78 #define UREG_MAX_PRED 1
84 } constant_range
[UREG_MAX_CONSTANT_RANGE
];
85 unsigned nr_constant_ranges
;
94 struct pipe_context
*pipe
;
97 unsigned semantic_name
;
98 unsigned semantic_index
;
100 } fs_input
[UREG_MAX_INPUT
];
101 unsigned nr_fs_inputs
;
103 unsigned vs_inputs
[UREG_MAX_INPUT
/32];
107 unsigned semantic_name
;
108 unsigned semantic_index
;
109 } gs_input
[UREG_MAX_INPUT
];
110 unsigned nr_gs_inputs
;
114 unsigned semantic_name
;
115 unsigned semantic_index
;
116 } system_value
[UREG_MAX_SYSTEM_VALUE
];
117 unsigned nr_system_values
;
120 unsigned semantic_name
;
121 unsigned semantic_index
;
122 } output
[UREG_MAX_OUTPUT
];
133 } immediate
[UREG_MAX_IMMEDIATE
];
134 unsigned nr_immediates
;
136 struct ureg_src sampler
[PIPE_MAX_SAMPLERS
];
137 unsigned nr_samplers
;
139 unsigned temps_active
[UREG_MAX_TEMP
/ 32];
142 struct const_decl const_decls
;
143 struct const_decl const_decls2D
[PIPE_MAX_CONSTANT_BUFFERS
];
145 unsigned property_gs_input_prim
;
146 unsigned property_gs_output_prim
;
147 unsigned property_gs_max_vertices
;
148 unsigned char property_fs_coord_origin
; /* = TGSI_FS_COORD_ORIGIN_* */
149 unsigned char property_fs_coord_pixel_center
; /* = TGSI_FS_COORD_PIXEL_CENTER_* */
154 unsigned nr_instructions
;
156 struct ureg_tokens domain
[2];
159 static union tgsi_any_token error_tokens
[32];
161 static void tokens_error( struct ureg_tokens
*tokens
)
163 if (tokens
->tokens
&& tokens
->tokens
!= error_tokens
)
164 FREE(tokens
->tokens
);
166 tokens
->tokens
= error_tokens
;
167 tokens
->size
= Elements(error_tokens
);
172 static void tokens_expand( struct ureg_tokens
*tokens
,
175 unsigned old_size
= tokens
->size
* sizeof(unsigned);
177 if (tokens
->tokens
== error_tokens
) {
181 while (tokens
->count
+ count
> tokens
->size
) {
182 tokens
->size
= (1 << ++tokens
->order
);
185 tokens
->tokens
= REALLOC(tokens
->tokens
,
187 tokens
->size
* sizeof(unsigned));
188 if (tokens
->tokens
== NULL
) {
189 tokens_error(tokens
);
193 static void set_bad( struct ureg_program
*ureg
)
195 tokens_error(&ureg
->domain
[0]);
200 static union tgsi_any_token
*get_tokens( struct ureg_program
*ureg
,
204 struct ureg_tokens
*tokens
= &ureg
->domain
[domain
];
205 union tgsi_any_token
*result
;
207 if (tokens
->count
+ count
> tokens
->size
)
208 tokens_expand(tokens
, count
);
210 result
= &tokens
->tokens
[tokens
->count
];
211 tokens
->count
+= count
;
216 static union tgsi_any_token
*retrieve_token( struct ureg_program
*ureg
,
220 if (ureg
->domain
[domain
].tokens
== error_tokens
)
221 return &error_tokens
[0];
223 return &ureg
->domain
[domain
].tokens
[nr
];
228 static INLINE
struct ureg_dst
229 ureg_dst_register( unsigned file
,
235 dst
.WriteMask
= TGSI_WRITEMASK_XYZW
;
237 dst
.IndirectIndex
= 0;
238 dst
.IndirectSwizzle
= 0;
242 dst
.PredSwizzleX
= TGSI_SWIZZLE_X
;
243 dst
.PredSwizzleY
= TGSI_SWIZZLE_Y
;
244 dst
.PredSwizzleZ
= TGSI_SWIZZLE_Z
;
245 dst
.PredSwizzleW
= TGSI_SWIZZLE_W
;
253 ureg_property_gs_input_prim(struct ureg_program
*ureg
,
256 ureg
->property_gs_input_prim
= input_prim
;
260 ureg_property_gs_output_prim(struct ureg_program
*ureg
,
261 unsigned output_prim
)
263 ureg
->property_gs_output_prim
= output_prim
;
267 ureg_property_gs_max_vertices(struct ureg_program
*ureg
,
268 unsigned max_vertices
)
270 ureg
->property_gs_max_vertices
= max_vertices
;
274 ureg_property_fs_coord_origin(struct ureg_program
*ureg
,
275 unsigned fs_coord_origin
)
277 ureg
->property_fs_coord_origin
= fs_coord_origin
;
281 ureg_property_fs_coord_pixel_center(struct ureg_program
*ureg
,
282 unsigned fs_coord_pixel_center
)
284 ureg
->property_fs_coord_pixel_center
= fs_coord_pixel_center
;
290 ureg_DECL_fs_input( struct ureg_program
*ureg
,
293 unsigned interp_mode
)
297 for (i
= 0; i
< ureg
->nr_fs_inputs
; i
++) {
298 if (ureg
->fs_input
[i
].semantic_name
== name
&&
299 ureg
->fs_input
[i
].semantic_index
== index
)
303 if (ureg
->nr_fs_inputs
< UREG_MAX_INPUT
) {
304 ureg
->fs_input
[i
].semantic_name
= name
;
305 ureg
->fs_input
[i
].semantic_index
= index
;
306 ureg
->fs_input
[i
].interp
= interp_mode
;
307 ureg
->nr_fs_inputs
++;
314 return ureg_src_register( TGSI_FILE_INPUT
, i
);
319 ureg_DECL_vs_input( struct ureg_program
*ureg
,
322 assert(ureg
->processor
== TGSI_PROCESSOR_VERTEX
);
324 ureg
->vs_inputs
[index
/32] |= 1 << (index
% 32);
325 return ureg_src_register( TGSI_FILE_INPUT
, index
);
330 ureg_DECL_gs_input(struct ureg_program
*ureg
,
332 unsigned semantic_name
,
333 unsigned semantic_index
)
335 if (ureg
->nr_gs_inputs
< UREG_MAX_INPUT
) {
336 ureg
->gs_input
[ureg
->nr_gs_inputs
].index
= index
;
337 ureg
->gs_input
[ureg
->nr_gs_inputs
].semantic_name
= semantic_name
;
338 ureg
->gs_input
[ureg
->nr_gs_inputs
].semantic_index
= semantic_index
;
339 ureg
->nr_gs_inputs
++;
344 /* XXX: Add suport for true 2D input registers. */
345 return ureg_src_register(TGSI_FILE_INPUT
, index
);
350 ureg_DECL_system_value(struct ureg_program
*ureg
,
352 unsigned semantic_name
,
353 unsigned semantic_index
)
355 if (ureg
->nr_system_values
< UREG_MAX_SYSTEM_VALUE
) {
356 ureg
->system_value
[ureg
->nr_system_values
].index
= index
;
357 ureg
->system_value
[ureg
->nr_system_values
].semantic_name
= semantic_name
;
358 ureg
->system_value
[ureg
->nr_system_values
].semantic_index
= semantic_index
;
359 ureg
->nr_system_values
++;
364 return ureg_src_register(TGSI_FILE_SYSTEM_VALUE
, index
);
369 ureg_DECL_output( struct ureg_program
*ureg
,
375 for (i
= 0; i
< ureg
->nr_outputs
; i
++) {
376 if (ureg
->output
[i
].semantic_name
== name
&&
377 ureg
->output
[i
].semantic_index
== index
)
381 if (ureg
->nr_outputs
< UREG_MAX_OUTPUT
) {
382 ureg
->output
[i
].semantic_name
= name
;
383 ureg
->output
[i
].semantic_index
= index
;
391 return ureg_dst_register( TGSI_FILE_OUTPUT
, i
);
395 /* Returns a new constant register. Keep track of which have been
396 * referred to so that we can emit decls later.
398 * Constant operands declared with this function must be addressed
399 * with a two-dimensional index.
401 * There is nothing in this code to bind this constant to any tracked
402 * value or manage any constant_buffer contents -- that's the
403 * resposibility of the calling code.
406 ureg_DECL_constant2D(struct ureg_program
*ureg
,
411 struct const_decl
*decl
= &ureg
->const_decls2D
[index2D
];
413 assert(index2D
< PIPE_MAX_CONSTANT_BUFFERS
);
415 if (decl
->nr_constant_ranges
< UREG_MAX_CONSTANT_RANGE
) {
416 uint i
= decl
->nr_constant_ranges
++;
418 decl
->constant_range
[i
].first
= first
;
419 decl
->constant_range
[i
].last
= last
;
424 /* A one-dimensional, depricated version of ureg_DECL_constant2D().
426 * Constant operands declared with this function must be addressed
427 * with a one-dimensional index.
430 ureg_DECL_constant(struct ureg_program
*ureg
,
433 struct const_decl
*decl
= &ureg
->const_decls
;
434 unsigned minconst
= index
, maxconst
= index
;
437 /* Inside existing range?
439 for (i
= 0; i
< decl
->nr_constant_ranges
; i
++) {
440 if (decl
->constant_range
[i
].first
<= index
&&
441 decl
->constant_range
[i
].last
>= index
) {
446 /* Extend existing range?
448 for (i
= 0; i
< decl
->nr_constant_ranges
; i
++) {
449 if (decl
->constant_range
[i
].last
== index
- 1) {
450 decl
->constant_range
[i
].last
= index
;
454 if (decl
->constant_range
[i
].first
== index
+ 1) {
455 decl
->constant_range
[i
].first
= index
;
459 minconst
= MIN2(minconst
, decl
->constant_range
[i
].first
);
460 maxconst
= MAX2(maxconst
, decl
->constant_range
[i
].last
);
465 if (decl
->nr_constant_ranges
< UREG_MAX_CONSTANT_RANGE
) {
466 i
= decl
->nr_constant_ranges
++;
467 decl
->constant_range
[i
].first
= index
;
468 decl
->constant_range
[i
].last
= index
;
472 /* Collapse all ranges down to one:
475 decl
->constant_range
[0].first
= minconst
;
476 decl
->constant_range
[0].last
= maxconst
;
477 decl
->nr_constant_ranges
= 1;
480 assert(i
< decl
->nr_constant_ranges
);
481 assert(decl
->constant_range
[i
].first
<= index
);
482 assert(decl
->constant_range
[i
].last
>= index
);
483 return ureg_src_register(TGSI_FILE_CONSTANT
, index
);
487 /* Allocate a new temporary. Temporaries greater than UREG_MAX_TEMP
488 * are legal, but will not be released.
490 struct ureg_dst
ureg_DECL_temporary( struct ureg_program
*ureg
)
494 for (i
= 0; i
< UREG_MAX_TEMP
; i
+= 32) {
495 int bit
= ffs(~ureg
->temps_active
[i
/32]);
502 /* No reusable temps, so allocate a new one:
504 i
= ureg
->nr_temps
++;
507 if (i
< UREG_MAX_TEMP
)
508 ureg
->temps_active
[i
/32] |= 1 << (i
% 32);
510 if (i
>= ureg
->nr_temps
)
511 ureg
->nr_temps
= i
+ 1;
513 return ureg_dst_register( TGSI_FILE_TEMPORARY
, i
);
517 void ureg_release_temporary( struct ureg_program
*ureg
,
518 struct ureg_dst tmp
)
520 if(tmp
.File
== TGSI_FILE_TEMPORARY
)
521 if (tmp
.Index
< UREG_MAX_TEMP
)
522 ureg
->temps_active
[tmp
.Index
/32] &= ~(1 << (tmp
.Index
% 32));
526 /* Allocate a new address register.
528 struct ureg_dst
ureg_DECL_address( struct ureg_program
*ureg
)
530 if (ureg
->nr_addrs
< UREG_MAX_ADDR
)
531 return ureg_dst_register( TGSI_FILE_ADDRESS
, ureg
->nr_addrs
++ );
534 return ureg_dst_register( TGSI_FILE_ADDRESS
, 0 );
537 /* Allocate a new loop register.
540 ureg_DECL_loop(struct ureg_program
*ureg
)
542 if (ureg
->nr_loops
< UREG_MAX_LOOP
) {
543 return ureg_dst_register(TGSI_FILE_LOOP
, ureg
->nr_loops
++);
547 return ureg_dst_register(TGSI_FILE_LOOP
, 0);
550 /* Allocate a new predicate register.
553 ureg_DECL_predicate(struct ureg_program
*ureg
)
555 if (ureg
->nr_preds
< UREG_MAX_PRED
) {
556 return ureg_dst_register(TGSI_FILE_PREDICATE
, ureg
->nr_preds
++);
560 return ureg_dst_register(TGSI_FILE_PREDICATE
, 0);
563 /* Allocate a new sampler.
565 struct ureg_src
ureg_DECL_sampler( struct ureg_program
*ureg
,
570 for (i
= 0; i
< ureg
->nr_samplers
; i
++)
571 if (ureg
->sampler
[i
].Index
== nr
)
572 return ureg
->sampler
[i
];
574 if (i
< PIPE_MAX_SAMPLERS
) {
575 ureg
->sampler
[i
] = ureg_src_register( TGSI_FILE_SAMPLER
, nr
);
577 return ureg
->sampler
[i
];
581 return ureg
->sampler
[0];
586 match_or_expand_immediate( const unsigned *v
,
592 unsigned nr2
= *pnr2
;
597 for (i
= 0; i
< nr
; i
++) {
598 boolean found
= FALSE
;
600 for (j
= 0; j
< nr2
&& !found
; j
++) {
602 *swizzle
|= j
<< (i
* 2);
613 *swizzle
|= nr2
<< (i
* 2);
618 /* Actually expand immediate only when fully succeeded.
625 static struct ureg_src
626 decl_immediate( struct ureg_program
*ureg
,
632 unsigned swizzle
= 0;
634 /* Could do a first pass where we examine all existing immediates
638 for (i
= 0; i
< ureg
->nr_immediates
; i
++) {
639 if (ureg
->immediate
[i
].type
!= type
) {
642 if (match_or_expand_immediate(v
,
644 ureg
->immediate
[i
].value
.u
,
645 &ureg
->immediate
[i
].nr
,
651 if (ureg
->nr_immediates
< UREG_MAX_IMMEDIATE
) {
652 i
= ureg
->nr_immediates
++;
653 ureg
->immediate
[i
].type
= type
;
654 if (match_or_expand_immediate(v
,
656 ureg
->immediate
[i
].value
.u
,
657 &ureg
->immediate
[i
].nr
,
666 /* Make sure that all referenced elements are from this immediate.
667 * Has the effect of making size-one immediates into scalars.
669 for (j
= nr
; j
< 4; j
++) {
670 swizzle
|= (swizzle
& 0x3) << (j
* 2);
673 return ureg_swizzle(ureg_src_register(TGSI_FILE_IMMEDIATE
, i
),
674 (swizzle
>> 0) & 0x3,
675 (swizzle
>> 2) & 0x3,
676 (swizzle
>> 4) & 0x3,
677 (swizzle
>> 6) & 0x3);
682 ureg_DECL_immediate( struct ureg_program
*ureg
,
692 for (i
= 0; i
< nr
; i
++) {
696 return decl_immediate(ureg
, fu
.u
, nr
, TGSI_IMM_FLOAT32
);
701 ureg_DECL_immediate_uint( struct ureg_program
*ureg
,
705 return decl_immediate(ureg
, v
, nr
, TGSI_IMM_UINT32
);
710 ureg_DECL_immediate_block_uint( struct ureg_program
*ureg
,
717 if (ureg
->nr_immediates
+ (nr
+ 3) / 4 > UREG_MAX_IMMEDIATE
) {
719 return ureg_src_register(TGSI_FILE_IMMEDIATE
, 0);
722 index
= ureg
->nr_immediates
;
723 ureg
->nr_immediates
+= (nr
+ 3) / 4;
725 for (i
= index
; i
< ureg
->nr_immediates
; i
++) {
726 ureg
->immediate
[i
].type
= TGSI_IMM_UINT32
;
727 ureg
->immediate
[i
].nr
= nr
> 4 ? 4 : nr
;
728 memcpy(ureg
->immediate
[i
].value
.u
,
730 ureg
->immediate
[i
].nr
* sizeof(uint
));
734 return ureg_src_register(TGSI_FILE_IMMEDIATE
, index
);
739 ureg_DECL_immediate_int( struct ureg_program
*ureg
,
743 return decl_immediate(ureg
, (const unsigned *)v
, nr
, TGSI_IMM_INT32
);
748 ureg_emit_src( struct ureg_program
*ureg
,
749 struct ureg_src src
)
751 unsigned size
= 1 + (src
.Indirect
? 1 : 0) + (src
.Dimension
? 1 : 0);
753 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_INSN
, size
);
756 assert(src
.File
!= TGSI_FILE_NULL
);
757 assert(src
.File
!= TGSI_FILE_OUTPUT
);
758 assert(src
.File
< TGSI_FILE_COUNT
);
761 out
[n
].src
.File
= src
.File
;
762 out
[n
].src
.SwizzleX
= src
.SwizzleX
;
763 out
[n
].src
.SwizzleY
= src
.SwizzleY
;
764 out
[n
].src
.SwizzleZ
= src
.SwizzleZ
;
765 out
[n
].src
.SwizzleW
= src
.SwizzleW
;
766 out
[n
].src
.Index
= src
.Index
;
767 out
[n
].src
.Negate
= src
.Negate
;
768 out
[0].src
.Absolute
= src
.Absolute
;
772 out
[0].src
.Indirect
= 1;
774 out
[n
].src
.File
= src
.IndirectFile
;
775 out
[n
].src
.SwizzleX
= src
.IndirectSwizzle
;
776 out
[n
].src
.SwizzleY
= src
.IndirectSwizzle
;
777 out
[n
].src
.SwizzleZ
= src
.IndirectSwizzle
;
778 out
[n
].src
.SwizzleW
= src
.IndirectSwizzle
;
779 out
[n
].src
.Index
= src
.IndirectIndex
;
784 out
[0].src
.Dimension
= 1;
785 out
[n
].dim
.Indirect
= 0;
786 out
[n
].dim
.Dimension
= 0;
787 out
[n
].dim
.Padding
= 0;
788 out
[n
].dim
.Index
= src
.DimensionIndex
;
797 ureg_emit_dst( struct ureg_program
*ureg
,
798 struct ureg_dst dst
)
801 (dst
.Indirect
? 1 : 0));
803 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_INSN
, size
);
806 assert(dst
.File
!= TGSI_FILE_NULL
);
807 assert(dst
.File
!= TGSI_FILE_CONSTANT
);
808 assert(dst
.File
!= TGSI_FILE_INPUT
);
809 assert(dst
.File
!= TGSI_FILE_SAMPLER
);
810 assert(dst
.File
!= TGSI_FILE_IMMEDIATE
);
811 assert(dst
.File
< TGSI_FILE_COUNT
);
814 out
[n
].dst
.File
= dst
.File
;
815 out
[n
].dst
.WriteMask
= dst
.WriteMask
;
816 out
[n
].dst
.Indirect
= dst
.Indirect
;
817 out
[n
].dst
.Index
= dst
.Index
;
822 out
[n
].src
.File
= TGSI_FILE_ADDRESS
;
823 out
[n
].src
.SwizzleX
= dst
.IndirectSwizzle
;
824 out
[n
].src
.SwizzleY
= dst
.IndirectSwizzle
;
825 out
[n
].src
.SwizzleZ
= dst
.IndirectSwizzle
;
826 out
[n
].src
.SwizzleW
= dst
.IndirectSwizzle
;
827 out
[n
].src
.Index
= dst
.IndirectIndex
;
835 static void validate( unsigned opcode
,
840 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( opcode
);
843 assert(nr_dst
== info
->num_dst
);
844 assert(nr_src
== info
->num_src
);
849 struct ureg_emit_insn_result
850 ureg_emit_insn(struct ureg_program
*ureg
,
855 unsigned pred_swizzle_x
,
856 unsigned pred_swizzle_y
,
857 unsigned pred_swizzle_z
,
858 unsigned pred_swizzle_w
,
862 union tgsi_any_token
*out
;
863 uint count
= predicate
? 2 : 1;
864 struct ureg_emit_insn_result result
;
866 validate( opcode
, num_dst
, num_src
);
868 out
= get_tokens( ureg
, DOMAIN_INSN
, count
);
869 out
[0].insn
= tgsi_default_instruction();
870 out
[0].insn
.Opcode
= opcode
;
871 out
[0].insn
.Saturate
= saturate
;
872 out
[0].insn
.NumDstRegs
= num_dst
;
873 out
[0].insn
.NumSrcRegs
= num_src
;
875 result
.insn_token
= ureg
->domain
[DOMAIN_INSN
].count
- count
;
876 result
.extended_token
= result
.insn_token
;
879 out
[0].insn
.Predicate
= 1;
880 out
[1].insn_predicate
= tgsi_default_instruction_predicate();
881 out
[1].insn_predicate
.Negate
= pred_negate
;
882 out
[1].insn_predicate
.SwizzleX
= pred_swizzle_x
;
883 out
[1].insn_predicate
.SwizzleY
= pred_swizzle_y
;
884 out
[1].insn_predicate
.SwizzleZ
= pred_swizzle_z
;
885 out
[1].insn_predicate
.SwizzleW
= pred_swizzle_w
;
888 ureg
->nr_instructions
++;
895 ureg_emit_label(struct ureg_program
*ureg
,
896 unsigned extended_token
,
897 unsigned *label_token
)
899 union tgsi_any_token
*out
, *insn
;
904 out
= get_tokens( ureg
, DOMAIN_INSN
, 1 );
907 insn
= retrieve_token( ureg
, DOMAIN_INSN
, extended_token
);
908 insn
->insn
.Label
= 1;
910 *label_token
= ureg
->domain
[DOMAIN_INSN
].count
- 1;
913 /* Will return a number which can be used in a label to point to the
914 * next instruction to be emitted.
917 ureg_get_instruction_number( struct ureg_program
*ureg
)
919 return ureg
->nr_instructions
;
922 /* Patch a given label (expressed as a token number) to point to a
923 * given instruction (expressed as an instruction number).
926 ureg_fixup_label(struct ureg_program
*ureg
,
927 unsigned label_token
,
928 unsigned instruction_number
)
930 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_INSN
, label_token
);
932 out
->insn_label
.Label
= instruction_number
;
937 ureg_emit_texture(struct ureg_program
*ureg
,
938 unsigned extended_token
,
941 union tgsi_any_token
*out
, *insn
;
943 out
= get_tokens( ureg
, DOMAIN_INSN
, 1 );
944 insn
= retrieve_token( ureg
, DOMAIN_INSN
, extended_token
);
946 insn
->insn
.Texture
= 1;
949 out
[0].insn_texture
.Texture
= target
;
954 ureg_fixup_insn_size(struct ureg_program
*ureg
,
957 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_INSN
, insn
);
959 assert(out
->insn
.Type
== TGSI_TOKEN_TYPE_INSTRUCTION
);
960 out
->insn
.NrTokens
= ureg
->domain
[DOMAIN_INSN
].count
- insn
- 1;
965 ureg_insn(struct ureg_program
*ureg
,
967 const struct ureg_dst
*dst
,
969 const struct ureg_src
*src
,
972 struct ureg_emit_insn_result insn
;
976 boolean negate
= FALSE
;
977 unsigned swizzle
[4] = { 0 };
979 saturate
= nr_dst
? dst
[0].Saturate
: FALSE
;
980 predicate
= nr_dst
? dst
[0].Predicate
: FALSE
;
982 negate
= dst
[0].PredNegate
;
983 swizzle
[0] = dst
[0].PredSwizzleX
;
984 swizzle
[1] = dst
[0].PredSwizzleY
;
985 swizzle
[2] = dst
[0].PredSwizzleZ
;
986 swizzle
[3] = dst
[0].PredSwizzleW
;
989 insn
= ureg_emit_insn(ureg
,
1001 for (i
= 0; i
< nr_dst
; i
++)
1002 ureg_emit_dst( ureg
, dst
[i
] );
1004 for (i
= 0; i
< nr_src
; i
++)
1005 ureg_emit_src( ureg
, src
[i
] );
1007 ureg_fixup_insn_size( ureg
, insn
.insn_token
);
1011 ureg_tex_insn(struct ureg_program
*ureg
,
1013 const struct ureg_dst
*dst
,
1016 const struct ureg_src
*src
,
1019 struct ureg_emit_insn_result insn
;
1023 boolean negate
= FALSE
;
1024 unsigned swizzle
[4] = { 0 };
1026 saturate
= nr_dst
? dst
[0].Saturate
: FALSE
;
1027 predicate
= nr_dst
? dst
[0].Predicate
: FALSE
;
1029 negate
= dst
[0].PredNegate
;
1030 swizzle
[0] = dst
[0].PredSwizzleX
;
1031 swizzle
[1] = dst
[0].PredSwizzleY
;
1032 swizzle
[2] = dst
[0].PredSwizzleZ
;
1033 swizzle
[3] = dst
[0].PredSwizzleW
;
1036 insn
= ureg_emit_insn(ureg
,
1048 ureg_emit_texture( ureg
, insn
.extended_token
, target
);
1050 for (i
= 0; i
< nr_dst
; i
++)
1051 ureg_emit_dst( ureg
, dst
[i
] );
1053 for (i
= 0; i
< nr_src
; i
++)
1054 ureg_emit_src( ureg
, src
[i
] );
1056 ureg_fixup_insn_size( ureg
, insn
.insn_token
);
1061 ureg_label_insn(struct ureg_program
*ureg
,
1063 const struct ureg_src
*src
,
1065 unsigned *label_token
)
1067 struct ureg_emit_insn_result insn
;
1070 insn
= ureg_emit_insn(ureg
,
1082 ureg_emit_label( ureg
, insn
.extended_token
, label_token
);
1084 for (i
= 0; i
< nr_src
; i
++)
1085 ureg_emit_src( ureg
, src
[i
] );
1087 ureg_fixup_insn_size( ureg
, insn
.insn_token
);
1092 static void emit_decl( struct ureg_program
*ureg
,
1095 unsigned semantic_name
,
1096 unsigned semantic_index
,
1099 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 3 );
1102 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1103 out
[0].decl
.NrTokens
= 3;
1104 out
[0].decl
.File
= file
;
1105 out
[0].decl
.UsageMask
= TGSI_WRITEMASK_XYZW
; /* FIXME! */
1106 out
[0].decl
.Interpolate
= interp
;
1107 out
[0].decl
.Semantic
= 1;
1110 out
[1].decl_range
.First
=
1111 out
[1].decl_range
.Last
= index
;
1114 out
[2].decl_semantic
.Name
= semantic_name
;
1115 out
[2].decl_semantic
.Index
= semantic_index
;
1120 static void emit_decl_range( struct ureg_program
*ureg
,
1125 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 2 );
1128 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1129 out
[0].decl
.NrTokens
= 2;
1130 out
[0].decl
.File
= file
;
1131 out
[0].decl
.UsageMask
= 0xf;
1132 out
[0].decl
.Interpolate
= TGSI_INTERPOLATE_CONSTANT
;
1133 out
[0].decl
.Semantic
= 0;
1136 out
[1].decl_range
.First
= first
;
1137 out
[1].decl_range
.Last
= first
+ count
- 1;
1141 emit_decl_range2D(struct ureg_program
*ureg
,
1147 union tgsi_any_token
*out
= get_tokens(ureg
, DOMAIN_DECL
, 3);
1150 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1151 out
[0].decl
.NrTokens
= 3;
1152 out
[0].decl
.File
= file
;
1153 out
[0].decl
.UsageMask
= 0xf;
1154 out
[0].decl
.Interpolate
= TGSI_INTERPOLATE_CONSTANT
;
1155 out
[0].decl
.Dimension
= 1;
1158 out
[1].decl_range
.First
= first
;
1159 out
[1].decl_range
.Last
= last
;
1162 out
[2].decl_dim
.Index2D
= index2D
;
1166 emit_immediate( struct ureg_program
*ureg
,
1170 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 5 );
1173 out
[0].imm
.Type
= TGSI_TOKEN_TYPE_IMMEDIATE
;
1174 out
[0].imm
.NrTokens
= 5;
1175 out
[0].imm
.DataType
= type
;
1176 out
[0].imm
.Padding
= 0;
1178 out
[1].imm_data
.Uint
= v
[0];
1179 out
[2].imm_data
.Uint
= v
[1];
1180 out
[3].imm_data
.Uint
= v
[2];
1181 out
[4].imm_data
.Uint
= v
[3];
1185 emit_property(struct ureg_program
*ureg
,
1189 union tgsi_any_token
*out
= get_tokens(ureg
, DOMAIN_DECL
, 2);
1192 out
[0].prop
.Type
= TGSI_TOKEN_TYPE_PROPERTY
;
1193 out
[0].prop
.NrTokens
= 2;
1194 out
[0].prop
.PropertyName
= name
;
1196 out
[1].prop_data
.Data
= data
;
1200 static void emit_decls( struct ureg_program
*ureg
)
1204 if (ureg
->property_gs_input_prim
!= ~0) {
1205 assert(ureg
->processor
== TGSI_PROCESSOR_GEOMETRY
);
1208 TGSI_PROPERTY_GS_INPUT_PRIM
,
1209 ureg
->property_gs_input_prim
);
1212 if (ureg
->property_gs_output_prim
!= ~0) {
1213 assert(ureg
->processor
== TGSI_PROCESSOR_GEOMETRY
);
1216 TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1217 ureg
->property_gs_output_prim
);
1220 if (ureg
->property_gs_max_vertices
!= ~0) {
1221 assert(ureg
->processor
== TGSI_PROCESSOR_GEOMETRY
);
1224 TGSI_PROPERTY_GS_MAX_VERTICES
,
1225 ureg
->property_gs_max_vertices
);
1228 if (ureg
->property_fs_coord_origin
) {
1229 assert(ureg
->processor
== TGSI_PROCESSOR_FRAGMENT
);
1232 TGSI_PROPERTY_FS_COORD_ORIGIN
,
1233 ureg
->property_fs_coord_origin
);
1236 if (ureg
->property_fs_coord_pixel_center
) {
1237 assert(ureg
->processor
== TGSI_PROCESSOR_FRAGMENT
);
1240 TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
,
1241 ureg
->property_fs_coord_pixel_center
);
1244 if (ureg
->processor
== TGSI_PROCESSOR_VERTEX
) {
1245 for (i
= 0; i
< UREG_MAX_INPUT
; i
++) {
1246 if (ureg
->vs_inputs
[i
/32] & (1 << (i
%32))) {
1247 emit_decl_range( ureg
, TGSI_FILE_INPUT
, i
, 1 );
1250 } else if (ureg
->processor
== TGSI_PROCESSOR_FRAGMENT
) {
1251 for (i
= 0; i
< ureg
->nr_fs_inputs
; i
++) {
1255 ureg
->fs_input
[i
].semantic_name
,
1256 ureg
->fs_input
[i
].semantic_index
,
1257 ureg
->fs_input
[i
].interp
);
1260 for (i
= 0; i
< ureg
->nr_gs_inputs
; i
++) {
1263 ureg
->gs_input
[i
].index
,
1264 ureg
->gs_input
[i
].semantic_name
,
1265 ureg
->gs_input
[i
].semantic_index
,
1266 TGSI_INTERPOLATE_CONSTANT
);
1270 for (i
= 0; i
< ureg
->nr_system_values
; i
++) {
1272 TGSI_FILE_SYSTEM_VALUE
,
1273 ureg
->system_value
[i
].index
,
1274 ureg
->system_value
[i
].semantic_name
,
1275 ureg
->system_value
[i
].semantic_index
,
1276 TGSI_INTERPOLATE_CONSTANT
);
1279 for (i
= 0; i
< ureg
->nr_outputs
; i
++) {
1283 ureg
->output
[i
].semantic_name
,
1284 ureg
->output
[i
].semantic_index
,
1285 TGSI_INTERPOLATE_CONSTANT
);
1288 for (i
= 0; i
< ureg
->nr_samplers
; i
++) {
1289 emit_decl_range( ureg
,
1291 ureg
->sampler
[i
].Index
, 1 );
1294 if (ureg
->const_decls
.nr_constant_ranges
) {
1295 for (i
= 0; i
< ureg
->const_decls
.nr_constant_ranges
; i
++) {
1296 emit_decl_range(ureg
,
1298 ureg
->const_decls
.constant_range
[i
].first
,
1299 ureg
->const_decls
.constant_range
[i
].last
- ureg
->const_decls
.constant_range
[i
].first
+ 1);
1303 for (i
= 0; i
< PIPE_MAX_CONSTANT_BUFFERS
; i
++) {
1304 struct const_decl
*decl
= &ureg
->const_decls2D
[i
];
1306 if (decl
->nr_constant_ranges
) {
1309 for (j
= 0; j
< decl
->nr_constant_ranges
; j
++) {
1310 emit_decl_range2D(ureg
,
1312 decl
->constant_range
[j
].first
,
1313 decl
->constant_range
[j
].last
,
1319 if (ureg
->nr_temps
) {
1320 emit_decl_range( ureg
,
1321 TGSI_FILE_TEMPORARY
,
1322 0, ureg
->nr_temps
);
1325 if (ureg
->nr_addrs
) {
1326 emit_decl_range( ureg
,
1328 0, ureg
->nr_addrs
);
1331 if (ureg
->nr_loops
) {
1332 emit_decl_range(ureg
,
1338 if (ureg
->nr_preds
) {
1339 emit_decl_range(ureg
,
1340 TGSI_FILE_PREDICATE
,
1345 for (i
= 0; i
< ureg
->nr_immediates
; i
++) {
1346 emit_immediate( ureg
,
1347 ureg
->immediate
[i
].value
.u
,
1348 ureg
->immediate
[i
].type
);
1352 /* Append the instruction tokens onto the declarations to build a
1353 * contiguous stream suitable to send to the driver.
1355 static void copy_instructions( struct ureg_program
*ureg
)
1357 unsigned nr_tokens
= ureg
->domain
[DOMAIN_INSN
].count
;
1358 union tgsi_any_token
*out
= get_tokens( ureg
,
1363 ureg
->domain
[DOMAIN_INSN
].tokens
,
1364 nr_tokens
* sizeof out
[0] );
1369 fixup_header_size(struct ureg_program
*ureg
)
1371 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_DECL
, 0 );
1373 out
->header
.BodySize
= ureg
->domain
[DOMAIN_DECL
].count
- 2;
1378 emit_header( struct ureg_program
*ureg
)
1380 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 2 );
1382 out
[0].header
.HeaderSize
= 2;
1383 out
[0].header
.BodySize
= 0;
1385 out
[1].processor
.Processor
= ureg
->processor
;
1386 out
[1].processor
.Padding
= 0;
1390 const struct tgsi_token
*ureg_finalize( struct ureg_program
*ureg
)
1392 const struct tgsi_token
*tokens
;
1394 emit_header( ureg
);
1396 copy_instructions( ureg
);
1397 fixup_header_size( ureg
);
1399 if (ureg
->domain
[0].tokens
== error_tokens
||
1400 ureg
->domain
[1].tokens
== error_tokens
) {
1401 debug_printf("%s: error in generated shader\n", __FUNCTION__
);
1406 tokens
= &ureg
->domain
[DOMAIN_DECL
].tokens
[0].token
;
1409 debug_printf("%s: emitted shader %d tokens:\n", __FUNCTION__
,
1410 ureg
->domain
[DOMAIN_DECL
].count
);
1411 tgsi_dump( tokens
, 0 );
1415 if (tokens
&& !tgsi_sanity_check(tokens
)) {
1416 debug_printf("tgsi_ureg.c, sanity check failed on generated tokens:\n");
1417 tgsi_dump(tokens
, 0);
1427 void *ureg_create_shader( struct ureg_program
*ureg
,
1428 struct pipe_context
*pipe
)
1430 struct pipe_shader_state state
;
1432 state
.tokens
= ureg_finalize(ureg
);
1436 if (ureg
->processor
== TGSI_PROCESSOR_VERTEX
)
1437 return pipe
->create_vs_state( pipe
, &state
);
1439 return pipe
->create_fs_state( pipe
, &state
);
1443 const struct tgsi_token
*ureg_get_tokens( struct ureg_program
*ureg
,
1444 unsigned *nr_tokens
)
1446 const struct tgsi_token
*tokens
;
1448 ureg_finalize(ureg
);
1450 tokens
= &ureg
->domain
[DOMAIN_DECL
].tokens
[0].token
;
1453 *nr_tokens
= ureg
->domain
[DOMAIN_DECL
].size
;
1455 ureg
->domain
[DOMAIN_DECL
].tokens
= 0;
1456 ureg
->domain
[DOMAIN_DECL
].size
= 0;
1457 ureg
->domain
[DOMAIN_DECL
].order
= 0;
1458 ureg
->domain
[DOMAIN_DECL
].count
= 0;
1464 struct ureg_program
*ureg_create( unsigned processor
)
1466 struct ureg_program
*ureg
= CALLOC_STRUCT( ureg_program
);
1470 ureg
->processor
= processor
;
1471 ureg
->property_gs_input_prim
= ~0;
1472 ureg
->property_gs_output_prim
= ~0;
1473 ureg
->property_gs_max_vertices
= ~0;
1478 void ureg_destroy( struct ureg_program
*ureg
)
1482 for (i
= 0; i
< Elements(ureg
->domain
); i
++) {
1483 if (ureg
->domain
[i
].tokens
&&
1484 ureg
->domain
[i
].tokens
!= error_tokens
)
1485 FREE(ureg
->domain
[i
].tokens
);