1 /**************************************************************************
3 * Copyright 2009-2010 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_declaration_resource decl_resource
;
51 struct tgsi_immediate imm
;
52 union tgsi_immediate_data imm_data
;
53 struct tgsi_instruction insn
;
54 struct tgsi_instruction_predicate insn_predicate
;
55 struct tgsi_instruction_label insn_label
;
56 struct tgsi_instruction_texture insn_texture
;
57 struct tgsi_src_register src
;
58 struct tgsi_dimension dim
;
59 struct tgsi_dst_register dst
;
65 union tgsi_any_token
*tokens
;
71 #define UREG_MAX_INPUT PIPE_MAX_ATTRIBS
72 #define UREG_MAX_SYSTEM_VALUE PIPE_MAX_ATTRIBS
73 #define UREG_MAX_OUTPUT PIPE_MAX_ATTRIBS
74 #define UREG_MAX_CONSTANT_RANGE 32
75 #define UREG_MAX_IMMEDIATE 256
76 #define UREG_MAX_TEMP 256
77 #define UREG_MAX_ADDR 2
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 unsigned char cylindrical_wrap
;
101 unsigned char centroid
;
102 } fs_input
[UREG_MAX_INPUT
];
103 unsigned nr_fs_inputs
;
105 unsigned vs_inputs
[UREG_MAX_INPUT
/32];
109 unsigned semantic_name
;
110 unsigned semantic_index
;
111 } gs_input
[UREG_MAX_INPUT
];
112 unsigned nr_gs_inputs
;
116 unsigned semantic_name
;
117 unsigned semantic_index
;
118 } system_value
[UREG_MAX_SYSTEM_VALUE
];
119 unsigned nr_system_values
;
122 unsigned semantic_name
;
123 unsigned semantic_index
;
124 } output
[UREG_MAX_OUTPUT
];
135 } immediate
[UREG_MAX_IMMEDIATE
];
136 unsigned nr_immediates
;
138 struct ureg_src sampler
[PIPE_MAX_SAMPLERS
];
139 unsigned nr_samplers
;
144 unsigned return_type_x
;
145 unsigned return_type_y
;
146 unsigned return_type_z
;
147 unsigned return_type_w
;
148 } resource
[PIPE_MAX_SHADER_RESOURCES
];
149 unsigned nr_resources
;
151 unsigned temps_active
[UREG_MAX_TEMP
/ 32];
154 struct const_decl const_decls
;
155 struct const_decl const_decls2D
[PIPE_MAX_CONSTANT_BUFFERS
];
157 unsigned property_gs_input_prim
;
158 unsigned property_gs_output_prim
;
159 unsigned property_gs_max_vertices
;
160 unsigned char property_fs_coord_origin
; /* = TGSI_FS_COORD_ORIGIN_* */
161 unsigned char property_fs_coord_pixel_center
; /* = TGSI_FS_COORD_PIXEL_CENTER_* */
162 unsigned char property_fs_color0_writes_all_cbufs
; /* = TGSI_FS_COLOR0_WRITES_ALL_CBUFS * */
166 unsigned nr_instructions
;
168 struct ureg_tokens domain
[2];
171 static union tgsi_any_token error_tokens
[32];
173 static void tokens_error( struct ureg_tokens
*tokens
)
175 if (tokens
->tokens
&& tokens
->tokens
!= error_tokens
)
176 FREE(tokens
->tokens
);
178 tokens
->tokens
= error_tokens
;
179 tokens
->size
= Elements(error_tokens
);
184 static void tokens_expand( struct ureg_tokens
*tokens
,
187 unsigned old_size
= tokens
->size
* sizeof(unsigned);
189 if (tokens
->tokens
== error_tokens
) {
193 while (tokens
->count
+ count
> tokens
->size
) {
194 tokens
->size
= (1 << ++tokens
->order
);
197 tokens
->tokens
= REALLOC(tokens
->tokens
,
199 tokens
->size
* sizeof(unsigned));
200 if (tokens
->tokens
== NULL
) {
201 tokens_error(tokens
);
205 static void set_bad( struct ureg_program
*ureg
)
207 tokens_error(&ureg
->domain
[0]);
212 static union tgsi_any_token
*get_tokens( struct ureg_program
*ureg
,
216 struct ureg_tokens
*tokens
= &ureg
->domain
[domain
];
217 union tgsi_any_token
*result
;
219 if (tokens
->count
+ count
> tokens
->size
)
220 tokens_expand(tokens
, count
);
222 result
= &tokens
->tokens
[tokens
->count
];
223 tokens
->count
+= count
;
228 static union tgsi_any_token
*retrieve_token( struct ureg_program
*ureg
,
232 if (ureg
->domain
[domain
].tokens
== error_tokens
)
233 return &error_tokens
[0];
235 return &ureg
->domain
[domain
].tokens
[nr
];
240 static INLINE
struct ureg_dst
241 ureg_dst_register( unsigned file
,
247 dst
.WriteMask
= TGSI_WRITEMASK_XYZW
;
249 dst
.IndirectIndex
= 0;
250 dst
.IndirectSwizzle
= 0;
254 dst
.PredSwizzleX
= TGSI_SWIZZLE_X
;
255 dst
.PredSwizzleY
= TGSI_SWIZZLE_Y
;
256 dst
.PredSwizzleZ
= TGSI_SWIZZLE_Z
;
257 dst
.PredSwizzleW
= TGSI_SWIZZLE_W
;
265 ureg_property_gs_input_prim(struct ureg_program
*ureg
,
268 ureg
->property_gs_input_prim
= input_prim
;
272 ureg_property_gs_output_prim(struct ureg_program
*ureg
,
273 unsigned output_prim
)
275 ureg
->property_gs_output_prim
= output_prim
;
279 ureg_property_gs_max_vertices(struct ureg_program
*ureg
,
280 unsigned max_vertices
)
282 ureg
->property_gs_max_vertices
= max_vertices
;
286 ureg_property_fs_coord_origin(struct ureg_program
*ureg
,
287 unsigned fs_coord_origin
)
289 ureg
->property_fs_coord_origin
= fs_coord_origin
;
293 ureg_property_fs_coord_pixel_center(struct ureg_program
*ureg
,
294 unsigned fs_coord_pixel_center
)
296 ureg
->property_fs_coord_pixel_center
= fs_coord_pixel_center
;
300 ureg_property_fs_color0_writes_all_cbufs(struct ureg_program
*ureg
,
301 unsigned fs_color0_writes_all_cbufs
)
303 ureg
->property_fs_color0_writes_all_cbufs
= fs_color0_writes_all_cbufs
;
307 ureg_DECL_fs_input_cyl_centroid(struct ureg_program
*ureg
,
308 unsigned semantic_name
,
309 unsigned semantic_index
,
310 unsigned interp_mode
,
311 unsigned cylindrical_wrap
,
316 for (i
= 0; i
< ureg
->nr_fs_inputs
; i
++) {
317 if (ureg
->fs_input
[i
].semantic_name
== semantic_name
&&
318 ureg
->fs_input
[i
].semantic_index
== semantic_index
) {
323 if (ureg
->nr_fs_inputs
< UREG_MAX_INPUT
) {
324 ureg
->fs_input
[i
].semantic_name
= semantic_name
;
325 ureg
->fs_input
[i
].semantic_index
= semantic_index
;
326 ureg
->fs_input
[i
].interp
= interp_mode
;
327 ureg
->fs_input
[i
].cylindrical_wrap
= cylindrical_wrap
;
328 ureg
->fs_input
[i
].centroid
= centroid
;
329 ureg
->nr_fs_inputs
++;
335 return ureg_src_register(TGSI_FILE_INPUT
, i
);
340 ureg_DECL_vs_input( struct ureg_program
*ureg
,
343 assert(ureg
->processor
== TGSI_PROCESSOR_VERTEX
);
345 ureg
->vs_inputs
[index
/32] |= 1 << (index
% 32);
346 return ureg_src_register( TGSI_FILE_INPUT
, index
);
351 ureg_DECL_gs_input(struct ureg_program
*ureg
,
353 unsigned semantic_name
,
354 unsigned semantic_index
)
356 if (ureg
->nr_gs_inputs
< UREG_MAX_INPUT
) {
357 ureg
->gs_input
[ureg
->nr_gs_inputs
].index
= index
;
358 ureg
->gs_input
[ureg
->nr_gs_inputs
].semantic_name
= semantic_name
;
359 ureg
->gs_input
[ureg
->nr_gs_inputs
].semantic_index
= semantic_index
;
360 ureg
->nr_gs_inputs
++;
365 /* XXX: Add suport for true 2D input registers. */
366 return ureg_src_register(TGSI_FILE_INPUT
, index
);
371 ureg_DECL_system_value(struct ureg_program
*ureg
,
373 unsigned semantic_name
,
374 unsigned semantic_index
)
376 if (ureg
->nr_system_values
< UREG_MAX_SYSTEM_VALUE
) {
377 ureg
->system_value
[ureg
->nr_system_values
].index
= index
;
378 ureg
->system_value
[ureg
->nr_system_values
].semantic_name
= semantic_name
;
379 ureg
->system_value
[ureg
->nr_system_values
].semantic_index
= semantic_index
;
380 ureg
->nr_system_values
++;
385 return ureg_src_register(TGSI_FILE_SYSTEM_VALUE
, index
);
390 ureg_DECL_output( struct ureg_program
*ureg
,
396 for (i
= 0; i
< ureg
->nr_outputs
; i
++) {
397 if (ureg
->output
[i
].semantic_name
== name
&&
398 ureg
->output
[i
].semantic_index
== index
)
402 if (ureg
->nr_outputs
< UREG_MAX_OUTPUT
) {
403 ureg
->output
[i
].semantic_name
= name
;
404 ureg
->output
[i
].semantic_index
= index
;
412 return ureg_dst_register( TGSI_FILE_OUTPUT
, i
);
416 /* Returns a new constant register. Keep track of which have been
417 * referred to so that we can emit decls later.
419 * Constant operands declared with this function must be addressed
420 * with a two-dimensional index.
422 * There is nothing in this code to bind this constant to any tracked
423 * value or manage any constant_buffer contents -- that's the
424 * resposibility of the calling code.
427 ureg_DECL_constant2D(struct ureg_program
*ureg
,
432 struct const_decl
*decl
= &ureg
->const_decls2D
[index2D
];
434 assert(index2D
< PIPE_MAX_CONSTANT_BUFFERS
);
436 if (decl
->nr_constant_ranges
< UREG_MAX_CONSTANT_RANGE
) {
437 uint i
= decl
->nr_constant_ranges
++;
439 decl
->constant_range
[i
].first
= first
;
440 decl
->constant_range
[i
].last
= last
;
445 /* A one-dimensional, depricated version of ureg_DECL_constant2D().
447 * Constant operands declared with this function must be addressed
448 * with a one-dimensional index.
451 ureg_DECL_constant(struct ureg_program
*ureg
,
454 struct const_decl
*decl
= &ureg
->const_decls
;
455 unsigned minconst
= index
, maxconst
= index
;
458 /* Inside existing range?
460 for (i
= 0; i
< decl
->nr_constant_ranges
; i
++) {
461 if (decl
->constant_range
[i
].first
<= index
&&
462 decl
->constant_range
[i
].last
>= index
) {
467 /* Extend existing range?
469 for (i
= 0; i
< decl
->nr_constant_ranges
; i
++) {
470 if (decl
->constant_range
[i
].last
== index
- 1) {
471 decl
->constant_range
[i
].last
= index
;
475 if (decl
->constant_range
[i
].first
== index
+ 1) {
476 decl
->constant_range
[i
].first
= index
;
480 minconst
= MIN2(minconst
, decl
->constant_range
[i
].first
);
481 maxconst
= MAX2(maxconst
, decl
->constant_range
[i
].last
);
486 if (decl
->nr_constant_ranges
< UREG_MAX_CONSTANT_RANGE
) {
487 i
= decl
->nr_constant_ranges
++;
488 decl
->constant_range
[i
].first
= index
;
489 decl
->constant_range
[i
].last
= index
;
493 /* Collapse all ranges down to one:
496 decl
->constant_range
[0].first
= minconst
;
497 decl
->constant_range
[0].last
= maxconst
;
498 decl
->nr_constant_ranges
= 1;
501 assert(i
< decl
->nr_constant_ranges
);
502 assert(decl
->constant_range
[i
].first
<= index
);
503 assert(decl
->constant_range
[i
].last
>= index
);
504 return ureg_src_register(TGSI_FILE_CONSTANT
, index
);
508 /* Allocate a new temporary. Temporaries greater than UREG_MAX_TEMP
509 * are legal, but will not be released.
511 struct ureg_dst
ureg_DECL_temporary( struct ureg_program
*ureg
)
515 for (i
= 0; i
< UREG_MAX_TEMP
; i
+= 32) {
516 int bit
= ffs(~ureg
->temps_active
[i
/32]);
523 /* No reusable temps, so allocate a new one:
525 i
= ureg
->nr_temps
++;
528 if (i
< UREG_MAX_TEMP
)
529 ureg
->temps_active
[i
/32] |= 1 << (i
% 32);
531 if (i
>= ureg
->nr_temps
)
532 ureg
->nr_temps
= i
+ 1;
534 return ureg_dst_register( TGSI_FILE_TEMPORARY
, i
);
538 void ureg_release_temporary( struct ureg_program
*ureg
,
539 struct ureg_dst tmp
)
541 if(tmp
.File
== TGSI_FILE_TEMPORARY
)
542 if (tmp
.Index
< UREG_MAX_TEMP
)
543 ureg
->temps_active
[tmp
.Index
/32] &= ~(1 << (tmp
.Index
% 32));
547 /* Allocate a new address register.
549 struct ureg_dst
ureg_DECL_address( struct ureg_program
*ureg
)
551 if (ureg
->nr_addrs
< UREG_MAX_ADDR
)
552 return ureg_dst_register( TGSI_FILE_ADDRESS
, ureg
->nr_addrs
++ );
555 return ureg_dst_register( TGSI_FILE_ADDRESS
, 0 );
558 /* Allocate a new predicate register.
561 ureg_DECL_predicate(struct ureg_program
*ureg
)
563 if (ureg
->nr_preds
< UREG_MAX_PRED
) {
564 return ureg_dst_register(TGSI_FILE_PREDICATE
, ureg
->nr_preds
++);
568 return ureg_dst_register(TGSI_FILE_PREDICATE
, 0);
571 /* Allocate a new sampler.
573 struct ureg_src
ureg_DECL_sampler( struct ureg_program
*ureg
,
578 for (i
= 0; i
< ureg
->nr_samplers
; i
++)
579 if (ureg
->sampler
[i
].Index
== nr
)
580 return ureg
->sampler
[i
];
582 if (i
< PIPE_MAX_SAMPLERS
) {
583 ureg
->sampler
[i
] = ureg_src_register( TGSI_FILE_SAMPLER
, nr
);
585 return ureg
->sampler
[i
];
589 return ureg
->sampler
[0];
593 * Allocate a new shader resource.
596 ureg_DECL_resource(struct ureg_program
*ureg
,
599 unsigned return_type_x
,
600 unsigned return_type_y
,
601 unsigned return_type_z
,
602 unsigned return_type_w
)
604 struct ureg_src reg
= ureg_src_register(TGSI_FILE_RESOURCE
, index
);
607 for (i
= 0; i
< ureg
->nr_resources
; i
++) {
608 if (ureg
->resource
[i
].index
== index
) {
613 if (i
< PIPE_MAX_SHADER_RESOURCES
) {
614 ureg
->resource
[i
].index
= index
;
615 ureg
->resource
[i
].target
= target
;
616 ureg
->resource
[i
].return_type_x
= return_type_x
;
617 ureg
->resource
[i
].return_type_y
= return_type_y
;
618 ureg
->resource
[i
].return_type_z
= return_type_z
;
619 ureg
->resource
[i
].return_type_w
= return_type_w
;
620 ureg
->nr_resources
++;
629 match_or_expand_immediate( const unsigned *v
,
635 unsigned nr2
= *pnr2
;
640 for (i
= 0; i
< nr
; i
++) {
641 boolean found
= FALSE
;
643 for (j
= 0; j
< nr2
&& !found
; j
++) {
645 *swizzle
|= j
<< (i
* 2);
656 *swizzle
|= nr2
<< (i
* 2);
661 /* Actually expand immediate only when fully succeeded.
668 static struct ureg_src
669 decl_immediate( struct ureg_program
*ureg
,
675 unsigned swizzle
= 0;
677 /* Could do a first pass where we examine all existing immediates
681 for (i
= 0; i
< ureg
->nr_immediates
; i
++) {
682 if (ureg
->immediate
[i
].type
!= type
) {
685 if (match_or_expand_immediate(v
,
687 ureg
->immediate
[i
].value
.u
,
688 &ureg
->immediate
[i
].nr
,
694 if (ureg
->nr_immediates
< UREG_MAX_IMMEDIATE
) {
695 i
= ureg
->nr_immediates
++;
696 ureg
->immediate
[i
].type
= type
;
697 if (match_or_expand_immediate(v
,
699 ureg
->immediate
[i
].value
.u
,
700 &ureg
->immediate
[i
].nr
,
709 /* Make sure that all referenced elements are from this immediate.
710 * Has the effect of making size-one immediates into scalars.
712 for (j
= nr
; j
< 4; j
++) {
713 swizzle
|= (swizzle
& 0x3) << (j
* 2);
716 return ureg_swizzle(ureg_src_register(TGSI_FILE_IMMEDIATE
, i
),
717 (swizzle
>> 0) & 0x3,
718 (swizzle
>> 2) & 0x3,
719 (swizzle
>> 4) & 0x3,
720 (swizzle
>> 6) & 0x3);
725 ureg_DECL_immediate( struct ureg_program
*ureg
,
735 for (i
= 0; i
< nr
; i
++) {
739 return decl_immediate(ureg
, fu
.u
, nr
, TGSI_IMM_FLOAT32
);
744 ureg_DECL_immediate_uint( struct ureg_program
*ureg
,
748 return decl_immediate(ureg
, v
, nr
, TGSI_IMM_UINT32
);
753 ureg_DECL_immediate_block_uint( struct ureg_program
*ureg
,
760 if (ureg
->nr_immediates
+ (nr
+ 3) / 4 > UREG_MAX_IMMEDIATE
) {
762 return ureg_src_register(TGSI_FILE_IMMEDIATE
, 0);
765 index
= ureg
->nr_immediates
;
766 ureg
->nr_immediates
+= (nr
+ 3) / 4;
768 for (i
= index
; i
< ureg
->nr_immediates
; i
++) {
769 ureg
->immediate
[i
].type
= TGSI_IMM_UINT32
;
770 ureg
->immediate
[i
].nr
= nr
> 4 ? 4 : nr
;
771 memcpy(ureg
->immediate
[i
].value
.u
,
773 ureg
->immediate
[i
].nr
* sizeof(uint
));
777 return ureg_src_register(TGSI_FILE_IMMEDIATE
, index
);
782 ureg_DECL_immediate_int( struct ureg_program
*ureg
,
786 return decl_immediate(ureg
, (const unsigned *)v
, nr
, TGSI_IMM_INT32
);
791 ureg_emit_src( struct ureg_program
*ureg
,
792 struct ureg_src src
)
794 unsigned size
= 1 + (src
.Indirect
? 1 : 0) +
795 (src
.Dimension
? (src
.DimIndirect
? 2 : 1) : 0);
797 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_INSN
, size
);
800 assert(src
.File
!= TGSI_FILE_NULL
);
801 assert(src
.File
!= TGSI_FILE_OUTPUT
);
802 assert(src
.File
< TGSI_FILE_COUNT
);
805 out
[n
].src
.File
= src
.File
;
806 out
[n
].src
.SwizzleX
= src
.SwizzleX
;
807 out
[n
].src
.SwizzleY
= src
.SwizzleY
;
808 out
[n
].src
.SwizzleZ
= src
.SwizzleZ
;
809 out
[n
].src
.SwizzleW
= src
.SwizzleW
;
810 out
[n
].src
.Index
= src
.Index
;
811 out
[n
].src
.Negate
= src
.Negate
;
812 out
[0].src
.Absolute
= src
.Absolute
;
816 out
[0].src
.Indirect
= 1;
818 out
[n
].src
.File
= src
.IndirectFile
;
819 out
[n
].src
.SwizzleX
= src
.IndirectSwizzle
;
820 out
[n
].src
.SwizzleY
= src
.IndirectSwizzle
;
821 out
[n
].src
.SwizzleZ
= src
.IndirectSwizzle
;
822 out
[n
].src
.SwizzleW
= src
.IndirectSwizzle
;
823 out
[n
].src
.Index
= src
.IndirectIndex
;
828 if (src
.DimIndirect
) {
829 out
[0].src
.Dimension
= 1;
830 out
[n
].dim
.Indirect
= 1;
831 out
[n
].dim
.Dimension
= 0;
832 out
[n
].dim
.Padding
= 0;
833 out
[n
].dim
.Index
= src
.DimensionIndex
;
836 out
[n
].src
.File
= src
.DimIndFile
;
837 out
[n
].src
.SwizzleX
= src
.DimIndSwizzle
;
838 out
[n
].src
.SwizzleY
= src
.DimIndSwizzle
;
839 out
[n
].src
.SwizzleZ
= src
.DimIndSwizzle
;
840 out
[n
].src
.SwizzleW
= src
.DimIndSwizzle
;
841 out
[n
].src
.Index
= src
.DimIndIndex
;
843 out
[0].src
.Dimension
= 1;
844 out
[n
].dim
.Indirect
= 0;
845 out
[n
].dim
.Dimension
= 0;
846 out
[n
].dim
.Padding
= 0;
847 out
[n
].dim
.Index
= src
.DimensionIndex
;
857 ureg_emit_dst( struct ureg_program
*ureg
,
858 struct ureg_dst dst
)
861 (dst
.Indirect
? 1 : 0));
863 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_INSN
, size
);
866 assert(dst
.File
!= TGSI_FILE_NULL
);
867 assert(dst
.File
!= TGSI_FILE_CONSTANT
);
868 assert(dst
.File
!= TGSI_FILE_INPUT
);
869 assert(dst
.File
!= TGSI_FILE_SAMPLER
);
870 assert(dst
.File
!= TGSI_FILE_RESOURCE
);
871 assert(dst
.File
!= TGSI_FILE_IMMEDIATE
);
872 assert(dst
.File
< TGSI_FILE_COUNT
);
875 out
[n
].dst
.File
= dst
.File
;
876 out
[n
].dst
.WriteMask
= dst
.WriteMask
;
877 out
[n
].dst
.Indirect
= dst
.Indirect
;
878 out
[n
].dst
.Index
= dst
.Index
;
883 out
[n
].src
.File
= TGSI_FILE_ADDRESS
;
884 out
[n
].src
.SwizzleX
= dst
.IndirectSwizzle
;
885 out
[n
].src
.SwizzleY
= dst
.IndirectSwizzle
;
886 out
[n
].src
.SwizzleZ
= dst
.IndirectSwizzle
;
887 out
[n
].src
.SwizzleW
= dst
.IndirectSwizzle
;
888 out
[n
].src
.Index
= dst
.IndirectIndex
;
896 static void validate( unsigned opcode
,
901 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( opcode
);
904 assert(nr_dst
== info
->num_dst
);
905 assert(nr_src
== info
->num_src
);
910 struct ureg_emit_insn_result
911 ureg_emit_insn(struct ureg_program
*ureg
,
916 unsigned pred_swizzle_x
,
917 unsigned pred_swizzle_y
,
918 unsigned pred_swizzle_z
,
919 unsigned pred_swizzle_w
,
923 union tgsi_any_token
*out
;
924 uint count
= predicate
? 2 : 1;
925 struct ureg_emit_insn_result result
;
927 validate( opcode
, num_dst
, num_src
);
929 out
= get_tokens( ureg
, DOMAIN_INSN
, count
);
930 out
[0].insn
= tgsi_default_instruction();
931 out
[0].insn
.Opcode
= opcode
;
932 out
[0].insn
.Saturate
= saturate
;
933 out
[0].insn
.NumDstRegs
= num_dst
;
934 out
[0].insn
.NumSrcRegs
= num_src
;
936 result
.insn_token
= ureg
->domain
[DOMAIN_INSN
].count
- count
;
937 result
.extended_token
= result
.insn_token
;
940 out
[0].insn
.Predicate
= 1;
941 out
[1].insn_predicate
= tgsi_default_instruction_predicate();
942 out
[1].insn_predicate
.Negate
= pred_negate
;
943 out
[1].insn_predicate
.SwizzleX
= pred_swizzle_x
;
944 out
[1].insn_predicate
.SwizzleY
= pred_swizzle_y
;
945 out
[1].insn_predicate
.SwizzleZ
= pred_swizzle_z
;
946 out
[1].insn_predicate
.SwizzleW
= pred_swizzle_w
;
949 ureg
->nr_instructions
++;
956 ureg_emit_label(struct ureg_program
*ureg
,
957 unsigned extended_token
,
958 unsigned *label_token
)
960 union tgsi_any_token
*out
, *insn
;
965 out
= get_tokens( ureg
, DOMAIN_INSN
, 1 );
968 insn
= retrieve_token( ureg
, DOMAIN_INSN
, extended_token
);
969 insn
->insn
.Label
= 1;
971 *label_token
= ureg
->domain
[DOMAIN_INSN
].count
- 1;
974 /* Will return a number which can be used in a label to point to the
975 * next instruction to be emitted.
978 ureg_get_instruction_number( struct ureg_program
*ureg
)
980 return ureg
->nr_instructions
;
983 /* Patch a given label (expressed as a token number) to point to a
984 * given instruction (expressed as an instruction number).
987 ureg_fixup_label(struct ureg_program
*ureg
,
988 unsigned label_token
,
989 unsigned instruction_number
)
991 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_INSN
, label_token
);
993 out
->insn_label
.Label
= instruction_number
;
998 ureg_emit_texture(struct ureg_program
*ureg
,
999 unsigned extended_token
,
1002 union tgsi_any_token
*out
, *insn
;
1004 out
= get_tokens( ureg
, DOMAIN_INSN
, 1 );
1005 insn
= retrieve_token( ureg
, DOMAIN_INSN
, extended_token
);
1007 insn
->insn
.Texture
= 1;
1010 out
[0].insn_texture
.Texture
= target
;
1015 ureg_fixup_insn_size(struct ureg_program
*ureg
,
1018 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_INSN
, insn
);
1020 assert(out
->insn
.Type
== TGSI_TOKEN_TYPE_INSTRUCTION
);
1021 out
->insn
.NrTokens
= ureg
->domain
[DOMAIN_INSN
].count
- insn
- 1;
1026 ureg_insn(struct ureg_program
*ureg
,
1028 const struct ureg_dst
*dst
,
1030 const struct ureg_src
*src
,
1033 struct ureg_emit_insn_result insn
;
1037 boolean negate
= FALSE
;
1038 unsigned swizzle
[4] = { 0 };
1040 saturate
= nr_dst
? dst
[0].Saturate
: FALSE
;
1041 predicate
= nr_dst
? dst
[0].Predicate
: FALSE
;
1043 negate
= dst
[0].PredNegate
;
1044 swizzle
[0] = dst
[0].PredSwizzleX
;
1045 swizzle
[1] = dst
[0].PredSwizzleY
;
1046 swizzle
[2] = dst
[0].PredSwizzleZ
;
1047 swizzle
[3] = dst
[0].PredSwizzleW
;
1050 insn
= ureg_emit_insn(ureg
,
1062 for (i
= 0; i
< nr_dst
; i
++)
1063 ureg_emit_dst( ureg
, dst
[i
] );
1065 for (i
= 0; i
< nr_src
; i
++)
1066 ureg_emit_src( ureg
, src
[i
] );
1068 ureg_fixup_insn_size( ureg
, insn
.insn_token
);
1072 ureg_tex_insn(struct ureg_program
*ureg
,
1074 const struct ureg_dst
*dst
,
1077 const struct ureg_src
*src
,
1080 struct ureg_emit_insn_result insn
;
1084 boolean negate
= FALSE
;
1085 unsigned swizzle
[4] = { 0 };
1087 saturate
= nr_dst
? dst
[0].Saturate
: FALSE
;
1088 predicate
= nr_dst
? dst
[0].Predicate
: FALSE
;
1090 negate
= dst
[0].PredNegate
;
1091 swizzle
[0] = dst
[0].PredSwizzleX
;
1092 swizzle
[1] = dst
[0].PredSwizzleY
;
1093 swizzle
[2] = dst
[0].PredSwizzleZ
;
1094 swizzle
[3] = dst
[0].PredSwizzleW
;
1097 insn
= ureg_emit_insn(ureg
,
1109 ureg_emit_texture( ureg
, insn
.extended_token
, target
);
1111 for (i
= 0; i
< nr_dst
; i
++)
1112 ureg_emit_dst( ureg
, dst
[i
] );
1114 for (i
= 0; i
< nr_src
; i
++)
1115 ureg_emit_src( ureg
, src
[i
] );
1117 ureg_fixup_insn_size( ureg
, insn
.insn_token
);
1122 ureg_label_insn(struct ureg_program
*ureg
,
1124 const struct ureg_src
*src
,
1126 unsigned *label_token
)
1128 struct ureg_emit_insn_result insn
;
1131 insn
= ureg_emit_insn(ureg
,
1143 ureg_emit_label( ureg
, insn
.extended_token
, label_token
);
1145 for (i
= 0; i
< nr_src
; i
++)
1146 ureg_emit_src( ureg
, src
[i
] );
1148 ureg_fixup_insn_size( ureg
, insn
.insn_token
);
1153 emit_decl_semantic(struct ureg_program
*ureg
,
1156 unsigned semantic_name
,
1157 unsigned semantic_index
)
1159 union tgsi_any_token
*out
= get_tokens(ureg
, DOMAIN_DECL
, 3);
1162 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1163 out
[0].decl
.NrTokens
= 3;
1164 out
[0].decl
.File
= file
;
1165 out
[0].decl
.UsageMask
= TGSI_WRITEMASK_XYZW
; /* FIXME! */
1166 out
[0].decl
.Semantic
= 1;
1169 out
[1].decl_range
.First
= index
;
1170 out
[1].decl_range
.Last
= index
;
1173 out
[2].decl_semantic
.Name
= semantic_name
;
1174 out
[2].decl_semantic
.Index
= semantic_index
;
1179 emit_decl_fs(struct ureg_program
*ureg
,
1182 unsigned semantic_name
,
1183 unsigned semantic_index
,
1184 unsigned interpolate
,
1185 unsigned cylindrical_wrap
,
1188 union tgsi_any_token
*out
= get_tokens(ureg
, DOMAIN_DECL
, 3);
1191 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1192 out
[0].decl
.NrTokens
= 3;
1193 out
[0].decl
.File
= file
;
1194 out
[0].decl
.UsageMask
= TGSI_WRITEMASK_XYZW
; /* FIXME! */
1195 out
[0].decl
.Interpolate
= interpolate
;
1196 out
[0].decl
.Semantic
= 1;
1197 out
[0].decl
.CylindricalWrap
= cylindrical_wrap
;
1198 out
[0].decl
.Centroid
= centroid
;
1201 out
[1].decl_range
.First
= index
;
1202 out
[1].decl_range
.Last
= index
;
1205 out
[2].decl_semantic
.Name
= semantic_name
;
1206 out
[2].decl_semantic
.Index
= semantic_index
;
1210 static void emit_decl_range( struct ureg_program
*ureg
,
1215 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 2 );
1218 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1219 out
[0].decl
.NrTokens
= 2;
1220 out
[0].decl
.File
= file
;
1221 out
[0].decl
.UsageMask
= TGSI_WRITEMASK_XYZW
;
1222 out
[0].decl
.Interpolate
= TGSI_INTERPOLATE_CONSTANT
;
1223 out
[0].decl
.Semantic
= 0;
1226 out
[1].decl_range
.First
= first
;
1227 out
[1].decl_range
.Last
= first
+ count
- 1;
1231 emit_decl_range2D(struct ureg_program
*ureg
,
1237 union tgsi_any_token
*out
= get_tokens(ureg
, DOMAIN_DECL
, 3);
1240 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1241 out
[0].decl
.NrTokens
= 3;
1242 out
[0].decl
.File
= file
;
1243 out
[0].decl
.UsageMask
= TGSI_WRITEMASK_XYZW
;
1244 out
[0].decl
.Interpolate
= TGSI_INTERPOLATE_CONSTANT
;
1245 out
[0].decl
.Dimension
= 1;
1248 out
[1].decl_range
.First
= first
;
1249 out
[1].decl_range
.Last
= last
;
1252 out
[2].decl_dim
.Index2D
= index2D
;
1256 emit_decl_resource(struct ureg_program
*ureg
,
1259 unsigned return_type_x
,
1260 unsigned return_type_y
,
1261 unsigned return_type_z
,
1262 unsigned return_type_w
)
1264 union tgsi_any_token
*out
= get_tokens(ureg
, DOMAIN_DECL
, 3);
1267 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1268 out
[0].decl
.NrTokens
= 3;
1269 out
[0].decl
.File
= TGSI_FILE_RESOURCE
;
1270 out
[0].decl
.UsageMask
= 0xf;
1271 out
[0].decl
.Interpolate
= TGSI_INTERPOLATE_CONSTANT
;
1274 out
[1].decl_range
.First
= index
;
1275 out
[1].decl_range
.Last
= index
;
1278 out
[2].decl_resource
.Resource
= target
;
1279 out
[2].decl_resource
.ReturnTypeX
= return_type_x
;
1280 out
[2].decl_resource
.ReturnTypeY
= return_type_y
;
1281 out
[2].decl_resource
.ReturnTypeZ
= return_type_z
;
1282 out
[2].decl_resource
.ReturnTypeW
= return_type_w
;
1286 emit_immediate( struct ureg_program
*ureg
,
1290 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 5 );
1293 out
[0].imm
.Type
= TGSI_TOKEN_TYPE_IMMEDIATE
;
1294 out
[0].imm
.NrTokens
= 5;
1295 out
[0].imm
.DataType
= type
;
1296 out
[0].imm
.Padding
= 0;
1298 out
[1].imm_data
.Uint
= v
[0];
1299 out
[2].imm_data
.Uint
= v
[1];
1300 out
[3].imm_data
.Uint
= v
[2];
1301 out
[4].imm_data
.Uint
= v
[3];
1305 emit_property(struct ureg_program
*ureg
,
1309 union tgsi_any_token
*out
= get_tokens(ureg
, DOMAIN_DECL
, 2);
1312 out
[0].prop
.Type
= TGSI_TOKEN_TYPE_PROPERTY
;
1313 out
[0].prop
.NrTokens
= 2;
1314 out
[0].prop
.PropertyName
= name
;
1316 out
[1].prop_data
.Data
= data
;
1320 static void emit_decls( struct ureg_program
*ureg
)
1324 if (ureg
->property_gs_input_prim
!= ~0) {
1325 assert(ureg
->processor
== TGSI_PROCESSOR_GEOMETRY
);
1328 TGSI_PROPERTY_GS_INPUT_PRIM
,
1329 ureg
->property_gs_input_prim
);
1332 if (ureg
->property_gs_output_prim
!= ~0) {
1333 assert(ureg
->processor
== TGSI_PROCESSOR_GEOMETRY
);
1336 TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1337 ureg
->property_gs_output_prim
);
1340 if (ureg
->property_gs_max_vertices
!= ~0) {
1341 assert(ureg
->processor
== TGSI_PROCESSOR_GEOMETRY
);
1344 TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1345 ureg
->property_gs_max_vertices
);
1348 if (ureg
->property_fs_coord_origin
) {
1349 assert(ureg
->processor
== TGSI_PROCESSOR_FRAGMENT
);
1352 TGSI_PROPERTY_FS_COORD_ORIGIN
,
1353 ureg
->property_fs_coord_origin
);
1356 if (ureg
->property_fs_coord_pixel_center
) {
1357 assert(ureg
->processor
== TGSI_PROCESSOR_FRAGMENT
);
1360 TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
,
1361 ureg
->property_fs_coord_pixel_center
);
1364 if (ureg
->property_fs_color0_writes_all_cbufs
) {
1365 assert(ureg
->processor
== TGSI_PROCESSOR_FRAGMENT
);
1368 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
,
1369 ureg
->property_fs_color0_writes_all_cbufs
);
1372 if (ureg
->processor
== TGSI_PROCESSOR_VERTEX
) {
1373 for (i
= 0; i
< UREG_MAX_INPUT
; i
++) {
1374 if (ureg
->vs_inputs
[i
/32] & (1 << (i
%32))) {
1375 emit_decl_range( ureg
, TGSI_FILE_INPUT
, i
, 1 );
1378 } else if (ureg
->processor
== TGSI_PROCESSOR_FRAGMENT
) {
1379 for (i
= 0; i
< ureg
->nr_fs_inputs
; i
++) {
1383 ureg
->fs_input
[i
].semantic_name
,
1384 ureg
->fs_input
[i
].semantic_index
,
1385 ureg
->fs_input
[i
].interp
,
1386 ureg
->fs_input
[i
].cylindrical_wrap
,
1387 ureg
->fs_input
[i
].centroid
);
1390 for (i
= 0; i
< ureg
->nr_gs_inputs
; i
++) {
1391 emit_decl_semantic(ureg
,
1393 ureg
->gs_input
[i
].index
,
1394 ureg
->gs_input
[i
].semantic_name
,
1395 ureg
->gs_input
[i
].semantic_index
);
1399 for (i
= 0; i
< ureg
->nr_system_values
; i
++) {
1400 emit_decl_semantic(ureg
,
1401 TGSI_FILE_SYSTEM_VALUE
,
1402 ureg
->system_value
[i
].index
,
1403 ureg
->system_value
[i
].semantic_name
,
1404 ureg
->system_value
[i
].semantic_index
);
1407 for (i
= 0; i
< ureg
->nr_outputs
; i
++) {
1408 emit_decl_semantic(ureg
,
1411 ureg
->output
[i
].semantic_name
,
1412 ureg
->output
[i
].semantic_index
);
1415 for (i
= 0; i
< ureg
->nr_samplers
; i
++) {
1416 emit_decl_range( ureg
,
1418 ureg
->sampler
[i
].Index
, 1 );
1421 for (i
= 0; i
< ureg
->nr_resources
; i
++) {
1422 emit_decl_resource(ureg
,
1423 ureg
->resource
[i
].index
,
1424 ureg
->resource
[i
].target
,
1425 ureg
->resource
[i
].return_type_x
,
1426 ureg
->resource
[i
].return_type_y
,
1427 ureg
->resource
[i
].return_type_z
,
1428 ureg
->resource
[i
].return_type_w
);
1431 if (ureg
->const_decls
.nr_constant_ranges
) {
1432 for (i
= 0; i
< ureg
->const_decls
.nr_constant_ranges
; i
++) {
1433 emit_decl_range(ureg
,
1435 ureg
->const_decls
.constant_range
[i
].first
,
1436 ureg
->const_decls
.constant_range
[i
].last
- ureg
->const_decls
.constant_range
[i
].first
+ 1);
1440 for (i
= 0; i
< PIPE_MAX_CONSTANT_BUFFERS
; i
++) {
1441 struct const_decl
*decl
= &ureg
->const_decls2D
[i
];
1443 if (decl
->nr_constant_ranges
) {
1446 for (j
= 0; j
< decl
->nr_constant_ranges
; j
++) {
1447 emit_decl_range2D(ureg
,
1449 decl
->constant_range
[j
].first
,
1450 decl
->constant_range
[j
].last
,
1456 if (ureg
->nr_temps
) {
1457 emit_decl_range( ureg
,
1458 TGSI_FILE_TEMPORARY
,
1459 0, ureg
->nr_temps
);
1462 if (ureg
->nr_addrs
) {
1463 emit_decl_range( ureg
,
1465 0, ureg
->nr_addrs
);
1468 if (ureg
->nr_preds
) {
1469 emit_decl_range(ureg
,
1470 TGSI_FILE_PREDICATE
,
1475 for (i
= 0; i
< ureg
->nr_immediates
; i
++) {
1476 emit_immediate( ureg
,
1477 ureg
->immediate
[i
].value
.u
,
1478 ureg
->immediate
[i
].type
);
1482 /* Append the instruction tokens onto the declarations to build a
1483 * contiguous stream suitable to send to the driver.
1485 static void copy_instructions( struct ureg_program
*ureg
)
1487 unsigned nr_tokens
= ureg
->domain
[DOMAIN_INSN
].count
;
1488 union tgsi_any_token
*out
= get_tokens( ureg
,
1493 ureg
->domain
[DOMAIN_INSN
].tokens
,
1494 nr_tokens
* sizeof out
[0] );
1499 fixup_header_size(struct ureg_program
*ureg
)
1501 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_DECL
, 0 );
1503 out
->header
.BodySize
= ureg
->domain
[DOMAIN_DECL
].count
- 2;
1508 emit_header( struct ureg_program
*ureg
)
1510 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 2 );
1512 out
[0].header
.HeaderSize
= 2;
1513 out
[0].header
.BodySize
= 0;
1515 out
[1].processor
.Processor
= ureg
->processor
;
1516 out
[1].processor
.Padding
= 0;
1520 const struct tgsi_token
*ureg_finalize( struct ureg_program
*ureg
)
1522 const struct tgsi_token
*tokens
;
1524 emit_header( ureg
);
1526 copy_instructions( ureg
);
1527 fixup_header_size( ureg
);
1529 if (ureg
->domain
[0].tokens
== error_tokens
||
1530 ureg
->domain
[1].tokens
== error_tokens
) {
1531 debug_printf("%s: error in generated shader\n", __FUNCTION__
);
1536 tokens
= &ureg
->domain
[DOMAIN_DECL
].tokens
[0].token
;
1539 debug_printf("%s: emitted shader %d tokens:\n", __FUNCTION__
,
1540 ureg
->domain
[DOMAIN_DECL
].count
);
1541 tgsi_dump( tokens
, 0 );
1545 if (tokens
&& !tgsi_sanity_check(tokens
)) {
1546 debug_printf("tgsi_ureg.c, sanity check failed on generated tokens:\n");
1547 tgsi_dump(tokens
, 0);
1557 void *ureg_create_shader( struct ureg_program
*ureg
,
1558 struct pipe_context
*pipe
)
1560 struct pipe_shader_state state
;
1562 state
.tokens
= ureg_finalize(ureg
);
1566 if (ureg
->processor
== TGSI_PROCESSOR_VERTEX
)
1567 return pipe
->create_vs_state( pipe
, &state
);
1569 return pipe
->create_fs_state( pipe
, &state
);
1573 const struct tgsi_token
*ureg_get_tokens( struct ureg_program
*ureg
,
1574 unsigned *nr_tokens
)
1576 const struct tgsi_token
*tokens
;
1578 ureg_finalize(ureg
);
1580 tokens
= &ureg
->domain
[DOMAIN_DECL
].tokens
[0].token
;
1583 *nr_tokens
= ureg
->domain
[DOMAIN_DECL
].size
;
1585 ureg
->domain
[DOMAIN_DECL
].tokens
= 0;
1586 ureg
->domain
[DOMAIN_DECL
].size
= 0;
1587 ureg
->domain
[DOMAIN_DECL
].order
= 0;
1588 ureg
->domain
[DOMAIN_DECL
].count
= 0;
1594 void ureg_free_tokens( const struct tgsi_token
*tokens
)
1596 FREE((struct tgsi_token
*)tokens
);
1600 struct ureg_program
*ureg_create( unsigned processor
)
1602 struct ureg_program
*ureg
= CALLOC_STRUCT( ureg_program
);
1606 ureg
->processor
= processor
;
1607 ureg
->property_gs_input_prim
= ~0;
1608 ureg
->property_gs_output_prim
= ~0;
1609 ureg
->property_gs_max_vertices
= ~0;
1614 void ureg_destroy( struct ureg_program
*ureg
)
1618 for (i
= 0; i
< Elements(ureg
->domain
); i
++) {
1619 if (ureg
->domain
[i
].tokens
&&
1620 ureg
->domain
[i
].tokens
!= error_tokens
)
1621 FREE(ureg
->domain
[i
].tokens
);