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