Merge commit 'origin/7.8'
[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 /* Convert info about VS output semantics into r300_shader_semantics. */
38 static void r300_shader_read_vs_outputs(
39 struct tgsi_shader_info* info,
40 struct r300_shader_semantics* vs_outputs)
41 {
42 int i;
43 unsigned index;
44
45 r300_shader_semantics_reset(vs_outputs);
46
47 for (i = 0; i < info->num_outputs; i++) {
48 index = info->output_semantic_index[i];
49
50 switch (info->output_semantic_name[i]) {
51 case TGSI_SEMANTIC_POSITION:
52 assert(index == 0);
53 vs_outputs->pos = i;
54 break;
55
56 case TGSI_SEMANTIC_PSIZE:
57 assert(index == 0);
58 vs_outputs->psize = i;
59 break;
60
61 case TGSI_SEMANTIC_COLOR:
62 assert(index < ATTR_COLOR_COUNT);
63 vs_outputs->color[index] = i;
64 break;
65
66 case TGSI_SEMANTIC_BCOLOR:
67 assert(index < ATTR_COLOR_COUNT);
68 vs_outputs->bcolor[index] = i;
69 break;
70
71 case TGSI_SEMANTIC_GENERIC:
72 assert(index < ATTR_GENERIC_COUNT);
73 vs_outputs->generic[index] = i;
74 break;
75
76 case TGSI_SEMANTIC_FOG:
77 assert(index == 0);
78 vs_outputs->fog = i;
79 break;
80
81 case TGSI_SEMANTIC_EDGEFLAG:
82 assert(index == 0);
83 fprintf(stderr, "r300 VP: cannot handle edgeflag output.\n");
84 break;
85
86 default:
87 fprintf(stderr, "r300 VP: unknown vertex output semantic: %i.\n",
88 info->output_semantic_name[i]);
89 }
90 }
91
92 /* WPOS is a straight copy of POSITION and it's always emitted. */
93 vs_outputs->wpos = i;
94 }
95
96 /* This function sets up:
97 * - VAP mapping, which maps VS registers to output semantics and
98 * at the same time it indicates which attributes are enabled and should
99 * be rasterized.
100 * - Stream mapping to VS outputs if TCL is not present. */
101 static void r300_init_vs_output_mapping(struct r300_vertex_shader* vs)
102 {
103 struct r300_shader_semantics* vs_outputs = &vs->outputs;
104 struct r300_vap_output_state *vap_out = &vs->vap_out;
105 int *stream_loc = vs->stream_loc_notcl;
106 int i, gen_count, tabi = 0;
107 boolean any_bcolor_used = vs_outputs->bcolor[0] != ATTR_UNUSED ||
108 vs_outputs->bcolor[1] != ATTR_UNUSED;
109
110 vap_out->vap_vtx_state_cntl = 0x5555; /* XXX this is classic Mesa bonghits */
111
112 /* Position. */
113 if (vs_outputs->pos != ATTR_UNUSED) {
114 vap_out->vap_vsm_vtx_assm |= R300_INPUT_CNTL_POS;
115 vap_out->vap_out_vtx_fmt[0] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT;
116
117 stream_loc[tabi++] = 0;
118 } else {
119 assert(0);
120 }
121
122 /* Point size. */
123 if (vs_outputs->psize != ATTR_UNUSED) {
124 vap_out->vap_out_vtx_fmt[0] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT;
125
126 stream_loc[tabi++] = 1;
127 }
128
129 /* Colors. */
130 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
131 if (vs_outputs->color[i] != ATTR_UNUSED || any_bcolor_used ||
132 vs_outputs->color[1] != ATTR_UNUSED) {
133 vap_out->vap_vsm_vtx_assm |= R300_INPUT_CNTL_COLOR;
134 vap_out->vap_out_vtx_fmt[0] |= R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << i;
135
136 stream_loc[tabi++] = 2 + i;
137 }
138 }
139
140 /* Back-face colors. */
141 if (any_bcolor_used) {
142 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
143 vap_out->vap_vsm_vtx_assm |= R300_INPUT_CNTL_COLOR;
144 vap_out->vap_out_vtx_fmt[0] |= R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << (2+i);
145
146 stream_loc[tabi++] = 4 + i;
147 }
148 }
149
150 /* Texture coordinates. */
151 gen_count = 0;
152 for (i = 0; i < ATTR_GENERIC_COUNT && gen_count < 8; i++) {
153 if (vs_outputs->generic[i] != ATTR_UNUSED) {
154 vap_out->vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << gen_count);
155 vap_out->vap_out_vtx_fmt[1] |= (4 << (3 * gen_count));
156
157 stream_loc[tabi++] = 6 + gen_count;
158 gen_count++;
159 }
160 }
161
162 /* Fog coordinates. */
163 if (gen_count < 8 && vs_outputs->fog != ATTR_UNUSED) {
164 vap_out->vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << gen_count);
165 vap_out->vap_out_vtx_fmt[1] |= (4 << (3 * gen_count));
166
167 stream_loc[tabi++] = 6 + gen_count;
168 gen_count++;
169 }
170
171 /* WPOS. */
172 if (gen_count < 8) {
173 vs->wpos_tex_output = gen_count;
174 stream_loc[tabi++] = 6 + gen_count;
175 } else {
176 vs_outputs->wpos = ATTR_UNUSED;
177 }
178
179 for (; tabi < 16;) {
180 stream_loc[tabi++] = -1;
181 }
182 }
183
184 static void set_vertex_inputs_outputs(struct r300_vertex_program_compiler * c)
185 {
186 struct r300_vertex_shader * vs = c->UserData;
187 struct r300_shader_semantics* outputs = &vs->outputs;
188 struct tgsi_shader_info* info = &vs->info;
189 int i, reg = 0;
190 boolean any_bcolor_used = outputs->bcolor[0] != ATTR_UNUSED ||
191 outputs->bcolor[1] != ATTR_UNUSED;
192
193 /* Fill in the input mapping */
194 for (i = 0; i < info->num_inputs; i++)
195 c->code->inputs[i] = i;
196
197 /* Position. */
198 if (outputs->pos != ATTR_UNUSED) {
199 c->code->outputs[outputs->pos] = reg++;
200 } else {
201 assert(0);
202 }
203
204 /* Point size. */
205 if (outputs->psize != ATTR_UNUSED) {
206 c->code->outputs[outputs->psize] = reg++;
207 }
208
209 /* If we're writing back facing colors we need to send
210 * four colors to make front/back face colors selection work.
211 * If the vertex program doesn't write all 4 colors, lets
212 * pretend it does by skipping output index reg so the colors
213 * get written into appropriate output vectors.
214 */
215
216 /* Colors. */
217 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
218 if (outputs->color[i] != ATTR_UNUSED) {
219 c->code->outputs[outputs->color[i]] = reg++;
220 } else if (any_bcolor_used ||
221 outputs->color[1] != ATTR_UNUSED) {
222 reg++;
223 }
224 }
225
226 /* Back-face colors. */
227 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
228 if (outputs->bcolor[i] != ATTR_UNUSED) {
229 c->code->outputs[outputs->bcolor[i]] = reg++;
230 } else if (any_bcolor_used) {
231 reg++;
232 }
233 }
234
235 /* Texture coordinates. */
236 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
237 if (outputs->generic[i] != ATTR_UNUSED) {
238 c->code->outputs[outputs->generic[i]] = reg++;
239 }
240 }
241
242 /* Fog coordinates. */
243 if (outputs->fog != ATTR_UNUSED) {
244 c->code->outputs[outputs->fog] = reg++;
245 }
246
247 /* WPOS. */
248 if (outputs->wpos != ATTR_UNUSED) {
249 c->code->outputs[outputs->wpos] = reg++;
250 }
251 }
252
253 void r300_vertex_shader_common_init(struct r300_vertex_shader *vs,
254 const struct pipe_shader_state *shader)
255 {
256 /* Copy state directly into shader. */
257 vs->state = *shader;
258 vs->state.tokens = tgsi_dup_tokens(shader->tokens);
259 tgsi_scan_shader(shader->tokens, &vs->info);
260
261 r300_shader_read_vs_outputs(&vs->info, &vs->outputs);
262 r300_init_vs_output_mapping(vs);
263 }
264
265 void r300_translate_vertex_shader(struct r300_context* r300,
266 struct r300_vertex_shader* vs)
267 {
268 struct r300_vertex_program_compiler compiler;
269 struct tgsi_to_rc ttr;
270
271 /* Setup the compiler */
272 rc_init(&compiler.Base);
273
274 compiler.Base.Debug = DBG_ON(r300, DBG_VP);
275 compiler.code = &vs->code;
276 compiler.UserData = vs;
277
278 if (compiler.Base.Debug) {
279 debug_printf("r300: Initial vertex program\n");
280 tgsi_dump(vs->state.tokens, 0);
281 }
282
283 /* Translate TGSI to our internal representation */
284 ttr.compiler = &compiler.Base;
285 ttr.info = &vs->info;
286 ttr.use_half_swizzles = FALSE;
287
288 r300_tgsi_to_rc(&ttr, vs->state.tokens);
289
290 compiler.RequiredOutputs = ~(~0 << (vs->info.num_outputs+1));
291 compiler.SetHwInputOutput = &set_vertex_inputs_outputs;
292
293 /* Insert the WPOS output. */
294 if (vs->outputs.wpos != ATTR_UNUSED) {
295 rc_copy_output(&compiler.Base, 0, vs->outputs.wpos);
296 }
297
298 /* Invoke the compiler */
299 r3xx_compile_vertex_program(&compiler);
300 if (compiler.Base.Error) {
301 /* XXX We should fallback using Draw. */
302 fprintf(stderr, "r300 VP: Compiler error:\n%s", compiler.Base.ErrorMsg);
303 abort();
304 }
305
306 /* And, finally... */
307 rc_destroy(&compiler.Base);
308 }
309
310 boolean r300_vertex_shader_setup_wpos(struct r300_context* r300)
311 {
312 struct r300_vertex_shader* vs = r300->vs_state.state;
313 struct r300_vap_output_state *vap_out = &vs->vap_out;
314 int tex_output = vs->wpos_tex_output;
315 uint32_t tex_fmt = R300_INPUT_CNTL_TC0 << tex_output;
316
317 if (vs->outputs.wpos == ATTR_UNUSED) {
318 return FALSE;
319 }
320
321 if (r300->fs->inputs.wpos != ATTR_UNUSED) {
322 /* Enable WPOS in VAP. */
323 if (!(vap_out->vap_vsm_vtx_assm & tex_fmt)) {
324 vap_out->vap_vsm_vtx_assm |= tex_fmt;
325 vap_out->vap_out_vtx_fmt[1] |= (4 << (3 * tex_output));
326 return TRUE;
327 }
328 } else {
329 /* Disable WPOS in VAP. */
330 if (vap_out->vap_vsm_vtx_assm & tex_fmt) {
331 vap_out->vap_vsm_vtx_assm &= ~tex_fmt;
332 vap_out->vap_out_vtx_fmt[1] &= ~(4 << (3 * tex_output));
333 return TRUE;
334 }
335 }
336 return FALSE;
337 }