Merge branch 'mesa_7_6_branch'
[mesa.git] / src / gallium / drivers / r300 / r300_state_derived.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "r300_state_derived.h"
24
25 #include "r300_fs.h"
26 #include "r300_state_inlines.h"
27 #include "r300_vs.h"
28
29 /* r300_state_derived: Various bits of state which are dependent upon
30 * currently bound CSO data. */
31
32 /* Set up the vs_tab and routes. */
33 static void r300_vs_tab_routes(struct r300_context* r300,
34 struct r300_vertex_format* vformat)
35 {
36 struct r300_screen* r300screen = r300_screen(r300->context.screen);
37 struct vertex_info* vinfo = &vformat->vinfo;
38 int* tab = vformat->vs_tab;
39 boolean pos = FALSE, psize = FALSE, fog = FALSE;
40 int i, texs = 0, cols = 0;
41 struct tgsi_shader_info* info;
42
43 if (r300screen->caps->has_tcl) {
44 /* Use vertex shader to determine required routes. */
45 info = &r300->vs->info;
46 } else {
47 /* Use fragment shader to determine required routes. */
48 info = &r300->fs->info;
49 }
50
51 assert(info->num_inputs <= 16);
52
53 if (!r300screen->caps->has_tcl || !r300->rs_state->enable_vte)
54 {
55 for (i = 0; i < info->num_inputs; i++) {
56 switch (r300->vs->code.inputs[i]) {
57 case TGSI_SEMANTIC_POSITION:
58 pos = TRUE;
59 tab[i] = 0;
60 break;
61 case TGSI_SEMANTIC_COLOR:
62 tab[i] = 2 + cols;
63 cols++;
64 break;
65 case TGSI_SEMANTIC_PSIZE:
66 assert(psize == FALSE);
67 psize = TRUE;
68 tab[i] = 15;
69 break;
70 case TGSI_SEMANTIC_FOG:
71 assert(fog == FALSE);
72 fog = TRUE;
73 /* Fall through */
74 case TGSI_SEMANTIC_GENERIC:
75 tab[i] = 6 + texs;
76 texs++;
77 break;
78 default:
79 debug_printf("r300: Unknown vertex input %d\n",
80 info->input_semantic_name[i]);
81 break;
82 }
83 }
84 }
85 else
86 {
87 /* Just copy vert attribs over as-is. */
88 for (i = 0; i < info->num_inputs; i++) {
89 tab[i] = i;
90 }
91
92 for (i = 0; i < info->num_outputs; i++) {
93 switch (info->output_semantic_name[i]) {
94 case TGSI_SEMANTIC_POSITION:
95 pos = TRUE;
96 break;
97 case TGSI_SEMANTIC_COLOR:
98 cols++;
99 break;
100 case TGSI_SEMANTIC_PSIZE:
101 psize = TRUE;
102 break;
103 case TGSI_SEMANTIC_FOG:
104 fog = TRUE;
105 /* Fall through */
106 case TGSI_SEMANTIC_GENERIC:
107 texs++;
108 break;
109 default:
110 debug_printf("r300: Unknown vertex output %d\n",
111 info->output_semantic_name[i]);
112 break;
113 }
114 }
115 }
116
117 /* XXX magic */
118 assert(texs <= 8);
119
120 /* Do the actual vertex_info setup.
121 *
122 * vertex_info has four uints of hardware-specific data in it.
123 * vinfo.hwfmt[0] is R300_VAP_VTX_STATE_CNTL
124 * vinfo.hwfmt[1] is R300_VAP_VSM_VTX_ASSM
125 * vinfo.hwfmt[2] is R300_VAP_OUTPUT_VTX_FMT_0
126 * vinfo.hwfmt[3] is R300_VAP_OUTPUT_VTX_FMT_1 */
127
128 vinfo->hwfmt[0] = 0x5555; /* XXX this is classic Mesa bonghits */
129
130 /* We need to add vertex position attribute only for SW TCL case,
131 * for HW TCL case it could be generated by vertex shader */
132 if (!pos && !r300screen->caps->has_tcl) {
133 debug_printf("r300: Forcing vertex position attribute emit...\n");
134 /* Make room for the position attribute
135 * at the beginning of the tab. */
136 for (i = 15; i > 0; i--) {
137 tab[i] = tab[i-1];
138 }
139 tab[0] = 0;
140 }
141 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE,
142 draw_find_vs_output(r300->draw, TGSI_SEMANTIC_POSITION, 0));
143 vinfo->hwfmt[1] |= R300_INPUT_CNTL_POS;
144 vinfo->hwfmt[2] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT;
145
146 if (psize) {
147 draw_emit_vertex_attr(vinfo, EMIT_1F_PSIZE, INTERP_POS,
148 draw_find_vs_output(r300->draw, TGSI_SEMANTIC_PSIZE, 0));
149 vinfo->hwfmt[2] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT;
150 }
151
152 for (i = 0; i < cols; i++) {
153 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_LINEAR,
154 draw_find_vs_output(r300->draw, TGSI_SEMANTIC_COLOR, i));
155 vinfo->hwfmt[1] |= R300_INPUT_CNTL_COLOR;
156 vinfo->hwfmt[2] |= (R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << i);
157 }
158
159 /* Init i right here, increment it if fog is enabled.
160 * This gets around a double-increment problem. */
161 i = 0;
162
163 if (fog) {
164 i++;
165 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE,
166 draw_find_vs_output(r300->draw, TGSI_SEMANTIC_FOG, 0));
167 vinfo->hwfmt[1] |= (R300_INPUT_CNTL_TC0 << i);
168 vinfo->hwfmt[3] |= (4 << (3 * i));
169 }
170
171 for (; i < texs; i++) {
172 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE,
173 draw_find_vs_output(r300->draw, TGSI_SEMANTIC_GENERIC, i));
174 vinfo->hwfmt[1] |= (R300_INPUT_CNTL_TC0 << i);
175 vinfo->hwfmt[3] |= (4 << (3 * i));
176 }
177
178 /* Handle the case where the vertex shader will be generating some of
179 * the attribs based on its inputs. */
180 if (r300screen->caps->has_tcl &&
181 info->num_inputs < info->num_outputs) {
182 vinfo->num_attribs = info->num_inputs;
183 }
184
185 draw_compute_vertex_size(vinfo);
186 }
187
188 /* Update the PSC tables. */
189 static void r300_vertex_psc(struct r300_context* r300,
190 struct r300_vertex_format* vformat)
191 {
192 struct r300_screen* r300screen = r300_screen(r300->context.screen);
193 struct vertex_info* vinfo = &vformat->vinfo;
194 int* tab = vformat->vs_tab;
195 uint32_t temp;
196 int i, attrib_count;
197
198 /* Vertex shaders have no semantics on their inputs,
199 * so PSC should just route stuff based on their info,
200 * and not on attrib information. */
201 if (r300screen->caps->has_tcl) {
202 attrib_count = r300->vs->info.num_inputs;
203 DBG(r300, DBG_DRAW, "r300: routing %d attribs in psc for vs\n",
204 attrib_count);
205 } else {
206 attrib_count = vinfo->num_attribs;
207 DBG(r300, DBG_DRAW, "r300: attrib count: %d\n", attrib_count);
208 for (i = 0; i < attrib_count; i++) {
209 DBG(r300, DBG_DRAW, "r300: attrib: offset %d, interp %d, size %d,"
210 " tab %d\n", vinfo->attrib[i].src_index,
211 vinfo->attrib[i].interp_mode, vinfo->attrib[i].emit,
212 tab[i]);
213 }
214 }
215
216 for (i = 0; i < attrib_count; i++) {
217 /* Make sure we have a proper destination for our attribute */
218 assert(tab[i] != -1);
219
220 /* Add the attribute to the PSC table. */
221 temp = r300screen->caps->has_tcl ?
222 R300_DATA_TYPE_FLOAT_4 :
223 translate_vertex_data_type(vinfo->attrib[i].emit);
224 temp |= tab[i] << R300_DST_VEC_LOC_SHIFT;
225
226 if (i & 1) {
227 vformat->vap_prog_stream_cntl[i >> 1] &= 0x0000ffff;
228 vformat->vap_prog_stream_cntl[i >> 1] |= temp << 16;
229
230 vformat->vap_prog_stream_cntl_ext[i >> 1] |=
231 (R300_VAP_SWIZZLE_XYZW << 16);
232 } else {
233 vformat->vap_prog_stream_cntl[i >> 1] &= 0xffff0000;
234 vformat->vap_prog_stream_cntl[i >> 1] |= temp << 0;
235
236 vformat->vap_prog_stream_cntl_ext[i >> 1] |=
237 (R300_VAP_SWIZZLE_XYZW << 0);
238 }
239 }
240
241 /* Set the last vector in the PSC. */
242 i--;
243 vformat->vap_prog_stream_cntl[i >> 1] |=
244 (R300_LAST_VEC << (i & 1 ? 16 : 0));
245 }
246
247 /* Update the vertex format. */
248 static void r300_update_vertex_format(struct r300_context* r300)
249 {
250 struct r300_vertex_format vformat;
251 int i;
252
253 memset(&vformat, 0, sizeof(struct r300_vertex_format));
254 for (i = 0; i < 16; i++) {
255 vformat.vs_tab[i] = -1;
256 vformat.fs_tab[i] = -1;
257 }
258
259 r300_vs_tab_routes(r300, &vformat);
260
261 r300_vertex_psc(r300, &vformat);
262
263 if (memcmp(&r300->vertex_info, &vformat,
264 sizeof(struct r300_vertex_format))) {
265 memcpy(&r300->vertex_info, &vformat,
266 sizeof(struct r300_vertex_format));
267 r300->dirty_state |= R300_NEW_VERTEX_FORMAT;
268 }
269 }
270
271 /* Set up the mappings from GB to US, for RS block. */
272 static void r300_update_fs_tab(struct r300_context* r300)
273 {
274 struct r300_vertex_format* vformat = &r300->vertex_info;
275 struct tgsi_shader_info* info = &r300->fs->info;
276 int i, cols = 0, texs = 0, cols_emitted = 0;
277 int* tab = vformat->fs_tab;
278
279 for (i = 0; i < 16; i++) {
280 tab[i] = -1;
281 }
282
283 assert(info->num_inputs <= 16);
284 for (i = 0; i < info->num_inputs; i++) {
285 switch (info->input_semantic_name[i]) {
286 case TGSI_SEMANTIC_COLOR:
287 tab[i] = INTERP_LINEAR;
288 cols++;
289 break;
290 case TGSI_SEMANTIC_POSITION:
291 case TGSI_SEMANTIC_PSIZE:
292 debug_printf("r300: Implementation error: Can't use "
293 "pos attribs in fragshader yet!\n");
294 /* Pass through for now */
295 case TGSI_SEMANTIC_FOG:
296 case TGSI_SEMANTIC_GENERIC:
297 tab[i] = INTERP_PERSPECTIVE;
298 break;
299 default:
300 debug_printf("r300: Unknown vertex input %d\n",
301 info->input_semantic_name[i]);
302 break;
303 }
304 }
305
306 /* Now that we know where everything is... */
307 DBG(r300, DBG_DRAW, "r300: fp input count: %d\n", info->num_inputs);
308 for (i = 0; i < info->num_inputs; i++) {
309 switch (tab[i]) {
310 case INTERP_LINEAR:
311 DBG(r300, DBG_DRAW, "r300: attrib: "
312 "stack offset %d, color, tab %d\n",
313 i, cols_emitted);
314 tab[i] = cols_emitted;
315 cols_emitted++;
316 break;
317 case INTERP_PERSPECTIVE:
318 DBG(r300, DBG_DRAW, "r300: attrib: "
319 "stack offset %d, texcoord, tab %d\n",
320 i, cols + texs);
321 tab[i] = cols + texs;
322 texs++;
323 break;
324 case -1:
325 debug_printf("r300: Implementation error: Bad fp interp!\n");
326 default:
327 break;
328 }
329 }
330
331 }
332
333 /* Set up the RS block. This is the part of the chipset that actually does
334 * the rasterization of vertices into fragments. This is also the part of the
335 * chipset that locks up if any part of it is even slightly wrong. */
336 static void r300_update_rs_block(struct r300_context* r300)
337 {
338 struct r300_rs_block* rs = r300->rs_block;
339 struct tgsi_shader_info* info = &r300->fs->info;
340 int* tab = r300->vertex_info.fs_tab;
341 int col_count = 0, fp_offset = 0, i, tex_count = 0;
342 int rs_tex_comp = 0;
343 memset(rs, 0, sizeof(struct r300_rs_block));
344
345 if (r300_screen(r300->context.screen)->caps->is_r500) {
346 for (i = 0; i < info->num_inputs; i++) {
347 assert(tab[i] != -1);
348 switch (info->input_semantic_name[i]) {
349 case TGSI_SEMANTIC_COLOR:
350 rs->ip[col_count] |=
351 R500_RS_COL_PTR(col_count) |
352 R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
353 col_count++;
354 break;
355 case TGSI_SEMANTIC_GENERIC:
356 rs->ip[tex_count] |=
357 R500_RS_SEL_S(rs_tex_comp) |
358 R500_RS_SEL_T(rs_tex_comp + 1) |
359 R500_RS_SEL_R(rs_tex_comp + 2) |
360 R500_RS_SEL_Q(rs_tex_comp + 3);
361 tex_count++;
362 rs_tex_comp += 4;
363 break;
364 default:
365 break;
366 }
367 }
368
369 /* Rasterize at least one color, or bad things happen. */
370 if ((col_count == 0) && (tex_count == 0)) {
371 rs->ip[0] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001);
372 col_count++;
373 }
374
375 for (i = 0; i < tex_count; i++) {
376 rs->inst[i] |= R500_RS_INST_TEX_ID(i) |
377 R500_RS_INST_TEX_CN_WRITE | R500_RS_INST_TEX_ADDR(fp_offset);
378 fp_offset++;
379 }
380
381 for (i = 0; i < col_count; i++) {
382 rs->inst[i] |= R500_RS_INST_COL_ID(i) |
383 R500_RS_INST_COL_CN_WRITE | R500_RS_INST_COL_ADDR(fp_offset);
384 fp_offset++;
385 }
386 } else {
387 for (i = 0; i < info->num_inputs; i++) {
388 assert(tab[i] != -1);
389 switch (info->input_semantic_name[i]) {
390 case TGSI_SEMANTIC_COLOR:
391 rs->ip[col_count] |=
392 R300_RS_COL_PTR(col_count) |
393 R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
394 col_count++;
395 break;
396 case TGSI_SEMANTIC_GENERIC:
397 rs->ip[tex_count] |=
398 R300_RS_TEX_PTR(rs_tex_comp) |
399 R300_RS_SEL_S(R300_RS_SEL_C0) |
400 R300_RS_SEL_T(R300_RS_SEL_C1) |
401 R300_RS_SEL_R(R300_RS_SEL_C2) |
402 R300_RS_SEL_Q(R300_RS_SEL_C3);
403 tex_count++;
404 rs_tex_comp+=4;
405 break;
406 default:
407 break;
408 }
409 }
410
411 if (col_count == 0) {
412 rs->ip[0] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001);
413 }
414
415 if (tex_count == 0) {
416 rs->ip[0] |=
417 R300_RS_SEL_S(R300_RS_SEL_K0) |
418 R300_RS_SEL_T(R300_RS_SEL_K0) |
419 R300_RS_SEL_R(R300_RS_SEL_K0) |
420 R300_RS_SEL_Q(R300_RS_SEL_K1);
421 }
422
423 /* Rasterize at least one color, or bad things happen. */
424 if ((col_count == 0) && (tex_count == 0)) {
425 col_count++;
426 }
427
428 for (i = 0; i < tex_count; i++) {
429 rs->inst[i] |= R300_RS_INST_TEX_ID(i) |
430 R300_RS_INST_TEX_CN_WRITE | R300_RS_INST_TEX_ADDR(fp_offset);
431 fp_offset++;
432 }
433
434 for (i = 0; i < col_count; i++) {
435 rs->inst[i] |= R300_RS_INST_COL_ID(i) |
436 R300_RS_INST_COL_CN_WRITE | R300_RS_INST_COL_ADDR(fp_offset);
437 fp_offset++;
438 }
439 }
440
441 rs->count = (rs_tex_comp) | (col_count << R300_IC_COUNT_SHIFT) |
442 R300_HIRES_EN;
443
444 rs->inst_count = MAX2(MAX2(col_count - 1, tex_count - 1), 0);
445 }
446
447 void r300_update_derived_state(struct r300_context* r300)
448 {
449 if (r300->dirty_state &
450 (R300_NEW_FRAGMENT_SHADER | R300_NEW_VERTEX_SHADER)) {
451 r300_update_vertex_format(r300);
452 }
453
454 if (r300->dirty_state & R300_NEW_VERTEX_FORMAT) {
455 r300_update_fs_tab(r300);
456 r300_update_rs_block(r300);
457 }
458 }