r300g: clean up derived states
[mesa.git] / src / gallium / drivers / r300 / r300_vs.c
1 /*
2 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24 #include "r300_vs.h"
25
26 #include "r300_context.h"
27 #include "r300_screen.h"
28 #include "r300_tgsi_to_rc.h"
29 #include "r300_reg.h"
30
31 #include "tgsi/tgsi_dump.h"
32 #include "tgsi/tgsi_parse.h"
33
34 #include "radeon_compiler.h"
35
36 /* Convert info about VS output semantics into r300_shader_semantics. */
37 static void r300_shader_read_vs_outputs(
38 struct tgsi_shader_info* info,
39 struct r300_shader_semantics* vs_outputs)
40 {
41 int i;
42 unsigned index;
43
44 r300_shader_semantics_reset(vs_outputs);
45
46 for (i = 0; i < info->num_outputs; i++) {
47 index = info->output_semantic_index[i];
48
49 switch (info->output_semantic_name[i]) {
50 case TGSI_SEMANTIC_POSITION:
51 assert(index == 0);
52 vs_outputs->pos = i;
53 break;
54
55 case TGSI_SEMANTIC_PSIZE:
56 assert(index == 0);
57 vs_outputs->psize = i;
58 break;
59
60 case TGSI_SEMANTIC_COLOR:
61 assert(index <= ATTR_COLOR_COUNT);
62 vs_outputs->color[index] = i;
63 break;
64
65 case TGSI_SEMANTIC_BCOLOR:
66 assert(index <= ATTR_COLOR_COUNT);
67 vs_outputs->bcolor[index] = i;
68 break;
69
70 case TGSI_SEMANTIC_GENERIC:
71 assert(index <= ATTR_GENERIC_COUNT);
72 vs_outputs->generic[index] = i;
73 break;
74
75 case TGSI_SEMANTIC_FOG:
76 assert(index == 0);
77 vs_outputs->fog = i;
78 break;
79
80 default:
81 assert(0);
82 }
83 }
84 }
85
86 static void r300_shader_vap_output_fmt(
87 struct r300_shader_semantics* vs_outputs,
88 uint* hwfmt)
89 {
90 int i, gen_count;
91
92 /* Do the actual vertex_info setup.
93 *
94 * vertex_info has four uints of hardware-specific data in it.
95 * vinfo.hwfmt[0] is R300_VAP_VTX_STATE_CNTL
96 * vinfo.hwfmt[1] is R300_VAP_VSM_VTX_ASSM
97 * vinfo.hwfmt[2] is R300_VAP_OUTPUT_VTX_FMT_0
98 * vinfo.hwfmt[3] is R300_VAP_OUTPUT_VTX_FMT_1 */
99
100 hwfmt[0] = 0x5555; /* XXX this is classic Mesa bonghits */
101
102 /* Position. */
103 if (vs_outputs->pos != ATTR_UNUSED) {
104 hwfmt[1] |= R300_INPUT_CNTL_POS;
105 hwfmt[2] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT;
106 } else {
107 assert(0);
108 }
109
110 /* Point size. */
111 if (vs_outputs->psize != ATTR_UNUSED) {
112 hwfmt[2] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT;
113 }
114
115 /* Colors. */
116 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
117 if (vs_outputs->color[i] != ATTR_UNUSED) {
118 hwfmt[1] |= R300_INPUT_CNTL_COLOR;
119 hwfmt[2] |= R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << i;
120 }
121 }
122
123 /* XXX Back-face colors. */
124
125 /* Texture coordinates. */
126 gen_count = 0;
127 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
128 if (vs_outputs->generic[i] != ATTR_UNUSED) {
129 hwfmt[1] |= (R300_INPUT_CNTL_TC0 << gen_count);
130 hwfmt[3] |= (4 << (3 * gen_count));
131 gen_count++;
132 }
133 }
134
135 /* Fog coordinates. */
136 if (vs_outputs->fog != ATTR_UNUSED) {
137 hwfmt[1] |= (R300_INPUT_CNTL_TC0 << gen_count);
138 hwfmt[3] |= (4 << (3 * gen_count));
139 gen_count++;
140 }
141
142 /* XXX magic */
143 assert(gen_count <= 8);
144 }
145
146 /* Set VS output stream locations for SWTCL. */
147 static void r300_stream_locations_swtcl(
148 struct r300_shader_semantics* vs_outputs,
149 int* output_stream_loc)
150 {
151 int i, tabi = 0, gen_count;
152
153 /* XXX Check whether the numbers (0, 1, 2+i, etc.) are correct.
154 * These should go to VAP_PROG_STREAM_CNTL/DST_VEC_LOC. */
155
156 /* Position. */
157 output_stream_loc[tabi++] = 0;
158
159 /* Point size. */
160 if (vs_outputs->psize != ATTR_UNUSED) {
161 output_stream_loc[tabi++] = 1;
162 }
163
164 /* Colors. */
165 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
166 if (vs_outputs->color[i] != ATTR_UNUSED) {
167 output_stream_loc[tabi++] = 2 + i;
168 }
169 }
170
171 /* Back-face colors. */
172 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
173 if (vs_outputs->bcolor[i] != ATTR_UNUSED) {
174 output_stream_loc[tabi++] = 4 + i;
175 }
176 }
177
178 /* Texture coordinates. */
179 gen_count = 0;
180 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
181 if (vs_outputs->bcolor[i] != ATTR_UNUSED) {
182 assert(tabi < 16);
183 output_stream_loc[tabi++] = 6 + gen_count;
184 gen_count++;
185 }
186 }
187
188 /* Fog coordinates. */
189 if (vs_outputs->fog != ATTR_UNUSED) {
190 assert(tabi < 16);
191 output_stream_loc[tabi++] = 6 + gen_count;
192 gen_count++;
193 }
194
195 /* XXX magic */
196 assert(gen_count <= 8);
197
198 for (; tabi < 16;) {
199 output_stream_loc[tabi++] = -1;
200 }
201 }
202
203 static void set_vertex_inputs_outputs(struct r300_vertex_program_compiler * c)
204 {
205 struct r300_vertex_shader * vs = c->UserData;
206 struct tgsi_shader_info* info = &vs->info;
207 struct tgsi_parse_context parser;
208 struct tgsi_full_declaration * decl;
209 boolean pointsize = FALSE;
210 int out_colors = 0;
211 int colors = 0;
212 int out_generic = 0;
213 int generic = 0;
214 int i;
215
216 /* Fill in the input mapping */
217 for (i = 0; i < info->num_inputs; i++)
218 c->code->inputs[i] = i;
219
220 /* Fill in the output mapping */
221 for (i = 0; i < info->num_outputs; i++) {
222 switch (info->output_semantic_name[i]) {
223 case TGSI_SEMANTIC_PSIZE:
224 pointsize = TRUE;
225 break;
226 case TGSI_SEMANTIC_COLOR:
227 out_colors++;
228 break;
229 case TGSI_SEMANTIC_FOG:
230 case TGSI_SEMANTIC_GENERIC:
231 out_generic++;
232 break;
233 }
234 }
235
236 tgsi_parse_init(&parser, vs->state.tokens);
237
238 while (!tgsi_parse_end_of_tokens(&parser)) {
239 tgsi_parse_token(&parser);
240
241 if (parser.FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
242 continue;
243
244 decl = &parser.FullToken.FullDeclaration;
245
246 if (decl->Declaration.File != TGSI_FILE_OUTPUT)
247 continue;
248
249 switch (decl->Semantic.SemanticName) {
250 case TGSI_SEMANTIC_POSITION:
251 c->code->outputs[decl->DeclarationRange.First] = 0;
252 break;
253 case TGSI_SEMANTIC_PSIZE:
254 c->code->outputs[decl->DeclarationRange.First] = 1;
255 break;
256 case TGSI_SEMANTIC_COLOR:
257 c->code->outputs[decl->DeclarationRange.First] = 1 +
258 (pointsize ? 1 : 0) +
259 colors++;
260 break;
261 case TGSI_SEMANTIC_FOG:
262 case TGSI_SEMANTIC_GENERIC:
263 c->code->outputs[decl->DeclarationRange.First] = 1 +
264 (pointsize ? 1 : 0) +
265 out_colors +
266 generic++;
267 break;
268 default:
269 debug_printf("r300: vs: Bad semantic declaration %d\n",
270 decl->Semantic.SemanticName);
271 assert(0);
272 }
273 }
274
275 tgsi_parse_free(&parser);
276 }
277
278 void r300_translate_vertex_shader(struct r300_context* r300,
279 struct r300_vertex_shader* vs)
280 {
281 struct r300_vertex_program_compiler compiler;
282 struct tgsi_to_rc ttr;
283
284 /* Initialize. */
285 r300_shader_read_vs_outputs(&vs->info, &vs->outputs);
286 r300_shader_vap_output_fmt(&vs->outputs, vs->hwfmt);
287
288 if (!r300_screen(r300->context.screen)->caps->has_tcl) {
289 r300_stream_locations_swtcl(&vs->outputs, vs->output_stream_loc_swtcl);
290 }
291
292 /* Setup the compiler */
293 rc_init(&compiler.Base);
294
295 compiler.Base.Debug = DBG_ON(r300, DBG_VP);
296 compiler.code = &vs->code;
297 compiler.UserData = vs;
298
299 if (compiler.Base.Debug) {
300 debug_printf("r300: Initial vertex program\n");
301 tgsi_dump(vs->state.tokens, 0);
302 }
303
304 /* Translate TGSI to our internal representation */
305 ttr.compiler = &compiler.Base;
306 ttr.info = &vs->info;
307
308 r300_tgsi_to_rc(&ttr, vs->state.tokens);
309
310 compiler.RequiredOutputs = ~(~0 << vs->info.num_outputs);
311 compiler.SetHwInputOutput = &set_vertex_inputs_outputs;
312
313 /* Invoke the compiler */
314 r3xx_compile_vertex_program(&compiler);
315 if (compiler.Base.Error) {
316 /* XXX Fail gracefully */
317 fprintf(stderr, "r300 VP: Compiler error\n");
318 abort();
319 }
320
321 /* And, finally... */
322 rc_destroy(&compiler.Base);
323 vs->translated = TRUE;
324 }