Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / drivers / r300 / r300_state_derived.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24 #include "draw/draw_context.h"
25
26 #include "util/u_math.h"
27 #include "util/u_memory.h"
28
29 #include "r300_context.h"
30 #include "r300_fs.h"
31 #include "r300_screen.h"
32 #include "r300_shader_semantics.h"
33 #include "r300_state_derived.h"
34 #include "r300_state_inlines.h"
35 #include "r300_vs.h"
36
37 /* r300_state_derived: Various bits of state which are dependent upon
38 * currently bound CSO data. */
39
40 struct r300_shader_key {
41 struct r300_vertex_shader* vs;
42 struct r300_fragment_shader* fs;
43 };
44
45 struct r300_shader_derived_value {
46 struct r300_vertex_format* vformat;
47 struct r300_rs_block* rs_block;
48 };
49
50 unsigned r300_shader_key_hash(void* key) {
51 struct r300_shader_key* shader_key = (struct r300_shader_key*)key;
52 unsigned vs = (intptr_t)shader_key->vs;
53 unsigned fs = (intptr_t)shader_key->fs;
54
55 return (vs << 16) | (fs & 0xffff);
56 }
57
58 int r300_shader_key_compare(void* key1, void* key2) {
59 struct r300_shader_key* shader_key1 = (struct r300_shader_key*)key1;
60 struct r300_shader_key* shader_key2 = (struct r300_shader_key*)key2;
61
62 return (shader_key1->vs == shader_key2->vs) &&
63 (shader_key1->fs == shader_key2->fs);
64 }
65
66 static void r300_draw_emit_attrib(struct r300_context* r300,
67 enum attrib_emit emit,
68 enum interp_mode interp,
69 int index)
70 {
71 struct tgsi_shader_info* info = &r300->vs->info;
72 int output;
73
74 output = draw_find_shader_output(r300->draw,
75 info->output_semantic_name[index],
76 info->output_semantic_index[index]);
77 draw_emit_vertex_attr(&r300->vertex_info->vinfo, emit, interp, output);
78 }
79
80 static void r300_draw_emit_all_attribs(struct r300_context* r300)
81 {
82 struct r300_shader_semantics* vs_outputs = &r300->vs->outputs;
83 int i, gen_count;
84
85 /* Position. */
86 if (vs_outputs->pos != ATTR_UNUSED) {
87 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
88 vs_outputs->pos);
89 } else {
90 assert(0);
91 }
92
93 /* Point size. */
94 if (vs_outputs->psize != ATTR_UNUSED) {
95 r300_draw_emit_attrib(r300, EMIT_1F_PSIZE, INTERP_POS,
96 vs_outputs->psize);
97 }
98
99 /* Colors. */
100 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
101 if (vs_outputs->color[i] != ATTR_UNUSED) {
102 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_LINEAR,
103 vs_outputs->color[i]);
104 }
105 }
106
107 /* XXX Back-face colors. */
108
109 /* Texture coordinates. */
110 gen_count = 0;
111 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
112 if (vs_outputs->generic[i] != ATTR_UNUSED) {
113 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
114 vs_outputs->generic[i]);
115 gen_count++;
116 }
117 }
118
119 /* Fog coordinates. */
120 if (vs_outputs->fog != ATTR_UNUSED) {
121 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
122 vs_outputs->fog);
123 gen_count++;
124 }
125
126 /* XXX magic */
127 assert(gen_count <= 8);
128 }
129
130 /* Update the PSC tables. */
131 static void r300_vertex_psc(struct r300_context* r300)
132 {
133 struct r300_vertex_info *vformat = r300->vertex_info;
134 uint16_t type, swizzle;
135 enum pipe_format format;
136 unsigned i;
137 int identity[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
138 int* stream_tab;
139
140 /* If TCL is bypassed, map vertex streams to equivalent VS output
141 * locations. */
142 if (r300->tcl_bypass) {
143 stream_tab = r300->vs->stream_loc_notcl;
144 } else {
145 stream_tab = identity;
146 }
147
148 /* Vertex shaders have no semantics on their inputs,
149 * so PSC should just route stuff based on the vertex elements,
150 * and not on attrib information. */
151 DBG(r300, DBG_DRAW, "r300: vs expects %d attribs, routing %d elements"
152 " in psc\n",
153 r300->vs->info.num_inputs,
154 r300->vertex_element_count);
155
156 for (i = 0; i < r300->vertex_element_count; i++) {
157 format = r300->vertex_element[i].src_format;
158
159 type = r300_translate_vertex_data_type(format) |
160 (stream_tab[i] << R300_DST_VEC_LOC_SHIFT);
161 swizzle = r300_translate_vertex_data_swizzle(format);
162
163 if (i & 1) {
164 vformat->vap_prog_stream_cntl[i >> 1] |= type << 16;
165 vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle << 16;
166 } else {
167 vformat->vap_prog_stream_cntl[i >> 1] |= type;
168 vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle;
169 }
170 }
171
172 assert(i <= 15);
173
174 /* Set the last vector in the PSC. */
175 if (i) {
176 i -= 1;
177 }
178 vformat->vap_prog_stream_cntl[i >> 1] |=
179 (R300_LAST_VEC << (i & 1 ? 16 : 0));
180 }
181
182 /* Update the PSC tables for SW TCL, using Draw. */
183 static void r300_swtcl_vertex_psc(struct r300_context* r300)
184 {
185 struct r300_vertex_info *vformat = r300->vertex_info;
186 struct vertex_info* vinfo = &vformat->vinfo;
187 uint16_t type, swizzle;
188 enum pipe_format format;
189 unsigned i, attrib_count;
190 int* vs_output_tab = r300->vs->stream_loc_notcl;
191
192 /* For each Draw attribute, route it to the fragment shader according
193 * to the vs_output_tab. */
194 attrib_count = vinfo->num_attribs;
195 DBG(r300, DBG_DRAW, "r300: attrib count: %d\n", attrib_count);
196 for (i = 0; i < attrib_count; i++) {
197 DBG(r300, DBG_DRAW, "r300: attrib: offset %d, interp %d, size %d,"
198 " vs_output_tab %d\n", vinfo->attrib[i].src_index,
199 vinfo->attrib[i].interp_mode, vinfo->attrib[i].emit,
200 vs_output_tab[i]);
201 }
202
203 for (i = 0; i < attrib_count; i++) {
204 /* Make sure we have a proper destination for our attribute. */
205 assert(vs_output_tab[i] != -1);
206
207 format = draw_translate_vinfo_format(vinfo->attrib[i].emit);
208
209 /* Obtain the type of data in this attribute. */
210 type = r300_translate_vertex_data_type(format) |
211 vs_output_tab[i] << R300_DST_VEC_LOC_SHIFT;
212
213 /* Obtain the swizzle for this attribute. Note that the default
214 * swizzle in the hardware is not XYZW! */
215 swizzle = r300_translate_vertex_data_swizzle(format);
216
217 /* Add the attribute to the PSC table. */
218 if (i & 1) {
219 vformat->vap_prog_stream_cntl[i >> 1] |= type << 16;
220 vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle << 16;
221 } else {
222 vformat->vap_prog_stream_cntl[i >> 1] |= type;
223 vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle;
224 }
225 }
226
227 /* Set the last vector in the PSC. */
228 if (i) {
229 i -= 1;
230 }
231 vformat->vap_prog_stream_cntl[i >> 1] |=
232 (R300_LAST_VEC << (i & 1 ? 16 : 0));
233 }
234
235 static void r300_rs_col(struct r300_rs_block* rs, int id, int ptr,
236 boolean swizzle_0001)
237 {
238 rs->ip[id] |= R300_RS_COL_PTR(ptr);
239 if (swizzle_0001) {
240 rs->ip[id] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001);
241 } else {
242 rs->ip[id] |= R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
243 }
244 rs->inst[id] |= R300_RS_INST_COL_ID(id);
245 }
246
247 static void r300_rs_col_write(struct r300_rs_block* rs, int id, int fp_offset)
248 {
249 rs->inst[id] |= R300_RS_INST_COL_CN_WRITE |
250 R300_RS_INST_COL_ADDR(fp_offset);
251 }
252
253 static void r300_rs_tex(struct r300_rs_block* rs, int id, int ptr,
254 boolean swizzle_X001)
255 {
256 if (swizzle_X001) {
257 rs->ip[id] |= R300_RS_TEX_PTR(ptr*4) |
258 R300_RS_SEL_S(R300_RS_SEL_C0) |
259 R300_RS_SEL_T(R300_RS_SEL_K0) |
260 R300_RS_SEL_R(R300_RS_SEL_K0) |
261 R300_RS_SEL_Q(R300_RS_SEL_K1);
262 } else {
263 rs->ip[id] |= R300_RS_TEX_PTR(ptr*4) |
264 R300_RS_SEL_S(R300_RS_SEL_C0) |
265 R300_RS_SEL_T(R300_RS_SEL_C1) |
266 R300_RS_SEL_R(R300_RS_SEL_C2) |
267 R300_RS_SEL_Q(R300_RS_SEL_C3);
268 }
269 rs->inst[id] |= R300_RS_INST_TEX_ID(id);
270 }
271
272 static void r300_rs_tex_write(struct r300_rs_block* rs, int id, int fp_offset)
273 {
274 rs->inst[id] |= R300_RS_INST_TEX_CN_WRITE |
275 R300_RS_INST_TEX_ADDR(fp_offset);
276 }
277
278 static void r500_rs_col(struct r300_rs_block* rs, int id, int ptr,
279 boolean swizzle_0001)
280 {
281 rs->ip[id] |= R500_RS_COL_PTR(ptr);
282 if (swizzle_0001) {
283 rs->ip[id] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001);
284 } else {
285 rs->ip[id] |= R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
286 }
287 rs->inst[id] |= R500_RS_INST_COL_ID(id);
288 }
289
290 static void r500_rs_col_write(struct r300_rs_block* rs, int id, int fp_offset)
291 {
292 rs->inst[id] |= R500_RS_INST_COL_CN_WRITE |
293 R500_RS_INST_COL_ADDR(fp_offset);
294 }
295
296 static void r500_rs_tex(struct r300_rs_block* rs, int id, int ptr,
297 boolean swizzle_X001)
298 {
299 int rs_tex_comp = ptr*4;
300
301 if (swizzle_X001) {
302 rs->ip[id] |= R500_RS_SEL_S(rs_tex_comp) |
303 R500_RS_SEL_T(R500_RS_IP_PTR_K0) |
304 R500_RS_SEL_R(R500_RS_IP_PTR_K0) |
305 R500_RS_SEL_Q(R500_RS_IP_PTR_K1);
306 } else {
307 rs->ip[id] |= R500_RS_SEL_S(rs_tex_comp) |
308 R500_RS_SEL_T(rs_tex_comp + 1) |
309 R500_RS_SEL_R(rs_tex_comp + 2) |
310 R500_RS_SEL_Q(rs_tex_comp + 3);
311 }
312 rs->inst[id] |= R500_RS_INST_TEX_ID(id);
313 }
314
315 static void r500_rs_tex_write(struct r300_rs_block* rs, int id, int fp_offset)
316 {
317 rs->inst[id] |= R500_RS_INST_TEX_CN_WRITE |
318 R500_RS_INST_TEX_ADDR(fp_offset);
319 }
320
321 /* Set up the RS block.
322 *
323 * This is the part of the chipset that actually does the rasterization
324 * of vertices into fragments. This is also the part of the chipset that
325 * locks up if any part of it is even slightly wrong. */
326 static void r300_update_rs_block(struct r300_context* r300,
327 struct r300_shader_semantics* vs_outputs,
328 struct r300_shader_semantics* fs_inputs)
329 {
330 struct r300_rs_block* rs = r300->rs_block;
331 int i, col_count = 0, tex_count = 0, fp_offset = 0;
332 void (*rX00_rs_col)(struct r300_rs_block*, int, int, boolean);
333 void (*rX00_rs_col_write)(struct r300_rs_block*, int, int);
334 void (*rX00_rs_tex)(struct r300_rs_block*, int, int, boolean);
335 void (*rX00_rs_tex_write)(struct r300_rs_block*, int, int);
336 boolean any_bcolor_used = vs_outputs->bcolor[0] != ATTR_UNUSED ||
337 vs_outputs->bcolor[1] != ATTR_UNUSED;
338
339 if (r300_screen(r300->context.screen)->caps->is_r500) {
340 rX00_rs_col = r500_rs_col;
341 rX00_rs_col_write = r500_rs_col_write;
342 rX00_rs_tex = r500_rs_tex;
343 rX00_rs_tex_write = r500_rs_tex_write;
344 } else {
345 rX00_rs_col = r300_rs_col;
346 rX00_rs_col_write = r300_rs_col_write;
347 rX00_rs_tex = r300_rs_tex;
348 rX00_rs_tex_write = r300_rs_tex_write;
349 }
350
351 /* Rasterize colors. */
352 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
353 if (vs_outputs->color[i] != ATTR_UNUSED || any_bcolor_used ||
354 vs_outputs->color[1] != ATTR_UNUSED) {
355 /* Always rasterize if it's written by the VS,
356 * otherwise it locks up. */
357 rX00_rs_col(rs, col_count, i, FALSE);
358
359 /* Write it to the FS input register if it's used by the FS. */
360 if (fs_inputs->color[i] != ATTR_UNUSED) {
361 rX00_rs_col_write(rs, col_count, fp_offset);
362 fp_offset++;
363 }
364 col_count++;
365 } else {
366 /* Skip the FS input register, leave it uninitialized. */
367 /* If we try to set it to (0,0,0,1), it will lock up. */
368 if (fs_inputs->color[i] != ATTR_UNUSED) {
369 fp_offset++;
370 }
371 }
372 }
373
374 /* Rasterize texture coordinates. */
375 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
376 if (vs_outputs->generic[i] != ATTR_UNUSED) {
377 /* Always rasterize if it's written by the VS,
378 * otherwise it locks up. */
379 rX00_rs_tex(rs, tex_count, tex_count, FALSE);
380
381 /* Write it to the FS input register if it's used by the FS. */
382 if (fs_inputs->generic[i] != ATTR_UNUSED) {
383 rX00_rs_tex_write(rs, tex_count, fp_offset);
384 fp_offset++;
385 }
386 tex_count++;
387 } else {
388 /* Skip the FS input register, leave it uninitialized. */
389 /* If we try to set it to (0,0,0,1), it will lock up. */
390 if (fs_inputs->generic[i] != ATTR_UNUSED) {
391 fp_offset++;
392 }
393 }
394 }
395
396 /* Rasterize fog coordinates. */
397 if (vs_outputs->fog != ATTR_UNUSED) {
398 /* Always rasterize if it's written by the VS,
399 * otherwise it locks up. */
400 rX00_rs_tex(rs, tex_count, tex_count, TRUE);
401
402 /* Write it to the FS input register if it's used by the FS. */
403 if (fs_inputs->fog != ATTR_UNUSED) {
404 rX00_rs_tex_write(rs, tex_count, fp_offset);
405 fp_offset++;
406 }
407 tex_count++;
408 } else {
409 /* Skip the FS input register, leave it uninitialized. */
410 /* If we try to set it to (0,0,0,1), it will lock up. */
411 if (fs_inputs->fog != ATTR_UNUSED) {
412 fp_offset++;
413 }
414 }
415
416 /* Rasterize WPOS. */
417 /* If the FS doesn't need it, it's not written by the VS. */
418 if (fs_inputs->wpos != ATTR_UNUSED) {
419 rX00_rs_tex(rs, tex_count, tex_count, FALSE);
420 rX00_rs_tex_write(rs, tex_count, fp_offset);
421
422 fp_offset++;
423 tex_count++;
424 }
425
426 /* Rasterize at least one color, or bad things happen. */
427 if (col_count == 0 && tex_count == 0) {
428 rX00_rs_col(rs, 0, 0, TRUE);
429 col_count++;
430 }
431
432 rs->count = (tex_count*4) | (col_count << R300_IC_COUNT_SHIFT) |
433 R300_HIRES_EN;
434
435 rs->inst_count = MAX3(col_count - 1, tex_count - 1, 0);
436 }
437
438 /* Update the vertex format. */
439 static void r300_update_derived_shader_state(struct r300_context* r300)
440 {
441 struct r300_screen* r300screen = r300_screen(r300->context.screen);
442
443 /*
444 struct r300_shader_key* key;
445 struct r300_shader_derived_value* value;
446 key = CALLOC_STRUCT(r300_shader_key);
447 key->vs = r300->vs;
448 key->fs = r300->fs;
449
450 value = (struct r300_shader_derived_value*)
451 util_hash_table_get(r300->shader_hash_table, (void*)key);
452 if (value) {
453 //vformat = value->vformat;
454 rs_block = value->rs_block;
455
456 FREE(key);
457 } else {
458 rs_block = CALLOC_STRUCT(r300_rs_block);
459 value = CALLOC_STRUCT(r300_shader_derived_value);
460
461 r300_update_rs_block(r300, rs_block);
462
463 //value->vformat = vformat;
464 value->rs_block = rs_block;
465 util_hash_table_set(r300->shader_hash_table,
466 (void*)key, (void*)value);
467 } */
468
469 /* Reset structures */
470 memset(r300->rs_block, 0, sizeof(struct r300_rs_block));
471 memset(r300->vertex_info, 0, sizeof(struct r300_vertex_info));
472 memcpy(r300->vertex_info->vinfo.hwfmt, r300->vs->hwfmt, sizeof(uint)*4);
473
474 r300_update_rs_block(r300, &r300->vs->outputs, &r300->fs->inputs);
475
476 if (r300screen->caps->has_tcl) {
477 r300_vertex_psc(r300);
478 } else {
479 r300_draw_emit_all_attribs(r300);
480 draw_compute_vertex_size(&r300->vertex_info->vinfo);
481 r300_swtcl_vertex_psc(r300);
482 }
483
484 r300->dirty_state |= R300_NEW_RS_BLOCK;
485 }
486
487 static boolean r300_dsa_writes_depth_stencil(struct r300_dsa_state* dsa)
488 {
489 /* We are interested only in the cases when a new depth or stencil value
490 * can be written and changed. */
491
492 /* We might optionally check for [Z func: never] and inspect the stencil
493 * state in a similar fashion, but it's not terribly important. */
494 return (dsa->z_buffer_control & R300_Z_WRITE_ENABLE) ||
495 (dsa->stencil_ref_mask & R300_STENCILWRITEMASK_MASK) ||
496 ((dsa->z_buffer_control & R500_STENCIL_REFMASK_FRONT_BACK) &&
497 (dsa->stencil_ref_bf & R300_STENCILWRITEMASK_MASK));
498 }
499
500 static boolean r300_dsa_alpha_test_enabled(struct r300_dsa_state* dsa)
501 {
502 /* We are interested only in the cases when alpha testing can kill
503 * a fragment. */
504 uint32_t af = dsa->alpha_function;
505
506 return (af & R300_FG_ALPHA_FUNC_ENABLE) &&
507 (af & R300_FG_ALPHA_FUNC_ALWAYS) != R300_FG_ALPHA_FUNC_ALWAYS;
508 }
509
510 static void r300_update_ztop(struct r300_context* r300)
511 {
512 struct r300_ztop_state* ztop_state =
513 (struct r300_ztop_state*)r300->ztop_state.state;
514
515 /* This is important enough that I felt it warranted a comment.
516 *
517 * According to the docs, these are the conditions where ZTOP must be
518 * disabled:
519 * 1) Alpha testing enabled
520 * 2) Texture kill instructions in fragment shader
521 * 3) Chroma key culling enabled
522 * 4) W-buffering enabled
523 *
524 * The docs claim that for the first three cases, if no ZS writes happen,
525 * then ZTOP can be used.
526 *
527 * (3) will never apply since we do not support chroma-keyed operations.
528 * (4) will need to be re-examined (and this comment updated) if/when
529 * Hyper-Z becomes supported.
530 *
531 * Additionally, the following conditions require disabled ZTOP:
532 * 5) Depth writes in fragment shader
533 * 6) Outstanding occlusion queries
534 *
535 * This register causes stalls all the way from SC to CB when changed,
536 * but it is buffered on-chip so it does not hurt to write it if it has
537 * not changed.
538 *
539 * ~C.
540 */
541
542 /* ZS writes */
543 if (r300_dsa_writes_depth_stencil(r300->dsa_state.state) &&
544 (r300_dsa_alpha_test_enabled(r300->dsa_state.state) ||/* (1) */
545 r300->fs->info.uses_kill)) { /* (2) */
546 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
547 } else if (r300_fragment_shader_writes_depth(r300->fs)) { /* (5) */
548 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
549 } else if (r300->query_current) { /* (6) */
550 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
551 } else {
552 ztop_state->z_buffer_top = R300_ZTOP_ENABLE;
553 }
554
555 r300->ztop_state.dirty = TRUE;
556 }
557
558 void r300_update_derived_state(struct r300_context* r300)
559 {
560 /* XXX */
561 if (r300->dirty_state &
562 (R300_NEW_FRAGMENT_SHADER | R300_NEW_VERTEX_SHADER |
563 R300_NEW_VERTEX_FORMAT) || r300->rs_state.dirty) {
564 r300_update_derived_shader_state(r300);
565 }
566
567 r300_update_ztop(r300);
568 }