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