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