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_swz src_ext_swz
;
55 struct tgsi_src_register_ext_mod src_ext_mod
;
56 struct tgsi_dimension dim
;
57 struct tgsi_dst_register dst
;
58 struct tgsi_dst_register_ext_concode dst_ext_code
;
59 struct tgsi_dst_register_ext_modulate dst_ext_mod
;
60 struct tgsi_dst_register_ext_predicate dst_ext_pred
;
66 union tgsi_any_token
*tokens
;
72 #define UREG_MAX_INPUT PIPE_MAX_ATTRIBS
73 #define UREG_MAX_OUTPUT PIPE_MAX_ATTRIBS
74 #define UREG_MAX_CONSTANT_RANGE 32
75 #define UREG_MAX_IMMEDIATE 32
76 #define UREG_MAX_TEMP 256
77 #define UREG_MAX_ADDR 2
85 struct pipe_context
*pipe
;
88 unsigned semantic_name
;
89 unsigned semantic_index
;
91 } fs_input
[UREG_MAX_INPUT
];
92 unsigned nr_fs_inputs
;
94 unsigned vs_inputs
[UREG_MAX_INPUT
/32];
97 unsigned semantic_name
;
98 unsigned semantic_index
;
99 } output
[UREG_MAX_OUTPUT
];
105 } immediate
[UREG_MAX_IMMEDIATE
];
106 unsigned nr_immediates
;
108 struct ureg_src sampler
[PIPE_MAX_SAMPLERS
];
109 unsigned nr_samplers
;
111 unsigned temps_active
[UREG_MAX_TEMP
/ 32];
117 } constant_range
[UREG_MAX_CONSTANT_RANGE
];
118 unsigned nr_constant_ranges
;
121 unsigned nr_instructions
;
123 struct ureg_tokens domain
[2];
126 static union tgsi_any_token error_tokens
[32];
128 static void tokens_error( struct ureg_tokens
*tokens
)
130 if (tokens
->tokens
&& tokens
->tokens
!= error_tokens
)
131 FREE(tokens
->tokens
);
133 tokens
->tokens
= error_tokens
;
134 tokens
->size
= Elements(error_tokens
);
139 static void tokens_expand( struct ureg_tokens
*tokens
,
142 unsigned old_size
= tokens
->size
* sizeof(unsigned);
144 if (tokens
->tokens
== error_tokens
)
147 while (tokens
->count
+ count
> tokens
->size
) {
148 tokens
->size
= (1 << ++tokens
->order
);
151 tokens
->tokens
= REALLOC(tokens
->tokens
,
153 tokens
->size
* sizeof(unsigned));
154 if (tokens
->tokens
== NULL
)
160 tokens_error(tokens
);
163 static void set_bad( struct ureg_program
*ureg
)
165 tokens_error(&ureg
->domain
[0]);
170 static union tgsi_any_token
*get_tokens( struct ureg_program
*ureg
,
174 struct ureg_tokens
*tokens
= &ureg
->domain
[domain
];
175 union tgsi_any_token
*result
;
177 if (tokens
->count
+ count
> tokens
->size
)
178 tokens_expand(tokens
, count
);
180 result
= &tokens
->tokens
[tokens
->count
];
181 tokens
->count
+= count
;
186 static union tgsi_any_token
*retrieve_token( struct ureg_program
*ureg
,
190 if (ureg
->domain
[domain
].tokens
== error_tokens
)
191 return &error_tokens
[0];
193 return &ureg
->domain
[domain
].tokens
[nr
];
198 static INLINE
struct ureg_dst
199 ureg_dst_register( unsigned file
,
205 dst
.WriteMask
= TGSI_WRITEMASK_XYZW
;
207 dst
.IndirectIndex
= 0;
208 dst
.IndirectSwizzle
= 0;
217 static INLINE
struct ureg_src
218 ureg_src_register( unsigned file
,
224 src
.SwizzleX
= TGSI_SWIZZLE_X
;
225 src
.SwizzleY
= TGSI_SWIZZLE_Y
;
226 src
.SwizzleZ
= TGSI_SWIZZLE_Z
;
227 src
.SwizzleW
= TGSI_SWIZZLE_W
;
230 src
.IndirectIndex
= 0;
231 src
.IndirectSwizzle
= 0;
243 ureg_DECL_fs_input( struct ureg_program
*ureg
,
246 unsigned interp_mode
)
250 for (i
= 0; i
< ureg
->nr_fs_inputs
; i
++) {
251 if (ureg
->fs_input
[i
].semantic_name
== name
&&
252 ureg
->fs_input
[i
].semantic_index
== index
)
256 if (ureg
->nr_fs_inputs
< UREG_MAX_INPUT
) {
257 ureg
->fs_input
[i
].semantic_name
= name
;
258 ureg
->fs_input
[i
].semantic_index
= index
;
259 ureg
->fs_input
[i
].interp
= interp_mode
;
260 ureg
->nr_fs_inputs
++;
267 return ureg_src_register( TGSI_FILE_INPUT
, i
);
272 ureg_DECL_vs_input( struct ureg_program
*ureg
,
275 assert(ureg
->processor
== TGSI_PROCESSOR_VERTEX
);
277 ureg
->vs_inputs
[index
/32] |= 1 << (index
% 32);
278 return ureg_src_register( TGSI_FILE_INPUT
, index
);
283 ureg_DECL_output( struct ureg_program
*ureg
,
289 for (i
= 0; i
< ureg
->nr_outputs
; i
++) {
290 if (ureg
->output
[i
].semantic_name
== name
&&
291 ureg
->output
[i
].semantic_index
== index
)
295 if (ureg
->nr_outputs
< UREG_MAX_OUTPUT
) {
296 ureg
->output
[i
].semantic_name
= name
;
297 ureg
->output
[i
].semantic_index
= index
;
305 return ureg_dst_register( TGSI_FILE_OUTPUT
, i
);
309 /* Returns a new constant register. Keep track of which have been
310 * referred to so that we can emit decls later.
312 * There is nothing in this code to bind this constant to any tracked
313 * value or manage any constant_buffer contents -- that's the
314 * resposibility of the calling code.
316 struct ureg_src
ureg_DECL_constant(struct ureg_program
*ureg
,
319 unsigned minconst
= index
, maxconst
= index
;
322 /* Inside existing range?
324 for (i
= 0; i
< ureg
->nr_constant_ranges
; i
++) {
325 if (ureg
->constant_range
[i
].first
<= index
&&
326 ureg
->constant_range
[i
].last
>= index
)
330 /* Extend existing range?
332 for (i
= 0; i
< ureg
->nr_constant_ranges
; i
++) {
333 if (ureg
->constant_range
[i
].last
== index
- 1) {
334 ureg
->constant_range
[i
].last
= index
;
338 if (ureg
->constant_range
[i
].first
== index
+ 1) {
339 ureg
->constant_range
[i
].first
= index
;
343 minconst
= MIN2(minconst
, ureg
->constant_range
[i
].first
);
344 maxconst
= MAX2(maxconst
, ureg
->constant_range
[i
].last
);
349 if (ureg
->nr_constant_ranges
< UREG_MAX_CONSTANT_RANGE
) {
350 i
= ureg
->nr_constant_ranges
++;
351 ureg
->constant_range
[i
].first
= index
;
352 ureg
->constant_range
[i
].last
= index
;
355 /* Collapse all ranges down to one:
358 ureg
->constant_range
[0].first
= minconst
;
359 ureg
->constant_range
[0].last
= maxconst
;
360 ureg
->nr_constant_ranges
= 1;
363 assert(i
< ureg
->nr_constant_ranges
);
364 assert(ureg
->constant_range
[i
].first
<= index
);
365 assert(ureg
->constant_range
[i
].last
>= index
);
366 return ureg_src_register( TGSI_FILE_CONSTANT
, index
);
370 /* Allocate a new temporary. Temporaries greater than UREG_MAX_TEMP
371 * are legal, but will not be released.
373 struct ureg_dst
ureg_DECL_temporary( struct ureg_program
*ureg
)
377 for (i
= 0; i
< UREG_MAX_TEMP
; i
+= 32) {
378 int bit
= ffs(~ureg
->temps_active
[i
/32]);
385 /* No reusable temps, so allocate a new one:
387 i
= ureg
->nr_temps
++;
390 if (i
< UREG_MAX_TEMP
)
391 ureg
->temps_active
[i
/32] |= 1 << (i
% 32);
393 if (i
>= ureg
->nr_temps
)
394 ureg
->nr_temps
= i
+ 1;
396 return ureg_dst_register( TGSI_FILE_TEMPORARY
, i
);
400 void ureg_release_temporary( struct ureg_program
*ureg
,
401 struct ureg_dst tmp
)
403 if(tmp
.File
== TGSI_FILE_TEMPORARY
)
404 if (tmp
.Index
< UREG_MAX_TEMP
)
405 ureg
->temps_active
[tmp
.Index
/32] &= ~(1 << (tmp
.Index
% 32));
409 /* Allocate a new address register.
411 struct ureg_dst
ureg_DECL_address( struct ureg_program
*ureg
)
413 if (ureg
->nr_addrs
< UREG_MAX_ADDR
)
414 return ureg_dst_register( TGSI_FILE_ADDRESS
, ureg
->nr_addrs
++ );
417 return ureg_dst_register( TGSI_FILE_ADDRESS
, 0 );
420 /* Allocate a new sampler.
422 struct ureg_src
ureg_DECL_sampler( struct ureg_program
*ureg
,
427 for (i
= 0; i
< ureg
->nr_samplers
; i
++)
428 if (ureg
->sampler
[i
].Index
== nr
)
429 return ureg
->sampler
[i
];
431 if (i
< PIPE_MAX_SAMPLERS
) {
432 ureg
->sampler
[i
] = ureg_src_register( TGSI_FILE_SAMPLER
, nr
);
434 return ureg
->sampler
[i
];
438 return ureg
->sampler
[0];
444 static int match_or_expand_immediate( const float *v
,
454 for (i
= 0; i
< nr
; i
++) {
455 boolean found
= FALSE
;
457 for (j
= 0; j
< *nr2
&& !found
; j
++) {
459 *swizzle
|= j
<< (i
* 2);
469 *swizzle
|= *nr2
<< (i
* 2);
480 struct ureg_src
ureg_DECL_immediate( struct ureg_program
*ureg
,
487 /* Could do a first pass where we examine all existing immediates
491 for (i
= 0; i
< ureg
->nr_immediates
; i
++) {
492 if (match_or_expand_immediate( v
,
494 ureg
->immediate
[i
].v
,
495 &ureg
->immediate
[i
].nr
,
500 if (ureg
->nr_immediates
< UREG_MAX_IMMEDIATE
) {
501 i
= ureg
->nr_immediates
++;
502 if (match_or_expand_immediate( v
,
504 ureg
->immediate
[i
].v
,
505 &ureg
->immediate
[i
].nr
,
513 /* Make sure that all referenced elements are from this immediate.
514 * Has the effect of making size-one immediates into scalars.
516 for (j
= nr
; j
< 4; j
++)
517 swizzle
|= (swizzle
& 0x3) << (j
* 2);
519 return ureg_swizzle( ureg_src_register( TGSI_FILE_IMMEDIATE
, i
),
520 (swizzle
>> 0) & 0x3,
521 (swizzle
>> 2) & 0x3,
522 (swizzle
>> 4) & 0x3,
523 (swizzle
>> 6) & 0x3);
528 ureg_emit_src( struct ureg_program
*ureg
,
529 struct ureg_src src
)
532 (src
.Absolute
? 1 : 0) +
533 (src
.Indirect
? 1 : 0));
535 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_INSN
, size
);
538 assert(src
.File
!= TGSI_FILE_NULL
);
539 assert(src
.File
!= TGSI_FILE_OUTPUT
);
540 assert(src
.File
< TGSI_FILE_COUNT
);
543 out
[n
].src
.File
= src
.File
;
544 out
[n
].src
.SwizzleX
= src
.SwizzleX
;
545 out
[n
].src
.SwizzleY
= src
.SwizzleY
;
546 out
[n
].src
.SwizzleZ
= src
.SwizzleZ
;
547 out
[n
].src
.SwizzleW
= src
.SwizzleW
;
548 out
[n
].src
.Index
= src
.Index
;
549 out
[n
].src
.Negate
= src
.Negate
;
553 out
[0].src
.Extended
= 1;
554 out
[0].src
.Negate
= 0;
556 out
[n
].src_ext_mod
.Type
= TGSI_SRC_REGISTER_EXT_TYPE_MOD
;
557 out
[n
].src_ext_mod
.Absolute
= 1;
558 out
[n
].src_ext_mod
.Negate
= src
.Negate
;
563 out
[0].src
.Indirect
= 1;
565 out
[n
].src
.File
= TGSI_FILE_ADDRESS
;
566 out
[n
].src
.SwizzleX
= src
.IndirectSwizzle
;
567 out
[n
].src
.SwizzleY
= src
.IndirectSwizzle
;
568 out
[n
].src
.SwizzleZ
= src
.IndirectSwizzle
;
569 out
[n
].src
.SwizzleW
= src
.IndirectSwizzle
;
570 out
[n
].src
.Index
= src
.IndirectIndex
;
579 ureg_emit_dst( struct ureg_program
*ureg
,
580 struct ureg_dst dst
)
583 (dst
.Indirect
? 1 : 0));
585 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_INSN
, size
);
588 assert(dst
.File
!= TGSI_FILE_NULL
);
589 assert(dst
.File
!= TGSI_FILE_CONSTANT
);
590 assert(dst
.File
!= TGSI_FILE_INPUT
);
591 assert(dst
.File
!= TGSI_FILE_SAMPLER
);
592 assert(dst
.File
!= TGSI_FILE_IMMEDIATE
);
593 assert(dst
.File
< TGSI_FILE_COUNT
);
596 out
[n
].dst
.File
= dst
.File
;
597 out
[n
].dst
.WriteMask
= dst
.WriteMask
;
598 out
[n
].dst
.Indirect
= dst
.Indirect
;
599 out
[n
].dst
.Index
= dst
.Index
;
604 out
[n
].src
.File
= TGSI_FILE_ADDRESS
;
605 out
[n
].src
.SwizzleX
= dst
.IndirectSwizzle
;
606 out
[n
].src
.SwizzleY
= dst
.IndirectSwizzle
;
607 out
[n
].src
.SwizzleZ
= dst
.IndirectSwizzle
;
608 out
[n
].src
.SwizzleW
= dst
.IndirectSwizzle
;
609 out
[n
].src
.Index
= dst
.IndirectIndex
;
617 static void validate( unsigned opcode
,
622 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( opcode
);
625 assert(nr_dst
== info
->num_dst
);
626 assert(nr_src
== info
->num_src
);
632 ureg_emit_insn(struct ureg_program
*ureg
,
638 union tgsi_any_token
*out
;
640 validate( opcode
, num_dst
, num_src
);
642 out
= get_tokens( ureg
, DOMAIN_INSN
, 1 );
644 out
[0].insn
.Type
= TGSI_TOKEN_TYPE_INSTRUCTION
;
645 out
[0].insn
.NrTokens
= 0;
646 out
[0].insn
.Opcode
= opcode
;
647 out
[0].insn
.Saturate
= saturate
;
648 out
[0].insn
.NumDstRegs
= num_dst
;
649 out
[0].insn
.NumSrcRegs
= num_src
;
650 out
[0].insn
.Padding
= 0;
651 out
[0].insn
.Extended
= 0;
653 ureg
->nr_instructions
++;
655 return ureg
->domain
[DOMAIN_INSN
].count
- 1;
660 ureg_emit_label(struct ureg_program
*ureg
,
662 unsigned *label_token
)
664 union tgsi_any_token
*out
, *insn
;
669 out
= get_tokens( ureg
, DOMAIN_INSN
, 1 );
670 insn
= retrieve_token( ureg
, DOMAIN_INSN
, insn_token
);
672 insn
->insn
.Extended
= 1;
675 out
[0].insn_ext_label
.Type
= TGSI_INSTRUCTION_EXT_TYPE_LABEL
;
677 *label_token
= ureg
->domain
[DOMAIN_INSN
].count
- 1;
680 /* Will return a number which can be used in a label to point to the
681 * next instruction to be emitted.
684 ureg_get_instruction_number( struct ureg_program
*ureg
)
686 return ureg
->nr_instructions
;
689 /* Patch a given label (expressed as a token number) to point to a
690 * given instruction (expressed as an instruction number).
693 ureg_fixup_label(struct ureg_program
*ureg
,
694 unsigned label_token
,
695 unsigned instruction_number
)
697 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_INSN
, label_token
);
699 assert(out
->insn_ext_label
.Type
== TGSI_INSTRUCTION_EXT_TYPE_LABEL
);
700 out
->insn_ext_label
.Label
= instruction_number
;
705 ureg_emit_texture(struct ureg_program
*ureg
,
709 union tgsi_any_token
*out
, *insn
;
711 out
= get_tokens( ureg
, DOMAIN_INSN
, 1 );
712 insn
= retrieve_token( ureg
, DOMAIN_INSN
, insn_token
);
714 insn
->insn
.Extended
= 1;
717 out
[0].insn_ext_texture
.Type
= TGSI_INSTRUCTION_EXT_TYPE_TEXTURE
;
718 out
[0].insn_ext_texture
.Texture
= target
;
723 ureg_fixup_insn_size(struct ureg_program
*ureg
,
726 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_INSN
, insn
);
728 assert(out
->insn
.Type
== TGSI_TOKEN_TYPE_INSTRUCTION
);
729 out
->insn
.NrTokens
= ureg
->domain
[DOMAIN_INSN
].count
- insn
- 1;
734 ureg_insn(struct ureg_program
*ureg
,
736 const struct ureg_dst
*dst
,
738 const struct ureg_src
*src
,
744 saturate
= nr_dst
? dst
[0].Saturate
: FALSE
;
746 insn
= ureg_emit_insn( ureg
, opcode
, saturate
, nr_dst
, nr_src
);
748 for (i
= 0; i
< nr_dst
; i
++)
749 ureg_emit_dst( ureg
, dst
[i
] );
751 for (i
= 0; i
< nr_src
; i
++)
752 ureg_emit_src( ureg
, src
[i
] );
754 ureg_fixup_insn_size( ureg
, insn
);
758 ureg_tex_insn(struct ureg_program
*ureg
,
760 const struct ureg_dst
*dst
,
763 const struct ureg_src
*src
,
769 saturate
= nr_dst
? dst
[0].Saturate
: FALSE
;
771 insn
= ureg_emit_insn( ureg
, opcode
, saturate
, nr_dst
, nr_src
);
773 ureg_emit_texture( ureg
, insn
, target
); \
775 for (i
= 0; i
< nr_dst
; i
++)
776 ureg_emit_dst( ureg
, dst
[i
] );
778 for (i
= 0; i
< nr_src
; i
++)
779 ureg_emit_src( ureg
, src
[i
] );
781 ureg_fixup_insn_size( ureg
, insn
);
786 ureg_label_insn(struct ureg_program
*ureg
,
788 const struct ureg_src
*src
,
790 unsigned *label_token
)
794 insn
= ureg_emit_insn( ureg
, opcode
, FALSE
, 0, nr_src
);
796 ureg_emit_label( ureg
, insn
, label_token
); \
798 for (i
= 0; i
< nr_src
; i
++)
799 ureg_emit_src( ureg
, src
[i
] );
801 ureg_fixup_insn_size( ureg
, insn
);
806 static void emit_decl( struct ureg_program
*ureg
,
809 unsigned semantic_name
,
810 unsigned semantic_index
,
813 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 3 );
816 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
817 out
[0].decl
.NrTokens
= 3;
818 out
[0].decl
.File
= file
;
819 out
[0].decl
.UsageMask
= TGSI_WRITEMASK_XYZW
; /* FIXME! */
820 out
[0].decl
.Interpolate
= interp
;
821 out
[0].decl
.Semantic
= 1;
824 out
[1].decl_range
.First
=
825 out
[1].decl_range
.Last
= index
;
828 out
[2].decl_semantic
.SemanticName
= semantic_name
;
829 out
[2].decl_semantic
.SemanticIndex
= semantic_index
;
834 static void emit_decl_range( struct ureg_program
*ureg
,
839 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 2 );
842 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
843 out
[0].decl
.NrTokens
= 2;
844 out
[0].decl
.File
= file
;
845 out
[0].decl
.UsageMask
= 0xf;
846 out
[0].decl
.Interpolate
= TGSI_INTERPOLATE_CONSTANT
;
847 out
[0].decl
.Semantic
= 0;
850 out
[1].decl_range
.First
= first
;
851 out
[1].decl_range
.Last
= first
+ count
- 1;
854 static void emit_immediate( struct ureg_program
*ureg
,
857 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 5 );
860 out
[0].imm
.Type
= TGSI_TOKEN_TYPE_IMMEDIATE
;
861 out
[0].imm
.NrTokens
= 5;
862 out
[0].imm
.DataType
= TGSI_IMM_FLOAT32
;
863 out
[0].imm
.Padding
= 0;
864 out
[0].imm
.Extended
= 0;
866 out
[1].imm_data
.Float
= v
[0];
867 out
[2].imm_data
.Float
= v
[1];
868 out
[3].imm_data
.Float
= v
[2];
869 out
[4].imm_data
.Float
= v
[3];
875 static void emit_decls( struct ureg_program
*ureg
)
879 if (ureg
->processor
== TGSI_PROCESSOR_VERTEX
) {
880 for (i
= 0; i
< UREG_MAX_INPUT
; i
++) {
881 if (ureg
->vs_inputs
[i
/32] & (1 << (i
%32))) {
882 emit_decl_range( ureg
, TGSI_FILE_INPUT
, i
, 1 );
887 for (i
= 0; i
< ureg
->nr_fs_inputs
; i
++) {
891 ureg
->fs_input
[i
].semantic_name
,
892 ureg
->fs_input
[i
].semantic_index
,
893 ureg
->fs_input
[i
].interp
);
897 for (i
= 0; i
< ureg
->nr_outputs
; i
++) {
901 ureg
->output
[i
].semantic_name
,
902 ureg
->output
[i
].semantic_index
,
903 TGSI_INTERPOLATE_CONSTANT
);
906 for (i
= 0; i
< ureg
->nr_samplers
; i
++) {
907 emit_decl_range( ureg
,
909 ureg
->sampler
[i
].Index
, 1 );
912 if (ureg
->nr_constant_ranges
) {
913 for (i
= 0; i
< ureg
->nr_constant_ranges
; i
++)
914 emit_decl_range( ureg
,
916 ureg
->constant_range
[i
].first
,
917 (ureg
->constant_range
[i
].last
+ 1 -
918 ureg
->constant_range
[i
].first
) );
921 if (ureg
->nr_temps
) {
922 emit_decl_range( ureg
,
927 if (ureg
->nr_addrs
) {
928 emit_decl_range( ureg
,
933 for (i
= 0; i
< ureg
->nr_immediates
; i
++) {
934 emit_immediate( ureg
,
935 ureg
->immediate
[i
].v
);
939 /* Append the instruction tokens onto the declarations to build a
940 * contiguous stream suitable to send to the driver.
942 static void copy_instructions( struct ureg_program
*ureg
)
944 unsigned nr_tokens
= ureg
->domain
[DOMAIN_INSN
].count
;
945 union tgsi_any_token
*out
= get_tokens( ureg
,
950 ureg
->domain
[DOMAIN_INSN
].tokens
,
951 nr_tokens
* sizeof out
[0] );
956 fixup_header_size(struct ureg_program
*ureg
)
958 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_DECL
, 1 );
960 out
->header
.BodySize
= ureg
->domain
[DOMAIN_DECL
].count
- 3;
965 emit_header( struct ureg_program
*ureg
)
967 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 3 );
969 out
[0].version
.MajorVersion
= 1;
970 out
[0].version
.MinorVersion
= 1;
971 out
[0].version
.Padding
= 0;
973 out
[1].header
.HeaderSize
= 2;
974 out
[1].header
.BodySize
= 0;
976 out
[2].processor
.Processor
= ureg
->processor
;
977 out
[2].processor
.Padding
= 0;
981 const struct tgsi_token
*ureg_finalize( struct ureg_program
*ureg
)
983 const struct tgsi_token
*tokens
;
987 copy_instructions( ureg
);
988 fixup_header_size( ureg
);
990 if (ureg
->domain
[0].tokens
== error_tokens
||
991 ureg
->domain
[1].tokens
== error_tokens
) {
992 debug_printf("%s: error in generated shader\n", __FUNCTION__
);
997 tokens
= &ureg
->domain
[DOMAIN_DECL
].tokens
[0].token
;
1000 debug_printf("%s: emitted shader %d tokens:\n", __FUNCTION__
,
1001 ureg
->domain
[DOMAIN_DECL
].count
);
1002 tgsi_dump( tokens
, 0 );
1006 if (tokens
&& !tgsi_sanity_check(tokens
)) {
1007 debug_printf("tgsi_ureg.c, sanity check failed on generated tokens:\n");
1008 tgsi_dump(tokens
, 0);
1018 void *ureg_create_shader( struct ureg_program
*ureg
,
1019 struct pipe_context
*pipe
)
1021 struct pipe_shader_state state
;
1023 state
.tokens
= ureg_finalize(ureg
);
1027 if (ureg
->processor
== TGSI_PROCESSOR_VERTEX
)
1028 return pipe
->create_vs_state( pipe
, &state
);
1030 return pipe
->create_fs_state( pipe
, &state
);
1034 const struct tgsi_token
*ureg_get_tokens( struct ureg_program
*ureg
,
1035 unsigned *nr_tokens
)
1037 const struct tgsi_token
*tokens
;
1039 ureg_finalize(ureg
);
1041 tokens
= &ureg
->domain
[DOMAIN_DECL
].tokens
[0].token
;
1044 *nr_tokens
= ureg
->domain
[DOMAIN_DECL
].size
;
1046 ureg
->domain
[DOMAIN_DECL
].tokens
= 0;
1047 ureg
->domain
[DOMAIN_DECL
].size
= 0;
1048 ureg
->domain
[DOMAIN_DECL
].order
= 0;
1049 ureg
->domain
[DOMAIN_DECL
].count
= 0;
1055 struct ureg_program
*ureg_create( unsigned processor
)
1057 struct ureg_program
*ureg
= CALLOC_STRUCT( ureg_program
);
1061 ureg
->processor
= processor
;
1066 void ureg_destroy( struct ureg_program
*ureg
)
1070 for (i
= 0; i
< Elements(ureg
->domain
); i
++) {
1071 if (ureg
->domain
[i
].tokens
&&
1072 ureg
->domain
[i
].tokens
!= error_tokens
)
1073 FREE(ureg
->domain
[i
].tokens
);