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