1 /**************************************************************************
3 * Copyright 2010 Luca Barbieri
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 **************************************************************************/
27 #include "util/u_debug.h"
28 #include "pipe/p_shader_tokens.h"
29 #include "tgsi/tgsi_parse.h"
30 #include "tgsi/tgsi_scan.h"
31 #include "util/u_linkage.h"
33 /* we must only record the registers that are actually used, not just declared */
35 util_semantic_set_test_and_set(struct util_semantic_set
*set
, unsigned value
)
37 unsigned mask
= 1 << (value
% (sizeof(long) * 8));
38 unsigned long *p
= &set
->masks
[value
/ (sizeof(long) * 8)];
39 unsigned long v
= *p
& mask
;
45 util_semantic_set_from_program_file(struct util_semantic_set
*set
, const struct tgsi_token
*tokens
, enum tgsi_file_type file
)
47 struct tgsi_shader_info info
;
48 struct tgsi_parse_context parse
;
51 ubyte
*semantic_index
;
53 tgsi_scan_shader(tokens
, &info
);
55 if(file
== TGSI_FILE_INPUT
)
57 semantic_name
= info
.input_semantic_name
;
58 semantic_index
= info
.input_semantic_index
;
60 else if(file
== TGSI_FILE_OUTPUT
)
62 semantic_name
= info
.output_semantic_name
;
63 semantic_index
= info
.output_semantic_index
;
69 semantic_index
= NULL
;
72 tgsi_parse_init(&parse
, tokens
);
74 memset(set
->masks
, 0, sizeof(set
->masks
));
75 while(!tgsi_parse_end_of_tokens(&parse
))
77 tgsi_parse_token(&parse
);
79 if(parse
.FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_INSTRUCTION
)
81 const struct tgsi_full_instruction
*finst
= &parse
.FullToken
.FullInstruction
;
83 for(i
= 0; i
< finst
->Instruction
.NumDstRegs
; ++i
)
85 if(finst
->Dst
[i
].Register
.File
== file
)
87 unsigned idx
= finst
->Dst
[i
].Register
.Index
;
88 if(semantic_name
[idx
] == TGSI_SEMANTIC_GENERIC
)
90 if(!util_semantic_set_test_and_set(set
, semantic_index
[idx
]))
96 for(i
= 0; i
< finst
->Instruction
.NumSrcRegs
; ++i
)
98 if(finst
->Src
[i
].Register
.File
== file
)
100 unsigned idx
= finst
->Src
[i
].Register
.Index
;
101 if(semantic_name
[idx
] == TGSI_SEMANTIC_GENERIC
)
103 if(!util_semantic_set_test_and_set(set
, semantic_index
[idx
]))
110 tgsi_parse_free(&parse
);
115 #define UTIL_SEMANTIC_SET_FOR_EACH(i, set) for(i = 0; i < 256; ++i) if(set->masks[i / (sizeof(long) * 8)] & (1 << (i % (sizeof(long) * 8))))
118 util_semantic_layout_from_set(unsigned char *layout
, const struct util_semantic_set
*set
, unsigned efficient_slots
, unsigned num_slots
)
124 memset(layout
, 0xff, num_slots
);
126 UTIL_SEMANTIC_SET_FOR_EACH(i
, set
)
133 if(last
< efficient_slots
)
135 UTIL_SEMANTIC_SET_FOR_EACH(i
, set
)
138 else if((last
- first
) < efficient_slots
)
140 UTIL_SEMANTIC_SET_FOR_EACH(i
, set
)
141 layout
[i
- first
] = i
;
146 UTIL_SEMANTIC_SET_FOR_EACH(i
, set
)