gallium: fix up drivers for edgeflag changes
[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 case TGSI_SEMANTIC_EDGEFLAG:
81 assert(index == 0);
82 fprintf(stderr, "r300 VP: cannot handle edgeflag output\n");
83 assert(0);
84 break;
85 default:
86 assert(0);
87 }
88 }
89 }
90
91 static void r300_shader_vap_output_fmt(
92 struct r300_shader_semantics* vs_outputs,
93 uint* hwfmt)
94 {
95 int i, gen_count;
96
97 /* Do the actual vertex_info setup.
98 *
99 * vertex_info has four uints of hardware-specific data in it.
100 * vinfo.hwfmt[0] is R300_VAP_VTX_STATE_CNTL
101 * vinfo.hwfmt[1] is R300_VAP_VSM_VTX_ASSM
102 * vinfo.hwfmt[2] is R300_VAP_OUTPUT_VTX_FMT_0
103 * vinfo.hwfmt[3] is R300_VAP_OUTPUT_VTX_FMT_1 */
104
105 hwfmt[0] = 0x5555; /* XXX this is classic Mesa bonghits */
106
107 /* Position. */
108 if (vs_outputs->pos != ATTR_UNUSED) {
109 hwfmt[1] |= R300_INPUT_CNTL_POS;
110 hwfmt[2] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT;
111 } else {
112 assert(0);
113 }
114
115 /* Point size. */
116 if (vs_outputs->psize != ATTR_UNUSED) {
117 hwfmt[2] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT;
118 }
119
120 /* Colors. */
121 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
122 if (vs_outputs->color[i] != ATTR_UNUSED) {
123 hwfmt[1] |= R300_INPUT_CNTL_COLOR;
124 hwfmt[2] |= R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << i;
125 }
126 }
127
128 /* XXX Back-face colors. */
129
130 /* Texture coordinates. */
131 gen_count = 0;
132 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
133 if (vs_outputs->generic[i] != ATTR_UNUSED) {
134 hwfmt[1] |= (R300_INPUT_CNTL_TC0 << gen_count);
135 hwfmt[3] |= (4 << (3 * gen_count));
136 gen_count++;
137 }
138 }
139
140 /* Fog coordinates. */
141 if (vs_outputs->fog != ATTR_UNUSED) {
142 hwfmt[1] |= (R300_INPUT_CNTL_TC0 << gen_count);
143 hwfmt[3] |= (4 << (3 * gen_count));
144 gen_count++;
145 }
146
147 /* XXX magic */
148 assert(gen_count <= 8);
149 }
150
151 /* Set VS output stream locations for SWTCL. */
152 static void r300_stream_locations_swtcl(
153 struct r300_shader_semantics* vs_outputs,
154 int* output_stream_loc)
155 {
156 int i, tabi = 0, gen_count;
157
158 /* XXX Check whether the numbers (0, 1, 2+i, etc.) are correct.
159 * These should go to VAP_PROG_STREAM_CNTL/DST_VEC_LOC. */
160
161 /* Position. */
162 output_stream_loc[tabi++] = 0;
163
164 /* Point size. */
165 if (vs_outputs->psize != ATTR_UNUSED) {
166 output_stream_loc[tabi++] = 1;
167 }
168
169 /* Colors. */
170 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
171 if (vs_outputs->color[i] != ATTR_UNUSED) {
172 output_stream_loc[tabi++] = 2 + i;
173 }
174 }
175
176 /* Back-face colors. */
177 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
178 if (vs_outputs->bcolor[i] != ATTR_UNUSED) {
179 output_stream_loc[tabi++] = 4 + i;
180 }
181 }
182
183 /* Texture coordinates. */
184 gen_count = 0;
185 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
186 if (vs_outputs->bcolor[i] != ATTR_UNUSED) {
187 assert(tabi < 16);
188 output_stream_loc[tabi++] = 6 + gen_count;
189 gen_count++;
190 }
191 }
192
193 /* Fog coordinates. */
194 if (vs_outputs->fog != ATTR_UNUSED) {
195 assert(tabi < 16);
196 output_stream_loc[tabi++] = 6 + gen_count;
197 gen_count++;
198 }
199
200 /* XXX magic */
201 assert(gen_count <= 8);
202
203 for (; tabi < 16;) {
204 output_stream_loc[tabi++] = -1;
205 }
206 }
207
208 static void set_vertex_inputs_outputs(struct r300_vertex_program_compiler * c)
209 {
210 struct r300_vertex_shader * vs = c->UserData;
211 struct r300_shader_semantics* outputs = &vs->outputs;
212 struct tgsi_shader_info* info = &vs->info;
213 int i, reg = 0;
214
215 /* Fill in the input mapping */
216 for (i = 0; i < info->num_inputs; i++)
217 c->code->inputs[i] = i;
218
219 /* Position. */
220 if (outputs->pos != ATTR_UNUSED) {
221 c->code->outputs[outputs->pos] = reg++;
222 } else {
223 assert(0);
224 }
225
226 /* Point size. */
227 if (outputs->psize != ATTR_UNUSED) {
228 c->code->outputs[outputs->psize] = reg++;
229 }
230
231 /* Colors. */
232 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
233 if (outputs->color[i] != ATTR_UNUSED) {
234 c->code->outputs[outputs->color[i]] = reg++;
235 }
236 }
237
238 /* XXX Back-face colors. */
239
240 /* Texture coordinates. */
241 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
242 if (outputs->generic[i] != ATTR_UNUSED) {
243 c->code->outputs[outputs->generic[i]] = reg++;
244 }
245 }
246
247 /* Fog coordinates. */
248 if (outputs->fog != ATTR_UNUSED) {
249 c->code->outputs[outputs->fog] = reg++;
250 }
251 }
252
253 void r300_translate_vertex_shader(struct r300_context* r300,
254 struct r300_vertex_shader* vs)
255 {
256 struct r300_vertex_program_compiler compiler;
257 struct tgsi_to_rc ttr;
258
259 /* Initialize. */
260 r300_shader_read_vs_outputs(&vs->info, &vs->outputs);
261 r300_shader_vap_output_fmt(&vs->outputs, vs->hwfmt);
262
263 if (!r300_screen(r300->context.screen)->caps->has_tcl) {
264 r300_stream_locations_swtcl(&vs->outputs, vs->output_stream_loc_swtcl);
265 }
266
267 /* Setup the compiler */
268 rc_init(&compiler.Base);
269
270 compiler.Base.Debug = DBG_ON(r300, DBG_VP);
271 compiler.code = &vs->code;
272 compiler.UserData = vs;
273
274 if (compiler.Base.Debug) {
275 debug_printf("r300: Initial vertex program\n");
276 tgsi_dump(vs->state.tokens, 0);
277 }
278
279 /* Translate TGSI to our internal representation */
280 ttr.compiler = &compiler.Base;
281 ttr.info = &vs->info;
282
283 r300_tgsi_to_rc(&ttr, vs->state.tokens);
284
285 compiler.RequiredOutputs = ~(~0 << vs->info.num_outputs);
286 compiler.SetHwInputOutput = &set_vertex_inputs_outputs;
287
288 /* Invoke the compiler */
289 r3xx_compile_vertex_program(&compiler);
290 if (compiler.Base.Error) {
291 /* XXX Fail gracefully */
292 fprintf(stderr, "r300 VP: Compiler error\n");
293 abort();
294 }
295
296 /* And, finally... */
297 rc_destroy(&compiler.Base);
298 vs->translated = TRUE;
299 }