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