cb4b6ee71e720667d07b613042dd1a994878c492
[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 #include "r300_fs.h"
26
27 #include "r300_context.h"
28 #include "r300_screen.h"
29 #include "r300_tgsi_to_rc.h"
30 #include "r300_reg.h"
31
32 #include "tgsi/tgsi_dump.h"
33 #include "tgsi/tgsi_parse.h"
34
35 #include "radeon_compiler.h"
36
37 #include "util/u_math.h"
38
39 /* Convert info about VS output semantics into r300_shader_semantics. */
40 static void r300_shader_read_vs_outputs(
41 struct tgsi_shader_info* info,
42 struct r300_shader_semantics* vs_outputs)
43 {
44 int i;
45 unsigned index;
46
47 r300_shader_semantics_reset(vs_outputs);
48
49 for (i = 0; i < info->num_outputs; i++) {
50 index = info->output_semantic_index[i];
51
52 switch (info->output_semantic_name[i]) {
53 case TGSI_SEMANTIC_POSITION:
54 assert(index == 0);
55 vs_outputs->pos = i;
56 break;
57
58 case TGSI_SEMANTIC_PSIZE:
59 assert(index == 0);
60 vs_outputs->psize = i;
61 break;
62
63 case TGSI_SEMANTIC_COLOR:
64 assert(index <= ATTR_COLOR_COUNT);
65 vs_outputs->color[index] = i;
66 break;
67
68 case TGSI_SEMANTIC_BCOLOR:
69 assert(index <= ATTR_COLOR_COUNT);
70 vs_outputs->bcolor[index] = i;
71 break;
72
73 case TGSI_SEMANTIC_GENERIC:
74 assert(index <= ATTR_GENERIC_COUNT);
75 vs_outputs->generic[index] = i;
76 break;
77
78 case TGSI_SEMANTIC_FOG:
79 assert(index == 0);
80 vs_outputs->fog = i;
81 break;
82
83 case TGSI_SEMANTIC_EDGEFLAG:
84 assert(index == 0);
85 fprintf(stderr, "r300 VP: cannot handle edgeflag output\n");
86 assert(0);
87 break;
88 default:
89 assert(0);
90 }
91 }
92 }
93
94 static void r300_shader_vap_output_fmt(struct r300_vertex_shader* vs)
95 {
96 struct r300_shader_semantics* vs_outputs = &vs->outputs;
97 uint32_t* hwfmt = vs->hwfmt;
98 int i, gen_count;
99
100 /* Do the actual vertex_info setup.
101 *
102 * vertex_info has four uints of hardware-specific data in it.
103 * vinfo.hwfmt[0] is R300_VAP_VTX_STATE_CNTL
104 * vinfo.hwfmt[1] is R300_VAP_VSM_VTX_ASSM
105 * vinfo.hwfmt[2] is R300_VAP_OUTPUT_VTX_FMT_0
106 * vinfo.hwfmt[3] is R300_VAP_OUTPUT_VTX_FMT_1 */
107
108 hwfmt[0] = 0x5555; /* XXX this is classic Mesa bonghits */
109
110 /* Position. */
111 if (vs_outputs->pos != ATTR_UNUSED) {
112 hwfmt[1] |= R300_INPUT_CNTL_POS;
113 hwfmt[2] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT;
114 } else {
115 assert(0);
116 }
117
118 /* Point size. */
119 if (vs_outputs->psize != ATTR_UNUSED) {
120 hwfmt[2] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT;
121 }
122
123 /* Colors. */
124 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
125 if (vs_outputs->color[i] != ATTR_UNUSED) {
126 hwfmt[1] |= R300_INPUT_CNTL_COLOR;
127 hwfmt[2] |= R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << i;
128 }
129 }
130
131 /* XXX Back-face colors. */
132
133 /* Texture coordinates. */
134 gen_count = 0;
135 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
136 if (vs_outputs->generic[i] != ATTR_UNUSED) {
137 hwfmt[1] |= (R300_INPUT_CNTL_TC0 << gen_count);
138 hwfmt[3] |= (4 << (3 * gen_count));
139 gen_count++;
140 }
141 }
142
143 /* Fog coordinates. */
144 if (vs_outputs->fog != ATTR_UNUSED) {
145 hwfmt[1] |= (R300_INPUT_CNTL_TC0 << gen_count);
146 hwfmt[3] |= (4 << (3 * gen_count));
147 gen_count++;
148 }
149
150 /* XXX magic */
151 assert(gen_count <= 8);
152
153 /* WPOS. */
154 vs->wpos_tex_output = gen_count;
155 }
156
157 /* Sets up stream mapping to equivalent VS outputs if TCL is bypassed
158 * or isn't present. */
159 static void r300_stream_locations_notcl(
160 struct r300_shader_semantics* vs_outputs,
161 int* stream_loc)
162 {
163 int i, tabi = 0, gen_count;
164
165 /* Position. */
166 stream_loc[tabi++] = 0;
167
168 /* Point size. */
169 if (vs_outputs->psize != ATTR_UNUSED) {
170 stream_loc[tabi++] = 1;
171 }
172
173 /* Colors. */
174 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
175 if (vs_outputs->color[i] != ATTR_UNUSED) {
176 stream_loc[tabi++] = 2 + i;
177 }
178 }
179
180 /* Back-face colors. */
181 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
182 if (vs_outputs->bcolor[i] != ATTR_UNUSED) {
183 stream_loc[tabi++] = 4 + i;
184 }
185 }
186
187 /* Texture coordinates. */
188 gen_count = 0;
189 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
190 if (vs_outputs->generic[i] != ATTR_UNUSED) {
191 assert(tabi < 16);
192 stream_loc[tabi++] = 6 + gen_count;
193 gen_count++;
194 }
195 }
196
197 /* Fog coordinates. */
198 if (vs_outputs->fog != ATTR_UNUSED) {
199 assert(tabi < 16);
200 stream_loc[tabi++] = 6 + gen_count;
201 gen_count++;
202 }
203
204 /* WPOS. */
205 if (vs_outputs->wpos != ATTR_UNUSED) {
206 assert(tabi < 16);
207 stream_loc[tabi++] = 6 + gen_count;
208 gen_count++;
209 }
210
211 for (; tabi < 16;) {
212 stream_loc[tabi++] = -1;
213 }
214 }
215
216 static void set_vertex_inputs_outputs(struct r300_vertex_program_compiler * c)
217 {
218 struct r300_vertex_shader * vs = c->UserData;
219 struct r300_shader_semantics* outputs = &vs->outputs;
220 struct tgsi_shader_info* info = &vs->info;
221 int i, reg = 0;
222
223 /* Fill in the input mapping */
224 for (i = 0; i < info->num_inputs; i++)
225 c->code->inputs[i] = i;
226
227 /* Position. */
228 if (outputs->pos != ATTR_UNUSED) {
229 c->code->outputs[outputs->pos] = reg++;
230 } else {
231 assert(0);
232 }
233
234 /* Point size. */
235 if (outputs->psize != ATTR_UNUSED) {
236 c->code->outputs[outputs->psize] = reg++;
237 }
238
239 /* Colors. */
240 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
241 if (outputs->color[i] != ATTR_UNUSED) {
242 c->code->outputs[outputs->color[i]] = reg++;
243 }
244 }
245
246 /* XXX Back-face colors. */
247
248 /* Texture coordinates. */
249 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
250 if (outputs->generic[i] != ATTR_UNUSED) {
251 c->code->outputs[outputs->generic[i]] = reg++;
252 }
253 }
254
255 /* Fog coordinates. */
256 if (outputs->fog != ATTR_UNUSED) {
257 c->code->outputs[outputs->fog] = reg++;
258 }
259
260 /* WPOS. */
261 if (outputs->wpos != ATTR_UNUSED) {
262 c->code->outputs[outputs->wpos] = reg++;
263 }
264 }
265
266 static void r300_insert_wpos(struct r300_vertex_program_compiler* c,
267 struct r300_shader_semantics* outputs)
268 {
269 int i, lastOutput = 0;
270
271 /* Find the max output index. */
272 lastOutput = MAX2(lastOutput, outputs->psize);
273 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
274 lastOutput = MAX2(lastOutput, outputs->color[i]);
275 lastOutput = MAX2(lastOutput, outputs->bcolor[i]);
276 }
277 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
278 lastOutput = MAX2(lastOutput, outputs->generic[i]);
279 }
280 lastOutput = MAX2(lastOutput, outputs->fog);
281
282 /* Set WPOS after the last output. */
283 lastOutput++;
284 rc_copy_output(&c->Base, 0, lastOutput); /* out[lastOutput] = out[0]; */
285 outputs->wpos = lastOutput;
286 }
287
288 void r300_translate_vertex_shader(struct r300_context* r300,
289 struct r300_vertex_shader* vs)
290 {
291 struct r300_vertex_program_compiler compiler;
292 struct tgsi_to_rc ttr;
293
294 /* Initialize. */
295 r300_shader_read_vs_outputs(&vs->info, &vs->outputs);
296
297 /* Setup the compiler */
298 rc_init(&compiler.Base);
299
300 compiler.Base.Debug = DBG_ON(r300, DBG_VP);
301 compiler.code = &vs->code;
302 compiler.UserData = vs;
303
304 if (compiler.Base.Debug) {
305 debug_printf("r300: Initial vertex program\n");
306 tgsi_dump(vs->state.tokens, 0);
307 }
308
309 /* Translate TGSI to our internal representation */
310 ttr.compiler = &compiler.Base;
311 ttr.info = &vs->info;
312
313 r300_tgsi_to_rc(&ttr, vs->state.tokens);
314
315 compiler.RequiredOutputs = ~(~0 << (vs->info.num_outputs+1));
316 compiler.SetHwInputOutput = &set_vertex_inputs_outputs;
317
318 /* Insert the WPOS output. */
319 r300_insert_wpos(&compiler, &vs->outputs);
320
321 r300_shader_vap_output_fmt(vs);
322 r300_stream_locations_notcl(&vs->outputs, vs->stream_loc_notcl);
323
324 /* Invoke the compiler */
325 r3xx_compile_vertex_program(&compiler);
326 if (compiler.Base.Error) {
327 /* XXX We should fallback using Draw. */
328 fprintf(stderr, "r300 VP: Compiler error\n");
329 abort();
330 }
331
332 /* And, finally... */
333 rc_destroy(&compiler.Base);
334 vs->translated = TRUE;
335 }
336
337 boolean r300_vertex_shader_setup_wpos(struct r300_context* r300)
338 {
339 struct r300_vertex_shader* vs = r300->vs;
340 int tex_output = r300->vs->wpos_tex_output;
341 uint32_t tex_fmt = R300_INPUT_CNTL_TC0 << tex_output;
342 uint32_t* hwfmt = vs->hwfmt;
343
344 if (r300->fs->inputs.wpos != ATTR_UNUSED) {
345 /* Enable WPOS in VAP. */
346 if (!(hwfmt[1] & tex_fmt)) {
347 hwfmt[1] |= tex_fmt;
348 hwfmt[3] |= (4 << (3 * tex_output));
349
350 assert(tex_output < 8);
351 return TRUE;
352 }
353 } else {
354 /* Disable WPOS in VAP. */
355 if (hwfmt[1] & tex_fmt) {
356 hwfmt[1] &= ~tex_fmt;
357 hwfmt[3] &= ~(4 << (3 * tex_output));
358 return TRUE;
359 }
360 }
361 return FALSE;
362 }