Merge remote branch 'origin/master' 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_hyperz.h"
33 #include "r300_screen.h"
34 #include "r300_shader_semantics.h"
35 #include "r300_state_derived.h"
36 #include "r300_state_inlines.h"
37 #include "r300_texture.h"
38 #include "r300_vs.h"
39
40 /* r300_state_derived: Various bits of state which are dependent upon
41 * currently bound CSO data. */
42
43 enum r300_rs_swizzle {
44 SWIZ_XYZW = 0,
45 SWIZ_X001,
46 SWIZ_XY01,
47 SWIZ_0001,
48 };
49
50 enum r300_rs_col_write_type {
51 WRITE_COLOR = 0,
52 WRITE_FACE
53 };
54
55 static void r300_draw_emit_attrib(struct r300_context* r300,
56 enum attrib_emit emit,
57 enum interp_mode interp,
58 int index)
59 {
60 struct r300_vertex_shader* vs = r300->vs_state.state;
61 struct tgsi_shader_info* info = &vs->info;
62 int output;
63
64 output = draw_find_shader_output(r300->draw,
65 info->output_semantic_name[index],
66 info->output_semantic_index[index]);
67 draw_emit_vertex_attr(&r300->vertex_info, emit, interp, output);
68 }
69
70 static void r300_draw_emit_all_attribs(struct r300_context* r300)
71 {
72 struct r300_vertex_shader* vs = r300->vs_state.state;
73 struct r300_shader_semantics* vs_outputs = &vs->outputs;
74 int i, gen_count;
75
76 /* Position. */
77 if (vs_outputs->pos != ATTR_UNUSED) {
78 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
79 vs_outputs->pos);
80 } else {
81 assert(0);
82 }
83
84 /* Point size. */
85 if (vs_outputs->psize != ATTR_UNUSED) {
86 r300_draw_emit_attrib(r300, EMIT_1F_PSIZE, INTERP_POS,
87 vs_outputs->psize);
88 }
89
90 /* Colors. */
91 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
92 if (vs_outputs->color[i] != ATTR_UNUSED) {
93 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_LINEAR,
94 vs_outputs->color[i]);
95 }
96 }
97
98 /* Back-face colors. */
99 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
100 if (vs_outputs->bcolor[i] != ATTR_UNUSED) {
101 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_LINEAR,
102 vs_outputs->bcolor[i]);
103 }
104 }
105
106 /* Texture coordinates. */
107 /* Only 8 generic vertex attributes can be used. If there are more,
108 * they won't be rasterized. */
109 gen_count = 0;
110 for (i = 0; i < ATTR_GENERIC_COUNT && gen_count < 8; i++) {
111 if (vs_outputs->generic[i] != ATTR_UNUSED &&
112 !(r300->sprite_coord_enable & (1 << i))) {
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 (gen_count < 8 && 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 /* WPOS. */
127 if (r300_fs(r300)->shader->inputs.wpos != ATTR_UNUSED && gen_count < 8) {
128 DBG(r300, DBG_SWTCL, "draw_emit_attrib: WPOS, index: %i\n",
129 vs_outputs->wpos);
130 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
131 vs_outputs->wpos);
132 }
133 }
134
135 /* Update the PSC tables for SW TCL, using Draw. */
136 static void r300_swtcl_vertex_psc(struct r300_context *r300)
137 {
138 struct r300_vertex_stream_state *vstream = r300->vertex_stream_state.state;
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->vertex_stream_state.dirty = TRUE;
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.\n", i);
494 }
495 }
496
497 /* Rasterize fog coordinates. */
498 if (vs_outputs->fog != ATTR_UNUSED && tex_count < 8) {
499 /* Set up the fog coordinates in VAP. */
500 rs.vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << tex_count);
501 rs.vap_out_vtx_fmt[1] |= (4 << (3 * tex_count));
502 stream_loc_notcl[loc++] = 6 + tex_count;
503
504 /* Rasterize it. */
505 rX00_rs_tex(&rs, tex_count, tex_ptr, SWIZ_X001);
506
507 /* Write it to the FS input register if it's needed by the FS. */
508 if (fs_inputs->fog != ATTR_UNUSED) {
509 rX00_rs_tex_write(&rs, tex_count, fp_offset);
510 fp_offset++;
511
512 DBG(r300, DBG_RS, "r300: Rasterized fog written to FS.\n");
513 } else {
514 DBG(r300, DBG_RS, "r300: Rasterized fog unused.\n");
515 }
516 tex_count++;
517 tex_ptr += 4;
518 } else {
519 /* Skip the FS input register, leave it uninitialized. */
520 /* If we try to set it to (0,0,0,1), it will lock up. */
521 if (fs_inputs->fog != ATTR_UNUSED) {
522 fp_offset++;
523
524 if (tex_count < 8) {
525 DBG(r300, DBG_RS, "r300: FS input fog unassigned.\n");
526 } else {
527 fprintf(stderr, "r300: ERROR: FS input fog unassigned, "
528 "not enough hardware slots.\n");
529 }
530 }
531 }
532
533 /* Rasterize WPOS. */
534 /* Don't set it in VAP if the FS doesn't need it. */
535 if (fs_inputs->wpos != ATTR_UNUSED && tex_count < 8) {
536 /* Set up the WPOS coordinates in VAP. */
537 rs.vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << tex_count);
538 rs.vap_out_vtx_fmt[1] |= (4 << (3 * tex_count));
539 stream_loc_notcl[loc++] = 6 + tex_count;
540
541 /* Rasterize it. */
542 rX00_rs_tex(&rs, tex_count, tex_ptr, SWIZ_XYZW);
543
544 /* Write it to the FS input register. */
545 rX00_rs_tex_write(&rs, tex_count, fp_offset);
546
547 DBG(r300, DBG_RS, "r300: Rasterized WPOS written to FS.\n");
548
549 fp_offset++;
550 tex_count++;
551 tex_ptr += 4;
552 } else {
553 if (fs_inputs->wpos != ATTR_UNUSED && tex_count >= 8) {
554 fprintf(stderr, "r300: ERROR: FS input WPOS unassigned, "
555 "not enough hardware slots.\n");
556 }
557 }
558
559 /* Invalidate the rest of the no-TCL (GA) stream locations. */
560 for (; loc < 16;) {
561 stream_loc_notcl[loc++] = -1;
562 }
563
564 /* Rasterize at least one color, or bad things happen. */
565 if (col_count == 0 && tex_count == 0) {
566 rX00_rs_col(&rs, 0, 0, SWIZ_0001);
567 col_count++;
568
569 DBG(r300, DBG_RS, "r300: Rasterized color 0 to prevent lockups.\n");
570 }
571
572 DBG(r300, DBG_RS, "r300: --- Rasterizer status ---: colors: %i, "
573 "generics: %i.\n", col_count, tex_count);
574
575 rs.count = MIN2(tex_ptr, 32) | (col_count << R300_IC_COUNT_SHIFT) |
576 R300_HIRES_EN;
577
578 count = MAX3(col_count, tex_count, 1);
579 rs.inst_count = count - 1;
580
581 /* set the GB enable flags */
582 if (r300->sprite_coord_enable)
583 stuffing_enable |= R300_GB_POINT_STUFF_ENABLE;
584
585 rs.gb_enable = stuffing_enable;
586
587 /* Now, after all that, see if we actually need to update the state. */
588 if (memcmp(r300->rs_block_state.state, &rs, sizeof(struct r300_rs_block))) {
589 memcpy(r300->rs_block_state.state, &rs, sizeof(struct r300_rs_block));
590 r300->rs_block_state.size = 13 + count*2;
591 }
592 }
593
594 static uint32_t r300_get_border_color(enum pipe_format format,
595 const float border[4])
596 {
597 const struct util_format_description *desc;
598 float border_swizzled[4] = {0};
599 unsigned i;
600 union util_color uc = {0};
601
602 desc = util_format_description(format);
603
604 /* Apply inverse swizzle of the format. */
605 for (i = 0; i < 4; i++) {
606 switch (desc->swizzle[i]) {
607 case UTIL_FORMAT_SWIZZLE_X:
608 border_swizzled[2] = border[i];
609 break;
610 case UTIL_FORMAT_SWIZZLE_Y:
611 border_swizzled[1] = border[i];
612 break;
613 case UTIL_FORMAT_SWIZZLE_Z:
614 border_swizzled[0] = border[i];
615 break;
616 case UTIL_FORMAT_SWIZZLE_W:
617 border_swizzled[3] = border[i];
618 break;
619 }
620 }
621
622 switch (desc->channel[0].size) {
623 case 4:
624 util_pack_color(border_swizzled, PIPE_FORMAT_B4G4R4A4_UNORM, &uc);
625 break;
626
627 case 5:
628 if (desc->channel[1].size == 5) {
629 util_pack_color(border_swizzled, PIPE_FORMAT_B5G5R5A1_UNORM, &uc);
630 } else if (desc->channel[1].size == 6) {
631 util_pack_color(border_swizzled, PIPE_FORMAT_B5G6R5_UNORM, &uc);
632 } else {
633 assert(0);
634 }
635 break;
636
637 default:
638 case 8:
639 util_pack_color(border_swizzled, PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
640 break;
641
642 case 10:
643 util_pack_color(border_swizzled, PIPE_FORMAT_B10G10R10A2_UNORM, &uc);
644 break;
645 }
646
647 return uc.ui;
648 }
649
650 static void r300_merge_textures_and_samplers(struct r300_context* r300)
651 {
652 struct r300_textures_state *state =
653 (struct r300_textures_state*)r300->textures_state.state;
654 struct r300_texture_sampler_state *texstate;
655 struct r300_sampler_state *sampler;
656 struct r300_sampler_view *view;
657 struct r300_texture *tex;
658 unsigned min_level, max_level, i, j, size;
659 unsigned count = MIN2(state->sampler_view_count,
660 state->sampler_state_count);
661
662 /* The KIL opcode fix, see below. */
663 if (!count && !r300->screen->caps.is_r500)
664 count = 1;
665
666 state->tx_enable = 0;
667 state->count = 0;
668 size = 2;
669
670 for (i = 0; i < count; i++) {
671 if (state->sampler_views[i] && state->sampler_states[i]) {
672 state->tx_enable |= 1 << i;
673
674 view = state->sampler_views[i];
675 tex = r300_texture(view->base.texture);
676 sampler = state->sampler_states[i];
677
678 texstate = &state->regs[i];
679 texstate->format = view->format;
680 texstate->filter0 = sampler->filter0;
681 texstate->filter1 = sampler->filter1;
682
683 /* Set the border color. */
684 texstate->border_color =
685 r300_get_border_color(view->base.format,
686 sampler->state.border_color);
687
688 /* determine min/max levels */
689 max_level = MIN3(sampler->max_lod + view->base.first_level,
690 tex->desc.b.b.last_level, view->base.last_level);
691 min_level = MIN2(sampler->min_lod + view->base.first_level,
692 max_level);
693
694 if (tex->desc.is_npot && min_level > 0) {
695 /* Even though we do not implement mipmapping for NPOT
696 * textures, we should at least honor the minimum level
697 * which is allowed to be displayed. We do this by setting up
698 * an i-th mipmap level as the zero level. */
699 r300_texture_setup_format_state(r300->screen, &tex->desc,
700 min_level,
701 &texstate->format);
702 texstate->format.tile_config |=
703 tex->desc.offset_in_bytes[min_level] & 0xffffffe0;
704 assert((tex->desc.offset_in_bytes[min_level] & 0x1f) == 0);
705 }
706
707 /* Assign a texture cache region. */
708 texstate->format.format1 |= view->texcache_region;
709
710 /* Depth textures are kinda special. */
711 if (util_format_is_depth_or_stencil(tex->desc.b.b.format)) {
712 unsigned char depth_swizzle[4];
713
714 if (!r300->screen->caps.is_r500 &&
715 util_format_get_blocksizebits(tex->desc.b.b.format) == 32) {
716 /* X24x8 is sampled as Y16X16 on r3xx-r4xx.
717 * The depth here is at the Y component. */
718 for (j = 0; j < 4; j++)
719 depth_swizzle[j] = UTIL_FORMAT_SWIZZLE_Y;
720 } else {
721 for (j = 0; j < 4; j++)
722 depth_swizzle[j] = UTIL_FORMAT_SWIZZLE_X;
723 }
724
725 /* If compare mode is disabled, sampler view swizzles
726 * are stored in the format.
727 * Otherwise, the swizzles must be applied after the compare
728 * mode in the fragment shader. */
729 if (sampler->state.compare_mode == PIPE_TEX_COMPARE_NONE) {
730 texstate->format.format1 |=
731 r300_get_swizzle_combined(depth_swizzle,
732 view->swizzle);
733 } else {
734 texstate->format.format1 |=
735 r300_get_swizzle_combined(depth_swizzle, 0);
736 }
737 }
738
739 /* to emulate 1D textures through 2D ones correctly */
740 if (tex->desc.b.b.target == PIPE_TEXTURE_1D) {
741 texstate->filter0 &= ~R300_TX_WRAP_T_MASK;
742 texstate->filter0 |= R300_TX_WRAP_T(R300_TX_CLAMP_TO_EDGE);
743 }
744
745 if (tex->desc.is_npot) {
746 /* NPOT textures don't support mip filter, unfortunately.
747 * This prevents incorrect rendering. */
748 texstate->filter0 &= ~R300_TX_MIN_FILTER_MIP_MASK;
749
750 /* Mask out the mirrored flag. */
751 if (texstate->filter0 & R300_TX_WRAP_S(R300_TX_MIRRORED)) {
752 texstate->filter0 &= ~R300_TX_WRAP_S(R300_TX_MIRRORED);
753 }
754 if (texstate->filter0 & R300_TX_WRAP_T(R300_TX_MIRRORED)) {
755 texstate->filter0 &= ~R300_TX_WRAP_T(R300_TX_MIRRORED);
756 }
757
758 /* Change repeat to clamp-to-edge.
759 * (the repeat bit has a value of 0, no masking needed). */
760 if ((texstate->filter0 & R300_TX_WRAP_S_MASK) ==
761 R300_TX_WRAP_S(R300_TX_REPEAT)) {
762 texstate->filter0 |= R300_TX_WRAP_S(R300_TX_CLAMP_TO_EDGE);
763 }
764 if ((texstate->filter0 & R300_TX_WRAP_T_MASK) ==
765 R300_TX_WRAP_T(R300_TX_REPEAT)) {
766 texstate->filter0 |= R300_TX_WRAP_T(R300_TX_CLAMP_TO_EDGE);
767 }
768 } else {
769 /* the MAX_MIP level is the largest (finest) one */
770 texstate->format.format0 |= R300_TX_NUM_LEVELS(max_level);
771 texstate->filter0 |= R300_TX_MAX_MIP_LEVEL(min_level);
772 }
773
774 texstate->filter0 |= i << 28;
775
776 size += 16;
777 state->count = i+1;
778 } else {
779 /* For the KIL opcode to work on r3xx-r4xx, the texture unit
780 * assigned to this opcode (it's always the first one) must be
781 * enabled. Otherwise the opcode doesn't work.
782 *
783 * In order to not depend on the fragment shader, we just make
784 * the first unit enabled all the time. */
785 if (i == 0 && !r300->screen->caps.is_r500) {
786 pipe_sampler_view_reference(
787 (struct pipe_sampler_view**)&state->sampler_views[i],
788 &r300->texkill_sampler->base);
789
790 state->tx_enable |= 1 << i;
791
792 texstate = &state->regs[i];
793
794 /* Just set some valid state. */
795 texstate->format = r300->texkill_sampler->format;
796 texstate->filter0 =
797 r300_translate_tex_filters(PIPE_TEX_FILTER_NEAREST,
798 PIPE_TEX_FILTER_NEAREST,
799 PIPE_TEX_FILTER_NEAREST,
800 FALSE);
801 texstate->filter1 = 0;
802 texstate->border_color = 0;
803
804 texstate->filter0 |= i << 28;
805 size += 16;
806 state->count = i+1;
807 }
808 }
809 }
810
811 r300->textures_state.size = size;
812
813 /* Pick a fragment shader based on either the texture compare state
814 * or the uses_pitch flag. */
815 if (r300->fs.state && count) {
816 if (r300_pick_fragment_shader(r300)) {
817 r300_mark_fs_code_dirty(r300);
818 }
819 }
820 }
821
822 /* We can't use compressed zbuffers as samplers. */
823 static void r300_flush_depth_textures(struct r300_context *r300)
824 {
825 struct r300_textures_state *state =
826 (struct r300_textures_state*)r300->textures_state.state;
827 unsigned i, level;
828 unsigned count = MIN2(state->sampler_view_count,
829 state->sampler_state_count);
830
831 if (r300->z_decomp_rd)
832 return;
833
834 for (i = 0; i < count; i++)
835 if (state->sampler_views[i] && state->sampler_states[i]) {
836 struct pipe_resource *tex = state->sampler_views[i]->base.texture;
837
838 if (tex->target == PIPE_TEXTURE_3D ||
839 tex->target == PIPE_TEXTURE_CUBE)
840 continue;
841
842 /* Ignore non-depth textures.
843 * Also ignore reinterpreted depth textures, e.g. resource_copy. */
844 if (!util_format_is_depth_or_stencil(tex->format))
845 continue;
846
847 for (level = 0; level <= tex->last_level; level++)
848 if (r300_texture(tex)->zmask_in_use[level]) {
849 /* We don't handle 3D textures and cubemaps yet. */
850 r300_flush_depth_stencil(&r300->context, tex,
851 u_subresource(0, level), 0);
852 }
853 }
854 }
855
856 void r300_update_derived_state(struct r300_context* r300)
857 {
858 r300_flush_depth_textures(r300);
859
860 if (r300->textures_state.dirty) {
861 r300_merge_textures_and_samplers(r300);
862 }
863
864 if (r300->rs_block_state.dirty) {
865 r300_update_rs_block(r300);
866
867 if (r300->draw) {
868 memset(&r300->vertex_info, 0, sizeof(struct vertex_info));
869 r300_draw_emit_all_attribs(r300);
870 draw_compute_vertex_size(&r300->vertex_info);
871 r300_swtcl_vertex_psc(r300);
872 }
873 }
874
875 r300_update_hyperz_state(r300);
876 }