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
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 /* 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; 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 assert(tabi < 16);
159 stream_loc[tabi++] = 6 + gen_count;
160 gen_count++;
161 }
162 }
163
164 /* Fog coordinates. */
165 if (vs_outputs->fog != ATTR_UNUSED) {
166 vap_out->vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << gen_count);
167 vap_out->vap_out_vtx_fmt[1] |= (4 << (3 * gen_count));
168
169 assert(tabi < 16);
170 stream_loc[tabi++] = 6 + gen_count;
171 gen_count++;
172 }
173
174 /* XXX magic */
175 assert(gen_count <= 8);
176
177 /* WPOS. */
178 vs->wpos_tex_output = gen_count;
179
180 assert(tabi < 16);
181 stream_loc[tabi++] = 6 + gen_count;
182
183 for (; tabi < 16;) {
184 stream_loc[tabi++] = -1;
185 }
186 }
187
188 static void set_vertex_inputs_outputs(struct r300_vertex_program_compiler * c)
189 {
190 struct r300_vertex_shader * vs = c->UserData;
191 struct r300_shader_semantics* outputs = &vs->outputs;
192 struct tgsi_shader_info* info = &vs->info;
193 int i, reg = 0;
194 boolean any_bcolor_used = outputs->bcolor[0] != ATTR_UNUSED ||
195 outputs->bcolor[1] != ATTR_UNUSED;
196
197 /* Fill in the input mapping */
198 for (i = 0; i < info->num_inputs; i++)
199 c->code->inputs[i] = i;
200
201 /* Position. */
202 if (outputs->pos != ATTR_UNUSED) {
203 c->code->outputs[outputs->pos] = reg++;
204 } else {
205 assert(0);
206 }
207
208 /* Point size. */
209 if (outputs->psize != ATTR_UNUSED) {
210 c->code->outputs[outputs->psize] = reg++;
211 }
212
213 /* If we're writing back facing colors we need to send
214 * four colors to make front/back face colors selection work.
215 * If the vertex program doesn't write all 4 colors, lets
216 * pretend it does by skipping output index reg so the colors
217 * get written into appropriate output vectors.
218 */
219
220 /* Colors. */
221 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
222 if (outputs->color[i] != ATTR_UNUSED) {
223 c->code->outputs[outputs->color[i]] = reg++;
224 } else if (any_bcolor_used ||
225 outputs->color[1] != ATTR_UNUSED) {
226 reg++;
227 }
228 }
229
230 /* Back-face colors. */
231 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
232 if (outputs->bcolor[i] != ATTR_UNUSED) {
233 c->code->outputs[outputs->bcolor[i]] = reg++;
234 } else if (any_bcolor_used) {
235 reg++;
236 }
237 }
238
239 /* Texture coordinates. */
240 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
241 if (outputs->generic[i] != ATTR_UNUSED) {
242 c->code->outputs[outputs->generic[i]] = reg++;
243 }
244 }
245
246 /* Fog coordinates. */
247 if (outputs->fog != ATTR_UNUSED) {
248 c->code->outputs[outputs->fog] = reg++;
249 }
250
251 /* WPOS. */
252 if (outputs->wpos != ATTR_UNUSED) {
253 c->code->outputs[outputs->wpos] = reg++;
254 }
255 }
256
257 void r300_vertex_shader_common_init(struct r300_vertex_shader *vs,
258 const struct pipe_shader_state *shader)
259 {
260 /* Copy state directly into shader. */
261 vs->state = *shader;
262 vs->state.tokens = tgsi_dup_tokens(shader->tokens);
263 tgsi_scan_shader(shader->tokens, &vs->info);
264
265 r300_shader_read_vs_outputs(&vs->info, &vs->outputs);
266 r300_init_vs_output_mapping(vs);
267 }
268
269 void r300_translate_vertex_shader(struct r300_context* r300,
270 struct r300_vertex_shader* vs)
271 {
272 struct r300_vertex_program_compiler compiler;
273 struct tgsi_to_rc ttr;
274
275 /* Setup the compiler */
276 rc_init(&compiler.Base);
277
278 compiler.Base.Debug = DBG_ON(r300, DBG_VP);
279 compiler.code = &vs->code;
280 compiler.UserData = vs;
281
282 if (compiler.Base.Debug) {
283 debug_printf("r300: Initial vertex program\n");
284 tgsi_dump(vs->state.tokens, 0);
285 }
286
287 /* Translate TGSI to our internal representation */
288 ttr.compiler = &compiler.Base;
289 ttr.info = &vs->info;
290 ttr.use_half_swizzles = FALSE;
291
292 r300_tgsi_to_rc(&ttr, vs->state.tokens);
293
294 compiler.RequiredOutputs = ~(~0 << (vs->info.num_outputs+1));
295 compiler.SetHwInputOutput = &set_vertex_inputs_outputs;
296
297 /* Insert the WPOS output. */
298 rc_copy_output(&compiler.Base, 0, vs->outputs.wpos);
299
300 /* Invoke the compiler */
301 r3xx_compile_vertex_program(&compiler);
302 if (compiler.Base.Error) {
303 /* XXX We should fallback using Draw. */
304 fprintf(stderr, "r300 VP: Compiler error\n");
305 abort();
306 }
307
308 /* And, finally... */
309 rc_destroy(&compiler.Base);
310 }
311
312 boolean r300_vertex_shader_setup_wpos(struct r300_context* r300)
313 {
314 struct r300_vertex_shader* vs = r300->vs_state.state;
315 struct r300_vap_output_state *vap_out = &vs->vap_out;
316 int tex_output = vs->wpos_tex_output;
317 uint32_t tex_fmt = R300_INPUT_CNTL_TC0 << tex_output;
318
319 if (r300->fs->inputs.wpos != ATTR_UNUSED) {
320 /* Enable WPOS in VAP. */
321 if (!(vap_out->vap_vsm_vtx_assm & tex_fmt)) {
322 vap_out->vap_vsm_vtx_assm |= tex_fmt;
323 vap_out->vap_out_vtx_fmt[1] |= (4 << (3 * tex_output));
324
325 assert(tex_output < 8);
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 }