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