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_info.h"
33 #include "tgsi/tgsi_dump.h"
34 #include "tgsi/tgsi_sanity.h"
35 #include "util/u_memory.h"
36 #include "util/u_math.h"
38 union tgsi_any_token
{
39 struct tgsi_version version
;
40 struct tgsi_header header
;
41 struct tgsi_processor processor
;
42 struct tgsi_token token
;
43 struct tgsi_declaration decl
;
44 struct tgsi_declaration_range decl_range
;
45 struct tgsi_declaration_semantic decl_semantic
;
46 struct tgsi_immediate imm
;
47 union tgsi_immediate_data imm_data
;
48 struct tgsi_instruction insn
;
49 struct tgsi_instruction_ext_nv insn_ext_nv
;
50 struct tgsi_instruction_ext_label insn_ext_label
;
51 struct tgsi_instruction_ext_texture insn_ext_texture
;
52 struct tgsi_instruction_ext_predicate insn_ext_predicate
;
53 struct tgsi_src_register src
;
54 struct tgsi_src_register_ext_mod src_ext_mod
;
55 struct tgsi_dimension dim
;
56 struct tgsi_dst_register dst
;
57 struct tgsi_dst_register_ext_concode dst_ext_code
;
58 struct tgsi_dst_register_ext_modulate dst_ext_mod
;
59 struct tgsi_dst_register_ext_predicate dst_ext_pred
;
65 union tgsi_any_token
*tokens
;
71 #define UREG_MAX_INPUT 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
84 struct pipe_context
*pipe
;
87 unsigned semantic_name
;
88 unsigned semantic_index
;
90 } fs_input
[UREG_MAX_INPUT
];
91 unsigned nr_fs_inputs
;
93 unsigned vs_inputs
[UREG_MAX_INPUT
/32];
96 unsigned semantic_name
;
97 unsigned semantic_index
;
98 } output
[UREG_MAX_OUTPUT
];
104 } immediate
[UREG_MAX_IMMEDIATE
];
105 unsigned nr_immediates
;
107 struct ureg_src sampler
[PIPE_MAX_SAMPLERS
];
108 unsigned nr_samplers
;
110 unsigned temps_active
[UREG_MAX_TEMP
/ 32];
116 } constant_range
[UREG_MAX_CONSTANT_RANGE
];
117 unsigned nr_constant_ranges
;
120 unsigned nr_instructions
;
122 struct ureg_tokens domain
[2];
125 static union tgsi_any_token error_tokens
[32];
127 static void tokens_error( struct ureg_tokens
*tokens
)
129 if (tokens
->tokens
&& tokens
->tokens
!= error_tokens
)
130 FREE(tokens
->tokens
);
132 tokens
->tokens
= error_tokens
;
133 tokens
->size
= Elements(error_tokens
);
138 static void tokens_expand( struct ureg_tokens
*tokens
,
141 unsigned old_size
= tokens
->size
* sizeof(unsigned);
143 if (tokens
->tokens
== error_tokens
)
146 while (tokens
->count
+ count
> tokens
->size
) {
147 tokens
->size
= (1 << ++tokens
->order
);
150 tokens
->tokens
= REALLOC(tokens
->tokens
,
152 tokens
->size
* sizeof(unsigned));
153 if (tokens
->tokens
== NULL
)
159 tokens_error(tokens
);
162 static void set_bad( struct ureg_program
*ureg
)
164 tokens_error(&ureg
->domain
[0]);
169 static union tgsi_any_token
*get_tokens( struct ureg_program
*ureg
,
173 struct ureg_tokens
*tokens
= &ureg
->domain
[domain
];
174 union tgsi_any_token
*result
;
176 if (tokens
->count
+ count
> tokens
->size
)
177 tokens_expand(tokens
, count
);
179 result
= &tokens
->tokens
[tokens
->count
];
180 tokens
->count
+= count
;
185 static union tgsi_any_token
*retrieve_token( struct ureg_program
*ureg
,
189 if (ureg
->domain
[domain
].tokens
== error_tokens
)
190 return &error_tokens
[0];
192 return &ureg
->domain
[domain
].tokens
[nr
];
197 static INLINE
struct ureg_dst
198 ureg_dst_register( unsigned file
,
204 dst
.WriteMask
= TGSI_WRITEMASK_XYZW
;
206 dst
.IndirectIndex
= 0;
207 dst
.IndirectSwizzle
= 0;
216 static INLINE
struct ureg_src
217 ureg_src_register( unsigned file
,
223 src
.SwizzleX
= TGSI_SWIZZLE_X
;
224 src
.SwizzleY
= TGSI_SWIZZLE_Y
;
225 src
.SwizzleZ
= TGSI_SWIZZLE_Z
;
226 src
.SwizzleW
= TGSI_SWIZZLE_W
;
229 src
.IndirectIndex
= 0;
230 src
.IndirectSwizzle
= 0;
242 ureg_DECL_fs_input( struct ureg_program
*ureg
,
245 unsigned interp_mode
)
249 for (i
= 0; i
< ureg
->nr_fs_inputs
; i
++) {
250 if (ureg
->fs_input
[i
].semantic_name
== name
&&
251 ureg
->fs_input
[i
].semantic_index
== index
)
255 if (ureg
->nr_fs_inputs
< UREG_MAX_INPUT
) {
256 ureg
->fs_input
[i
].semantic_name
= name
;
257 ureg
->fs_input
[i
].semantic_index
= index
;
258 ureg
->fs_input
[i
].interp
= interp_mode
;
259 ureg
->nr_fs_inputs
++;
266 return ureg_src_register( TGSI_FILE_INPUT
, i
);
271 ureg_DECL_vs_input( struct ureg_program
*ureg
,
274 assert(ureg
->processor
== TGSI_PROCESSOR_VERTEX
);
276 ureg
->vs_inputs
[index
/32] |= 1 << (index
% 32);
277 return ureg_src_register( TGSI_FILE_INPUT
, index
);
282 ureg_DECL_output( struct ureg_program
*ureg
,
288 for (i
= 0; i
< ureg
->nr_outputs
; i
++) {
289 if (ureg
->output
[i
].semantic_name
== name
&&
290 ureg
->output
[i
].semantic_index
== index
)
294 if (ureg
->nr_outputs
< UREG_MAX_OUTPUT
) {
295 ureg
->output
[i
].semantic_name
= name
;
296 ureg
->output
[i
].semantic_index
= index
;
304 return ureg_dst_register( TGSI_FILE_OUTPUT
, i
);
308 /* Returns a new constant register. Keep track of which have been
309 * referred to so that we can emit decls later.
311 * There is nothing in this code to bind this constant to any tracked
312 * value or manage any constant_buffer contents -- that's the
313 * resposibility of the calling code.
315 struct ureg_src
ureg_DECL_constant(struct ureg_program
*ureg
,
318 unsigned minconst
= index
, maxconst
= index
;
321 /* Inside existing range?
323 for (i
= 0; i
< ureg
->nr_constant_ranges
; i
++) {
324 if (ureg
->constant_range
[i
].first
<= index
&&
325 ureg
->constant_range
[i
].last
>= index
)
329 /* Extend existing range?
331 for (i
= 0; i
< ureg
->nr_constant_ranges
; i
++) {
332 if (ureg
->constant_range
[i
].last
== index
- 1) {
333 ureg
->constant_range
[i
].last
= index
;
337 if (ureg
->constant_range
[i
].first
== index
+ 1) {
338 ureg
->constant_range
[i
].first
= index
;
342 minconst
= MIN2(minconst
, ureg
->constant_range
[i
].first
);
343 maxconst
= MAX2(maxconst
, ureg
->constant_range
[i
].last
);
348 if (ureg
->nr_constant_ranges
< UREG_MAX_CONSTANT_RANGE
) {
349 i
= ureg
->nr_constant_ranges
++;
350 ureg
->constant_range
[i
].first
= index
;
351 ureg
->constant_range
[i
].last
= index
;
354 /* Collapse all ranges down to one:
357 ureg
->constant_range
[0].first
= minconst
;
358 ureg
->constant_range
[0].last
= maxconst
;
359 ureg
->nr_constant_ranges
= 1;
362 assert(i
< ureg
->nr_constant_ranges
);
363 assert(ureg
->constant_range
[i
].first
<= index
);
364 assert(ureg
->constant_range
[i
].last
>= index
);
365 return ureg_src_register( TGSI_FILE_CONSTANT
, index
);
369 /* Allocate a new temporary. Temporaries greater than UREG_MAX_TEMP
370 * are legal, but will not be released.
372 struct ureg_dst
ureg_DECL_temporary( struct ureg_program
*ureg
)
376 for (i
= 0; i
< UREG_MAX_TEMP
; i
+= 32) {
377 int bit
= ffs(~ureg
->temps_active
[i
/32]);
384 /* No reusable temps, so allocate a new one:
386 i
= ureg
->nr_temps
++;
389 if (i
< UREG_MAX_TEMP
)
390 ureg
->temps_active
[i
/32] |= 1 << (i
% 32);
392 if (i
>= ureg
->nr_temps
)
393 ureg
->nr_temps
= i
+ 1;
395 return ureg_dst_register( TGSI_FILE_TEMPORARY
, i
);
399 void ureg_release_temporary( struct ureg_program
*ureg
,
400 struct ureg_dst tmp
)
402 if(tmp
.File
== TGSI_FILE_TEMPORARY
)
403 if (tmp
.Index
< UREG_MAX_TEMP
)
404 ureg
->temps_active
[tmp
.Index
/32] &= ~(1 << (tmp
.Index
% 32));
408 /* Allocate a new address register.
410 struct ureg_dst
ureg_DECL_address( struct ureg_program
*ureg
)
412 if (ureg
->nr_addrs
< UREG_MAX_ADDR
)
413 return ureg_dst_register( TGSI_FILE_ADDRESS
, ureg
->nr_addrs
++ );
416 return ureg_dst_register( TGSI_FILE_ADDRESS
, 0 );
419 /* Allocate a new sampler.
421 struct ureg_src
ureg_DECL_sampler( struct ureg_program
*ureg
,
426 for (i
= 0; i
< ureg
->nr_samplers
; i
++)
427 if (ureg
->sampler
[i
].Index
== nr
)
428 return ureg
->sampler
[i
];
430 if (i
< PIPE_MAX_SAMPLERS
) {
431 ureg
->sampler
[i
] = ureg_src_register( TGSI_FILE_SAMPLER
, nr
);
433 return ureg
->sampler
[i
];
437 return ureg
->sampler
[0];
443 static int match_or_expand_immediate( const float *v
,
453 for (i
= 0; i
< nr
; i
++) {
454 boolean found
= FALSE
;
456 for (j
= 0; j
< *nr2
&& !found
; j
++) {
458 *swizzle
|= j
<< (i
* 2);
468 *swizzle
|= *nr2
<< (i
* 2);
479 struct ureg_src
ureg_DECL_immediate( struct ureg_program
*ureg
,
486 /* Could do a first pass where we examine all existing immediates
490 for (i
= 0; i
< ureg
->nr_immediates
; i
++) {
491 if (match_or_expand_immediate( v
,
493 ureg
->immediate
[i
].v
,
494 &ureg
->immediate
[i
].nr
,
499 if (ureg
->nr_immediates
< UREG_MAX_IMMEDIATE
) {
500 i
= ureg
->nr_immediates
++;
501 if (match_or_expand_immediate( v
,
503 ureg
->immediate
[i
].v
,
504 &ureg
->immediate
[i
].nr
,
512 /* Make sure that all referenced elements are from this immediate.
513 * Has the effect of making size-one immediates into scalars.
515 for (j
= nr
; j
< 4; j
++)
516 swizzle
|= (swizzle
& 0x3) << (j
* 2);
518 return ureg_swizzle( ureg_src_register( TGSI_FILE_IMMEDIATE
, i
),
519 (swizzle
>> 0) & 0x3,
520 (swizzle
>> 2) & 0x3,
521 (swizzle
>> 4) & 0x3,
522 (swizzle
>> 6) & 0x3);
527 ureg_emit_src( struct ureg_program
*ureg
,
528 struct ureg_src src
)
531 (src
.Absolute
? 1 : 0) +
532 (src
.Indirect
? 1 : 0));
534 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_INSN
, size
);
537 assert(src
.File
!= TGSI_FILE_NULL
);
538 assert(src
.File
!= TGSI_FILE_OUTPUT
);
539 assert(src
.File
< TGSI_FILE_COUNT
);
542 out
[n
].src
.File
= src
.File
;
543 out
[n
].src
.SwizzleX
= src
.SwizzleX
;
544 out
[n
].src
.SwizzleY
= src
.SwizzleY
;
545 out
[n
].src
.SwizzleZ
= src
.SwizzleZ
;
546 out
[n
].src
.SwizzleW
= src
.SwizzleW
;
547 out
[n
].src
.Index
= src
.Index
;
548 out
[n
].src
.Negate
= src
.Negate
;
552 out
[0].src
.Extended
= 1;
553 out
[0].src
.Negate
= 0;
555 out
[n
].src_ext_mod
.Type
= TGSI_SRC_REGISTER_EXT_TYPE_MOD
;
556 out
[n
].src_ext_mod
.Absolute
= 1;
557 out
[n
].src_ext_mod
.Negate
= src
.Negate
;
562 out
[0].src
.Indirect
= 1;
564 out
[n
].src
.File
= TGSI_FILE_ADDRESS
;
565 out
[n
].src
.SwizzleX
= src
.IndirectSwizzle
;
566 out
[n
].src
.SwizzleY
= src
.IndirectSwizzle
;
567 out
[n
].src
.SwizzleZ
= src
.IndirectSwizzle
;
568 out
[n
].src
.SwizzleW
= src
.IndirectSwizzle
;
569 out
[n
].src
.Index
= src
.IndirectIndex
;
578 ureg_emit_dst( struct ureg_program
*ureg
,
579 struct ureg_dst dst
)
582 (dst
.Indirect
? 1 : 0));
584 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_INSN
, size
);
587 assert(dst
.File
!= TGSI_FILE_NULL
);
588 assert(dst
.File
!= TGSI_FILE_CONSTANT
);
589 assert(dst
.File
!= TGSI_FILE_INPUT
);
590 assert(dst
.File
!= TGSI_FILE_SAMPLER
);
591 assert(dst
.File
!= TGSI_FILE_IMMEDIATE
);
592 assert(dst
.File
< TGSI_FILE_COUNT
);
595 out
[n
].dst
.File
= dst
.File
;
596 out
[n
].dst
.WriteMask
= dst
.WriteMask
;
597 out
[n
].dst
.Indirect
= dst
.Indirect
;
598 out
[n
].dst
.Index
= dst
.Index
;
603 out
[n
].src
.File
= TGSI_FILE_ADDRESS
;
604 out
[n
].src
.SwizzleX
= dst
.IndirectSwizzle
;
605 out
[n
].src
.SwizzleY
= dst
.IndirectSwizzle
;
606 out
[n
].src
.SwizzleZ
= dst
.IndirectSwizzle
;
607 out
[n
].src
.SwizzleW
= dst
.IndirectSwizzle
;
608 out
[n
].src
.Index
= dst
.IndirectIndex
;
616 static void validate( unsigned opcode
,
621 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( opcode
);
624 assert(nr_dst
== info
->num_dst
);
625 assert(nr_src
== info
->num_src
);
631 ureg_emit_insn(struct ureg_program
*ureg
,
637 union tgsi_any_token
*out
;
639 validate( opcode
, num_dst
, num_src
);
641 out
= get_tokens( ureg
, DOMAIN_INSN
, 1 );
643 out
[0].insn
.Type
= TGSI_TOKEN_TYPE_INSTRUCTION
;
644 out
[0].insn
.NrTokens
= 0;
645 out
[0].insn
.Opcode
= opcode
;
646 out
[0].insn
.Saturate
= saturate
;
647 out
[0].insn
.NumDstRegs
= num_dst
;
648 out
[0].insn
.NumSrcRegs
= num_src
;
649 out
[0].insn
.Padding
= 0;
650 out
[0].insn
.Extended
= 0;
652 ureg
->nr_instructions
++;
654 return ureg
->domain
[DOMAIN_INSN
].count
- 1;
659 ureg_emit_label(struct ureg_program
*ureg
,
661 unsigned *label_token
)
663 union tgsi_any_token
*out
, *insn
;
668 out
= get_tokens( ureg
, DOMAIN_INSN
, 1 );
669 insn
= retrieve_token( ureg
, DOMAIN_INSN
, insn_token
);
671 insn
->insn
.Extended
= 1;
674 out
[0].insn_ext_label
.Type
= TGSI_INSTRUCTION_EXT_TYPE_LABEL
;
676 *label_token
= ureg
->domain
[DOMAIN_INSN
].count
- 1;
679 /* Will return a number which can be used in a label to point to the
680 * next instruction to be emitted.
683 ureg_get_instruction_number( struct ureg_program
*ureg
)
685 return ureg
->nr_instructions
;
688 /* Patch a given label (expressed as a token number) to point to a
689 * given instruction (expressed as an instruction number).
692 ureg_fixup_label(struct ureg_program
*ureg
,
693 unsigned label_token
,
694 unsigned instruction_number
)
696 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_INSN
, label_token
);
698 assert(out
->insn_ext_label
.Type
== TGSI_INSTRUCTION_EXT_TYPE_LABEL
);
699 out
->insn_ext_label
.Label
= instruction_number
;
704 ureg_emit_texture(struct ureg_program
*ureg
,
708 union tgsi_any_token
*out
, *insn
;
710 out
= get_tokens( ureg
, DOMAIN_INSN
, 1 );
711 insn
= retrieve_token( ureg
, DOMAIN_INSN
, insn_token
);
713 insn
->insn
.Extended
= 1;
716 out
[0].insn_ext_texture
.Type
= TGSI_INSTRUCTION_EXT_TYPE_TEXTURE
;
717 out
[0].insn_ext_texture
.Texture
= target
;
722 ureg_fixup_insn_size(struct ureg_program
*ureg
,
725 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_INSN
, insn
);
727 assert(out
->insn
.Type
== TGSI_TOKEN_TYPE_INSTRUCTION
);
728 out
->insn
.NrTokens
= ureg
->domain
[DOMAIN_INSN
].count
- insn
- 1;
733 ureg_insn(struct ureg_program
*ureg
,
735 const struct ureg_dst
*dst
,
737 const struct ureg_src
*src
,
743 saturate
= nr_dst
? dst
[0].Saturate
: FALSE
;
745 insn
= ureg_emit_insn( ureg
, opcode
, saturate
, nr_dst
, nr_src
);
747 for (i
= 0; i
< nr_dst
; i
++)
748 ureg_emit_dst( ureg
, dst
[i
] );
750 for (i
= 0; i
< nr_src
; i
++)
751 ureg_emit_src( ureg
, src
[i
] );
753 ureg_fixup_insn_size( ureg
, insn
);
757 ureg_tex_insn(struct ureg_program
*ureg
,
759 const struct ureg_dst
*dst
,
762 const struct ureg_src
*src
,
768 saturate
= nr_dst
? dst
[0].Saturate
: FALSE
;
770 insn
= ureg_emit_insn( ureg
, opcode
, saturate
, nr_dst
, nr_src
);
772 ureg_emit_texture( ureg
, insn
, target
); \
774 for (i
= 0; i
< nr_dst
; i
++)
775 ureg_emit_dst( ureg
, dst
[i
] );
777 for (i
= 0; i
< nr_src
; i
++)
778 ureg_emit_src( ureg
, src
[i
] );
780 ureg_fixup_insn_size( ureg
, insn
);
785 ureg_label_insn(struct ureg_program
*ureg
,
787 const struct ureg_src
*src
,
789 unsigned *label_token
)
793 insn
= ureg_emit_insn( ureg
, opcode
, FALSE
, 0, nr_src
);
795 ureg_emit_label( ureg
, insn
, label_token
); \
797 for (i
= 0; i
< nr_src
; i
++)
798 ureg_emit_src( ureg
, src
[i
] );
800 ureg_fixup_insn_size( ureg
, insn
);
805 static void emit_decl( struct ureg_program
*ureg
,
808 unsigned semantic_name
,
809 unsigned semantic_index
,
812 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 3 );
815 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
816 out
[0].decl
.NrTokens
= 3;
817 out
[0].decl
.File
= file
;
818 out
[0].decl
.UsageMask
= TGSI_WRITEMASK_XYZW
; /* FIXME! */
819 out
[0].decl
.Interpolate
= interp
;
820 out
[0].decl
.Semantic
= 1;
823 out
[1].decl_range
.First
=
824 out
[1].decl_range
.Last
= index
;
827 out
[2].decl_semantic
.SemanticName
= semantic_name
;
828 out
[2].decl_semantic
.SemanticIndex
= semantic_index
;
833 static void emit_decl_range( struct ureg_program
*ureg
,
838 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 2 );
841 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
842 out
[0].decl
.NrTokens
= 2;
843 out
[0].decl
.File
= file
;
844 out
[0].decl
.UsageMask
= 0xf;
845 out
[0].decl
.Interpolate
= TGSI_INTERPOLATE_CONSTANT
;
846 out
[0].decl
.Semantic
= 0;
849 out
[1].decl_range
.First
= first
;
850 out
[1].decl_range
.Last
= first
+ count
- 1;
853 static void emit_immediate( struct ureg_program
*ureg
,
856 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 5 );
859 out
[0].imm
.Type
= TGSI_TOKEN_TYPE_IMMEDIATE
;
860 out
[0].imm
.NrTokens
= 5;
861 out
[0].imm
.DataType
= TGSI_IMM_FLOAT32
;
862 out
[0].imm
.Padding
= 0;
863 out
[0].imm
.Extended
= 0;
865 out
[1].imm_data
.Float
= v
[0];
866 out
[2].imm_data
.Float
= v
[1];
867 out
[3].imm_data
.Float
= v
[2];
868 out
[4].imm_data
.Float
= v
[3];
874 static void emit_decls( struct ureg_program
*ureg
)
878 if (ureg
->processor
== TGSI_PROCESSOR_VERTEX
) {
879 for (i
= 0; i
< UREG_MAX_INPUT
; i
++) {
880 if (ureg
->vs_inputs
[i
/32] & (1 << (i
%32))) {
881 emit_decl_range( ureg
, TGSI_FILE_INPUT
, i
, 1 );
886 for (i
= 0; i
< ureg
->nr_fs_inputs
; i
++) {
890 ureg
->fs_input
[i
].semantic_name
,
891 ureg
->fs_input
[i
].semantic_index
,
892 ureg
->fs_input
[i
].interp
);
896 for (i
= 0; i
< ureg
->nr_outputs
; i
++) {
900 ureg
->output
[i
].semantic_name
,
901 ureg
->output
[i
].semantic_index
,
902 TGSI_INTERPOLATE_CONSTANT
);
905 for (i
= 0; i
< ureg
->nr_samplers
; i
++) {
906 emit_decl_range( ureg
,
908 ureg
->sampler
[i
].Index
, 1 );
911 if (ureg
->nr_constant_ranges
) {
912 for (i
= 0; i
< ureg
->nr_constant_ranges
; i
++)
913 emit_decl_range( ureg
,
915 ureg
->constant_range
[i
].first
,
916 (ureg
->constant_range
[i
].last
+ 1 -
917 ureg
->constant_range
[i
].first
) );
920 if (ureg
->nr_temps
) {
921 emit_decl_range( ureg
,
926 if (ureg
->nr_addrs
) {
927 emit_decl_range( ureg
,
932 for (i
= 0; i
< ureg
->nr_immediates
; i
++) {
933 emit_immediate( ureg
,
934 ureg
->immediate
[i
].v
);
938 /* Append the instruction tokens onto the declarations to build a
939 * contiguous stream suitable to send to the driver.
941 static void copy_instructions( struct ureg_program
*ureg
)
943 unsigned nr_tokens
= ureg
->domain
[DOMAIN_INSN
].count
;
944 union tgsi_any_token
*out
= get_tokens( ureg
,
949 ureg
->domain
[DOMAIN_INSN
].tokens
,
950 nr_tokens
* sizeof out
[0] );
955 fixup_header_size(struct ureg_program
*ureg
)
957 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_DECL
, 1 );
959 out
->header
.BodySize
= ureg
->domain
[DOMAIN_DECL
].count
- 3;
964 emit_header( struct ureg_program
*ureg
)
966 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 3 );
968 out
[0].version
.MajorVersion
= 1;
969 out
[0].version
.MinorVersion
= 1;
970 out
[0].version
.Padding
= 0;
972 out
[1].header
.HeaderSize
= 2;
973 out
[1].header
.BodySize
= 0;
975 out
[2].processor
.Processor
= ureg
->processor
;
976 out
[2].processor
.Padding
= 0;
980 const struct tgsi_token
*ureg_finalize( struct ureg_program
*ureg
)
982 const struct tgsi_token
*tokens
;
986 copy_instructions( ureg
);
987 fixup_header_size( ureg
);
989 if (ureg
->domain
[0].tokens
== error_tokens
||
990 ureg
->domain
[1].tokens
== error_tokens
) {
991 debug_printf("%s: error in generated shader\n", __FUNCTION__
);
996 tokens
= &ureg
->domain
[DOMAIN_DECL
].tokens
[0].token
;
999 debug_printf("%s: emitted shader %d tokens:\n", __FUNCTION__
,
1000 ureg
->domain
[DOMAIN_DECL
].count
);
1001 tgsi_dump( tokens
, 0 );
1005 if (tokens
&& !tgsi_sanity_check(tokens
)) {
1006 debug_printf("tgsi_ureg.c, sanity check failed on generated tokens:\n");
1007 tgsi_dump(tokens
, 0);
1017 void *ureg_create_shader( struct ureg_program
*ureg
,
1018 struct pipe_context
*pipe
)
1020 struct pipe_shader_state state
;
1022 state
.tokens
= ureg_finalize(ureg
);
1026 if (ureg
->processor
== TGSI_PROCESSOR_VERTEX
)
1027 return pipe
->create_vs_state( pipe
, &state
);
1029 return pipe
->create_fs_state( pipe
, &state
);
1033 const struct tgsi_token
*ureg_get_tokens( struct ureg_program
*ureg
,
1034 unsigned *nr_tokens
)
1036 const struct tgsi_token
*tokens
;
1038 ureg_finalize(ureg
);
1040 tokens
= &ureg
->domain
[DOMAIN_DECL
].tokens
[0].token
;
1043 *nr_tokens
= ureg
->domain
[DOMAIN_DECL
].size
;
1045 ureg
->domain
[DOMAIN_DECL
].tokens
= 0;
1046 ureg
->domain
[DOMAIN_DECL
].size
= 0;
1047 ureg
->domain
[DOMAIN_DECL
].order
= 0;
1048 ureg
->domain
[DOMAIN_DECL
].count
= 0;
1054 struct ureg_program
*ureg_create( unsigned processor
)
1056 struct ureg_program
*ureg
= CALLOC_STRUCT( ureg_program
);
1060 ureg
->processor
= processor
;
1065 void ureg_destroy( struct ureg_program
*ureg
)
1069 for (i
= 0; i
< Elements(ureg
->domain
); i
++) {
1070 if (ureg
->domain
[i
].tokens
&&
1071 ureg
->domain
[i
].tokens
!= error_tokens
)
1072 FREE(ureg
->domain
[i
].tokens
);