Merge branch 'mesa_7_6_branch' into mesa_7_7_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 "draw/draw_context.h"
24
25 #include "util/u_math.h"
26 #include "util/u_memory.h"
27
28 #include "r300_context.h"
29 #include "r300_fs.h"
30 #include "r300_screen.h"
31 #include "r300_state_derived.h"
32 #include "r300_state_inlines.h"
33 #include "r300_vs.h"
34
35 /* r300_state_derived: Various bits of state which are dependent upon
36 * currently bound CSO data. */
37
38 struct r300_shader_key {
39 struct r300_vertex_shader* vs;
40 struct r300_fragment_shader* fs;
41 };
42
43 struct r300_shader_derived_value {
44 struct r300_vertex_format* vformat;
45 struct r300_rs_block* rs_block;
46 };
47
48 unsigned r300_shader_key_hash(void* key) {
49 struct r300_shader_key* shader_key = (struct r300_shader_key*)key;
50 unsigned vs = (unsigned)shader_key->vs;
51 unsigned fs = (unsigned)shader_key->fs;
52
53 return (vs << 16) | (fs & 0xffff);
54 }
55
56 int r300_shader_key_compare(void* key1, void* key2) {
57 struct r300_shader_key* shader_key1 = (struct r300_shader_key*)key1;
58 struct r300_shader_key* shader_key2 = (struct r300_shader_key*)key2;
59
60 return (shader_key1->vs == shader_key2->vs) &&
61 (shader_key1->fs == shader_key2->fs);
62 }
63
64 /* Set up the vs_tab and routes. */
65 static void r300_vs_tab_routes(struct r300_context* r300,
66 struct r300_vertex_info* vformat)
67 {
68 struct r300_screen* r300screen = r300_screen(r300->context.screen);
69 struct vertex_info* vinfo = &vformat->vinfo;
70 int* tab = vformat->vs_tab;
71 boolean pos = FALSE, psize = FALSE, fog = FALSE;
72 int i, texs = 0, cols = 0;
73 struct tgsi_shader_info* info;
74
75 if (r300screen->caps->has_tcl) {
76 /* Use vertex shader to determine required routes. */
77 info = &r300->vs->info;
78 } else {
79 /* Use fragment shader to determine required routes. */
80 info = &r300->fs->info;
81 }
82
83 assert(info->num_inputs <= 16);
84
85 if (!r300screen->caps->has_tcl || !r300->rs_state->enable_vte)
86 {
87 for (i = 0; i < info->num_inputs; i++) {
88 switch (r300->vs->code.inputs[i]) {
89 case TGSI_SEMANTIC_POSITION:
90 pos = TRUE;
91 tab[i] = 0;
92 break;
93 case TGSI_SEMANTIC_COLOR:
94 tab[i] = 2 + cols;
95 cols++;
96 break;
97 case TGSI_SEMANTIC_PSIZE:
98 assert(psize == FALSE);
99 psize = TRUE;
100 tab[i] = 15;
101 break;
102 case TGSI_SEMANTIC_FOG:
103 assert(fog == FALSE);
104 fog = TRUE;
105 /* Fall through */
106 case TGSI_SEMANTIC_GENERIC:
107 tab[i] = 6 + texs;
108 texs++;
109 break;
110 default:
111 debug_printf("r300: Unknown vertex input %d\n",
112 info->input_semantic_name[i]);
113 break;
114 }
115 }
116 }
117 else
118 {
119 /* Just copy vert attribs over as-is. */
120 for (i = 0; i < info->num_inputs; i++) {
121 tab[i] = i;
122 }
123
124 for (i = 0; i < info->num_outputs; i++) {
125 switch (info->output_semantic_name[i]) {
126 case TGSI_SEMANTIC_POSITION:
127 pos = TRUE;
128 break;
129 case TGSI_SEMANTIC_COLOR:
130 cols++;
131 break;
132 case TGSI_SEMANTIC_PSIZE:
133 psize = TRUE;
134 break;
135 case TGSI_SEMANTIC_FOG:
136 fog = TRUE;
137 /* Fall through */
138 case TGSI_SEMANTIC_GENERIC:
139 texs++;
140 break;
141 default:
142 debug_printf("r300: Unknown vertex output %d\n",
143 info->output_semantic_name[i]);
144 break;
145 }
146 }
147 }
148
149 /* XXX magic */
150 assert(texs <= 8);
151
152 /* Do the actual vertex_info setup.
153 *
154 * vertex_info has four uints of hardware-specific data in it.
155 * vinfo.hwfmt[0] is R300_VAP_VTX_STATE_CNTL
156 * vinfo.hwfmt[1] is R300_VAP_VSM_VTX_ASSM
157 * vinfo.hwfmt[2] is R300_VAP_OUTPUT_VTX_FMT_0
158 * vinfo.hwfmt[3] is R300_VAP_OUTPUT_VTX_FMT_1 */
159
160 vinfo->hwfmt[0] = 0x5555; /* XXX this is classic Mesa bonghits */
161
162 /* We need to add vertex position attribute only for SW TCL case,
163 * for HW TCL case it could be generated by vertex shader */
164 if (!pos && !r300screen->caps->has_tcl) {
165 debug_printf("r300: Forcing vertex position attribute emit...\n");
166 /* Make room for the position attribute
167 * at the beginning of the tab. */
168 for (i = 15; i > 0; i--) {
169 tab[i] = tab[i-1];
170 }
171 tab[0] = 0;
172 }
173
174 /* Position. */
175 if (r300->draw) {
176 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE,
177 draw_find_vs_output(r300->draw, TGSI_SEMANTIC_POSITION, 0));
178 }
179 vinfo->hwfmt[1] |= R300_INPUT_CNTL_POS;
180 vinfo->hwfmt[2] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT;
181
182 /* Point size. */
183 if (psize) {
184 if (r300->draw) {
185 draw_emit_vertex_attr(vinfo, EMIT_1F_PSIZE, INTERP_POS,
186 draw_find_vs_output(r300->draw, TGSI_SEMANTIC_PSIZE, 0));
187 }
188 vinfo->hwfmt[2] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT;
189 }
190
191 /* Colors. */
192 for (i = 0; i < cols; i++) {
193 if (r300->draw) {
194 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_LINEAR,
195 draw_find_vs_output(r300->draw, TGSI_SEMANTIC_COLOR, i));
196 }
197 vinfo->hwfmt[1] |= R300_INPUT_CNTL_COLOR;
198 vinfo->hwfmt[2] |= (R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << i);
199 }
200
201 /* Init i right here, increment it if fog is enabled.
202 * This gets around a double-increment problem. */
203 i = 0;
204
205 /* Fog. This is a special-cased texcoord. */
206 if (fog) {
207 i++;
208 if (r300->draw) {
209 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE,
210 draw_find_vs_output(r300->draw, TGSI_SEMANTIC_FOG, 0));
211 }
212 vinfo->hwfmt[1] |= (R300_INPUT_CNTL_TC0 << i);
213 vinfo->hwfmt[3] |= (4 << (3 * i));
214 }
215
216 /* Texcoords. */
217 for (; i < texs; i++) {
218 if (r300->draw) {
219 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE,
220 draw_find_vs_output(r300->draw, TGSI_SEMANTIC_GENERIC, i));
221 }
222 vinfo->hwfmt[1] |= (R300_INPUT_CNTL_TC0 << i);
223 vinfo->hwfmt[3] |= (4 << (3 * i));
224 }
225
226 draw_compute_vertex_size(vinfo);
227 }
228
229 /* Update the PSC tables. */
230 static void r300_vertex_psc(struct r300_context* r300,
231 struct r300_vertex_info* vformat)
232 {
233 struct r300_screen* r300screen = r300_screen(r300->context.screen);
234 struct vertex_info* vinfo = &vformat->vinfo;
235 int* tab = vformat->vs_tab;
236 uint16_t type, swizzle;
237 enum pipe_format format;
238 unsigned i, attrib_count;
239
240 /* Vertex shaders have no semantics on their inputs,
241 * so PSC should just route stuff based on their info,
242 * and not on attrib information. */
243 if (r300screen->caps->has_tcl) {
244 attrib_count = r300->vs->info.num_inputs;
245 DBG(r300, DBG_DRAW, "r300: routing %d attribs in psc for vs\n",
246 attrib_count);
247 } else {
248 attrib_count = vinfo->num_attribs;
249 DBG(r300, DBG_DRAW, "r300: attrib count: %d\n", attrib_count);
250 for (i = 0; i < attrib_count; i++) {
251 DBG(r300, DBG_DRAW, "r300: attrib: offset %d, interp %d, size %d,"
252 " tab %d\n", vinfo->attrib[i].src_index,
253 vinfo->attrib[i].interp_mode, vinfo->attrib[i].emit,
254 tab[i]);
255 }
256 }
257
258 for (i = 0; i < attrib_count; i++) {
259 /* Make sure we have a proper destination for our attribute. */
260 assert(tab[i] != -1);
261
262 format = draw_translate_vinfo_format(vinfo->attrib[i].emit);
263
264 /* Obtain the type of data in this attribute. */
265 type = r300_translate_vertex_data_type(format) |
266 tab[i] << R300_DST_VEC_LOC_SHIFT;
267
268 /* Obtain the swizzle for this attribute. Note that the default
269 * swizzle in the hardware is not XYZW! */
270 swizzle = r300_translate_vertex_data_swizzle(format);
271
272 /* Add the attribute to the PSC table. */
273 if (i & 1) {
274 vformat->vap_prog_stream_cntl[i >> 1] |= type << 16;
275
276 vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle << 16;
277 } else {
278 vformat->vap_prog_stream_cntl[i >> 1] |= type << 0;
279
280 vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle << 0;
281 }
282 }
283
284 /* Set the last vector in the PSC. */
285 if (i) {
286 i -= 1;
287 }
288 vformat->vap_prog_stream_cntl[i >> 1] |=
289 (R300_LAST_VEC << (i & 1 ? 16 : 0));
290 }
291
292 /* Set up the mappings from GB to US, for RS block. */
293 static void r300_update_fs_tab(struct r300_context* r300,
294 struct r300_vertex_info* vformat)
295 {
296 struct tgsi_shader_info* info = &r300->fs->info;
297 int i, cols = 0, texs = 0, cols_emitted = 0;
298 int* tab = vformat->fs_tab;
299
300 for (i = 0; i < 16; i++) {
301 tab[i] = -1;
302 }
303
304 assert(info->num_inputs <= 16);
305 for (i = 0; i < info->num_inputs; i++) {
306 switch (info->input_semantic_name[i]) {
307 case TGSI_SEMANTIC_COLOR:
308 tab[i] = INTERP_LINEAR;
309 cols++;
310 break;
311 case TGSI_SEMANTIC_POSITION:
312 case TGSI_SEMANTIC_PSIZE:
313 debug_printf("r300: Implementation error: Can't use "
314 "pos attribs in fragshader yet!\n");
315 /* Pass through for now */
316 case TGSI_SEMANTIC_FOG:
317 case TGSI_SEMANTIC_GENERIC:
318 tab[i] = INTERP_PERSPECTIVE;
319 break;
320 default:
321 debug_printf("r300: Unknown vertex input %d\n",
322 info->input_semantic_name[i]);
323 break;
324 }
325 }
326
327 /* Now that we know where everything is... */
328 DBG(r300, DBG_DRAW, "r300: fp input count: %d\n", info->num_inputs);
329 for (i = 0; i < info->num_inputs; i++) {
330 switch (tab[i]) {
331 case INTERP_LINEAR:
332 DBG(r300, DBG_DRAW, "r300: attrib: "
333 "stack offset %d, color, tab %d\n",
334 i, cols_emitted);
335 tab[i] = cols_emitted;
336 cols_emitted++;
337 break;
338 case INTERP_PERSPECTIVE:
339 DBG(r300, DBG_DRAW, "r300: attrib: "
340 "stack offset %d, texcoord, tab %d\n",
341 i, cols + texs);
342 tab[i] = cols + texs;
343 texs++;
344 break;
345 case -1:
346 debug_printf("r300: Implementation error: Bad fp interp!\n");
347 default:
348 break;
349 }
350 }
351
352 }
353
354 /* Set up the RS block. This is the part of the chipset that actually does
355 * the rasterization of vertices into fragments. This is also the part of the
356 * chipset that locks up if any part of it is even slightly wrong. */
357 static void r300_update_rs_block(struct r300_context* r300,
358 struct r300_rs_block* rs)
359 {
360 struct tgsi_shader_info* info = &r300->fs->info;
361 int col_count = 0, fp_offset = 0, i, tex_count = 0;
362 int rs_tex_comp = 0;
363
364 if (r300_screen(r300->context.screen)->caps->is_r500) {
365 for (i = 0; i < info->num_inputs; i++) {
366 switch (info->input_semantic_name[i]) {
367 case TGSI_SEMANTIC_COLOR:
368 rs->ip[col_count] |=
369 R500_RS_COL_PTR(col_count) |
370 R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
371 col_count++;
372 break;
373 case TGSI_SEMANTIC_GENERIC:
374 rs->ip[tex_count] |=
375 R500_RS_SEL_S(rs_tex_comp) |
376 R500_RS_SEL_T(rs_tex_comp + 1) |
377 R500_RS_SEL_R(rs_tex_comp + 2) |
378 R500_RS_SEL_Q(rs_tex_comp + 3);
379 tex_count++;
380 rs_tex_comp += 4;
381 break;
382 default:
383 break;
384 }
385 }
386
387 /* Rasterize at least one color, or bad things happen. */
388 if ((col_count == 0) && (tex_count == 0)) {
389 rs->ip[0] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001);
390 col_count++;
391 }
392
393 for (i = 0; i < tex_count; i++) {
394 rs->inst[i] |= R500_RS_INST_TEX_ID(i) |
395 R500_RS_INST_TEX_CN_WRITE | R500_RS_INST_TEX_ADDR(fp_offset);
396 fp_offset++;
397 }
398
399 for (i = 0; i < col_count; i++) {
400 rs->inst[i] |= R500_RS_INST_COL_ID(i) |
401 R500_RS_INST_COL_CN_WRITE | R500_RS_INST_COL_ADDR(fp_offset);
402 fp_offset++;
403 }
404 } else {
405 for (i = 0; i < info->num_inputs; i++) {
406 switch (info->input_semantic_name[i]) {
407 case TGSI_SEMANTIC_COLOR:
408 rs->ip[col_count] |=
409 R300_RS_COL_PTR(col_count) |
410 R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
411 col_count++;
412 break;
413 case TGSI_SEMANTIC_GENERIC:
414 rs->ip[tex_count] |=
415 R300_RS_TEX_PTR(rs_tex_comp) |
416 R300_RS_SEL_S(R300_RS_SEL_C0) |
417 R300_RS_SEL_T(R300_RS_SEL_C1) |
418 R300_RS_SEL_R(R300_RS_SEL_C2) |
419 R300_RS_SEL_Q(R300_RS_SEL_C3);
420 tex_count++;
421 rs_tex_comp+=4;
422 break;
423 default:
424 break;
425 }
426 }
427
428 if (col_count == 0) {
429 rs->ip[0] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001);
430 }
431
432 if (tex_count == 0) {
433 rs->ip[0] |=
434 R300_RS_SEL_S(R300_RS_SEL_K0) |
435 R300_RS_SEL_T(R300_RS_SEL_K0) |
436 R300_RS_SEL_R(R300_RS_SEL_K0) |
437 R300_RS_SEL_Q(R300_RS_SEL_K1);
438 }
439
440 /* Rasterize at least one color, or bad things happen. */
441 if ((col_count == 0) && (tex_count == 0)) {
442 col_count++;
443 }
444
445 for (i = 0; i < tex_count; i++) {
446 rs->inst[i] |= R300_RS_INST_TEX_ID(i) |
447 R300_RS_INST_TEX_CN_WRITE | R300_RS_INST_TEX_ADDR(fp_offset);
448 fp_offset++;
449 }
450
451 for (i = 0; i < col_count; i++) {
452 rs->inst[i] |= R300_RS_INST_COL_ID(i) |
453 R300_RS_INST_COL_CN_WRITE | R300_RS_INST_COL_ADDR(fp_offset);
454 fp_offset++;
455 }
456 }
457
458 rs->count = (rs_tex_comp) | (col_count << R300_IC_COUNT_SHIFT) |
459 R300_HIRES_EN;
460
461 rs->inst_count = MAX2(MAX2(col_count - 1, tex_count - 1), 0);
462 }
463
464 /* Update the vertex format. */
465 static void r300_update_derived_shader_state(struct r300_context* r300)
466 {
467 struct r300_screen* r300screen = r300_screen(r300->context.screen);
468 struct r300_vertex_info* vformat;
469 struct r300_rs_block* rs_block;
470 int i;
471
472 /*
473 struct r300_shader_key* key;
474 struct r300_shader_derived_value* value;
475 key = CALLOC_STRUCT(r300_shader_key);
476 key->vs = r300->vs;
477 key->fs = r300->fs;
478
479 value = (struct r300_shader_derived_value*)
480 util_hash_table_get(r300->shader_hash_table, (void*)key);
481 if (value) {
482 //vformat = value->vformat;
483 rs_block = value->rs_block;
484
485 FREE(key);
486 } else {
487 rs_block = CALLOC_STRUCT(r300_rs_block);
488 value = CALLOC_STRUCT(r300_shader_derived_value);
489
490 r300_update_rs_block(r300, rs_block);
491
492 //value->vformat = vformat;
493 value->rs_block = rs_block;
494 util_hash_table_set(r300->shader_hash_table,
495 (void*)key, (void*)value);
496 } */
497
498 /* XXX This will be refactored ASAP. */
499 vformat = CALLOC_STRUCT(r300_vertex_info);
500 rs_block = CALLOC_STRUCT(r300_rs_block);
501
502 for (i = 0; i < 16; i++) {
503 vformat->vs_tab[i] = -1;
504 vformat->fs_tab[i] = -1;
505 }
506
507 r300_vs_tab_routes(r300, vformat);
508 r300_vertex_psc(r300, vformat);
509 r300_update_fs_tab(r300, vformat);
510
511 r300_update_rs_block(r300, rs_block);
512
513 FREE(r300->vertex_info);
514 FREE(r300->rs_block);
515
516 r300->vertex_info = vformat;
517 r300->rs_block = rs_block;
518 r300->dirty_state |= (R300_NEW_VERTEX_FORMAT | R300_NEW_RS_BLOCK);
519 }
520
521 static void r300_update_ztop(struct r300_context* r300)
522 {
523 r300->ztop_state.z_buffer_top = R300_ZTOP_ENABLE;
524
525 /* This is important enough that I felt it warranted a comment.
526 *
527 * According to the docs, these are the conditions where ZTOP must be
528 * disabled:
529 * 1) Alpha testing enabled
530 * 2) Texture kill instructions in fragment shader
531 * 3) Chroma key culling enabled
532 * 4) W-buffering enabled
533 *
534 * The docs claim that for the first three cases, if no ZS writes happen,
535 * then ZTOP can be used.
536 *
537 * Additionally, the following conditions require disabled ZTOP:
538 * ~) Depth writes in fragment shader
539 * ~) Outstanding occlusion queries
540 *
541 * ~C.
542 */
543 if (r300->dsa_state->alpha_function) {
544 r300->ztop_state.z_buffer_top = R300_ZTOP_DISABLE;
545 } else if (r300->fs->info.uses_kill) {
546 r300->ztop_state.z_buffer_top = R300_ZTOP_DISABLE;
547 } else if (r300_fragment_shader_writes_depth(r300->fs)) {
548 r300->ztop_state.z_buffer_top = R300_ZTOP_DISABLE;
549 } else if (r300->query_current) {
550 r300->ztop_state.z_buffer_top = R300_ZTOP_DISABLE;
551 }
552 }
553
554 void r300_update_derived_state(struct r300_context* r300)
555 {
556 /* XXX */
557 if (TRUE || r300->dirty_state &
558 (R300_NEW_FRAGMENT_SHADER | R300_NEW_VERTEX_SHADER)) {
559 r300_update_derived_shader_state(r300);
560 }
561
562 if (r300->dirty_state &
563 (R300_NEW_DSA | R300_NEW_FRAGMENT_SHADER | R300_NEW_QUERY)) {
564 r300_update_ztop(r300);
565 }
566 }