r300g: Silence uninitialized variable warning.
[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_hyperz.h"
32 #include "r300_screen.h"
33 #include "r300_shader_semantics.h"
34 #include "r300_state_derived.h"
35 #include "r300_state_inlines.h"
36 #include "r300_texture.h"
37 #include "r300_vs.h"
38
39 /* r300_state_derived: Various bits of state which are dependent upon
40 * currently bound CSO data. */
41
42 enum r300_rs_swizzle {
43 SWIZ_XYZW = 0,
44 SWIZ_X001,
45 SWIZ_XY01,
46 SWIZ_0001,
47 };
48
49 enum r300_rs_col_write_type {
50 WRITE_COLOR = 0,
51 WRITE_FACE
52 };
53
54 static void r300_draw_emit_attrib(struct r300_context* r300,
55 enum attrib_emit emit,
56 enum interp_mode interp,
57 int index)
58 {
59 struct r300_vertex_shader* vs = r300->vs_state.state;
60 struct tgsi_shader_info* info = &vs->info;
61 int output;
62
63 output = draw_find_shader_output(r300->draw,
64 info->output_semantic_name[index],
65 info->output_semantic_index[index]);
66 draw_emit_vertex_attr(&r300->vertex_info, emit, interp, output);
67 }
68
69 static void r300_draw_emit_all_attribs(struct r300_context* r300)
70 {
71 struct r300_vertex_shader* vs = r300->vs_state.state;
72 struct r300_shader_semantics* vs_outputs = &vs->outputs;
73 int i, gen_count;
74
75 /* Position. */
76 if (vs_outputs->pos != ATTR_UNUSED) {
77 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
78 vs_outputs->pos);
79 } else {
80 assert(0);
81 }
82
83 /* Point size. */
84 if (vs_outputs->psize != ATTR_UNUSED) {
85 r300_draw_emit_attrib(r300, EMIT_1F_PSIZE, INTERP_POS,
86 vs_outputs->psize);
87 }
88
89 /* Colors. */
90 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
91 if (vs_outputs->color[i] != ATTR_UNUSED) {
92 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_LINEAR,
93 vs_outputs->color[i]);
94 }
95 }
96
97 /* Back-face colors. */
98 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
99 if (vs_outputs->bcolor[i] != ATTR_UNUSED) {
100 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_LINEAR,
101 vs_outputs->bcolor[i]);
102 }
103 }
104
105 /* Texture coordinates. */
106 /* Only 8 generic vertex attributes can be used. If there are more,
107 * they won't be rasterized. */
108 gen_count = 0;
109 for (i = 0; i < ATTR_GENERIC_COUNT && gen_count < 8; i++) {
110 if (vs_outputs->generic[i] != ATTR_UNUSED &&
111 !(r300->sprite_coord_enable & (1 << i))) {
112 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
113 vs_outputs->generic[i]);
114 gen_count++;
115 }
116 }
117
118 /* Fog coordinates. */
119 if (gen_count < 8 && vs_outputs->fog != ATTR_UNUSED) {
120 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
121 vs_outputs->fog);
122 gen_count++;
123 }
124
125 /* WPOS. */
126 if (r300_fs(r300)->shader->inputs.wpos != ATTR_UNUSED && gen_count < 8) {
127 DBG(r300, DBG_SWTCL, "draw_emit_attrib: WPOS, index: %i\n",
128 vs_outputs->wpos);
129 r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
130 vs_outputs->wpos);
131 }
132 }
133
134 /* Update the PSC tables for SW TCL, using Draw. */
135 static void r300_swtcl_vertex_psc(struct r300_context *r300)
136 {
137 struct r300_vertex_stream_state *vstream = r300->vertex_stream_state.state;
138 struct vertex_info *vinfo = &r300->vertex_info;
139 uint16_t type, swizzle;
140 enum pipe_format format;
141 unsigned i, attrib_count;
142 int* vs_output_tab = r300->stream_loc_notcl;
143
144 memset(vstream, 0, sizeof(struct r300_vertex_stream_state));
145
146 /* For each Draw attribute, route it to the fragment shader according
147 * to the vs_output_tab. */
148 attrib_count = vinfo->num_attribs;
149 DBG(r300, DBG_SWTCL, "r300: attrib count: %d\n", attrib_count);
150 for (i = 0; i < attrib_count; i++) {
151 if (vs_output_tab[i] == -1) {
152 assert(0);
153 abort();
154 }
155
156 format = draw_translate_vinfo_format(vinfo->attrib[i].emit);
157
158 DBG(r300, DBG_SWTCL,
159 "r300: swtcl_vertex_psc [%i] <- %s\n",
160 vs_output_tab[i], util_format_short_name(format));
161
162 /* Obtain the type of data in this attribute. */
163 type = r300_translate_vertex_data_type(format);
164 if (type == R300_INVALID_FORMAT) {
165 fprintf(stderr, "r300: Bad vertex format %s.\n",
166 util_format_short_name(format));
167 assert(0);
168 abort();
169 }
170
171 type |= vs_output_tab[i] << R300_DST_VEC_LOC_SHIFT;
172
173 /* Obtain the swizzle for this attribute. Note that the default
174 * swizzle in the hardware is not XYZW! */
175 swizzle = r300_translate_vertex_data_swizzle(format);
176
177 /* Add the attribute to the PSC table. */
178 if (i & 1) {
179 vstream->vap_prog_stream_cntl[i >> 1] |= type << 16;
180 vstream->vap_prog_stream_cntl_ext[i >> 1] |= swizzle << 16;
181 } else {
182 vstream->vap_prog_stream_cntl[i >> 1] |= type;
183 vstream->vap_prog_stream_cntl_ext[i >> 1] |= swizzle;
184 }
185 }
186
187 /* Set the last vector in the PSC. */
188 if (i) {
189 i -= 1;
190 }
191 vstream->vap_prog_stream_cntl[i >> 1] |=
192 (R300_LAST_VEC << (i & 1 ? 16 : 0));
193
194 vstream->count = (i >> 1) + 1;
195 r300->vertex_stream_state.dirty = TRUE;
196 r300->vertex_stream_state.size = (1 + vstream->count) * 2;
197 }
198
199 static void r300_rs_col(struct r300_rs_block* rs, int id, int ptr,
200 enum r300_rs_swizzle swiz)
201 {
202 rs->ip[id] |= R300_RS_COL_PTR(ptr);
203 if (swiz == SWIZ_0001) {
204 rs->ip[id] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001);
205 } else {
206 rs->ip[id] |= R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
207 }
208 rs->inst[id] |= R300_RS_INST_COL_ID(id);
209 }
210
211 static void r300_rs_col_write(struct r300_rs_block* rs, int id, int fp_offset,
212 enum r300_rs_col_write_type type)
213 {
214 assert(type == WRITE_COLOR);
215 rs->inst[id] |= R300_RS_INST_COL_CN_WRITE |
216 R300_RS_INST_COL_ADDR(fp_offset);
217 }
218
219 static void r300_rs_tex(struct r300_rs_block* rs, int id, int ptr,
220 enum r300_rs_swizzle swiz)
221 {
222 if (swiz == SWIZ_X001) {
223 rs->ip[id] |= R300_RS_TEX_PTR(ptr) |
224 R300_RS_SEL_S(R300_RS_SEL_C0) |
225 R300_RS_SEL_T(R300_RS_SEL_K0) |
226 R300_RS_SEL_R(R300_RS_SEL_K0) |
227 R300_RS_SEL_Q(R300_RS_SEL_K1);
228 } else if (swiz == SWIZ_XY01) {
229 rs->ip[id] |= R300_RS_TEX_PTR(ptr) |
230 R300_RS_SEL_S(R300_RS_SEL_C0) |
231 R300_RS_SEL_T(R300_RS_SEL_C1) |
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) |
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 enum r300_rs_swizzle swiz)
252 {
253 rs->ip[id] |= R500_RS_COL_PTR(ptr);
254 if (swiz == SWIZ_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 enum r300_rs_col_write_type type)
264 {
265 if (type == WRITE_FACE)
266 rs->inst[id] |= R500_RS_INST_COL_CN_WRITE_BACKFACE |
267 R500_RS_INST_COL_ADDR(fp_offset);
268 else
269 rs->inst[id] |= R500_RS_INST_COL_CN_WRITE |
270 R500_RS_INST_COL_ADDR(fp_offset);
271
272 }
273
274 static void r500_rs_tex(struct r300_rs_block* rs, int id, int ptr,
275 enum r300_rs_swizzle swiz)
276 {
277 if (swiz == SWIZ_X001) {
278 rs->ip[id] |= R500_RS_SEL_S(ptr) |
279 R500_RS_SEL_T(R500_RS_IP_PTR_K0) |
280 R500_RS_SEL_R(R500_RS_IP_PTR_K0) |
281 R500_RS_SEL_Q(R500_RS_IP_PTR_K1);
282 } else if (swiz == SWIZ_XY01) {
283 rs->ip[id] |= R500_RS_SEL_S(ptr) |
284 R500_RS_SEL_T(ptr + 1) |
285 R500_RS_SEL_R(R500_RS_IP_PTR_K0) |
286 R500_RS_SEL_Q(R500_RS_IP_PTR_K1);
287 } else {
288 rs->ip[id] |= R500_RS_SEL_S(ptr) |
289 R500_RS_SEL_T(ptr + 1) |
290 R500_RS_SEL_R(ptr + 2) |
291 R500_RS_SEL_Q(ptr + 3);
292 }
293 rs->inst[id] |= R500_RS_INST_TEX_ID(id);
294 }
295
296 static void r500_rs_tex_write(struct r300_rs_block* rs, int id, int fp_offset)
297 {
298 rs->inst[id] |= R500_RS_INST_TEX_CN_WRITE |
299 R500_RS_INST_TEX_ADDR(fp_offset);
300 }
301
302 /* Set up the RS block.
303 *
304 * This is the part of the chipset that is responsible for linking vertex
305 * and fragment shaders and stuffed texture coordinates.
306 *
307 * The rasterizer reads data from VAP, which produces vertex shader outputs,
308 * and GA, which produces stuffed texture coordinates. VAP outputs have
309 * precedence over GA. All outputs must be rasterized otherwise it locks up.
310 * If there are more outputs rasterized than is set in VAP/GA, it locks up
311 * too. The funky part is that this info has been pretty much obtained by trial
312 * and error. */
313 static void r300_update_rs_block(struct r300_context *r300)
314 {
315 struct r300_vertex_shader *vs = r300->vs_state.state;
316 struct r300_shader_semantics *vs_outputs = &vs->outputs;
317 struct r300_shader_semantics *fs_inputs = &r300_fs(r300)->shader->inputs;
318 struct r300_rs_block rs = {0};
319 int i, col_count = 0, tex_count = 0, fp_offset = 0, count, loc = 0, tex_ptr = 0;
320 void (*rX00_rs_col)(struct r300_rs_block*, int, int, enum r300_rs_swizzle);
321 void (*rX00_rs_col_write)(struct r300_rs_block*, int, int, enum r300_rs_col_write_type);
322 void (*rX00_rs_tex)(struct r300_rs_block*, int, int, enum r300_rs_swizzle);
323 void (*rX00_rs_tex_write)(struct r300_rs_block*, int, int);
324 boolean any_bcolor_used = vs_outputs->bcolor[0] != ATTR_UNUSED ||
325 vs_outputs->bcolor[1] != ATTR_UNUSED;
326 int *stream_loc_notcl = r300->stream_loc_notcl;
327
328 if (r300->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 /* 0x5555 copied from classic, which means:
341 * Select user color 0 for COLOR0 up to COLOR7.
342 * What the hell does that mean? */
343 rs.vap_vtx_state_cntl = 0x5555;
344
345 /* The position is always present in VAP. */
346 rs.vap_vsm_vtx_assm |= R300_INPUT_CNTL_POS;
347 rs.vap_out_vtx_fmt[0] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT;
348 stream_loc_notcl[loc++] = 0;
349
350 /* Set up the point size in VAP. */
351 if (vs_outputs->psize != ATTR_UNUSED) {
352 rs.vap_out_vtx_fmt[0] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT;
353 stream_loc_notcl[loc++] = 1;
354 }
355
356 /* Set up and rasterize colors. */
357 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
358 if (vs_outputs->color[i] != ATTR_UNUSED || any_bcolor_used ||
359 vs_outputs->color[1] != ATTR_UNUSED) {
360 /* Set up the color in VAP. */
361 rs.vap_vsm_vtx_assm |= R300_INPUT_CNTL_COLOR;
362 rs.vap_out_vtx_fmt[0] |=
363 R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << i;
364 stream_loc_notcl[loc++] = 2 + i;
365
366 /* Rasterize it. */
367 rX00_rs_col(&rs, col_count, col_count, SWIZ_XYZW);
368
369 /* Write it to the FS input register if it's needed by the FS. */
370 if (fs_inputs->color[i] != ATTR_UNUSED) {
371 rX00_rs_col_write(&rs, col_count, fp_offset, WRITE_COLOR);
372 fp_offset++;
373
374 DBG(r300, DBG_RS,
375 "r300: Rasterized color %i written to FS.\n", i);
376 } else {
377 DBG(r300, DBG_RS, "r300: Rasterized color %i unused.\n", i);
378 }
379 col_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->color[i] != ATTR_UNUSED) {
384 fp_offset++;
385
386 DBG(r300, DBG_RS, "r300: FS input color %i unassigned%s.\n",
387 i);
388 }
389 }
390 }
391
392 /* Set up back-face colors. The rasterizer will do the color selection
393 * automatically. */
394 if (any_bcolor_used) {
395 if (r300->two_sided_color) {
396 /* Rasterize as back-face colors. */
397 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
398 rs.vap_vsm_vtx_assm |= R300_INPUT_CNTL_COLOR;
399 rs.vap_out_vtx_fmt[0] |= R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << (2+i);
400 stream_loc_notcl[loc++] = 4 + i;
401 }
402 } else {
403 /* Rasterize two fake texcoords to prevent from the two-sided color
404 * selection. */
405 /* XXX Consider recompiling the vertex shader to save 2 RS units. */
406 for (i = 0; i < 2; i++) {
407 rs.vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << tex_count);
408 rs.vap_out_vtx_fmt[1] |= (4 << (3 * tex_count));
409 stream_loc_notcl[loc++] = 6 + tex_count;
410
411 /* Rasterize it. */
412 rX00_rs_tex(&rs, tex_count, tex_ptr, SWIZ_XYZW);
413 tex_count++;
414 tex_ptr += 4;
415 }
416 }
417 }
418
419 /* gl_FrontFacing.
420 * Note that we can use either the two-sided color selection based on
421 * the front and back vertex shader colors, or gl_FrontFacing,
422 * but not both! It locks up otherwise.
423 *
424 * In Direct3D 9, the two-sided color selection can be used
425 * with shaders 2.0 only, while gl_FrontFacing can be used
426 * with shaders 3.0 only. The hardware apparently hasn't been designed
427 * to support both at the same time. */
428 if (r300->screen->caps.is_r500 && fs_inputs->face != ATTR_UNUSED &&
429 !(any_bcolor_used && r300->two_sided_color)) {
430 rX00_rs_col(&rs, col_count, col_count, SWIZ_XYZW);
431 rX00_rs_col_write(&rs, col_count, fp_offset, WRITE_FACE);
432 fp_offset++;
433 col_count++;
434 DBG(r300, DBG_RS, "r300: Rasterized FACE written to FS.\n");
435 }
436
437 /* Rasterize texture coordinates. */
438 for (i = 0; i < ATTR_GENERIC_COUNT && tex_count < 8; i++) {
439 bool sprite_coord = !!(r300->sprite_coord_enable & (1 << i));
440
441 if (vs_outputs->generic[i] != ATTR_UNUSED || sprite_coord) {
442 if (!sprite_coord) {
443 /* Set up the texture coordinates in VAP. */
444 rs.vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << tex_count);
445 rs.vap_out_vtx_fmt[1] |= (4 << (3 * tex_count));
446 stream_loc_notcl[loc++] = 6 + tex_count;
447 }
448
449 /* Rasterize it. */
450 rX00_rs_tex(&rs, tex_count, tex_ptr,
451 sprite_coord ? SWIZ_XY01 : SWIZ_XYZW);
452
453 /* Write it to the FS input register if it's needed by the FS. */
454 if (fs_inputs->generic[i] != ATTR_UNUSED) {
455 rX00_rs_tex_write(&rs, tex_count, fp_offset);
456 fp_offset++;
457
458 DBG(r300, DBG_RS,
459 "r300: Rasterized generic %i written to FS%s.\n",
460 i, sprite_coord ? " (sprite coord)" : "");
461 } else {
462 DBG(r300, DBG_RS,
463 "r300: Rasterized generic %i unused%s.\n",
464 i, sprite_coord ? " (sprite coord)" : "");
465 }
466 tex_count++;
467 tex_ptr += sprite_coord ? 2 : 4;
468 } else {
469 /* Skip the FS input register, leave it uninitialized. */
470 /* If we try to set it to (0,0,0,1), it will lock up. */
471 if (fs_inputs->generic[i] != ATTR_UNUSED) {
472 fp_offset++;
473
474 DBG(r300, DBG_RS, "r300: FS input generic %i unassigned%s.\n",
475 i, sprite_coord ? " (sprite coord)" : "");
476 }
477 }
478 }
479
480 if (DBG_ON(r300, DBG_RS)) {
481 for (; i < ATTR_GENERIC_COUNT; i++) {
482 if (fs_inputs->generic[i] != ATTR_UNUSED) {
483 DBG(r300, DBG_RS,
484 "r300: FS input generic %i unassigned.\n", i);
485 }
486 }
487 }
488
489 /* Rasterize fog coordinates. */
490 if (vs_outputs->fog != ATTR_UNUSED && tex_count < 8) {
491 /* Set up the fog coordinates in VAP. */
492 rs.vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << tex_count);
493 rs.vap_out_vtx_fmt[1] |= (4 << (3 * tex_count));
494 stream_loc_notcl[loc++] = 6 + tex_count;
495
496 /* Rasterize it. */
497 rX00_rs_tex(&rs, tex_count, tex_ptr, SWIZ_X001);
498
499 /* Write it to the FS input register if it's needed by the FS. */
500 if (fs_inputs->fog != ATTR_UNUSED) {
501 rX00_rs_tex_write(&rs, tex_count, fp_offset);
502 fp_offset++;
503
504 DBG(r300, DBG_RS, "r300: Rasterized fog written to FS.\n");
505 } else {
506 DBG(r300, DBG_RS, "r300: Rasterized fog unused.\n");
507 }
508 tex_count++;
509 tex_ptr += 4;
510 } else {
511 /* Skip the FS input register, leave it uninitialized. */
512 /* If we try to set it to (0,0,0,1), it will lock up. */
513 if (fs_inputs->fog != ATTR_UNUSED) {
514 fp_offset++;
515
516 DBG(r300, DBG_RS, "r300: FS input fog unassigned.\n");
517 }
518 }
519
520 /* Rasterize WPOS. */
521 /* Don't set it in VAP if the FS doesn't need it. */
522 if (fs_inputs->wpos != ATTR_UNUSED && tex_count < 8) {
523 /* Set up the WPOS coordinates in VAP. */
524 rs.vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << tex_count);
525 rs.vap_out_vtx_fmt[1] |= (4 << (3 * tex_count));
526 stream_loc_notcl[loc++] = 6 + tex_count;
527
528 /* Rasterize it. */
529 rX00_rs_tex(&rs, tex_count, tex_ptr, SWIZ_XYZW);
530
531 /* Write it to the FS input register. */
532 rX00_rs_tex_write(&rs, tex_count, fp_offset);
533
534 DBG(r300, DBG_RS, "r300: Rasterized WPOS written to FS.\n");
535
536 fp_offset++;
537 tex_count++;
538 tex_ptr += 4;
539 }
540
541 /* Invalidate the rest of the no-TCL (GA) stream locations. */
542 for (; loc < 16;) {
543 stream_loc_notcl[loc++] = -1;
544 }
545
546 /* Rasterize at least one color, or bad things happen. */
547 if (col_count == 0 && tex_count == 0) {
548 rX00_rs_col(&rs, 0, 0, SWIZ_0001);
549 col_count++;
550
551 DBG(r300, DBG_RS, "r300: Rasterized color 0 to prevent lockups.\n");
552 }
553
554 DBG(r300, DBG_RS, "r300: --- Rasterizer status ---: colors: %i, "
555 "generics: %i.\n", col_count, tex_count);
556
557 rs.count = MIN2(tex_ptr, 32) | (col_count << R300_IC_COUNT_SHIFT) |
558 R300_HIRES_EN;
559
560 count = MAX3(col_count, tex_count, 1);
561 rs.inst_count = count - 1;
562
563 /* Now, after all that, see if we actually need to update the state. */
564 if (memcmp(r300->rs_block_state.state, &rs, sizeof(struct r300_rs_block))) {
565 memcpy(r300->rs_block_state.state, &rs, sizeof(struct r300_rs_block));
566 r300->rs_block_state.size = 11 + count*2;
567 }
568 }
569
570 static uint32_t r300_get_border_color(enum pipe_format format,
571 const unsigned char swizzle_view[4],
572 const float border[4])
573 {
574 const struct util_format_description *desc;
575 unsigned char swizzle[4];
576 unsigned i;
577 float border_swizzled[4];
578 uint32_t r;
579
580 desc = util_format_description(format);
581
582 /* Combine the swizzles. */
583 for (i = 0; i < 4; i++) {
584 swizzle[i] = swizzle_view[i] <= UTIL_FORMAT_SWIZZLE_W ?
585 desc->swizzle[swizzle_view[i]] : swizzle_view[i];
586 }
587
588 /* Apply swizzling. */
589 for (i = 0; i < 4; i++) {
590 switch (swizzle[i]) {
591 case UTIL_FORMAT_SWIZZLE_X:
592 border_swizzled[i] = border[0];
593 break;
594 case UTIL_FORMAT_SWIZZLE_Y:
595 border_swizzled[i] = border[1];
596 break;
597 case UTIL_FORMAT_SWIZZLE_Z:
598 border_swizzled[i] = border[2];
599 break;
600 case UTIL_FORMAT_SWIZZLE_W:
601 border_swizzled[i] = border[3];
602 break;
603 case UTIL_FORMAT_SWIZZLE_0:
604 border_swizzled[i] = 0;
605 break;
606 default: /* 1, NONE */
607 border_swizzled[i] = 1;
608 }
609 }
610
611 /* We don't use util_pack_format because it does not handle the formats
612 * we want, e.g. R4G4B4A4 is non-existent in Gallium. */
613 switch (desc->channel[0].size) {
614 case 4:
615 r = ((float_to_ubyte(border_swizzled[0]) & 0xf0) >> 4) |
616 ((float_to_ubyte(border_swizzled[1]) & 0xf0) << 0) |
617 ((float_to_ubyte(border_swizzled[2]) & 0xf0) << 4) |
618 ((float_to_ubyte(border_swizzled[3]) & 0xf0) << 8);
619 break;
620
621 case 5:
622 if (desc->channel[1].size == 5) {
623 r = ((float_to_ubyte(border_swizzled[0]) & 0xf8) >> 3) |
624 ((float_to_ubyte(border_swizzled[1]) & 0xf8) << 2) |
625 ((float_to_ubyte(border_swizzled[2]) & 0xf8) << 7) |
626 ((float_to_ubyte(border_swizzled[3]) & 0x80) << 8);
627 } else if (desc->channel[1].size == 6) {
628 r = ((float_to_ubyte(border_swizzled[0]) & 0xf8) >> 3) |
629 ((float_to_ubyte(border_swizzled[1]) & 0xfc) << 3) |
630 ((float_to_ubyte(border_swizzled[2]) & 0xf8) << 8);
631 } else {
632 assert(0);
633 r = 0;
634 }
635 break;
636
637 default:
638 /* I think the fat formats (16, 32) are specified
639 * as the 8-bit ones. I am not sure how compressed formats
640 * work here. */
641 r = ((float_to_ubyte(border_swizzled[0]) & 0xff) << 0) |
642 ((float_to_ubyte(border_swizzled[1]) & 0xff) << 8) |
643 ((float_to_ubyte(border_swizzled[2]) & 0xff) << 16) |
644 ((float_to_ubyte(border_swizzled[3]) & 0xff) << 24);
645 }
646
647 return r;
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, view->swizzle,
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 }