Merge remote-tracking branch 'mareko/r300g-draw-instanced' into pipe-video
[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 #include "util/u_pack_color.h"
29
30 #include "r300_context.h"
31 #include "r300_fs.h"
32 #include "r300_screen.h"
33 #include "r300_shader_semantics.h"
34 #include "r300_state_inlines.h"
35 #include "r300_texture.h"
36 #include "r300_vs.h"
37
38 /* r300_state_derived: Various bits of state which are dependent upon
39 * currently bound CSO data. */
40
41 enum r300_rs_swizzle {
42 SWIZ_XYZW = 0,
43 SWIZ_X001,
44 SWIZ_XY01,
45 SWIZ_0001,
46 };
47
48 enum r300_rs_col_write_type {
49 WRITE_COLOR = 0,
50 WRITE_FACE
51 };
52
53 static void r300_draw_emit_attrib(struct r300_context* r300,
54 enum attrib_emit emit,
55 enum interp_mode interp,
56 int index)
57 {
58 struct r300_vertex_shader* vs = r300->vs_state.state;
59 struct tgsi_shader_info* info = &vs->info;
60 int output;
61
62 output = draw_find_shader_output(r300->draw,
63 info->output_semantic_name[index],
64 info->output_semantic_index[index]);
65 draw_emit_vertex_attr(&r300->vertex_info, emit, interp, output);
66 }
67
68 static void r300_draw_emit_all_attribs(struct r300_context* r300)
69 {
70 struct r300_vertex_shader* vs = r300->vs_state.state;
71 struct r300_shader_semantics* vs_outputs = &vs->outputs;
72 int i, gen_count;
73
74 /* Position. */
75 if (vs_outputs->pos != ATTR_UNUSED) {
76 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
77 vs_outputs->pos);
78 } else {
79 assert(0);
80 }
81
82 /* Point size. */
83 if (vs_outputs->psize != ATTR_UNUSED) {
84 r300_draw_emit_attrib(r300, EMIT_1F_PSIZE, INTERP_POS,
85 vs_outputs->psize);
86 }
87
88 /* Colors. */
89 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
90 if (vs_outputs->color[i] != ATTR_UNUSED) {
91 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_LINEAR,
92 vs_outputs->color[i]);
93 }
94 }
95
96 /* Back-face colors. */
97 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
98 if (vs_outputs->bcolor[i] != ATTR_UNUSED) {
99 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_LINEAR,
100 vs_outputs->bcolor[i]);
101 }
102 }
103
104 /* Texture coordinates. */
105 /* Only 8 generic vertex attributes can be used. If there are more,
106 * they won't be rasterized. */
107 gen_count = 0;
108 for (i = 0; i < ATTR_GENERIC_COUNT && gen_count < 8; i++) {
109 if (vs_outputs->generic[i] != ATTR_UNUSED &&
110 !(r300->sprite_coord_enable & (1 << i))) {
111 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
112 vs_outputs->generic[i]);
113 gen_count++;
114 }
115 }
116
117 /* Fog coordinates. */
118 if (gen_count < 8 && vs_outputs->fog != ATTR_UNUSED) {
119 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
120 vs_outputs->fog);
121 gen_count++;
122 }
123
124 /* WPOS. */
125 if (r300_fs(r300)->shader->inputs.wpos != ATTR_UNUSED && gen_count < 8) {
126 DBG(r300, DBG_SWTCL, "draw_emit_attrib: WPOS, index: %i\n",
127 vs_outputs->wpos);
128 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
129 vs_outputs->wpos);
130 }
131 }
132
133 /* Update the PSC tables for SW TCL, using Draw. */
134 static void r300_swtcl_vertex_psc(struct r300_context *r300)
135 {
136 struct r300_vertex_element_state *velems =
137 (struct r300_vertex_element_state*)r300->vertex_stream_state.state;
138 struct r300_vertex_stream_state *vstream = &velems->vertex_stream;
139 struct vertex_info *vinfo = &r300->vertex_info;
140 uint16_t type, swizzle;
141 enum pipe_format format;
142 unsigned i, attrib_count;
143 int* vs_output_tab = r300->stream_loc_notcl;
144
145 memset(vstream, 0, sizeof(struct r300_vertex_stream_state));
146
147 /* For each Draw attribute, route it to the fragment shader according
148 * to the vs_output_tab. */
149 attrib_count = vinfo->num_attribs;
150 DBG(r300, DBG_SWTCL, "r300: attrib count: %d\n", attrib_count);
151 for (i = 0; i < attrib_count; i++) {
152 if (vs_output_tab[i] == -1) {
153 assert(0);
154 abort();
155 }
156
157 format = draw_translate_vinfo_format(vinfo->attrib[i].emit);
158
159 DBG(r300, DBG_SWTCL,
160 "r300: swtcl_vertex_psc [%i] <- %s\n",
161 vs_output_tab[i], util_format_short_name(format));
162
163 /* Obtain the type of data in this attribute. */
164 type = r300_translate_vertex_data_type(format);
165 if (type == R300_INVALID_FORMAT) {
166 fprintf(stderr, "r300: Bad vertex format %s.\n",
167 util_format_short_name(format));
168 assert(0);
169 abort();
170 }
171
172 type |= vs_output_tab[i] << R300_DST_VEC_LOC_SHIFT;
173
174 /* Obtain the swizzle for this attribute. Note that the default
175 * swizzle in the hardware is not XYZW! */
176 swizzle = r300_translate_vertex_data_swizzle(format);
177
178 /* Add the attribute to the PSC table. */
179 if (i & 1) {
180 vstream->vap_prog_stream_cntl[i >> 1] |= type << 16;
181 vstream->vap_prog_stream_cntl_ext[i >> 1] |= swizzle << 16;
182 } else {
183 vstream->vap_prog_stream_cntl[i >> 1] |= type;
184 vstream->vap_prog_stream_cntl_ext[i >> 1] |= swizzle;
185 }
186 }
187
188 /* Set the last vector in the PSC. */
189 if (i) {
190 i -= 1;
191 }
192 vstream->vap_prog_stream_cntl[i >> 1] |=
193 (R300_LAST_VEC << (i & 1 ? 16 : 0));
194
195 vstream->count = (i >> 1) + 1;
196 r300_mark_atom_dirty(r300, &r300->vertex_stream_state);
197 r300->vertex_stream_state.size = (1 + vstream->count) * 2;
198 }
199
200 static void r300_rs_col(struct r300_rs_block* rs, int id, int ptr,
201 enum r300_rs_swizzle swiz)
202 {
203 rs->ip[id] |= R300_RS_COL_PTR(ptr);
204 if (swiz == SWIZ_0001) {
205 rs->ip[id] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001);
206 } else {
207 rs->ip[id] |= R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
208 }
209 rs->inst[id] |= R300_RS_INST_COL_ID(id);
210 }
211
212 static void r300_rs_col_write(struct r300_rs_block* rs, int id, int fp_offset,
213 enum r300_rs_col_write_type type)
214 {
215 assert(type == WRITE_COLOR);
216 rs->inst[id] |= R300_RS_INST_COL_CN_WRITE |
217 R300_RS_INST_COL_ADDR(fp_offset);
218 }
219
220 static void r300_rs_tex(struct r300_rs_block* rs, int id, int ptr,
221 enum r300_rs_swizzle swiz)
222 {
223 if (swiz == SWIZ_X001) {
224 rs->ip[id] |= R300_RS_TEX_PTR(ptr) |
225 R300_RS_SEL_S(R300_RS_SEL_C0) |
226 R300_RS_SEL_T(R300_RS_SEL_K0) |
227 R300_RS_SEL_R(R300_RS_SEL_K0) |
228 R300_RS_SEL_Q(R300_RS_SEL_K1);
229 } else if (swiz == SWIZ_XY01) {
230 rs->ip[id] |= R300_RS_TEX_PTR(ptr) |
231 R300_RS_SEL_S(R300_RS_SEL_C0) |
232 R300_RS_SEL_T(R300_RS_SEL_C1) |
233 R300_RS_SEL_R(R300_RS_SEL_K0) |
234 R300_RS_SEL_Q(R300_RS_SEL_K1);
235 } else {
236 rs->ip[id] |= R300_RS_TEX_PTR(ptr) |
237 R300_RS_SEL_S(R300_RS_SEL_C0) |
238 R300_RS_SEL_T(R300_RS_SEL_C1) |
239 R300_RS_SEL_R(R300_RS_SEL_C2) |
240 R300_RS_SEL_Q(R300_RS_SEL_C3);
241 }
242 rs->inst[id] |= R300_RS_INST_TEX_ID(id);
243 }
244
245 static void r300_rs_tex_write(struct r300_rs_block* rs, int id, int fp_offset)
246 {
247 rs->inst[id] |= R300_RS_INST_TEX_CN_WRITE |
248 R300_RS_INST_TEX_ADDR(fp_offset);
249 }
250
251 static void r500_rs_col(struct r300_rs_block* rs, int id, int ptr,
252 enum r300_rs_swizzle swiz)
253 {
254 rs->ip[id] |= R500_RS_COL_PTR(ptr);
255 if (swiz == SWIZ_0001) {
256 rs->ip[id] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001);
257 } else {
258 rs->ip[id] |= R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
259 }
260 rs->inst[id] |= R500_RS_INST_COL_ID(id);
261 }
262
263 static void r500_rs_col_write(struct r300_rs_block* rs, int id, int fp_offset,
264 enum r300_rs_col_write_type type)
265 {
266 if (type == WRITE_FACE)
267 rs->inst[id] |= R500_RS_INST_COL_CN_WRITE_BACKFACE |
268 R500_RS_INST_COL_ADDR(fp_offset);
269 else
270 rs->inst[id] |= R500_RS_INST_COL_CN_WRITE |
271 R500_RS_INST_COL_ADDR(fp_offset);
272
273 }
274
275 static void r500_rs_tex(struct r300_rs_block* rs, int id, int ptr,
276 enum r300_rs_swizzle swiz)
277 {
278 if (swiz == SWIZ_X001) {
279 rs->ip[id] |= R500_RS_SEL_S(ptr) |
280 R500_RS_SEL_T(R500_RS_IP_PTR_K0) |
281 R500_RS_SEL_R(R500_RS_IP_PTR_K0) |
282 R500_RS_SEL_Q(R500_RS_IP_PTR_K1);
283 } else if (swiz == SWIZ_XY01) {
284 rs->ip[id] |= R500_RS_SEL_S(ptr) |
285 R500_RS_SEL_T(ptr + 1) |
286 R500_RS_SEL_R(R500_RS_IP_PTR_K0) |
287 R500_RS_SEL_Q(R500_RS_IP_PTR_K1);
288 } else {
289 rs->ip[id] |= R500_RS_SEL_S(ptr) |
290 R500_RS_SEL_T(ptr + 1) |
291 R500_RS_SEL_R(ptr + 2) |
292 R500_RS_SEL_Q(ptr + 3);
293 }
294 rs->inst[id] |= R500_RS_INST_TEX_ID(id);
295 }
296
297 static void r500_rs_tex_write(struct r300_rs_block* rs, int id, int fp_offset)
298 {
299 rs->inst[id] |= R500_RS_INST_TEX_CN_WRITE |
300 R500_RS_INST_TEX_ADDR(fp_offset);
301 }
302
303 /* Set up the RS block.
304 *
305 * This is the part of the chipset that is responsible for linking vertex
306 * and fragment shaders and stuffed texture coordinates.
307 *
308 * The rasterizer reads data from VAP, which produces vertex shader outputs,
309 * and GA, which produces stuffed texture coordinates. VAP outputs have
310 * precedence over GA. All outputs must be rasterized otherwise it locks up.
311 * If there are more outputs rasterized than is set in VAP/GA, it locks up
312 * too. The funky part is that this info has been pretty much obtained by trial
313 * and error. */
314 static void r300_update_rs_block(struct r300_context *r300)
315 {
316 struct r300_vertex_shader *vs = r300->vs_state.state;
317 struct r300_shader_semantics *vs_outputs = &vs->outputs;
318 struct r300_shader_semantics *fs_inputs = &r300_fs(r300)->shader->inputs;
319 struct r300_rs_block rs = {0};
320 int i, col_count = 0, tex_count = 0, fp_offset = 0, count, loc = 0, tex_ptr = 0;
321 void (*rX00_rs_col)(struct r300_rs_block*, int, int, enum r300_rs_swizzle);
322 void (*rX00_rs_col_write)(struct r300_rs_block*, int, int, enum r300_rs_col_write_type);
323 void (*rX00_rs_tex)(struct r300_rs_block*, int, int, enum r300_rs_swizzle);
324 void (*rX00_rs_tex_write)(struct r300_rs_block*, int, int);
325 boolean any_bcolor_used = vs_outputs->bcolor[0] != ATTR_UNUSED ||
326 vs_outputs->bcolor[1] != ATTR_UNUSED;
327 int *stream_loc_notcl = r300->stream_loc_notcl;
328 uint32_t stuffing_enable = 0;
329
330 if (r300->screen->caps.is_r500) {
331 rX00_rs_col = r500_rs_col;
332 rX00_rs_col_write = r500_rs_col_write;
333 rX00_rs_tex = r500_rs_tex;
334 rX00_rs_tex_write = r500_rs_tex_write;
335 } else {
336 rX00_rs_col = r300_rs_col;
337 rX00_rs_col_write = r300_rs_col_write;
338 rX00_rs_tex = r300_rs_tex;
339 rX00_rs_tex_write = r300_rs_tex_write;
340 }
341
342 /* 0x5555 copied from classic, which means:
343 * Select user color 0 for COLOR0 up to COLOR7.
344 * What the hell does that mean? */
345 rs.vap_vtx_state_cntl = 0x5555;
346
347 /* The position is always present in VAP. */
348 rs.vap_vsm_vtx_assm |= R300_INPUT_CNTL_POS;
349 rs.vap_out_vtx_fmt[0] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT;
350 stream_loc_notcl[loc++] = 0;
351
352 /* Set up the point size in VAP. */
353 if (vs_outputs->psize != ATTR_UNUSED) {
354 rs.vap_out_vtx_fmt[0] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT;
355 stream_loc_notcl[loc++] = 1;
356 }
357
358 /* Set up and rasterize colors. */
359 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
360 if (vs_outputs->color[i] != ATTR_UNUSED || any_bcolor_used ||
361 vs_outputs->color[1] != ATTR_UNUSED) {
362 /* Set up the color in VAP. */
363 rs.vap_vsm_vtx_assm |= R300_INPUT_CNTL_COLOR;
364 rs.vap_out_vtx_fmt[0] |=
365 R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << i;
366 stream_loc_notcl[loc++] = 2 + i;
367
368 /* Rasterize it. */
369 rX00_rs_col(&rs, col_count, col_count, SWIZ_XYZW);
370
371 /* Write it to the FS input register if it's needed by the FS. */
372 if (fs_inputs->color[i] != ATTR_UNUSED) {
373 rX00_rs_col_write(&rs, col_count, fp_offset, WRITE_COLOR);
374 fp_offset++;
375
376 DBG(r300, DBG_RS,
377 "r300: Rasterized color %i written to FS.\n", i);
378 } else {
379 DBG(r300, DBG_RS, "r300: Rasterized color %i unused.\n", i);
380 }
381 col_count++;
382 } else {
383 /* Skip the FS input register, leave it uninitialized. */
384 /* If we try to set it to (0,0,0,1), it will lock up. */
385 if (fs_inputs->color[i] != ATTR_UNUSED) {
386 fp_offset++;
387
388 DBG(r300, DBG_RS, "r300: FS input color %i unassigned%s.\n",
389 i);
390 }
391 }
392 }
393
394 /* Set up back-face colors. The rasterizer will do the color selection
395 * automatically. */
396 if (any_bcolor_used) {
397 if (r300->two_sided_color) {
398 /* Rasterize as back-face colors. */
399 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
400 rs.vap_vsm_vtx_assm |= R300_INPUT_CNTL_COLOR;
401 rs.vap_out_vtx_fmt[0] |= R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << (2+i);
402 stream_loc_notcl[loc++] = 4 + i;
403 }
404 } else {
405 /* Rasterize two fake texcoords to prevent from the two-sided color
406 * selection. */
407 /* XXX Consider recompiling the vertex shader to save 2 RS units. */
408 for (i = 0; i < 2; i++) {
409 rs.vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << tex_count);
410 rs.vap_out_vtx_fmt[1] |= (4 << (3 * tex_count));
411 stream_loc_notcl[loc++] = 6 + tex_count;
412
413 /* Rasterize it. */
414 rX00_rs_tex(&rs, tex_count, tex_ptr, SWIZ_XYZW);
415 tex_count++;
416 tex_ptr += 4;
417 }
418 }
419 }
420
421 /* gl_FrontFacing.
422 * Note that we can use either the two-sided color selection based on
423 * the front and back vertex shader colors, or gl_FrontFacing,
424 * but not both! It locks up otherwise.
425 *
426 * In Direct3D 9, the two-sided color selection can be used
427 * with shaders 2.0 only, while gl_FrontFacing can be used
428 * with shaders 3.0 only. The hardware apparently hasn't been designed
429 * to support both at the same time. */
430 if (r300->screen->caps.is_r500 && fs_inputs->face != ATTR_UNUSED &&
431 !(any_bcolor_used && r300->two_sided_color)) {
432 rX00_rs_col(&rs, col_count, col_count, SWIZ_XYZW);
433 rX00_rs_col_write(&rs, col_count, fp_offset, WRITE_FACE);
434 fp_offset++;
435 col_count++;
436 DBG(r300, DBG_RS, "r300: Rasterized FACE written to FS.\n");
437 } else if (fs_inputs->face != ATTR_UNUSED) {
438 fprintf(stderr, "r300: ERROR: FS input FACE unassigned.\n");
439 }
440
441 /* Rasterize texture coordinates. */
442 for (i = 0; i < ATTR_GENERIC_COUNT && tex_count < 8; i++) {
443 bool sprite_coord = false;
444
445 if (fs_inputs->generic[i] != ATTR_UNUSED) {
446 sprite_coord = !!(r300->sprite_coord_enable & (1 << i));
447 }
448
449 if (vs_outputs->generic[i] != ATTR_UNUSED || sprite_coord) {
450 if (!sprite_coord) {
451 /* Set up the texture coordinates in VAP. */
452 rs.vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << tex_count);
453 rs.vap_out_vtx_fmt[1] |= (4 << (3 * tex_count));
454 stream_loc_notcl[loc++] = 6 + tex_count;
455 } else
456 stuffing_enable |=
457 R300_GB_TEX_ST << (R300_GB_TEX0_SOURCE_SHIFT + (tex_count*2));
458
459 /* Rasterize it. */
460 rX00_rs_tex(&rs, tex_count, tex_ptr,
461 sprite_coord ? SWIZ_XY01 : SWIZ_XYZW);
462
463 /* Write it to the FS input register if it's needed by the FS. */
464 if (fs_inputs->generic[i] != ATTR_UNUSED) {
465 rX00_rs_tex_write(&rs, tex_count, fp_offset);
466 fp_offset++;
467
468 DBG(r300, DBG_RS,
469 "r300: Rasterized generic %i written to FS%s in texcoord %d.\n",
470 i, sprite_coord ? " (sprite coord)" : "", tex_count);
471 } else {
472 DBG(r300, DBG_RS,
473 "r300: Rasterized generic %i unused%s.\n",
474 i, sprite_coord ? " (sprite coord)" : "");
475 }
476 tex_count++;
477 tex_ptr += sprite_coord ? 2 : 4;
478 } else {
479 /* Skip the FS input register, leave it uninitialized. */
480 /* If we try to set it to (0,0,0,1), it will lock up. */
481 if (fs_inputs->generic[i] != ATTR_UNUSED) {
482 fp_offset++;
483
484 DBG(r300, DBG_RS, "r300: FS input generic %i unassigned%s.\n",
485 i, sprite_coord ? " (sprite coord)" : "");
486 }
487 }
488 }
489
490 for (; i < ATTR_GENERIC_COUNT; i++) {
491 if (fs_inputs->generic[i] != ATTR_UNUSED) {
492 fprintf(stderr, "r300: ERROR: FS input generic %i unassigned, "
493 "not enough hardware slots (it's not a bug, do not "
494 "report it).\n", i);
495 }
496 }
497
498 /* Rasterize fog coordinates. */
499 if (vs_outputs->fog != ATTR_UNUSED && tex_count < 8) {
500 /* Set up the fog coordinates in VAP. */
501 rs.vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << tex_count);
502 rs.vap_out_vtx_fmt[1] |= (4 << (3 * tex_count));
503 stream_loc_notcl[loc++] = 6 + tex_count;
504
505 /* Rasterize it. */
506 rX00_rs_tex(&rs, tex_count, tex_ptr, SWIZ_X001);
507
508 /* Write it to the FS input register if it's needed by the FS. */
509 if (fs_inputs->fog != ATTR_UNUSED) {
510 rX00_rs_tex_write(&rs, tex_count, fp_offset);
511 fp_offset++;
512
513 DBG(r300, DBG_RS, "r300: Rasterized fog written to FS.\n");
514 } else {
515 DBG(r300, DBG_RS, "r300: Rasterized fog unused.\n");
516 }
517 tex_count++;
518 tex_ptr += 4;
519 } else {
520 /* Skip the FS input register, leave it uninitialized. */
521 /* If we try to set it to (0,0,0,1), it will lock up. */
522 if (fs_inputs->fog != ATTR_UNUSED) {
523 fp_offset++;
524
525 if (tex_count < 8) {
526 DBG(r300, DBG_RS, "r300: FS input fog unassigned.\n");
527 } else {
528 fprintf(stderr, "r300: ERROR: FS input fog unassigned, "
529 "not enough hardware slots. (it's not a bug, "
530 "do not report it)\n");
531 }
532 }
533 }
534
535 /* Rasterize WPOS. */
536 /* Don't set it in VAP if the FS doesn't need it. */
537 if (fs_inputs->wpos != ATTR_UNUSED && tex_count < 8) {
538 /* Set up the WPOS coordinates in VAP. */
539 rs.vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << tex_count);
540 rs.vap_out_vtx_fmt[1] |= (4 << (3 * tex_count));
541 stream_loc_notcl[loc++] = 6 + tex_count;
542
543 /* Rasterize it. */
544 rX00_rs_tex(&rs, tex_count, tex_ptr, SWIZ_XYZW);
545
546 /* Write it to the FS input register. */
547 rX00_rs_tex_write(&rs, tex_count, fp_offset);
548
549 DBG(r300, DBG_RS, "r300: Rasterized WPOS written to FS.\n");
550
551 fp_offset++;
552 tex_count++;
553 tex_ptr += 4;
554 } else {
555 if (fs_inputs->wpos != ATTR_UNUSED && tex_count >= 8) {
556 fprintf(stderr, "r300: ERROR: FS input WPOS unassigned, "
557 "not enough hardware slots. (it's not a bug, do not "
558 "report it)\n");
559 }
560 }
561
562 /* Invalidate the rest of the no-TCL (GA) stream locations. */
563 for (; loc < 16;) {
564 stream_loc_notcl[loc++] = -1;
565 }
566
567 /* Rasterize at least one color, or bad things happen. */
568 if (col_count == 0 && tex_count == 0) {
569 rX00_rs_col(&rs, 0, 0, SWIZ_0001);
570 col_count++;
571
572 DBG(r300, DBG_RS, "r300: Rasterized color 0 to prevent lockups.\n");
573 }
574
575 DBG(r300, DBG_RS, "r300: --- Rasterizer status ---: colors: %i, "
576 "generics: %i.\n", col_count, tex_count);
577
578 rs.count = MIN2(tex_ptr, 32) | (col_count << R300_IC_COUNT_SHIFT) |
579 R300_HIRES_EN;
580
581 count = MAX3(col_count, tex_count, 1);
582 rs.inst_count = count - 1;
583
584 /* set the GB enable flags */
585 if (r300->sprite_coord_enable)
586 stuffing_enable |= R300_GB_POINT_STUFF_ENABLE;
587
588 rs.gb_enable = stuffing_enable;
589
590 /* Now, after all that, see if we actually need to update the state. */
591 if (memcmp(r300->rs_block_state.state, &rs, sizeof(struct r300_rs_block))) {
592 memcpy(r300->rs_block_state.state, &rs, sizeof(struct r300_rs_block));
593 r300->rs_block_state.size = 13 + count*2;
594 }
595 }
596
597 static void rgba_to_bgra(float color[4])
598 {
599 float x = color[0];
600 color[0] = color[2];
601 color[2] = x;
602 }
603
604 static uint32_t r300_get_border_color(enum pipe_format format,
605 const float border[4],
606 boolean is_r500)
607 {
608 const struct util_format_description *desc;
609 float border_swizzled[4] = {0};
610 unsigned i;
611 union util_color uc = {0};
612
613 desc = util_format_description(format);
614
615 /* Do depth formats first. */
616 if (util_format_is_depth_or_stencil(format)) {
617 switch (format) {
618 case PIPE_FORMAT_Z16_UNORM:
619 return util_pack_z(PIPE_FORMAT_Z16_UNORM, border[0]);
620 case PIPE_FORMAT_X8Z24_UNORM:
621 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
622 if (is_r500) {
623 return util_pack_z(PIPE_FORMAT_X8Z24_UNORM, border[0]);
624 } else {
625 return util_pack_z(PIPE_FORMAT_Z16_UNORM, border[0]) << 16;
626 }
627 default:
628 assert(0);
629 return 0;
630 }
631 }
632
633 /* Apply inverse swizzle of the format. */
634 for (i = 0; i < 4; i++) {
635 switch (desc->swizzle[i]) {
636 case UTIL_FORMAT_SWIZZLE_X:
637 border_swizzled[0] = border[i];
638 break;
639 case UTIL_FORMAT_SWIZZLE_Y:
640 border_swizzled[1] = border[i];
641 break;
642 case UTIL_FORMAT_SWIZZLE_Z:
643 border_swizzled[2] = border[i];
644 break;
645 case UTIL_FORMAT_SWIZZLE_W:
646 border_swizzled[3] = border[i];
647 break;
648 }
649 }
650
651 /* Compressed formats. */
652 if (util_format_is_compressed(format)) {
653 switch (format) {
654 case PIPE_FORMAT_RGTC1_SNORM:
655 case PIPE_FORMAT_LATC1_SNORM:
656 border_swizzled[0] = border_swizzled[0] < 0 ?
657 border_swizzled[0]*0.5+1 :
658 border_swizzled[0]*0.5;
659 /* Pass through. */
660
661 case PIPE_FORMAT_RGTC1_UNORM:
662 case PIPE_FORMAT_LATC1_UNORM:
663 /* Add 1/32 to round the border color instead of truncating. */
664 /* The Y component is used for the border color. */
665 border_swizzled[1] = border_swizzled[0] + 1.0f/32;
666 util_pack_color(border_swizzled, PIPE_FORMAT_B4G4R4A4_UNORM, &uc);
667 return uc.ui;
668 case PIPE_FORMAT_RGTC2_SNORM:
669 case PIPE_FORMAT_LATC2_SNORM:
670 util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_SNORM, &uc);
671 return uc.ui;
672 case PIPE_FORMAT_RGTC2_UNORM:
673 case PIPE_FORMAT_LATC2_UNORM:
674 util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
675 return uc.ui;
676 default:
677 util_pack_color(border_swizzled, PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
678 return uc.ui;
679 }
680 }
681
682 switch (desc->channel[0].size) {
683 case 2:
684 rgba_to_bgra(border_swizzled);
685 util_pack_color(border_swizzled, PIPE_FORMAT_B2G3R3_UNORM, &uc);
686 break;
687
688 case 4:
689 rgba_to_bgra(border_swizzled);
690 util_pack_color(border_swizzled, PIPE_FORMAT_B4G4R4A4_UNORM, &uc);
691 break;
692
693 case 5:
694 rgba_to_bgra(border_swizzled);
695 if (desc->channel[1].size == 5) {
696 util_pack_color(border_swizzled, PIPE_FORMAT_B5G5R5A1_UNORM, &uc);
697 } else if (desc->channel[1].size == 6) {
698 util_pack_color(border_swizzled, PIPE_FORMAT_B5G6R5_UNORM, &uc);
699 } else {
700 assert(0);
701 }
702 break;
703
704 default:
705 case 8:
706 if (desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED)
707 util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_SNORM, &uc);
708 else
709 util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
710 break;
711
712 case 10:
713 util_pack_color(border_swizzled, PIPE_FORMAT_R10G10B10A2_UNORM, &uc);
714 break;
715
716 case 16:
717 if (desc->nr_channels <= 2) {
718 if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT) {
719 util_pack_color(border_swizzled, PIPE_FORMAT_R16G16_FLOAT, &uc);
720 } else if (desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) {
721 util_pack_color(border_swizzled, PIPE_FORMAT_R16G16_SNORM, &uc);
722 } else {
723 util_pack_color(border_swizzled, PIPE_FORMAT_R16G16_UNORM, &uc);
724 }
725 } else {
726 if (desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) {
727 util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_SNORM, &uc);
728 } else {
729 util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
730 }
731 }
732 break;
733
734 case 32:
735 if (desc->nr_channels == 1) {
736 util_pack_color(border_swizzled, PIPE_FORMAT_R32_FLOAT, &uc);
737 } else {
738 util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
739 }
740 break;
741 }
742
743 return uc.ui;
744 }
745
746 static void r300_merge_textures_and_samplers(struct r300_context* r300)
747 {
748 struct r300_textures_state *state =
749 (struct r300_textures_state*)r300->textures_state.state;
750 struct r300_texture_sampler_state *texstate;
751 struct r300_sampler_state *sampler;
752 struct r300_sampler_view *view;
753 struct r300_resource *tex;
754 unsigned base_level, min_level, level_count, i, j, size;
755 unsigned count = MIN2(state->sampler_view_count,
756 state->sampler_state_count);
757 boolean has_us_format = r300->screen->caps.has_us_format;
758
759 /* The KIL opcode fix, see below. */
760 if (!count && !r300->screen->caps.is_r500)
761 count = 1;
762
763 state->tx_enable = 0;
764 state->count = 0;
765 size = 2;
766
767 for (i = 0; i < count; i++) {
768 if (state->sampler_views[i] && state->sampler_states[i]) {
769 state->tx_enable |= 1 << i;
770
771 view = state->sampler_views[i];
772 tex = r300_resource(view->base.texture);
773 sampler = state->sampler_states[i];
774
775 texstate = &state->regs[i];
776 texstate->format = view->format;
777 texstate->filter0 = sampler->filter0;
778 texstate->filter1 = sampler->filter1;
779
780 /* Set the border color. */
781 texstate->border_color =
782 r300_get_border_color(view->base.format,
783 sampler->state.border_color,
784 r300->screen->caps.is_r500);
785
786 /* determine min/max levels */
787 base_level = view->base.u.tex.first_level;
788 min_level = sampler->min_lod;
789 level_count = MIN3(sampler->max_lod,
790 tex->b.b.b.last_level - base_level,
791 view->base.u.tex.last_level - base_level);
792
793 if (base_level + min_level) {
794 unsigned offset;
795
796 if (tex->tex.is_npot) {
797 /* Even though we do not implement mipmapping for NPOT
798 * textures, we should at least honor the minimum level
799 * which is allowed to be displayed. We do this by setting up
800 * an i-th mipmap level as the zero level. */
801 base_level += min_level;
802 }
803 offset = tex->tex_offset +
804 tex->tex.offset_in_bytes[base_level];
805
806 r300_texture_setup_format_state(r300->screen, tex,
807 base_level,
808 &texstate->format);
809 texstate->format.tile_config |= offset & 0xffffffe0;
810 assert((offset & 0x1f) == 0);
811 } else {
812 texstate->format.tile_config |= tex->tex_offset & 0xffffffe0;
813 assert((tex->tex_offset & 0x1f) == 0);
814 }
815
816 /* Assign a texture cache region. */
817 texstate->format.format1 |= view->texcache_region;
818
819 /* Depth textures are kinda special. */
820 if (util_format_is_depth_or_stencil(tex->b.b.b.format)) {
821 unsigned char depth_swizzle[4];
822
823 if (!r300->screen->caps.is_r500 &&
824 util_format_get_blocksizebits(tex->b.b.b.format) == 32) {
825 /* X24x8 is sampled as Y16X16 on r3xx-r4xx.
826 * The depth here is at the Y component. */
827 for (j = 0; j < 4; j++)
828 depth_swizzle[j] = UTIL_FORMAT_SWIZZLE_Y;
829 } else {
830 for (j = 0; j < 4; j++)
831 depth_swizzle[j] = UTIL_FORMAT_SWIZZLE_X;
832 }
833
834 /* If compare mode is disabled, sampler view swizzles
835 * are stored in the format.
836 * Otherwise, the swizzles must be applied after the compare
837 * mode in the fragment shader. */
838 if (sampler->state.compare_mode == PIPE_TEX_COMPARE_NONE) {
839 texstate->format.format1 |=
840 r300_get_swizzle_combined(depth_swizzle,
841 view->swizzle, FALSE);
842 } else {
843 texstate->format.format1 |=
844 r300_get_swizzle_combined(depth_swizzle, 0, FALSE);
845 }
846 }
847
848 if (r300->screen->caps.dxtc_swizzle &&
849 util_format_is_compressed(tex->b.b.b.format)) {
850 texstate->filter1 |= R400_DXTC_SWIZZLE_ENABLE;
851 }
852
853 /* to emulate 1D textures through 2D ones correctly */
854 if (tex->b.b.b.target == PIPE_TEXTURE_1D) {
855 texstate->filter0 &= ~R300_TX_WRAP_T_MASK;
856 texstate->filter0 |= R300_TX_WRAP_T(R300_TX_CLAMP_TO_EDGE);
857 }
858
859 if (tex->tex.is_npot) {
860 /* NPOT textures don't support mip filter, unfortunately.
861 * This prevents incorrect rendering. */
862 texstate->filter0 &= ~R300_TX_MIN_FILTER_MIP_MASK;
863
864 /* Mask out the mirrored flag. */
865 if (texstate->filter0 & R300_TX_WRAP_S(R300_TX_MIRRORED)) {
866 texstate->filter0 &= ~R300_TX_WRAP_S(R300_TX_MIRRORED);
867 }
868 if (texstate->filter0 & R300_TX_WRAP_T(R300_TX_MIRRORED)) {
869 texstate->filter0 &= ~R300_TX_WRAP_T(R300_TX_MIRRORED);
870 }
871
872 /* Change repeat to clamp-to-edge.
873 * (the repeat bit has a value of 0, no masking needed). */
874 if ((texstate->filter0 & R300_TX_WRAP_S_MASK) ==
875 R300_TX_WRAP_S(R300_TX_REPEAT)) {
876 texstate->filter0 |= R300_TX_WRAP_S(R300_TX_CLAMP_TO_EDGE);
877 }
878 if ((texstate->filter0 & R300_TX_WRAP_T_MASK) ==
879 R300_TX_WRAP_T(R300_TX_REPEAT)) {
880 texstate->filter0 |= R300_TX_WRAP_T(R300_TX_CLAMP_TO_EDGE);
881 }
882 } else {
883 /* the MAX_MIP level is the largest (finest) one */
884 texstate->format.format0 |= R300_TX_NUM_LEVELS(level_count);
885 texstate->filter0 |= R300_TX_MAX_MIP_LEVEL(min_level);
886 }
887
888 /* Float textures only support nearest and mip-nearest filtering. */
889 if (util_format_is_float(tex->b.b.b.format)) {
890 /* No MAG linear filtering. */
891 if ((texstate->filter0 & R300_TX_MAG_FILTER_MASK) ==
892 R300_TX_MAG_FILTER_LINEAR) {
893 texstate->filter0 &= ~R300_TX_MAG_FILTER_MASK;
894 texstate->filter0 |= R300_TX_MAG_FILTER_NEAREST;
895 }
896 /* No MIN linear filtering. */
897 if ((texstate->filter0 & R300_TX_MIN_FILTER_MASK) ==
898 R300_TX_MIN_FILTER_LINEAR) {
899 texstate->filter0 &= ~R300_TX_MIN_FILTER_MASK;
900 texstate->filter0 |= R300_TX_MIN_FILTER_NEAREST;
901 }
902 /* No mipmap linear filtering. */
903 if ((texstate->filter0 & R300_TX_MIN_FILTER_MIP_MASK) ==
904 R300_TX_MIN_FILTER_MIP_LINEAR) {
905 texstate->filter0 &= ~R300_TX_MIN_FILTER_MIP_MASK;
906 texstate->filter0 |= R300_TX_MIN_FILTER_MIP_NEAREST;
907 }
908 /* No anisotropic filtering. */
909 texstate->filter0 &= ~R300_TX_MAX_ANISO_MASK;
910 texstate->filter1 &= ~R500_TX_MAX_ANISO_MASK;
911 texstate->filter1 &= ~R500_TX_ANISO_HIGH_QUALITY;
912 }
913
914 texstate->filter0 |= i << 28;
915
916 size += 16 + (has_us_format ? 2 : 0);
917 state->count = i+1;
918 } else {
919 /* For the KIL opcode to work on r3xx-r4xx, the texture unit
920 * assigned to this opcode (it's always the first one) must be
921 * enabled. Otherwise the opcode doesn't work.
922 *
923 * In order to not depend on the fragment shader, we just make
924 * the first unit enabled all the time. */
925 if (i == 0 && !r300->screen->caps.is_r500) {
926 pipe_sampler_view_reference(
927 (struct pipe_sampler_view**)&state->sampler_views[i],
928 &r300->texkill_sampler->base);
929
930 state->tx_enable |= 1 << i;
931
932 texstate = &state->regs[i];
933
934 /* Just set some valid state. */
935 texstate->format = r300->texkill_sampler->format;
936 texstate->filter0 =
937 r300_translate_tex_filters(PIPE_TEX_FILTER_NEAREST,
938 PIPE_TEX_FILTER_NEAREST,
939 PIPE_TEX_FILTER_NEAREST,
940 FALSE);
941 texstate->filter1 = 0;
942 texstate->border_color = 0;
943
944 texstate->filter0 |= i << 28;
945 size += 16 + (has_us_format ? 2 : 0);
946 state->count = i+1;
947 }
948 }
949 }
950
951 r300->textures_state.size = size;
952
953 /* Pick a fragment shader based on either the texture compare state
954 * or the uses_pitch flag or some other external state. */
955 if (count &&
956 r300->fs_status == FRAGMENT_SHADER_VALID) {
957 r300->fs_status = FRAGMENT_SHADER_MAYBE_DIRTY;
958 }
959 }
960
961 static void r300_decompress_depth_textures(struct r300_context *r300)
962 {
963 struct r300_textures_state *state =
964 (struct r300_textures_state*)r300->textures_state.state;
965 struct pipe_resource *tex;
966 unsigned count = MIN2(state->sampler_view_count,
967 state->sampler_state_count);
968 unsigned i;
969
970 if (!r300->locked_zbuffer) {
971 return;
972 }
973
974 for (i = 0; i < count; i++) {
975 if (state->sampler_views[i] && state->sampler_states[i]) {
976 tex = state->sampler_views[i]->base.texture;
977
978 if (tex == r300->locked_zbuffer->texture) {
979 r300_decompress_zmask_locked(r300);
980 return;
981 }
982 }
983 }
984 }
985
986 static void r300_validate_fragment_shader(struct r300_context *r300)
987 {
988 struct pipe_framebuffer_state *fb = r300->fb_state.state;
989
990 if (r300->fs.state && r300->fs_status != FRAGMENT_SHADER_VALID) {
991 /* Pick the fragment shader based on external states.
992 * Then mark the state dirty if the fragment shader is either dirty
993 * or the function r300_pick_fragment_shader changed the shader. */
994 if (r300_pick_fragment_shader(r300) ||
995 r300->fs_status == FRAGMENT_SHADER_DIRTY) {
996 /* Mark the state atom as dirty. */
997 r300_mark_fs_code_dirty(r300);
998
999 /* Does Multiwrite need to be changed? */
1000 if (fb->nr_cbufs > 1) {
1001 boolean new_multiwrite =
1002 r300_fragment_shader_writes_all(r300_fs(r300));
1003
1004 if (r300->fb_multiwrite != new_multiwrite) {
1005 r300->fb_multiwrite = new_multiwrite;
1006 r300_mark_fb_state_dirty(r300, R300_CHANGED_MULTIWRITE);
1007 }
1008 }
1009 }
1010 r300->fs_status = FRAGMENT_SHADER_VALID;
1011 }
1012 }
1013
1014 void r300_update_derived_state(struct r300_context* r300)
1015 {
1016 if (r300->textures_state.dirty) {
1017 r300_decompress_depth_textures(r300);
1018 r300_merge_textures_and_samplers(r300);
1019 }
1020
1021 r300_validate_fragment_shader(r300);
1022
1023 if (r300->rs_block_state.dirty) {
1024 r300_update_rs_block(r300);
1025
1026 if (r300->draw) {
1027 memset(&r300->vertex_info, 0, sizeof(struct vertex_info));
1028 r300_draw_emit_all_attribs(r300);
1029 draw_compute_vertex_size(&r300->vertex_info);
1030 r300_swtcl_vertex_psc(r300);
1031 }
1032 }
1033
1034 r300_update_hyperz_state(r300);
1035 }