Merge commit 'origin/master' into i965g-restart
[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_vs_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
138 /* Vertex shaders have no semantics on their inputs,
139 * so PSC should just route stuff based on the vertex elements,
140 * and not on attrib information. */
141 DBG(r300, DBG_DRAW, "r300: vs expects %d attribs, routing %d elements"
142 " in psc\n",
143 r300->vs->info.num_inputs,
144 r300->vertex_element_count);
145
146 for (i = 0; i < r300->vertex_element_count; i++) {
147 format = r300->vertex_element[i].src_format;
148
149 type = r300_translate_vertex_data_type(format) |
150 (i << R300_DST_VEC_LOC_SHIFT);
151 swizzle = r300_translate_vertex_data_swizzle(format);
152
153 if (i % 2) {
154 vformat->vap_prog_stream_cntl[i >> 1] |= type << 16;
155 vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle << 16;
156 } else {
157 vformat->vap_prog_stream_cntl[i >> 1] |= type;
158 vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle;
159 }
160 }
161
162
163 assert(i <= 15);
164
165 /* Set the last vector in the PSC. */
166 if (i) {
167 i -= 1;
168 }
169 vformat->vap_prog_stream_cntl[i >> 1] |=
170 (R300_LAST_VEC << (i & 1 ? 16 : 0));
171 }
172
173 /* Update the PSC tables for SW TCL, using Draw. */
174 static void r300_swtcl_vertex_psc(struct r300_context* r300)
175 {
176 struct r300_vertex_info *vformat = r300->vertex_info;
177 struct vertex_info* vinfo = &vformat->vinfo;
178 uint16_t type, swizzle;
179 enum pipe_format format;
180 unsigned i, attrib_count;
181 int* vs_output_tab = r300->vs->output_stream_loc_swtcl;
182
183 /* For each Draw attribute, route it to the fragment shader according
184 * to the vs_output_tab. */
185 attrib_count = vinfo->num_attribs;
186 DBG(r300, DBG_DRAW, "r300: attrib count: %d\n", attrib_count);
187 for (i = 0; i < attrib_count; i++) {
188 DBG(r300, DBG_DRAW, "r300: attrib: offset %d, interp %d, size %d,"
189 " vs_output_tab %d\n", vinfo->attrib[i].src_index,
190 vinfo->attrib[i].interp_mode, vinfo->attrib[i].emit,
191 vs_output_tab[i]);
192 }
193
194 for (i = 0; i < attrib_count; i++) {
195 /* Make sure we have a proper destination for our attribute. */
196 assert(vs_output_tab[i] != -1);
197
198 format = draw_translate_vinfo_format(vinfo->attrib[i].emit);
199
200 /* Obtain the type of data in this attribute. */
201 type = r300_translate_vertex_data_type(format) |
202 vs_output_tab[i] << R300_DST_VEC_LOC_SHIFT;
203
204 /* Obtain the swizzle for this attribute. Note that the default
205 * swizzle in the hardware is not XYZW! */
206 swizzle = r300_translate_vertex_data_swizzle(format);
207
208 /* Add the attribute to the PSC table. */
209 if (i & 1) {
210 vformat->vap_prog_stream_cntl[i >> 1] |= type << 16;
211 vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle << 16;
212 } else {
213 vformat->vap_prog_stream_cntl[i >> 1] |= type;
214 vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle;
215 }
216 }
217
218 /* Set the last vector in the PSC. */
219 if (i) {
220 i -= 1;
221 }
222 vformat->vap_prog_stream_cntl[i >> 1] |=
223 (R300_LAST_VEC << (i & 1 ? 16 : 0));
224 }
225
226 static void r300_rs_col(struct r300_rs_block* rs, int id, int ptr,
227 boolean swizzle_0001)
228 {
229 rs->ip[id] |= R300_RS_COL_PTR(ptr);
230 if (swizzle_0001) {
231 rs->ip[id] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001);
232 } else {
233 rs->ip[id] |= R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
234 }
235 rs->inst[id] |= R300_RS_INST_COL_ID(id);
236 }
237
238 static void r300_rs_col_write(struct r300_rs_block* rs, int id, int fp_offset)
239 {
240 rs->inst[id] |= R300_RS_INST_COL_CN_WRITE |
241 R300_RS_INST_COL_ADDR(fp_offset);
242 }
243
244 static void r300_rs_tex(struct r300_rs_block* rs, int id, int ptr,
245 boolean swizzle_X001)
246 {
247 if (swizzle_X001) {
248 rs->ip[id] |= R300_RS_TEX_PTR(ptr*4) |
249 R300_RS_SEL_S(R300_RS_SEL_C0) |
250 R300_RS_SEL_T(R300_RS_SEL_K0) |
251 R300_RS_SEL_R(R300_RS_SEL_K0) |
252 R300_RS_SEL_Q(R300_RS_SEL_K1);
253 } else {
254 rs->ip[id] |= R300_RS_TEX_PTR(ptr*4) |
255 R300_RS_SEL_S(R300_RS_SEL_C0) |
256 R300_RS_SEL_T(R300_RS_SEL_C1) |
257 R300_RS_SEL_R(R300_RS_SEL_C2) |
258 R300_RS_SEL_Q(R300_RS_SEL_C3);
259 }
260 rs->inst[id] |= R300_RS_INST_TEX_ID(id);
261 }
262
263 static void r300_rs_tex_write(struct r300_rs_block* rs, int id, int fp_offset)
264 {
265 rs->inst[id] |= R300_RS_INST_TEX_CN_WRITE |
266 R300_RS_INST_TEX_ADDR(fp_offset);
267 }
268
269 static void r500_rs_col(struct r300_rs_block* rs, int id, int ptr,
270 boolean swizzle_0001)
271 {
272 rs->ip[id] |= R500_RS_COL_PTR(ptr);
273 if (swizzle_0001) {
274 rs->ip[id] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001);
275 } else {
276 rs->ip[id] |= R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
277 }
278 rs->inst[id] |= R500_RS_INST_COL_ID(id);
279 }
280
281 static void r500_rs_col_write(struct r300_rs_block* rs, int id, int fp_offset)
282 {
283 rs->inst[id] |= R500_RS_INST_COL_CN_WRITE |
284 R500_RS_INST_COL_ADDR(fp_offset);
285 }
286
287 static void r500_rs_tex(struct r300_rs_block* rs, int id, int ptr,
288 boolean swizzle_X001)
289 {
290 int rs_tex_comp = ptr*4;
291
292 if (swizzle_X001) {
293 rs->ip[id] |= R500_RS_SEL_S(rs_tex_comp) |
294 R500_RS_SEL_T(R500_RS_IP_PTR_K0) |
295 R500_RS_SEL_R(R500_RS_IP_PTR_K0) |
296 R500_RS_SEL_Q(R500_RS_IP_PTR_K1);
297 } else {
298 rs->ip[id] |= R500_RS_SEL_S(rs_tex_comp) |
299 R500_RS_SEL_T(rs_tex_comp + 1) |
300 R500_RS_SEL_R(rs_tex_comp + 2) |
301 R500_RS_SEL_Q(rs_tex_comp + 3);
302 }
303 rs->inst[id] |= R500_RS_INST_TEX_ID(id);
304 }
305
306 static void r500_rs_tex_write(struct r300_rs_block* rs, int id, int fp_offset)
307 {
308 rs->inst[id] |= R500_RS_INST_TEX_CN_WRITE |
309 R500_RS_INST_TEX_ADDR(fp_offset);
310 }
311
312 /* Set up the RS block.
313 *
314 * This is the part of the chipset that actually does the rasterization
315 * of vertices into fragments. This is also the part of the chipset that
316 * locks up if any part of it is even slightly wrong. */
317 static void r300_update_rs_block(struct r300_context* r300,
318 struct r300_shader_semantics* vs_outputs,
319 struct r300_shader_semantics* fs_inputs)
320 {
321 struct r300_rs_block* rs = r300->rs_block;
322 int i, col_count = 0, tex_count = 0, fp_offset = 0;
323 void (*rX00_rs_col)(struct r300_rs_block*, int, int, boolean);
324 void (*rX00_rs_col_write)(struct r300_rs_block*, int, int);
325 void (*rX00_rs_tex)(struct r300_rs_block*, int, int, boolean);
326 void (*rX00_rs_tex_write)(struct r300_rs_block*, int, int);
327
328 if (r300_screen(r300->context.screen)->caps->is_r500) {
329 rX00_rs_col = r500_rs_col;
330 rX00_rs_col_write = r500_rs_col_write;
331 rX00_rs_tex = r500_rs_tex;
332 rX00_rs_tex_write = r500_rs_tex_write;
333 } else {
334 rX00_rs_col = r300_rs_col;
335 rX00_rs_col_write = r300_rs_col_write;
336 rX00_rs_tex = r300_rs_tex;
337 rX00_rs_tex_write = r300_rs_tex_write;
338 }
339
340 /* Rasterize colors. */
341 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
342 if (vs_outputs->color[i] != ATTR_UNUSED) {
343 /* Always rasterize if it's written by the VS,
344 * otherwise it locks up. */
345 rX00_rs_col(rs, col_count, i, FALSE);
346
347 /* Write it to the FS input register if it's used by the FS. */
348 if (fs_inputs->color[i] != ATTR_UNUSED) {
349 rX00_rs_col_write(rs, col_count, fp_offset);
350 fp_offset++;
351 }
352 col_count++;
353 } else {
354 /* Skip the FS input register, leave it uninitialized. */
355 /* If we try to set it to (0,0,0,1), it will lock up. */
356 if (fs_inputs->color[i] != ATTR_UNUSED) {
357 fp_offset++;
358 }
359 }
360 }
361
362 /* Rasterize texture coordinates. */
363 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
364 if (vs_outputs->generic[i] != ATTR_UNUSED) {
365 /* Always rasterize if it's written by the VS,
366 * otherwise it locks up. */
367 rX00_rs_tex(rs, tex_count, tex_count, FALSE);
368
369 /* Write it to the FS input register if it's used by the FS. */
370 if (fs_inputs->generic[i] != ATTR_UNUSED) {
371 rX00_rs_tex_write(rs, tex_count, fp_offset);
372 fp_offset++;
373 }
374 tex_count++;
375 } else {
376 /* Skip the FS input register, leave it uninitialized. */
377 /* If we try to set it to (0,0,0,1), it will lock up. */
378 if (fs_inputs->generic[i] != ATTR_UNUSED) {
379 fp_offset++;
380 }
381 }
382 }
383
384 /* Rasterize fog coordinates. */
385 if (vs_outputs->fog != ATTR_UNUSED) {
386 /* Always rasterize if it's written by the VS,
387 * otherwise it locks up. */
388 rX00_rs_tex(rs, tex_count, tex_count, TRUE);
389
390 /* Write it to the FS input register if it's used by the FS. */
391 if (fs_inputs->fog != ATTR_UNUSED) {
392 rX00_rs_tex_write(rs, tex_count, fp_offset);
393 fp_offset++;
394 }
395 tex_count++;
396 } else {
397 /* Skip the FS input register, leave it uninitialized. */
398 /* If we try to set it to (0,0,0,1), it will lock up. */
399 if (fs_inputs->fog != ATTR_UNUSED) {
400 fp_offset++;
401 }
402 }
403
404 /* Rasterize at least one color, or bad things happen. */
405 if (col_count == 0 && tex_count == 0) {
406 rX00_rs_col(rs, 0, 0, TRUE);
407 col_count++;
408 }
409
410 rs->count = (tex_count*4) | (col_count << R300_IC_COUNT_SHIFT) |
411 R300_HIRES_EN;
412
413 rs->inst_count = MAX3(col_count - 1, tex_count - 1, 0);
414 }
415
416 /* Update the vertex format. */
417 static void r300_update_derived_shader_state(struct r300_context* r300)
418 {
419 struct r300_screen* r300screen = r300_screen(r300->context.screen);
420
421 /*
422 struct r300_shader_key* key;
423 struct r300_shader_derived_value* value;
424 key = CALLOC_STRUCT(r300_shader_key);
425 key->vs = r300->vs;
426 key->fs = r300->fs;
427
428 value = (struct r300_shader_derived_value*)
429 util_hash_table_get(r300->shader_hash_table, (void*)key);
430 if (value) {
431 //vformat = value->vformat;
432 rs_block = value->rs_block;
433
434 FREE(key);
435 } else {
436 rs_block = CALLOC_STRUCT(r300_rs_block);
437 value = CALLOC_STRUCT(r300_shader_derived_value);
438
439 r300_update_rs_block(r300, rs_block);
440
441 //value->vformat = vformat;
442 value->rs_block = rs_block;
443 util_hash_table_set(r300->shader_hash_table,
444 (void*)key, (void*)value);
445 } */
446
447 /* Reset structures */
448 memset(r300->rs_block, 0, sizeof(struct r300_rs_block));
449 memset(r300->vertex_info, 0, sizeof(struct r300_vertex_info));
450 memcpy(r300->vertex_info->vinfo.hwfmt, r300->vs->hwfmt, sizeof(uint)*4);
451
452 r300_update_rs_block(r300, &r300->vs->outputs, &r300->fs->inputs);
453
454 if (r300screen->caps->has_tcl) {
455 r300_vertex_psc(r300);
456 } else {
457 r300_draw_emit_all_attribs(r300);
458 draw_compute_vertex_size(&r300->vertex_info->vinfo);
459 r300_swtcl_vertex_psc(r300);
460 }
461
462 r300->dirty_state |= R300_NEW_RS_BLOCK;
463 }
464
465 static void r300_update_ztop(struct r300_context* r300)
466 {
467 r300->ztop_state.z_buffer_top = R300_ZTOP_ENABLE;
468
469 /* This is important enough that I felt it warranted a comment.
470 *
471 * According to the docs, these are the conditions where ZTOP must be
472 * disabled:
473 * 1) Alpha testing enabled
474 * 2) Texture kill instructions in fragment shader
475 * 3) Chroma key culling enabled
476 * 4) W-buffering enabled
477 *
478 * The docs claim that for the first three cases, if no ZS writes happen,
479 * then ZTOP can be used.
480 *
481 * Additionally, the following conditions require disabled ZTOP:
482 * ~) Depth writes in fragment shader
483 * ~) Outstanding occlusion queries
484 *
485 * ~C.
486 */
487 if (r300->dsa_state->alpha_function) {
488 r300->ztop_state.z_buffer_top = R300_ZTOP_DISABLE;
489 } else if (r300->fs->info.uses_kill) {
490 r300->ztop_state.z_buffer_top = R300_ZTOP_DISABLE;
491 } else if (r300_fragment_shader_writes_depth(r300->fs)) {
492 r300->ztop_state.z_buffer_top = R300_ZTOP_DISABLE;
493 } else if (r300->query_current) {
494 r300->ztop_state.z_buffer_top = R300_ZTOP_DISABLE;
495 }
496 }
497
498 void r300_update_derived_state(struct r300_context* r300)
499 {
500 if (r300->dirty_state &
501 (R300_NEW_FRAGMENT_SHADER | R300_NEW_VERTEX_SHADER |
502 R300_NEW_VERTEX_FORMAT)) {
503 r300_update_derived_shader_state(r300);
504 }
505
506 if (r300->dirty_state &
507 (R300_NEW_DSA | R300_NEW_FRAGMENT_SHADER | R300_NEW_QUERY)) {
508 r300_update_ztop(r300);
509 }
510 }