778eaaacd99942b7b3278ae64a308c4e999592d6
[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 static void r300_draw_emit_attrib(struct r300_context* r300,
41 enum attrib_emit emit,
42 enum interp_mode interp,
43 int index)
44 {
45 struct tgsi_shader_info* info = &r300->vs->info;
46 int output;
47
48 output = draw_find_shader_output(r300->draw,
49 info->output_semantic_name[index],
50 info->output_semantic_index[index]);
51 draw_emit_vertex_attr(
52 (struct vertex_info*)r300->vertex_format_state.state,
53 emit, interp, output);
54 }
55
56 static void r300_draw_emit_all_attribs(struct r300_context* r300)
57 {
58 struct r300_shader_semantics* vs_outputs = &r300->vs->outputs;
59 int i, gen_count;
60
61 /* Position. */
62 if (vs_outputs->pos != ATTR_UNUSED) {
63 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
64 vs_outputs->pos);
65 } else {
66 assert(0);
67 }
68
69 /* Point size. */
70 if (vs_outputs->psize != ATTR_UNUSED) {
71 r300_draw_emit_attrib(r300, EMIT_1F_PSIZE, INTERP_POS,
72 vs_outputs->psize);
73 }
74
75 /* Colors. */
76 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
77 if (vs_outputs->color[i] != ATTR_UNUSED) {
78 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_LINEAR,
79 vs_outputs->color[i]);
80 }
81 }
82
83 /* XXX Back-face colors. */
84
85 /* Texture coordinates. */
86 gen_count = 0;
87 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
88 if (vs_outputs->generic[i] != ATTR_UNUSED) {
89 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
90 vs_outputs->generic[i]);
91 gen_count++;
92 }
93 }
94
95 /* Fog coordinates. */
96 if (vs_outputs->fog != ATTR_UNUSED) {
97 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
98 vs_outputs->fog);
99 gen_count++;
100 }
101
102 /* XXX magic */
103 assert(gen_count <= 8);
104 }
105
106 /* Update the PSC tables. */
107 static void r300_vertex_psc(struct r300_context* r300)
108 {
109 struct r300_vertex_info *vformat =
110 (struct r300_vertex_info*)r300->vertex_format_state.state;
111 uint16_t type, swizzle;
112 enum pipe_format format;
113 unsigned i;
114 int identity[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
115 int* stream_tab;
116
117 stream_tab = identity;
118
119 /* Vertex shaders have no semantics on their inputs,
120 * so PSC should just route stuff based on the vertex elements,
121 * and not on attrib information. */
122 DBG(r300, DBG_DRAW, "r300: vs expects %d attribs, routing %d elements"
123 " in psc\n",
124 r300->vs->info.num_inputs,
125 r300->vertex_element_count);
126
127 for (i = 0; i < r300->vertex_element_count; i++) {
128 format = r300->vertex_element[i].src_format;
129
130 type = r300_translate_vertex_data_type(format) |
131 (stream_tab[i] << R300_DST_VEC_LOC_SHIFT);
132 swizzle = r300_translate_vertex_data_swizzle(format);
133
134 if (i & 1) {
135 vformat->vap_prog_stream_cntl[i >> 1] |= type << 16;
136 vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle << 16;
137 } else {
138 vformat->vap_prog_stream_cntl[i >> 1] |= type;
139 vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle;
140 }
141 }
142
143 assert(i <= 15);
144
145 /* Set the last vector in the PSC. */
146 if (i) {
147 i -= 1;
148 }
149 vformat->vap_prog_stream_cntl[i >> 1] |=
150 (R300_LAST_VEC << (i & 1 ? 16 : 0));
151 }
152
153 /* Update the PSC tables for SW TCL, using Draw. */
154 static void r300_swtcl_vertex_psc(struct r300_context* r300)
155 {
156 struct r300_vertex_info *vformat =
157 (struct r300_vertex_info*)r300->vertex_format_state.state;
158 struct vertex_info* vinfo = &vformat->vinfo;
159 uint16_t type, swizzle;
160 enum pipe_format format;
161 unsigned i, attrib_count;
162 int* vs_output_tab = r300->vs->stream_loc_notcl;
163
164 /* For each Draw attribute, route it to the fragment shader according
165 * to the vs_output_tab. */
166 attrib_count = vinfo->num_attribs;
167 DBG(r300, DBG_DRAW, "r300: attrib count: %d\n", attrib_count);
168 for (i = 0; i < attrib_count; i++) {
169 DBG(r300, DBG_DRAW, "r300: attrib: offset %d, interp %d, size %d,"
170 " vs_output_tab %d\n", vinfo->attrib[i].src_index,
171 vinfo->attrib[i].interp_mode, vinfo->attrib[i].emit,
172 vs_output_tab[i]);
173 }
174
175 for (i = 0; i < attrib_count; i++) {
176 /* Make sure we have a proper destination for our attribute. */
177 assert(vs_output_tab[i] != -1);
178
179 format = draw_translate_vinfo_format(vinfo->attrib[i].emit);
180
181 /* Obtain the type of data in this attribute. */
182 type = r300_translate_vertex_data_type(format) |
183 vs_output_tab[i] << R300_DST_VEC_LOC_SHIFT;
184
185 /* Obtain the swizzle for this attribute. Note that the default
186 * swizzle in the hardware is not XYZW! */
187 swizzle = r300_translate_vertex_data_swizzle(format);
188
189 /* Add the attribute to the PSC table. */
190 if (i & 1) {
191 vformat->vap_prog_stream_cntl[i >> 1] |= type << 16;
192 vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle << 16;
193 } else {
194 vformat->vap_prog_stream_cntl[i >> 1] |= type;
195 vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle;
196 }
197 }
198
199 /* Set the last vector in the PSC. */
200 if (i) {
201 i -= 1;
202 }
203 vformat->vap_prog_stream_cntl[i >> 1] |=
204 (R300_LAST_VEC << (i & 1 ? 16 : 0));
205 }
206
207 static void r300_rs_col(struct r300_rs_block* rs, int id, int ptr,
208 boolean swizzle_0001)
209 {
210 rs->ip[id] |= R300_RS_COL_PTR(ptr);
211 if (swizzle_0001) {
212 rs->ip[id] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001);
213 } else {
214 rs->ip[id] |= R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
215 }
216 rs->inst[id] |= R300_RS_INST_COL_ID(id);
217 }
218
219 static void r300_rs_col_write(struct r300_rs_block* rs, int id, int fp_offset)
220 {
221 rs->inst[id] |= R300_RS_INST_COL_CN_WRITE |
222 R300_RS_INST_COL_ADDR(fp_offset);
223 }
224
225 static void r300_rs_tex(struct r300_rs_block* rs, int id, int ptr,
226 boolean swizzle_X001)
227 {
228 if (swizzle_X001) {
229 rs->ip[id] |= R300_RS_TEX_PTR(ptr*4) |
230 R300_RS_SEL_S(R300_RS_SEL_C0) |
231 R300_RS_SEL_T(R300_RS_SEL_K0) |
232 R300_RS_SEL_R(R300_RS_SEL_K0) |
233 R300_RS_SEL_Q(R300_RS_SEL_K1);
234 } else {
235 rs->ip[id] |= R300_RS_TEX_PTR(ptr*4) |
236 R300_RS_SEL_S(R300_RS_SEL_C0) |
237 R300_RS_SEL_T(R300_RS_SEL_C1) |
238 R300_RS_SEL_R(R300_RS_SEL_C2) |
239 R300_RS_SEL_Q(R300_RS_SEL_C3);
240 }
241 rs->inst[id] |= R300_RS_INST_TEX_ID(id);
242 }
243
244 static void r300_rs_tex_write(struct r300_rs_block* rs, int id, int fp_offset)
245 {
246 rs->inst[id] |= R300_RS_INST_TEX_CN_WRITE |
247 R300_RS_INST_TEX_ADDR(fp_offset);
248 }
249
250 static void r500_rs_col(struct r300_rs_block* rs, int id, int ptr,
251 boolean swizzle_0001)
252 {
253 rs->ip[id] |= R500_RS_COL_PTR(ptr);
254 if (swizzle_0001) {
255 rs->ip[id] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001);
256 } else {
257 rs->ip[id] |= R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
258 }
259 rs->inst[id] |= R500_RS_INST_COL_ID(id);
260 }
261
262 static void r500_rs_col_write(struct r300_rs_block* rs, int id, int fp_offset)
263 {
264 rs->inst[id] |= R500_RS_INST_COL_CN_WRITE |
265 R500_RS_INST_COL_ADDR(fp_offset);
266 }
267
268 static void r500_rs_tex(struct r300_rs_block* rs, int id, int ptr,
269 boolean swizzle_X001)
270 {
271 int rs_tex_comp = ptr*4;
272
273 if (swizzle_X001) {
274 rs->ip[id] |= R500_RS_SEL_S(rs_tex_comp) |
275 R500_RS_SEL_T(R500_RS_IP_PTR_K0) |
276 R500_RS_SEL_R(R500_RS_IP_PTR_K0) |
277 R500_RS_SEL_Q(R500_RS_IP_PTR_K1);
278 } else {
279 rs->ip[id] |= R500_RS_SEL_S(rs_tex_comp) |
280 R500_RS_SEL_T(rs_tex_comp + 1) |
281 R500_RS_SEL_R(rs_tex_comp + 2) |
282 R500_RS_SEL_Q(rs_tex_comp + 3);
283 }
284 rs->inst[id] |= R500_RS_INST_TEX_ID(id);
285 }
286
287 static void r500_rs_tex_write(struct r300_rs_block* rs, int id, int fp_offset)
288 {
289 rs->inst[id] |= R500_RS_INST_TEX_CN_WRITE |
290 R500_RS_INST_TEX_ADDR(fp_offset);
291 }
292
293 /* Set up the RS block.
294 *
295 * This is the part of the chipset that actually does the rasterization
296 * of vertices into fragments. This is also the part of the chipset that
297 * locks up if any part of it is even slightly wrong. */
298 static void r300_update_rs_block(struct r300_context* r300,
299 struct r300_shader_semantics* vs_outputs,
300 struct r300_shader_semantics* fs_inputs)
301 {
302 struct r300_rs_block rs = { { 0 } };
303 int i, col_count = 0, tex_count = 0, fp_offset = 0, count;
304 void (*rX00_rs_col)(struct r300_rs_block*, int, int, boolean);
305 void (*rX00_rs_col_write)(struct r300_rs_block*, int, int);
306 void (*rX00_rs_tex)(struct r300_rs_block*, int, int, boolean);
307 void (*rX00_rs_tex_write)(struct r300_rs_block*, int, int);
308 boolean any_bcolor_used = vs_outputs->bcolor[0] != ATTR_UNUSED ||
309 vs_outputs->bcolor[1] != ATTR_UNUSED;
310
311 if (r300_screen(r300->context.screen)->caps->is_r500) {
312 rX00_rs_col = r500_rs_col;
313 rX00_rs_col_write = r500_rs_col_write;
314 rX00_rs_tex = r500_rs_tex;
315 rX00_rs_tex_write = r500_rs_tex_write;
316 } else {
317 rX00_rs_col = r300_rs_col;
318 rX00_rs_col_write = r300_rs_col_write;
319 rX00_rs_tex = r300_rs_tex;
320 rX00_rs_tex_write = r300_rs_tex_write;
321 }
322
323 /* Rasterize colors. */
324 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
325 if (vs_outputs->color[i] != ATTR_UNUSED || any_bcolor_used ||
326 vs_outputs->color[1] != ATTR_UNUSED) {
327 /* Always rasterize if it's written by the VS,
328 * otherwise it locks up. */
329 rX00_rs_col(&rs, col_count, i, FALSE);
330
331 /* Write it to the FS input register if it's used by the FS. */
332 if (fs_inputs->color[i] != ATTR_UNUSED) {
333 rX00_rs_col_write(&rs, col_count, fp_offset);
334 fp_offset++;
335 }
336 col_count++;
337 } else {
338 /* Skip the FS input register, leave it uninitialized. */
339 /* If we try to set it to (0,0,0,1), it will lock up. */
340 if (fs_inputs->color[i] != ATTR_UNUSED) {
341 fp_offset++;
342 }
343 }
344 }
345
346 /* Rasterize texture coordinates. */
347 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
348 if (vs_outputs->generic[i] != ATTR_UNUSED) {
349 /* Always rasterize if it's written by the VS,
350 * otherwise it locks up. */
351 rX00_rs_tex(&rs, tex_count, tex_count, FALSE);
352
353 /* Write it to the FS input register if it's used by the FS. */
354 if (fs_inputs->generic[i] != ATTR_UNUSED) {
355 rX00_rs_tex_write(&rs, tex_count, fp_offset);
356 fp_offset++;
357 }
358 tex_count++;
359 } else {
360 /* Skip the FS input register, leave it uninitialized. */
361 /* If we try to set it to (0,0,0,1), it will lock up. */
362 if (fs_inputs->generic[i] != ATTR_UNUSED) {
363 fp_offset++;
364 }
365 }
366 }
367
368 /* Rasterize fog coordinates. */
369 if (vs_outputs->fog != ATTR_UNUSED) {
370 /* Always rasterize if it's written by the VS,
371 * otherwise it locks up. */
372 rX00_rs_tex(&rs, tex_count, tex_count, TRUE);
373
374 /* Write it to the FS input register if it's used by the FS. */
375 if (fs_inputs->fog != ATTR_UNUSED) {
376 rX00_rs_tex_write(&rs, tex_count, fp_offset);
377 fp_offset++;
378 }
379 tex_count++;
380 } else {
381 /* Skip the FS input register, leave it uninitialized. */
382 /* If we try to set it to (0,0,0,1), it will lock up. */
383 if (fs_inputs->fog != ATTR_UNUSED) {
384 fp_offset++;
385 }
386 }
387
388 /* Rasterize WPOS. */
389 /* If the FS doesn't need it, it's not written by the VS. */
390 if (fs_inputs->wpos != ATTR_UNUSED) {
391 rX00_rs_tex(&rs, tex_count, tex_count, FALSE);
392 rX00_rs_tex_write(&rs, tex_count, fp_offset);
393
394 fp_offset++;
395 tex_count++;
396 }
397
398 /* Rasterize at least one color, or bad things happen. */
399 if (col_count == 0 && tex_count == 0) {
400 rX00_rs_col(&rs, 0, 0, TRUE);
401 col_count++;
402 }
403
404 rs.count = (tex_count*4) | (col_count << R300_IC_COUNT_SHIFT) |
405 R300_HIRES_EN;
406
407 count = MAX3(col_count, tex_count, 1);
408 rs.inst_count = count - 1;
409
410 /* Now, after all that, see if we actually need to update the state. */
411 if (memcmp(r300->rs_block_state.state, &rs, sizeof(struct r300_rs_block))) {
412 memcpy(r300->rs_block_state.state, &rs, sizeof(struct r300_rs_block));
413 r300->rs_block_state.size = 5 + count;
414 r300->rs_block_state.dirty = TRUE;
415 }
416 }
417
418 /* Update the shader-dependant states. */
419 static void r300_update_derived_shader_state(struct r300_context* r300)
420 {
421 struct r300_screen* r300screen = r300_screen(r300->context.screen);
422 struct r300_vertex_info *vformat =
423 (struct r300_vertex_info*)r300->vertex_format_state.state;
424 struct vertex_info* vinfo = &vformat->vinfo;
425
426 /* Mmm, delicious hax */
427 memset(r300->vertex_format_state.state, 0, sizeof(struct r300_vertex_info));
428 memcpy(vinfo->hwfmt, r300->vs->hwfmt, sizeof(uint)*4);
429
430 r300_update_rs_block(r300, &r300->vs->outputs, &r300->fs->inputs);
431
432 if (r300screen->caps->has_tcl) {
433 r300_vertex_psc(r300);
434 } else {
435 r300_draw_emit_all_attribs(r300);
436 draw_compute_vertex_size(
437 (struct vertex_info*)r300->vertex_format_state.state);
438 r300_swtcl_vertex_psc(r300);
439 }
440 }
441
442 static boolean r300_dsa_writes_depth_stencil(struct r300_dsa_state* dsa)
443 {
444 /* We are interested only in the cases when a new depth or stencil value
445 * can be written and changed. */
446
447 /* We might optionally check for [Z func: never] and inspect the stencil
448 * state in a similar fashion, but it's not terribly important. */
449 return (dsa->z_buffer_control & R300_Z_WRITE_ENABLE) ||
450 (dsa->stencil_ref_mask & R300_STENCILWRITEMASK_MASK) ||
451 ((dsa->z_buffer_control & R500_STENCIL_REFMASK_FRONT_BACK) &&
452 (dsa->stencil_ref_bf & R300_STENCILWRITEMASK_MASK));
453 }
454
455 static boolean r300_dsa_alpha_test_enabled(struct r300_dsa_state* dsa)
456 {
457 /* We are interested only in the cases when alpha testing can kill
458 * a fragment. */
459 uint32_t af = dsa->alpha_function;
460
461 return (af & R300_FG_ALPHA_FUNC_ENABLE) &&
462 (af & R300_FG_ALPHA_FUNC_ALWAYS) != R300_FG_ALPHA_FUNC_ALWAYS;
463 }
464
465 static void r300_update_ztop(struct r300_context* r300)
466 {
467 struct r300_ztop_state* ztop_state =
468 (struct r300_ztop_state*)r300->ztop_state.state;
469
470 /* This is important enough that I felt it warranted a comment.
471 *
472 * According to the docs, these are the conditions where ZTOP must be
473 * disabled:
474 * 1) Alpha testing enabled
475 * 2) Texture kill instructions in fragment shader
476 * 3) Chroma key culling enabled
477 * 4) W-buffering enabled
478 *
479 * The docs claim that for the first three cases, if no ZS writes happen,
480 * then ZTOP can be used.
481 *
482 * (3) will never apply since we do not support chroma-keyed operations.
483 * (4) will need to be re-examined (and this comment updated) if/when
484 * Hyper-Z becomes supported.
485 *
486 * Additionally, the following conditions require disabled ZTOP:
487 * 5) Depth writes in fragment shader
488 * 6) Outstanding occlusion queries
489 *
490 * This register causes stalls all the way from SC to CB when changed,
491 * but it is buffered on-chip so it does not hurt to write it if it has
492 * not changed.
493 *
494 * ~C.
495 */
496
497 /* ZS writes */
498 if (r300_dsa_writes_depth_stencil(r300->dsa_state.state) &&
499 (r300_dsa_alpha_test_enabled(r300->dsa_state.state) ||/* (1) */
500 r300->fs->info.uses_kill)) { /* (2) */
501 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
502 } else if (r300_fragment_shader_writes_depth(r300->fs)) { /* (5) */
503 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
504 } else if (r300->query_current) { /* (6) */
505 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
506 } else {
507 ztop_state->z_buffer_top = R300_ZTOP_ENABLE;
508 }
509
510 r300->ztop_state.dirty = TRUE;
511 }
512
513 void r300_update_derived_state(struct r300_context* r300)
514 {
515 /* XXX */
516 if (r300->dirty_state &
517 (R300_NEW_FRAGMENT_SHADER | R300_NEW_VERTEX_SHADER) ||
518 r300->vertex_format_state.dirty || r300->rs_state.dirty) {
519 r300_update_derived_shader_state(r300);
520 }
521
522 r300_update_ztop(r300);
523 }