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_memory.h"
37 #include "util/u_math.h"
39 union tgsi_any_token
{
40 struct tgsi_header header
;
41 struct tgsi_processor processor
;
42 struct tgsi_token token
;
43 struct tgsi_property prop
;
44 struct tgsi_property_data prop_data
;
45 struct tgsi_declaration decl
;
46 struct tgsi_declaration_range decl_range
;
47 struct tgsi_declaration_dimension decl_dim
;
48 struct tgsi_declaration_semantic decl_semantic
;
49 struct tgsi_immediate imm
;
50 union tgsi_immediate_data imm_data
;
51 struct tgsi_instruction insn
;
52 struct tgsi_instruction_predicate insn_predicate
;
53 struct tgsi_instruction_label insn_label
;
54 struct tgsi_instruction_texture insn_texture
;
55 struct tgsi_src_register src
;
56 struct tgsi_dimension dim
;
57 struct tgsi_dst_register dst
;
63 union tgsi_any_token
*tokens
;
69 #define UREG_MAX_INPUT PIPE_MAX_ATTRIBS
70 #define UREG_MAX_SYSTEM_VALUE PIPE_MAX_ATTRIBS
71 #define UREG_MAX_OUTPUT PIPE_MAX_ATTRIBS
72 #define UREG_MAX_CONSTANT_RANGE 32
73 #define UREG_MAX_IMMEDIATE 32
74 #define UREG_MAX_TEMP 256
75 #define UREG_MAX_ADDR 2
76 #define UREG_MAX_LOOP 1
77 #define UREG_MAX_PRED 1
83 } constant_range
[UREG_MAX_CONSTANT_RANGE
];
84 unsigned nr_constant_ranges
;
93 struct pipe_context
*pipe
;
96 unsigned semantic_name
;
97 unsigned semantic_index
;
99 } fs_input
[UREG_MAX_INPUT
];
100 unsigned nr_fs_inputs
;
102 unsigned vs_inputs
[UREG_MAX_INPUT
/32];
106 } gs_input
[UREG_MAX_INPUT
];
107 unsigned nr_gs_inputs
;
111 unsigned semantic_name
;
112 unsigned semantic_index
;
113 } system_value
[UREG_MAX_SYSTEM_VALUE
];
114 unsigned nr_system_values
;
117 unsigned semantic_name
;
118 unsigned semantic_index
;
119 } output
[UREG_MAX_OUTPUT
];
130 } immediate
[UREG_MAX_IMMEDIATE
];
131 unsigned nr_immediates
;
133 struct ureg_src sampler
[PIPE_MAX_SAMPLERS
];
134 unsigned nr_samplers
;
136 unsigned temps_active
[UREG_MAX_TEMP
/ 32];
139 struct const_decl const_decls
;
140 struct const_decl const_decls2D
[PIPE_MAX_CONSTANT_BUFFERS
];
142 unsigned property_gs_input_prim
;
143 unsigned property_gs_output_prim
;
144 unsigned property_gs_max_vertices
;
145 unsigned char property_fs_coord_origin
; /* = TGSI_FS_COORD_ORIGIN_* */
146 unsigned char property_fs_coord_pixel_center
; /* = TGSI_FS_COORD_PIXEL_CENTER_* */
151 unsigned nr_instructions
;
153 struct ureg_tokens domain
[2];
156 static union tgsi_any_token error_tokens
[32];
158 static void tokens_error( struct ureg_tokens
*tokens
)
160 if (tokens
->tokens
&& tokens
->tokens
!= error_tokens
)
161 FREE(tokens
->tokens
);
163 tokens
->tokens
= error_tokens
;
164 tokens
->size
= Elements(error_tokens
);
169 static void tokens_expand( struct ureg_tokens
*tokens
,
172 unsigned old_size
= tokens
->size
* sizeof(unsigned);
174 if (tokens
->tokens
== error_tokens
) {
178 while (tokens
->count
+ count
> tokens
->size
) {
179 tokens
->size
= (1 << ++tokens
->order
);
182 tokens
->tokens
= REALLOC(tokens
->tokens
,
184 tokens
->size
* sizeof(unsigned));
185 if (tokens
->tokens
== NULL
) {
186 tokens_error(tokens
);
190 static void set_bad( struct ureg_program
*ureg
)
192 tokens_error(&ureg
->domain
[0]);
197 static union tgsi_any_token
*get_tokens( struct ureg_program
*ureg
,
201 struct ureg_tokens
*tokens
= &ureg
->domain
[domain
];
202 union tgsi_any_token
*result
;
204 if (tokens
->count
+ count
> tokens
->size
)
205 tokens_expand(tokens
, count
);
207 result
= &tokens
->tokens
[tokens
->count
];
208 tokens
->count
+= count
;
213 static union tgsi_any_token
*retrieve_token( struct ureg_program
*ureg
,
217 if (ureg
->domain
[domain
].tokens
== error_tokens
)
218 return &error_tokens
[0];
220 return &ureg
->domain
[domain
].tokens
[nr
];
225 static INLINE
struct ureg_dst
226 ureg_dst_register( unsigned file
,
232 dst
.WriteMask
= TGSI_WRITEMASK_XYZW
;
234 dst
.IndirectIndex
= 0;
235 dst
.IndirectSwizzle
= 0;
239 dst
.PredSwizzleX
= TGSI_SWIZZLE_X
;
240 dst
.PredSwizzleY
= TGSI_SWIZZLE_Y
;
241 dst
.PredSwizzleZ
= TGSI_SWIZZLE_Z
;
242 dst
.PredSwizzleW
= TGSI_SWIZZLE_W
;
250 ureg_property_gs_input_prim(struct ureg_program
*ureg
,
253 ureg
->property_gs_input_prim
= input_prim
;
257 ureg_property_gs_output_prim(struct ureg_program
*ureg
,
258 unsigned output_prim
)
260 ureg
->property_gs_output_prim
= output_prim
;
264 ureg_property_gs_max_vertices(struct ureg_program
*ureg
,
265 unsigned max_vertices
)
267 ureg
->property_gs_max_vertices
= max_vertices
;
271 ureg_property_fs_coord_origin(struct ureg_program
*ureg
,
272 unsigned fs_coord_origin
)
274 ureg
->property_fs_coord_origin
= fs_coord_origin
;
278 ureg_property_fs_coord_pixel_center(struct ureg_program
*ureg
,
279 unsigned fs_coord_pixel_center
)
281 ureg
->property_fs_coord_pixel_center
= fs_coord_pixel_center
;
287 ureg_DECL_fs_input( struct ureg_program
*ureg
,
290 unsigned interp_mode
)
294 for (i
= 0; i
< ureg
->nr_fs_inputs
; i
++) {
295 if (ureg
->fs_input
[i
].semantic_name
== name
&&
296 ureg
->fs_input
[i
].semantic_index
== index
)
300 if (ureg
->nr_fs_inputs
< UREG_MAX_INPUT
) {
301 ureg
->fs_input
[i
].semantic_name
= name
;
302 ureg
->fs_input
[i
].semantic_index
= index
;
303 ureg
->fs_input
[i
].interp
= interp_mode
;
304 ureg
->nr_fs_inputs
++;
311 return ureg_src_register( TGSI_FILE_INPUT
, i
);
316 ureg_DECL_vs_input( struct ureg_program
*ureg
,
319 assert(ureg
->processor
== TGSI_PROCESSOR_VERTEX
);
321 ureg
->vs_inputs
[index
/32] |= 1 << (index
% 32);
322 return ureg_src_register( TGSI_FILE_INPUT
, index
);
327 ureg_DECL_gs_input(struct ureg_program
*ureg
,
330 if (ureg
->nr_gs_inputs
< UREG_MAX_INPUT
) {
331 ureg
->gs_input
[ureg
->nr_gs_inputs
].index
= index
;
332 ureg
->nr_gs_inputs
++;
337 /* XXX: Add suport for true 2D input registers. */
338 return ureg_src_register(TGSI_FILE_INPUT
, index
);
343 ureg_DECL_system_value(struct ureg_program
*ureg
,
345 unsigned semantic_name
,
346 unsigned semantic_index
)
348 if (ureg
->nr_system_values
< UREG_MAX_SYSTEM_VALUE
) {
349 ureg
->system_value
[ureg
->nr_system_values
].index
= index
;
350 ureg
->system_value
[ureg
->nr_system_values
].semantic_name
= semantic_name
;
351 ureg
->system_value
[ureg
->nr_system_values
].semantic_index
= semantic_index
;
352 ureg
->nr_system_values
++;
357 return ureg_src_register(TGSI_FILE_SYSTEM_VALUE
, index
);
362 ureg_DECL_output( struct ureg_program
*ureg
,
368 for (i
= 0; i
< ureg
->nr_outputs
; i
++) {
369 if (ureg
->output
[i
].semantic_name
== name
&&
370 ureg
->output
[i
].semantic_index
== index
)
374 if (ureg
->nr_outputs
< UREG_MAX_OUTPUT
) {
375 ureg
->output
[i
].semantic_name
= name
;
376 ureg
->output
[i
].semantic_index
= index
;
384 return ureg_dst_register( TGSI_FILE_OUTPUT
, i
);
388 /* Returns a new constant register. Keep track of which have been
389 * referred to so that we can emit decls later.
391 * Constant operands declared with this function must be addressed
392 * with a two-dimensional index.
394 * There is nothing in this code to bind this constant to any tracked
395 * value or manage any constant_buffer contents -- that's the
396 * resposibility of the calling code.
399 ureg_DECL_constant2D(struct ureg_program
*ureg
,
404 struct const_decl
*decl
= &ureg
->const_decls2D
[index2D
];
406 assert(index2D
< PIPE_MAX_CONSTANT_BUFFERS
);
408 if (decl
->nr_constant_ranges
< UREG_MAX_CONSTANT_RANGE
) {
409 uint i
= decl
->nr_constant_ranges
++;
411 decl
->constant_range
[i
].first
= first
;
412 decl
->constant_range
[i
].last
= last
;
417 /* A one-dimensional, depricated version of ureg_DECL_constant2D().
419 * Constant operands declared with this function must be addressed
420 * with a one-dimensional index.
423 ureg_DECL_constant(struct ureg_program
*ureg
,
426 struct const_decl
*decl
= &ureg
->const_decls
;
427 unsigned minconst
= index
, maxconst
= index
;
430 /* Inside existing range?
432 for (i
= 0; i
< decl
->nr_constant_ranges
; i
++) {
433 if (decl
->constant_range
[i
].first
<= index
&&
434 decl
->constant_range
[i
].last
>= index
) {
439 /* Extend existing range?
441 for (i
= 0; i
< decl
->nr_constant_ranges
; i
++) {
442 if (decl
->constant_range
[i
].last
== index
- 1) {
443 decl
->constant_range
[i
].last
= index
;
447 if (decl
->constant_range
[i
].first
== index
+ 1) {
448 decl
->constant_range
[i
].first
= index
;
452 minconst
= MIN2(minconst
, decl
->constant_range
[i
].first
);
453 maxconst
= MAX2(maxconst
, decl
->constant_range
[i
].last
);
458 if (decl
->nr_constant_ranges
< UREG_MAX_CONSTANT_RANGE
) {
459 i
= decl
->nr_constant_ranges
++;
460 decl
->constant_range
[i
].first
= index
;
461 decl
->constant_range
[i
].last
= index
;
465 /* Collapse all ranges down to one:
468 decl
->constant_range
[0].first
= minconst
;
469 decl
->constant_range
[0].last
= maxconst
;
470 decl
->nr_constant_ranges
= 1;
473 assert(i
< decl
->nr_constant_ranges
);
474 assert(decl
->constant_range
[i
].first
<= index
);
475 assert(decl
->constant_range
[i
].last
>= index
);
476 return ureg_src_register(TGSI_FILE_CONSTANT
, index
);
480 /* Allocate a new temporary. Temporaries greater than UREG_MAX_TEMP
481 * are legal, but will not be released.
483 struct ureg_dst
ureg_DECL_temporary( struct ureg_program
*ureg
)
487 for (i
= 0; i
< UREG_MAX_TEMP
; i
+= 32) {
488 int bit
= ffs(~ureg
->temps_active
[i
/32]);
495 /* No reusable temps, so allocate a new one:
497 i
= ureg
->nr_temps
++;
500 if (i
< UREG_MAX_TEMP
)
501 ureg
->temps_active
[i
/32] |= 1 << (i
% 32);
503 if (i
>= ureg
->nr_temps
)
504 ureg
->nr_temps
= i
+ 1;
506 return ureg_dst_register( TGSI_FILE_TEMPORARY
, i
);
510 void ureg_release_temporary( struct ureg_program
*ureg
,
511 struct ureg_dst tmp
)
513 if(tmp
.File
== TGSI_FILE_TEMPORARY
)
514 if (tmp
.Index
< UREG_MAX_TEMP
)
515 ureg
->temps_active
[tmp
.Index
/32] &= ~(1 << (tmp
.Index
% 32));
519 /* Allocate a new address register.
521 struct ureg_dst
ureg_DECL_address( struct ureg_program
*ureg
)
523 if (ureg
->nr_addrs
< UREG_MAX_ADDR
)
524 return ureg_dst_register( TGSI_FILE_ADDRESS
, ureg
->nr_addrs
++ );
527 return ureg_dst_register( TGSI_FILE_ADDRESS
, 0 );
530 /* Allocate a new loop register.
533 ureg_DECL_loop(struct ureg_program
*ureg
)
535 if (ureg
->nr_loops
< UREG_MAX_LOOP
) {
536 return ureg_dst_register(TGSI_FILE_LOOP
, ureg
->nr_loops
++);
540 return ureg_dst_register(TGSI_FILE_LOOP
, 0);
543 /* Allocate a new predicate register.
546 ureg_DECL_predicate(struct ureg_program
*ureg
)
548 if (ureg
->nr_preds
< UREG_MAX_PRED
) {
549 return ureg_dst_register(TGSI_FILE_PREDICATE
, ureg
->nr_preds
++);
553 return ureg_dst_register(TGSI_FILE_PREDICATE
, 0);
556 /* Allocate a new sampler.
558 struct ureg_src
ureg_DECL_sampler( struct ureg_program
*ureg
,
563 for (i
= 0; i
< ureg
->nr_samplers
; i
++)
564 if (ureg
->sampler
[i
].Index
== nr
)
565 return ureg
->sampler
[i
];
567 if (i
< PIPE_MAX_SAMPLERS
) {
568 ureg
->sampler
[i
] = ureg_src_register( TGSI_FILE_SAMPLER
, nr
);
570 return ureg
->sampler
[i
];
574 return ureg
->sampler
[0];
579 match_or_expand_immediate( const unsigned *v
,
585 unsigned nr2
= *pnr2
;
590 for (i
= 0; i
< nr
; i
++) {
591 boolean found
= FALSE
;
593 for (j
= 0; j
< nr2
&& !found
; j
++) {
595 *swizzle
|= j
<< (i
* 2);
606 *swizzle
|= nr2
<< (i
* 2);
611 /* Actually expand immediate only when fully succeeded.
618 static struct ureg_src
619 decl_immediate( struct ureg_program
*ureg
,
625 unsigned swizzle
= 0;
627 /* Could do a first pass where we examine all existing immediates
631 for (i
= 0; i
< ureg
->nr_immediates
; i
++) {
632 if (ureg
->immediate
[i
].type
!= type
) {
635 if (match_or_expand_immediate(v
,
637 ureg
->immediate
[i
].value
.u
,
638 &ureg
->immediate
[i
].nr
,
644 if (ureg
->nr_immediates
< UREG_MAX_IMMEDIATE
) {
645 i
= ureg
->nr_immediates
++;
646 ureg
->immediate
[i
].type
= type
;
647 if (match_or_expand_immediate(v
,
649 ureg
->immediate
[i
].value
.u
,
650 &ureg
->immediate
[i
].nr
,
659 /* Make sure that all referenced elements are from this immediate.
660 * Has the effect of making size-one immediates into scalars.
662 for (j
= nr
; j
< 4; j
++) {
663 swizzle
|= (swizzle
& 0x3) << (j
* 2);
666 return ureg_swizzle(ureg_src_register(TGSI_FILE_IMMEDIATE
, i
),
667 (swizzle
>> 0) & 0x3,
668 (swizzle
>> 2) & 0x3,
669 (swizzle
>> 4) & 0x3,
670 (swizzle
>> 6) & 0x3);
675 ureg_DECL_immediate( struct ureg_program
*ureg
,
685 for (i
= 0; i
< nr
; i
++) {
689 return decl_immediate(ureg
, fu
.u
, nr
, TGSI_IMM_FLOAT32
);
694 ureg_DECL_immediate_uint( struct ureg_program
*ureg
,
698 return decl_immediate(ureg
, v
, nr
, TGSI_IMM_UINT32
);
703 ureg_DECL_immediate_block_uint( struct ureg_program
*ureg
,
710 if (ureg
->nr_immediates
+ (nr
+ 3) / 4 > UREG_MAX_IMMEDIATE
) {
712 return ureg_src_register(TGSI_FILE_IMMEDIATE
, 0);
715 index
= ureg
->nr_immediates
;
716 ureg
->nr_immediates
+= (nr
+ 3) / 4;
718 for (i
= index
; i
< ureg
->nr_immediates
; i
++) {
719 ureg
->immediate
[i
].type
= TGSI_IMM_UINT32
;
720 ureg
->immediate
[i
].nr
= nr
> 4 ? 4 : nr
;
721 memcpy(ureg
->immediate
[i
].value
.u
,
723 ureg
->immediate
[i
].nr
* sizeof(uint
));
727 return ureg_src_register(TGSI_FILE_IMMEDIATE
, index
);
732 ureg_DECL_immediate_int( struct ureg_program
*ureg
,
736 return decl_immediate(ureg
, (const unsigned *)v
, nr
, TGSI_IMM_INT32
);
741 ureg_emit_src( struct ureg_program
*ureg
,
742 struct ureg_src src
)
744 unsigned size
= 1 + (src
.Indirect
? 1 : 0) + (src
.Dimension
? 1 : 0);
746 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_INSN
, size
);
749 assert(src
.File
!= TGSI_FILE_NULL
);
750 assert(src
.File
!= TGSI_FILE_OUTPUT
);
751 assert(src
.File
< TGSI_FILE_COUNT
);
754 out
[n
].src
.File
= src
.File
;
755 out
[n
].src
.SwizzleX
= src
.SwizzleX
;
756 out
[n
].src
.SwizzleY
= src
.SwizzleY
;
757 out
[n
].src
.SwizzleZ
= src
.SwizzleZ
;
758 out
[n
].src
.SwizzleW
= src
.SwizzleW
;
759 out
[n
].src
.Index
= src
.Index
;
760 out
[n
].src
.Negate
= src
.Negate
;
761 out
[0].src
.Absolute
= src
.Absolute
;
765 out
[0].src
.Indirect
= 1;
767 out
[n
].src
.File
= src
.IndirectFile
;
768 out
[n
].src
.SwizzleX
= src
.IndirectSwizzle
;
769 out
[n
].src
.SwizzleY
= src
.IndirectSwizzle
;
770 out
[n
].src
.SwizzleZ
= src
.IndirectSwizzle
;
771 out
[n
].src
.SwizzleW
= src
.IndirectSwizzle
;
772 out
[n
].src
.Index
= src
.IndirectIndex
;
777 out
[0].src
.Dimension
= 1;
778 out
[n
].dim
.Indirect
= 0;
779 out
[n
].dim
.Dimension
= 0;
780 out
[n
].dim
.Padding
= 0;
781 out
[n
].dim
.Index
= src
.DimensionIndex
;
790 ureg_emit_dst( struct ureg_program
*ureg
,
791 struct ureg_dst dst
)
794 (dst
.Indirect
? 1 : 0));
796 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_INSN
, size
);
799 assert(dst
.File
!= TGSI_FILE_NULL
);
800 assert(dst
.File
!= TGSI_FILE_CONSTANT
);
801 assert(dst
.File
!= TGSI_FILE_INPUT
);
802 assert(dst
.File
!= TGSI_FILE_SAMPLER
);
803 assert(dst
.File
!= TGSI_FILE_IMMEDIATE
);
804 assert(dst
.File
< TGSI_FILE_COUNT
);
807 out
[n
].dst
.File
= dst
.File
;
808 out
[n
].dst
.WriteMask
= dst
.WriteMask
;
809 out
[n
].dst
.Indirect
= dst
.Indirect
;
810 out
[n
].dst
.Index
= dst
.Index
;
815 out
[n
].src
.File
= TGSI_FILE_ADDRESS
;
816 out
[n
].src
.SwizzleX
= dst
.IndirectSwizzle
;
817 out
[n
].src
.SwizzleY
= dst
.IndirectSwizzle
;
818 out
[n
].src
.SwizzleZ
= dst
.IndirectSwizzle
;
819 out
[n
].src
.SwizzleW
= dst
.IndirectSwizzle
;
820 out
[n
].src
.Index
= dst
.IndirectIndex
;
828 static void validate( unsigned opcode
,
833 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( opcode
);
836 assert(nr_dst
== info
->num_dst
);
837 assert(nr_src
== info
->num_src
);
842 struct ureg_emit_insn_result
843 ureg_emit_insn(struct ureg_program
*ureg
,
848 unsigned pred_swizzle_x
,
849 unsigned pred_swizzle_y
,
850 unsigned pred_swizzle_z
,
851 unsigned pred_swizzle_w
,
855 union tgsi_any_token
*out
;
856 uint count
= predicate
? 2 : 1;
857 struct ureg_emit_insn_result result
;
859 validate( opcode
, num_dst
, num_src
);
861 out
= get_tokens( ureg
, DOMAIN_INSN
, count
);
862 out
[0].insn
= tgsi_default_instruction();
863 out
[0].insn
.Opcode
= opcode
;
864 out
[0].insn
.Saturate
= saturate
;
865 out
[0].insn
.NumDstRegs
= num_dst
;
866 out
[0].insn
.NumSrcRegs
= num_src
;
868 result
.insn_token
= ureg
->domain
[DOMAIN_INSN
].count
- count
;
869 result
.extended_token
= result
.insn_token
;
872 out
[0].insn
.Predicate
= 1;
873 out
[1].insn_predicate
= tgsi_default_instruction_predicate();
874 out
[1].insn_predicate
.Negate
= pred_negate
;
875 out
[1].insn_predicate
.SwizzleX
= pred_swizzle_x
;
876 out
[1].insn_predicate
.SwizzleY
= pred_swizzle_y
;
877 out
[1].insn_predicate
.SwizzleZ
= pred_swizzle_z
;
878 out
[1].insn_predicate
.SwizzleW
= pred_swizzle_w
;
881 ureg
->nr_instructions
++;
888 ureg_emit_label(struct ureg_program
*ureg
,
889 unsigned extended_token
,
890 unsigned *label_token
)
892 union tgsi_any_token
*out
, *insn
;
897 out
= get_tokens( ureg
, DOMAIN_INSN
, 1 );
900 insn
= retrieve_token( ureg
, DOMAIN_INSN
, extended_token
);
901 insn
->insn
.Label
= 1;
903 *label_token
= ureg
->domain
[DOMAIN_INSN
].count
- 1;
906 /* Will return a number which can be used in a label to point to the
907 * next instruction to be emitted.
910 ureg_get_instruction_number( struct ureg_program
*ureg
)
912 return ureg
->nr_instructions
;
915 /* Patch a given label (expressed as a token number) to point to a
916 * given instruction (expressed as an instruction number).
919 ureg_fixup_label(struct ureg_program
*ureg
,
920 unsigned label_token
,
921 unsigned instruction_number
)
923 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_INSN
, label_token
);
925 out
->insn_label
.Label
= instruction_number
;
930 ureg_emit_texture(struct ureg_program
*ureg
,
931 unsigned extended_token
,
934 union tgsi_any_token
*out
, *insn
;
936 out
= get_tokens( ureg
, DOMAIN_INSN
, 1 );
937 insn
= retrieve_token( ureg
, DOMAIN_INSN
, extended_token
);
939 insn
->insn
.Texture
= 1;
942 out
[0].insn_texture
.Texture
= target
;
947 ureg_fixup_insn_size(struct ureg_program
*ureg
,
950 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_INSN
, insn
);
952 assert(out
->insn
.Type
== TGSI_TOKEN_TYPE_INSTRUCTION
);
953 out
->insn
.NrTokens
= ureg
->domain
[DOMAIN_INSN
].count
- insn
- 1;
958 ureg_insn(struct ureg_program
*ureg
,
960 const struct ureg_dst
*dst
,
962 const struct ureg_src
*src
,
965 struct ureg_emit_insn_result insn
;
969 boolean negate
= FALSE
;
970 unsigned swizzle
[4] = { 0 };
972 saturate
= nr_dst
? dst
[0].Saturate
: FALSE
;
973 predicate
= nr_dst
? dst
[0].Predicate
: FALSE
;
975 negate
= dst
[0].PredNegate
;
976 swizzle
[0] = dst
[0].PredSwizzleX
;
977 swizzle
[1] = dst
[0].PredSwizzleY
;
978 swizzle
[2] = dst
[0].PredSwizzleZ
;
979 swizzle
[3] = dst
[0].PredSwizzleW
;
982 insn
= ureg_emit_insn(ureg
,
994 for (i
= 0; i
< nr_dst
; i
++)
995 ureg_emit_dst( ureg
, dst
[i
] );
997 for (i
= 0; i
< nr_src
; i
++)
998 ureg_emit_src( ureg
, src
[i
] );
1000 ureg_fixup_insn_size( ureg
, insn
.insn_token
);
1004 ureg_tex_insn(struct ureg_program
*ureg
,
1006 const struct ureg_dst
*dst
,
1009 const struct ureg_src
*src
,
1012 struct ureg_emit_insn_result insn
;
1016 boolean negate
= FALSE
;
1017 unsigned swizzle
[4] = { 0 };
1019 saturate
= nr_dst
? dst
[0].Saturate
: FALSE
;
1020 predicate
= nr_dst
? dst
[0].Predicate
: FALSE
;
1022 negate
= dst
[0].PredNegate
;
1023 swizzle
[0] = dst
[0].PredSwizzleX
;
1024 swizzle
[1] = dst
[0].PredSwizzleY
;
1025 swizzle
[2] = dst
[0].PredSwizzleZ
;
1026 swizzle
[3] = dst
[0].PredSwizzleW
;
1029 insn
= ureg_emit_insn(ureg
,
1041 ureg_emit_texture( ureg
, insn
.extended_token
, target
);
1043 for (i
= 0; i
< nr_dst
; i
++)
1044 ureg_emit_dst( ureg
, dst
[i
] );
1046 for (i
= 0; i
< nr_src
; i
++)
1047 ureg_emit_src( ureg
, src
[i
] );
1049 ureg_fixup_insn_size( ureg
, insn
.insn_token
);
1054 ureg_label_insn(struct ureg_program
*ureg
,
1056 const struct ureg_src
*src
,
1058 unsigned *label_token
)
1060 struct ureg_emit_insn_result insn
;
1063 insn
= ureg_emit_insn(ureg
,
1075 ureg_emit_label( ureg
, insn
.extended_token
, label_token
);
1077 for (i
= 0; i
< nr_src
; i
++)
1078 ureg_emit_src( ureg
, src
[i
] );
1080 ureg_fixup_insn_size( ureg
, insn
.insn_token
);
1085 static void emit_decl( struct ureg_program
*ureg
,
1088 unsigned semantic_name
,
1089 unsigned semantic_index
,
1092 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 3 );
1095 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1096 out
[0].decl
.NrTokens
= 3;
1097 out
[0].decl
.File
= file
;
1098 out
[0].decl
.UsageMask
= TGSI_WRITEMASK_XYZW
; /* FIXME! */
1099 out
[0].decl
.Interpolate
= interp
;
1100 out
[0].decl
.Semantic
= 1;
1103 out
[1].decl_range
.First
=
1104 out
[1].decl_range
.Last
= index
;
1107 out
[2].decl_semantic
.Name
= semantic_name
;
1108 out
[2].decl_semantic
.Index
= semantic_index
;
1113 static void emit_decl_range( struct ureg_program
*ureg
,
1118 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 2 );
1121 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1122 out
[0].decl
.NrTokens
= 2;
1123 out
[0].decl
.File
= file
;
1124 out
[0].decl
.UsageMask
= 0xf;
1125 out
[0].decl
.Interpolate
= TGSI_INTERPOLATE_CONSTANT
;
1126 out
[0].decl
.Semantic
= 0;
1129 out
[1].decl_range
.First
= first
;
1130 out
[1].decl_range
.Last
= first
+ count
- 1;
1134 emit_decl_range2D(struct ureg_program
*ureg
,
1140 union tgsi_any_token
*out
= get_tokens(ureg
, DOMAIN_DECL
, 3);
1143 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1144 out
[0].decl
.NrTokens
= 3;
1145 out
[0].decl
.File
= file
;
1146 out
[0].decl
.UsageMask
= 0xf;
1147 out
[0].decl
.Interpolate
= TGSI_INTERPOLATE_CONSTANT
;
1148 out
[0].decl
.Dimension
= 1;
1151 out
[1].decl_range
.First
= first
;
1152 out
[1].decl_range
.Last
= last
;
1155 out
[2].decl_dim
.Index2D
= index2D
;
1159 emit_immediate( struct ureg_program
*ureg
,
1163 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 5 );
1166 out
[0].imm
.Type
= TGSI_TOKEN_TYPE_IMMEDIATE
;
1167 out
[0].imm
.NrTokens
= 5;
1168 out
[0].imm
.DataType
= type
;
1169 out
[0].imm
.Padding
= 0;
1171 out
[1].imm_data
.Uint
= v
[0];
1172 out
[2].imm_data
.Uint
= v
[1];
1173 out
[3].imm_data
.Uint
= v
[2];
1174 out
[4].imm_data
.Uint
= v
[3];
1178 emit_property(struct ureg_program
*ureg
,
1182 union tgsi_any_token
*out
= get_tokens(ureg
, DOMAIN_DECL
, 2);
1185 out
[0].prop
.Type
= TGSI_TOKEN_TYPE_PROPERTY
;
1186 out
[0].prop
.NrTokens
= 2;
1187 out
[0].prop
.PropertyName
= name
;
1189 out
[1].prop_data
.Data
= data
;
1193 static void emit_decls( struct ureg_program
*ureg
)
1197 if (ureg
->property_gs_input_prim
!= ~0) {
1198 assert(ureg
->processor
== TGSI_PROCESSOR_GEOMETRY
);
1201 TGSI_PROPERTY_GS_INPUT_PRIM
,
1202 ureg
->property_gs_input_prim
);
1205 if (ureg
->property_gs_output_prim
!= ~0) {
1206 assert(ureg
->processor
== TGSI_PROCESSOR_GEOMETRY
);
1209 TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1210 ureg
->property_gs_output_prim
);
1213 if (ureg
->property_gs_max_vertices
!= ~0) {
1214 assert(ureg
->processor
== TGSI_PROCESSOR_GEOMETRY
);
1217 TGSI_PROPERTY_GS_MAX_VERTICES
,
1218 ureg
->property_gs_max_vertices
);
1221 if (ureg
->property_fs_coord_origin
) {
1222 assert(ureg
->processor
== TGSI_PROCESSOR_FRAGMENT
);
1225 TGSI_PROPERTY_FS_COORD_ORIGIN
,
1226 ureg
->property_fs_coord_origin
);
1229 if (ureg
->property_fs_coord_pixel_center
) {
1230 assert(ureg
->processor
== TGSI_PROCESSOR_FRAGMENT
);
1233 TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
,
1234 ureg
->property_fs_coord_pixel_center
);
1237 if (ureg
->processor
== TGSI_PROCESSOR_VERTEX
) {
1238 for (i
= 0; i
< UREG_MAX_INPUT
; i
++) {
1239 if (ureg
->vs_inputs
[i
/32] & (1 << (i
%32))) {
1240 emit_decl_range( ureg
, TGSI_FILE_INPUT
, i
, 1 );
1243 } else if (ureg
->processor
== TGSI_PROCESSOR_FRAGMENT
) {
1244 for (i
= 0; i
< ureg
->nr_fs_inputs
; i
++) {
1248 ureg
->fs_input
[i
].semantic_name
,
1249 ureg
->fs_input
[i
].semantic_index
,
1250 ureg
->fs_input
[i
].interp
);
1253 for (i
= 0; i
< ureg
->nr_gs_inputs
; i
++) {
1254 emit_decl_range(ureg
,
1256 ureg
->gs_input
[i
].index
,
1261 for (i
= 0; i
< ureg
->nr_system_values
; i
++) {
1263 TGSI_FILE_SYSTEM_VALUE
,
1264 ureg
->system_value
[i
].index
,
1265 ureg
->system_value
[i
].semantic_name
,
1266 ureg
->system_value
[i
].semantic_index
,
1267 TGSI_INTERPOLATE_CONSTANT
);
1270 for (i
= 0; i
< ureg
->nr_outputs
; i
++) {
1274 ureg
->output
[i
].semantic_name
,
1275 ureg
->output
[i
].semantic_index
,
1276 TGSI_INTERPOLATE_CONSTANT
);
1279 for (i
= 0; i
< ureg
->nr_samplers
; i
++) {
1280 emit_decl_range( ureg
,
1282 ureg
->sampler
[i
].Index
, 1 );
1285 if (ureg
->const_decls
.nr_constant_ranges
) {
1286 for (i
= 0; i
< ureg
->const_decls
.nr_constant_ranges
; i
++) {
1287 emit_decl_range(ureg
,
1289 ureg
->const_decls
.constant_range
[i
].first
,
1290 ureg
->const_decls
.constant_range
[i
].last
- ureg
->const_decls
.constant_range
[i
].first
+ 1);
1294 for (i
= 0; i
< PIPE_MAX_CONSTANT_BUFFERS
; i
++) {
1295 struct const_decl
*decl
= &ureg
->const_decls2D
[i
];
1297 if (decl
->nr_constant_ranges
) {
1300 for (j
= 0; j
< decl
->nr_constant_ranges
; j
++) {
1301 emit_decl_range2D(ureg
,
1303 decl
->constant_range
[j
].first
,
1304 decl
->constant_range
[j
].last
,
1310 if (ureg
->nr_temps
) {
1311 emit_decl_range( ureg
,
1312 TGSI_FILE_TEMPORARY
,
1313 0, ureg
->nr_temps
);
1316 if (ureg
->nr_addrs
) {
1317 emit_decl_range( ureg
,
1319 0, ureg
->nr_addrs
);
1322 if (ureg
->nr_loops
) {
1323 emit_decl_range(ureg
,
1329 if (ureg
->nr_preds
) {
1330 emit_decl_range(ureg
,
1331 TGSI_FILE_PREDICATE
,
1336 for (i
= 0; i
< ureg
->nr_immediates
; i
++) {
1337 emit_immediate( ureg
,
1338 ureg
->immediate
[i
].value
.u
,
1339 ureg
->immediate
[i
].type
);
1343 /* Append the instruction tokens onto the declarations to build a
1344 * contiguous stream suitable to send to the driver.
1346 static void copy_instructions( struct ureg_program
*ureg
)
1348 unsigned nr_tokens
= ureg
->domain
[DOMAIN_INSN
].count
;
1349 union tgsi_any_token
*out
= get_tokens( ureg
,
1354 ureg
->domain
[DOMAIN_INSN
].tokens
,
1355 nr_tokens
* sizeof out
[0] );
1360 fixup_header_size(struct ureg_program
*ureg
)
1362 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_DECL
, 0 );
1364 out
->header
.BodySize
= ureg
->domain
[DOMAIN_DECL
].count
- 2;
1369 emit_header( struct ureg_program
*ureg
)
1371 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 2 );
1373 out
[0].header
.HeaderSize
= 2;
1374 out
[0].header
.BodySize
= 0;
1376 out
[1].processor
.Processor
= ureg
->processor
;
1377 out
[1].processor
.Padding
= 0;
1381 const struct tgsi_token
*ureg_finalize( struct ureg_program
*ureg
)
1383 const struct tgsi_token
*tokens
;
1385 emit_header( ureg
);
1387 copy_instructions( ureg
);
1388 fixup_header_size( ureg
);
1390 if (ureg
->domain
[0].tokens
== error_tokens
||
1391 ureg
->domain
[1].tokens
== error_tokens
) {
1392 debug_printf("%s: error in generated shader\n", __FUNCTION__
);
1397 tokens
= &ureg
->domain
[DOMAIN_DECL
].tokens
[0].token
;
1400 debug_printf("%s: emitted shader %d tokens:\n", __FUNCTION__
,
1401 ureg
->domain
[DOMAIN_DECL
].count
);
1402 tgsi_dump( tokens
, 0 );
1406 if (tokens
&& !tgsi_sanity_check(tokens
)) {
1407 debug_printf("tgsi_ureg.c, sanity check failed on generated tokens:\n");
1408 tgsi_dump(tokens
, 0);
1418 void *ureg_create_shader( struct ureg_program
*ureg
,
1419 struct pipe_context
*pipe
)
1421 struct pipe_shader_state state
;
1423 state
.tokens
= ureg_finalize(ureg
);
1427 if (ureg
->processor
== TGSI_PROCESSOR_VERTEX
)
1428 return pipe
->create_vs_state( pipe
, &state
);
1430 return pipe
->create_fs_state( pipe
, &state
);
1434 const struct tgsi_token
*ureg_get_tokens( struct ureg_program
*ureg
,
1435 unsigned *nr_tokens
)
1437 const struct tgsi_token
*tokens
;
1439 ureg_finalize(ureg
);
1441 tokens
= &ureg
->domain
[DOMAIN_DECL
].tokens
[0].token
;
1444 *nr_tokens
= ureg
->domain
[DOMAIN_DECL
].size
;
1446 ureg
->domain
[DOMAIN_DECL
].tokens
= 0;
1447 ureg
->domain
[DOMAIN_DECL
].size
= 0;
1448 ureg
->domain
[DOMAIN_DECL
].order
= 0;
1449 ureg
->domain
[DOMAIN_DECL
].count
= 0;
1455 struct ureg_program
*ureg_create( unsigned processor
)
1457 struct ureg_program
*ureg
= CALLOC_STRUCT( ureg_program
);
1461 ureg
->processor
= processor
;
1462 ureg
->property_gs_input_prim
= ~0;
1463 ureg
->property_gs_output_prim
= ~0;
1464 ureg
->property_gs_max_vertices
= ~0;
1469 void ureg_destroy( struct ureg_program
*ureg
)
1473 for (i
= 0; i
< Elements(ureg
->domain
); i
++) {
1474 if (ureg
->domain
[i
].tokens
&&
1475 ureg
->domain
[i
].tokens
!= error_tokens
)
1476 FREE(ureg
->domain
[i
].tokens
);