r300g: rework vertex format fallback
[mesa.git] / src / gallium / drivers / r300 / r300_emit.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 /* r300_emit: Functions for emitting state. */
25
26 #include "util/u_format.h"
27 #include "util/u_math.h"
28 #include "util/u_mm.h"
29
30 #include "r300_context.h"
31 #include "r300_cb.h"
32 #include "r300_cs.h"
33 #include "r300_emit.h"
34 #include "r300_fs.h"
35 #include "r300_screen.h"
36 #include "r300_screen_buffer.h"
37 #include "r300_vs.h"
38
39 void r300_emit_blend_state(struct r300_context* r300,
40 unsigned size, void* state)
41 {
42 struct r300_blend_state* blend = (struct r300_blend_state*)state;
43 struct pipe_framebuffer_state* fb =
44 (struct pipe_framebuffer_state*)r300->fb_state.state;
45 CS_LOCALS(r300);
46
47 if (fb->nr_cbufs) {
48 WRITE_CS_TABLE(blend->cb, size);
49 } else {
50 WRITE_CS_TABLE(blend->cb_no_readwrite, size);
51 }
52 }
53
54 void r300_emit_blend_color_state(struct r300_context* r300,
55 unsigned size, void* state)
56 {
57 struct r300_blend_color_state* bc = (struct r300_blend_color_state*)state;
58 CS_LOCALS(r300);
59
60 WRITE_CS_TABLE(bc->cb, size);
61 }
62
63 void r300_emit_clip_state(struct r300_context* r300,
64 unsigned size, void* state)
65 {
66 struct r300_clip_state* clip = (struct r300_clip_state*)state;
67 CS_LOCALS(r300);
68
69 WRITE_CS_TABLE(clip->cb, size);
70 }
71
72 void r300_emit_dsa_state(struct r300_context* r300, unsigned size, void* state)
73 {
74 struct r300_dsa_state* dsa = (struct r300_dsa_state*)state;
75 struct pipe_framebuffer_state* fb =
76 (struct pipe_framebuffer_state*)r300->fb_state.state;
77 CS_LOCALS(r300);
78
79 if (fb->zsbuf) {
80 WRITE_CS_TABLE(&dsa->cb_begin, size);
81 } else {
82 WRITE_CS_TABLE(dsa->cb_no_readwrite, size);
83 }
84 }
85
86 static void get_rc_constant_state(
87 float vec[4],
88 struct r300_context * r300,
89 struct rc_constant * constant)
90 {
91 struct r300_textures_state* texstate = r300->textures_state.state;
92 struct r300_texture *tex;
93
94 assert(constant->Type == RC_CONSTANT_STATE);
95
96 /* vec should either be (0, 0, 0, 1), which should be a relatively safe
97 * RGBA or STRQ value, or it could be one of the RC_CONSTANT_STATE
98 * state factors. */
99
100 switch (constant->u.State[0]) {
101 /* Factor for converting rectangle coords to
102 * normalized coords. Should only show up on non-r500. */
103 case RC_STATE_R300_TEXRECT_FACTOR:
104 tex = r300_texture(texstate->sampler_views[constant->u.State[1]]->base.texture);
105 vec[0] = 1.0 / tex->desc.width0;
106 vec[1] = 1.0 / tex->desc.height0;
107 vec[2] = 0;
108 vec[3] = 1;
109 break;
110
111 case RC_STATE_R300_TEXSCALE_FACTOR:
112 tex = r300_texture(texstate->sampler_views[constant->u.State[1]]->base.texture);
113 /* Add a small number to the texture size to work around rounding errors in hw. */
114 vec[0] = tex->desc.b.b.width0 / (tex->desc.width0 + 0.001f);
115 vec[1] = tex->desc.b.b.height0 / (tex->desc.height0 + 0.001f);
116 vec[2] = tex->desc.b.b.depth0 / (tex->desc.depth0 + 0.001f);
117 vec[3] = 1;
118 break;
119
120 case RC_STATE_R300_VIEWPORT_SCALE:
121 vec[0] = r300->viewport.scale[0];
122 vec[1] = r300->viewport.scale[1];
123 vec[2] = r300->viewport.scale[2];
124 vec[3] = 1;
125 break;
126
127 case RC_STATE_R300_VIEWPORT_OFFSET:
128 vec[0] = r300->viewport.translate[0];
129 vec[1] = r300->viewport.translate[1];
130 vec[2] = r300->viewport.translate[2];
131 vec[3] = 1;
132 break;
133
134 default:
135 fprintf(stderr, "r300: Implementation error: "
136 "Unknown RC_CONSTANT type %d\n", constant->u.State[0]);
137 vec[0] = 0;
138 vec[1] = 0;
139 vec[2] = 0;
140 vec[3] = 1;
141 }
142 }
143
144 /* Convert a normal single-precision float into the 7.16 format
145 * used by the R300 fragment shader.
146 */
147 uint32_t pack_float24(float f)
148 {
149 union {
150 float fl;
151 uint32_t u;
152 } u;
153 float mantissa;
154 int exponent;
155 uint32_t float24 = 0;
156
157 if (f == 0.0)
158 return 0;
159
160 u.fl = f;
161
162 mantissa = frexpf(f, &exponent);
163
164 /* Handle -ve */
165 if (mantissa < 0) {
166 float24 |= (1 << 23);
167 mantissa = mantissa * -1.0;
168 }
169 /* Handle exponent, bias of 63 */
170 exponent += 62;
171 float24 |= (exponent << 16);
172 /* Kill 7 LSB of mantissa */
173 float24 |= (u.u & 0x7FFFFF) >> 7;
174
175 return float24;
176 }
177
178 void r300_emit_fs(struct r300_context* r300, unsigned size, void *state)
179 {
180 struct r300_fragment_shader *fs = r300_fs(r300);
181 CS_LOCALS(r300);
182
183 WRITE_CS_TABLE(fs->shader->cb_code, fs->shader->cb_code_size);
184 }
185
186 void r300_emit_fs_constants(struct r300_context* r300, unsigned size, void *state)
187 {
188 struct r300_fragment_shader *fs = r300_fs(r300);
189 struct r300_constant_buffer *buf = (struct r300_constant_buffer*)state;
190 unsigned count = fs->shader->externals_count;
191 unsigned i, j;
192 CS_LOCALS(r300);
193
194 if (count == 0)
195 return;
196
197 BEGIN_CS(size);
198 OUT_CS_REG_SEQ(R300_PFS_PARAM_0_X, count * 4);
199 if (buf->remap_table){
200 for (i = 0; i < count; i++) {
201 float *data = (float*)&buf->ptr[buf->remap_table[i]*4];
202 for (j = 0; j < 4; j++)
203 OUT_CS(pack_float24(data[j]));
204 }
205 } else {
206 for (i = 0; i < count; i++)
207 for (j = 0; j < 4; j++)
208 OUT_CS(pack_float24(*(float*)&buf->ptr[i*4+j]));
209 }
210
211 END_CS;
212 }
213
214 void r300_emit_fs_rc_constant_state(struct r300_context* r300, unsigned size, void *state)
215 {
216 struct r300_fragment_shader *fs = r300_fs(r300);
217 struct rc_constant_list *constants = &fs->shader->code.constants;
218 unsigned i;
219 unsigned count = fs->shader->rc_state_count;
220 unsigned first = fs->shader->externals_count;
221 unsigned end = constants->Count;
222 unsigned j;
223 CS_LOCALS(r300);
224
225 if (count == 0)
226 return;
227
228 BEGIN_CS(size);
229 for(i = first; i < end; ++i) {
230 if (constants->Constants[i].Type == RC_CONSTANT_STATE) {
231 float data[4];
232
233 get_rc_constant_state(data, r300, &constants->Constants[i]);
234
235 OUT_CS_REG_SEQ(R300_PFS_PARAM_0_X + i * 16, 4);
236 for (j = 0; j < 4; j++)
237 OUT_CS(pack_float24(data[j]));
238 }
239 }
240 END_CS;
241 }
242
243 void r500_emit_fs(struct r300_context* r300, unsigned size, void *state)
244 {
245 struct r300_fragment_shader *fs = r300_fs(r300);
246 CS_LOCALS(r300);
247
248 WRITE_CS_TABLE(fs->shader->cb_code, fs->shader->cb_code_size);
249 }
250
251 void r500_emit_fs_constants(struct r300_context* r300, unsigned size, void *state)
252 {
253 struct r300_fragment_shader *fs = r300_fs(r300);
254 struct r300_constant_buffer *buf = (struct r300_constant_buffer*)state;
255 unsigned count = fs->shader->externals_count;
256 CS_LOCALS(r300);
257
258 if (count == 0)
259 return;
260
261 BEGIN_CS(size);
262 OUT_CS_REG(R500_GA_US_VECTOR_INDEX, R500_GA_US_VECTOR_INDEX_TYPE_CONST);
263 OUT_CS_ONE_REG(R500_GA_US_VECTOR_DATA, count * 4);
264 if (buf->remap_table){
265 for (unsigned i = 0; i < count; i++) {
266 uint32_t *data = &buf->ptr[buf->remap_table[i]*4];
267 OUT_CS_TABLE(data, 4);
268 }
269 } else {
270 OUT_CS_TABLE(buf->ptr, count * 4);
271 }
272 END_CS;
273 }
274
275 void r500_emit_fs_rc_constant_state(struct r300_context* r300, unsigned size, void *state)
276 {
277 struct r300_fragment_shader *fs = r300_fs(r300);
278 struct rc_constant_list *constants = &fs->shader->code.constants;
279 unsigned i;
280 unsigned count = fs->shader->rc_state_count;
281 unsigned first = fs->shader->externals_count;
282 unsigned end = constants->Count;
283 CS_LOCALS(r300);
284
285 if (count == 0)
286 return;
287
288 BEGIN_CS(size);
289 for(i = first; i < end; ++i) {
290 if (constants->Constants[i].Type == RC_CONSTANT_STATE) {
291 float data[4];
292
293 get_rc_constant_state(data, r300, &constants->Constants[i]);
294
295 OUT_CS_REG(R500_GA_US_VECTOR_INDEX,
296 R500_GA_US_VECTOR_INDEX_TYPE_CONST |
297 (i & R500_GA_US_VECTOR_INDEX_MASK));
298 OUT_CS_ONE_REG(R500_GA_US_VECTOR_DATA, 4);
299 OUT_CS_TABLE(data, 4);
300 }
301 }
302 END_CS;
303 }
304
305 void r300_emit_gpu_flush(struct r300_context *r300, unsigned size, void *state)
306 {
307 struct r300_gpu_flush *gpuflush = (struct r300_gpu_flush*)state;
308 struct pipe_framebuffer_state* fb =
309 (struct pipe_framebuffer_state*)r300->fb_state.state;
310 uint32_t height = fb->height;
311 uint32_t width = fb->width;
312 CS_LOCALS(r300);
313
314 if (r300->cbzb_clear) {
315 struct r300_surface *surf = r300_surface(fb->cbufs[0]);
316
317 height = surf->cbzb_height;
318 width = surf->cbzb_width;
319 }
320
321 DBG(r300, DBG_SCISSOR,
322 "r300: Scissor width: %i, height: %i, CBZB clear: %s\n",
323 width, height, r300->cbzb_clear ? "YES" : "NO");
324
325 BEGIN_CS(size);
326
327 /* Set up scissors.
328 * By writing to the SC registers, SC & US assert idle. */
329 OUT_CS_REG_SEQ(R300_SC_SCISSORS_TL, 2);
330 if (r300->screen->caps.is_r500) {
331 OUT_CS(0);
332 OUT_CS(((width - 1) << R300_SCISSORS_X_SHIFT) |
333 ((height - 1) << R300_SCISSORS_Y_SHIFT));
334 } else {
335 OUT_CS((1440 << R300_SCISSORS_X_SHIFT) |
336 (1440 << R300_SCISSORS_Y_SHIFT));
337 OUT_CS(((width + 1440-1) << R300_SCISSORS_X_SHIFT) |
338 ((height + 1440-1) << R300_SCISSORS_Y_SHIFT));
339 }
340
341 /* Flush CB & ZB caches and wait until the 3D engine is idle and clean. */
342 OUT_CS_TABLE(gpuflush->cb_flush_clean, 6);
343 END_CS;
344 }
345
346 void r300_emit_aa_state(struct r300_context *r300, unsigned size, void *state)
347 {
348 struct r300_aa_state *aa = (struct r300_aa_state*)state;
349 CS_LOCALS(r300);
350
351 BEGIN_CS(size);
352 OUT_CS_REG(R300_GB_AA_CONFIG, aa->aa_config);
353
354 if (aa->dest) {
355 OUT_CS_REG_SEQ(R300_RB3D_AARESOLVE_OFFSET, 1);
356 OUT_CS_RELOC(aa->dest->cs_buffer, aa->dest->offset);
357
358 OUT_CS_REG_SEQ(R300_RB3D_AARESOLVE_PITCH, 1);
359 OUT_CS_RELOC(aa->dest->cs_buffer, aa->dest->pitch);
360 }
361
362 OUT_CS_REG(R300_RB3D_AARESOLVE_CTL, aa->aaresolve_ctl);
363 END_CS;
364 }
365
366 void r300_emit_fb_state(struct r300_context* r300, unsigned size, void* state)
367 {
368 struct pipe_framebuffer_state* fb = (struct pipe_framebuffer_state*)state;
369 struct r300_surface* surf;
370 unsigned i;
371 boolean can_hyperz = r300->rws->get_value(r300->rws, R300_CAN_HYPERZ);
372 uint32_t rb3d_cctl = 0;
373
374 CS_LOCALS(r300);
375
376 BEGIN_CS(size);
377
378 /* NUM_MULTIWRITES replicates COLOR[0] to all colorbuffers, which is not
379 * what we usually want. */
380 if (r300->screen->caps.is_r500) {
381 rb3d_cctl = R300_RB3D_CCTL_INDEPENDENT_COLORFORMAT_ENABLE_ENABLE;
382 }
383 if (fb->nr_cbufs &&
384 r300_fragment_shader_writes_all(r300_fs(r300))) {
385 rb3d_cctl |= R300_RB3D_CCTL_NUM_MULTIWRITES(fb->nr_cbufs);
386 }
387
388 OUT_CS_REG(R300_RB3D_CCTL, rb3d_cctl);
389
390 /* Set up colorbuffers. */
391 for (i = 0; i < fb->nr_cbufs; i++) {
392 surf = r300_surface(fb->cbufs[i]);
393
394 OUT_CS_REG_SEQ(R300_RB3D_COLOROFFSET0 + (4 * i), 1);
395 OUT_CS_RELOC(surf->cs_buffer, surf->offset);
396
397 OUT_CS_REG_SEQ(R300_RB3D_COLORPITCH0 + (4 * i), 1);
398 OUT_CS_RELOC(surf->cs_buffer, surf->pitch);
399 }
400
401 /* Set up the ZB part of the CBZB clear. */
402 if (r300->cbzb_clear) {
403 surf = r300_surface(fb->cbufs[0]);
404
405 OUT_CS_REG(R300_ZB_FORMAT, surf->cbzb_format);
406
407 OUT_CS_REG_SEQ(R300_ZB_DEPTHOFFSET, 1);
408 OUT_CS_RELOC(surf->cs_buffer, surf->cbzb_midpoint_offset);
409
410 OUT_CS_REG_SEQ(R300_ZB_DEPTHPITCH, 1);
411 OUT_CS_RELOC(surf->cs_buffer, surf->cbzb_pitch);
412
413 DBG(r300, DBG_CBZB,
414 "CBZB clearing cbuf %08x %08x\n", surf->cbzb_format,
415 surf->cbzb_pitch);
416 }
417 /* Set up a zbuffer. */
418 else if (fb->zsbuf) {
419 surf = r300_surface(fb->zsbuf);
420
421 OUT_CS_REG(R300_ZB_FORMAT, surf->format);
422
423 OUT_CS_REG_SEQ(R300_ZB_DEPTHOFFSET, 1);
424 OUT_CS_RELOC(surf->cs_buffer, surf->offset);
425
426 OUT_CS_REG_SEQ(R300_ZB_DEPTHPITCH, 1);
427 OUT_CS_RELOC(surf->cs_buffer, surf->pitch);
428
429 if (can_hyperz) {
430 uint32_t surf_pitch;
431 struct r300_texture *tex;
432 int level = surf->base.u.tex.level;
433 tex = r300_texture(surf->base.texture);
434
435 surf_pitch = surf->pitch & R300_DEPTHPITCH_MASK;
436
437 /* HiZ RAM. */
438 if (r300->screen->caps.hiz_ram) {
439 if (tex->hiz_mem[level]) {
440 OUT_CS_REG(R300_ZB_HIZ_OFFSET, tex->hiz_mem[level]->ofs << 2);
441 OUT_CS_REG(R300_ZB_HIZ_PITCH, surf_pitch);
442 } else {
443 OUT_CS_REG(R300_ZB_HIZ_OFFSET, 0);
444 OUT_CS_REG(R300_ZB_HIZ_PITCH, 0);
445 }
446 }
447
448 /* Z Mask RAM. (compressed zbuffer) */
449 OUT_CS_REG(R300_ZB_ZMASK_OFFSET, 0);
450 OUT_CS_REG(R300_ZB_ZMASK_PITCH, surf_pitch);
451 }
452 }
453
454 END_CS;
455 }
456
457 void r300_emit_hyperz_state(struct r300_context *r300,
458 unsigned size, void *state)
459 {
460 struct r300_hyperz_state *z = state;
461 CS_LOCALS(r300);
462
463 if (z->flush)
464 WRITE_CS_TABLE(&z->cb_flush_begin, size);
465 else
466 WRITE_CS_TABLE(&z->cb_begin, size - 2);
467 }
468
469 void r300_emit_hyperz_end(struct r300_context *r300)
470 {
471 struct r300_hyperz_state z =
472 *(struct r300_hyperz_state*)r300->hyperz_state.state;
473
474 z.flush = 1;
475 z.zb_bw_cntl = 0;
476 z.zb_depthclearvalue = 0;
477 z.sc_hyperz = R300_SC_HYPERZ_ADJ_2;
478 z.gb_z_peq_config = 0;
479
480 r300_emit_hyperz_state(r300, r300->hyperz_state.size, &z);
481 }
482
483 void r300_emit_fb_state_pipelined(struct r300_context *r300,
484 unsigned size, void *state)
485 {
486 struct pipe_framebuffer_state* fb =
487 (struct pipe_framebuffer_state*)r300->fb_state.state;
488 unsigned i, num_cbufs = fb->nr_cbufs;
489 CS_LOCALS(r300);
490
491 /* If we use the multiwrite feature, the colorbuffers 2,3,4 must be
492 * marked as UNUSED in the US block. */
493 if (r300_fragment_shader_writes_all(r300_fs(r300))) {
494 num_cbufs = MIN2(num_cbufs, 1);
495 }
496
497 BEGIN_CS(size);
498
499 /* Colorbuffer format in the US block.
500 * (must be written after unpipelined regs) */
501 OUT_CS_REG_SEQ(R300_US_OUT_FMT_0, 4);
502 for (i = 0; i < num_cbufs; i++) {
503 OUT_CS(r300_surface(fb->cbufs[i])->format);
504 }
505 for (; i < 4; i++) {
506 OUT_CS(R300_US_OUT_FMT_UNUSED);
507 }
508
509 /* Multisampling. Depends on framebuffer sample count.
510 * These are pipelined regs and as such cannot be moved
511 * to the AA state. */
512 if (r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0)) {
513 unsigned mspos0 = 0x66666666;
514 unsigned mspos1 = 0x6666666;
515
516 if (fb->nr_cbufs && fb->cbufs[0]->texture->nr_samples > 1) {
517 /* Subsample placement. These may not be optimal. */
518 switch (fb->cbufs[0]->texture->nr_samples) {
519 case 2:
520 mspos0 = 0x33996633;
521 mspos1 = 0x6666663;
522 break;
523 case 3:
524 mspos0 = 0x33936933;
525 mspos1 = 0x6666663;
526 break;
527 case 4:
528 mspos0 = 0x33939933;
529 mspos1 = 0x3966663;
530 break;
531 case 6:
532 mspos0 = 0x22a2aa22;
533 mspos1 = 0x2a65672;
534 break;
535 default:
536 debug_printf("r300: Bad number of multisamples!\n");
537 }
538 }
539
540 OUT_CS_REG_SEQ(R300_GB_MSPOS0, 2);
541 OUT_CS(mspos0);
542 OUT_CS(mspos1);
543 }
544 END_CS;
545 }
546
547 void r300_emit_query_start(struct r300_context *r300, unsigned size, void*state)
548 {
549 struct r300_query *query = r300->query_current;
550 CS_LOCALS(r300);
551
552 if (!query)
553 return;
554
555 BEGIN_CS(size);
556 if (r300->screen->caps.family == CHIP_FAMILY_RV530) {
557 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL);
558 } else {
559 OUT_CS_REG(R300_SU_REG_DEST, R300_RASTER_PIPE_SELECT_ALL);
560 }
561 OUT_CS_REG(R300_ZB_ZPASS_DATA, 0);
562 END_CS;
563 query->begin_emitted = TRUE;
564 query->flushed = FALSE;
565 }
566
567 static void r300_emit_query_end_frag_pipes(struct r300_context *r300,
568 struct r300_query *query)
569 {
570 struct r300_capabilities* caps = &r300->screen->caps;
571 struct r300_winsys_cs_buffer *buf = r300->query_current->cs_buffer;
572 CS_LOCALS(r300);
573
574 assert(caps->num_frag_pipes);
575
576 BEGIN_CS(6 * caps->num_frag_pipes + 2);
577 /* I'm not so sure I like this switch, but it's hard to be elegant
578 * when there's so many special cases...
579 *
580 * So here's the basic idea. For each pipe, enable writes to it only,
581 * then put out the relocation for ZPASS_ADDR, taking into account a
582 * 4-byte offset for each pipe. RV380 and older are special; they have
583 * only two pipes, and the second pipe's enable is on bit 3, not bit 1,
584 * so there's a chipset cap for that. */
585 switch (caps->num_frag_pipes) {
586 case 4:
587 /* pipe 3 only */
588 OUT_CS_REG(R300_SU_REG_DEST, 1 << 3);
589 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
590 OUT_CS_RELOC(buf, (query->num_results + 3) * 4);
591 case 3:
592 /* pipe 2 only */
593 OUT_CS_REG(R300_SU_REG_DEST, 1 << 2);
594 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
595 OUT_CS_RELOC(buf, (query->num_results + 2) * 4);
596 case 2:
597 /* pipe 1 only */
598 /* As mentioned above, accomodate RV380 and older. */
599 OUT_CS_REG(R300_SU_REG_DEST,
600 1 << (caps->high_second_pipe ? 3 : 1));
601 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
602 OUT_CS_RELOC(buf, (query->num_results + 1) * 4);
603 case 1:
604 /* pipe 0 only */
605 OUT_CS_REG(R300_SU_REG_DEST, 1 << 0);
606 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
607 OUT_CS_RELOC(buf, (query->num_results + 0) * 4);
608 break;
609 default:
610 fprintf(stderr, "r300: Implementation error: Chipset reports %d"
611 " pixel pipes!\n", caps->num_frag_pipes);
612 abort();
613 }
614
615 /* And, finally, reset it to normal... */
616 OUT_CS_REG(R300_SU_REG_DEST, 0xF);
617 END_CS;
618 }
619
620 static void rv530_emit_query_end_single_z(struct r300_context *r300,
621 struct r300_query *query)
622 {
623 struct r300_winsys_cs_buffer *buf = r300->query_current->cs_buffer;
624 CS_LOCALS(r300);
625
626 BEGIN_CS(8);
627 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_0);
628 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
629 OUT_CS_RELOC(buf, query->num_results * 4);
630 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL);
631 END_CS;
632 }
633
634 static void rv530_emit_query_end_double_z(struct r300_context *r300,
635 struct r300_query *query)
636 {
637 struct r300_winsys_cs_buffer *buf = r300->query_current->cs_buffer;
638 CS_LOCALS(r300);
639
640 BEGIN_CS(14);
641 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_0);
642 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
643 OUT_CS_RELOC(buf, (query->num_results + 0) * 4);
644 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_1);
645 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
646 OUT_CS_RELOC(buf, (query->num_results + 1) * 4);
647 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL);
648 END_CS;
649 }
650
651 void r300_emit_query_end(struct r300_context* r300)
652 {
653 struct r300_capabilities *caps = &r300->screen->caps;
654 struct r300_query *query = r300->query_current;
655
656 if (!query)
657 return;
658
659 if (query->begin_emitted == FALSE)
660 return;
661
662 if (caps->family == CHIP_FAMILY_RV530) {
663 if (caps->num_z_pipes == 2)
664 rv530_emit_query_end_double_z(r300, query);
665 else
666 rv530_emit_query_end_single_z(r300, query);
667 } else
668 r300_emit_query_end_frag_pipes(r300, query);
669
670 query->begin_emitted = FALSE;
671 query->num_results += query->num_pipes;
672
673 /* XXX grab all the results and reset the counter. */
674 if (query->num_results >= query->buffer_size / 4 - 4) {
675 query->num_results = (query->buffer_size / 4) / 2;
676 fprintf(stderr, "r300: Rewinding OQBO...\n");
677 }
678 }
679
680 void r300_emit_invariant_state(struct r300_context *r300,
681 unsigned size, void *state)
682 {
683 CS_LOCALS(r300);
684 WRITE_CS_TABLE(state, size);
685 }
686
687 void r300_emit_rs_state(struct r300_context* r300, unsigned size, void* state)
688 {
689 struct r300_rs_state* rs = state;
690 CS_LOCALS(r300);
691
692 BEGIN_CS(size);
693 OUT_CS_TABLE(rs->cb_main, RS_STATE_MAIN_SIZE);
694 if (rs->polygon_offset_enable) {
695 if (r300->zbuffer_bpp == 16) {
696 OUT_CS_TABLE(rs->cb_poly_offset_zb16, 5);
697 } else {
698 OUT_CS_TABLE(rs->cb_poly_offset_zb24, 5);
699 }
700 }
701 END_CS;
702 }
703
704 void r300_emit_rs_block_state(struct r300_context* r300,
705 unsigned size, void* state)
706 {
707 struct r300_rs_block* rs = (struct r300_rs_block*)state;
708 unsigned i;
709 /* It's the same for both INST and IP tables */
710 unsigned count = (rs->inst_count & R300_RS_INST_COUNT_MASK) + 1;
711 CS_LOCALS(r300);
712
713 if (DBG_ON(r300, DBG_RS_BLOCK)) {
714 r500_dump_rs_block(rs);
715
716 fprintf(stderr, "r300: RS emit:\n");
717
718 for (i = 0; i < count; i++)
719 fprintf(stderr, " : ip %d: 0x%08x\n", i, rs->ip[i]);
720
721 for (i = 0; i < count; i++)
722 fprintf(stderr, " : inst %d: 0x%08x\n", i, rs->inst[i]);
723
724 fprintf(stderr, " : count: 0x%08x inst_count: 0x%08x\n",
725 rs->count, rs->inst_count);
726 }
727
728 BEGIN_CS(size);
729 OUT_CS_REG_SEQ(R300_VAP_VTX_STATE_CNTL, 2);
730 OUT_CS(rs->vap_vtx_state_cntl);
731 OUT_CS(rs->vap_vsm_vtx_assm);
732 OUT_CS_REG_SEQ(R300_VAP_OUTPUT_VTX_FMT_0, 2);
733 OUT_CS(rs->vap_out_vtx_fmt[0]);
734 OUT_CS(rs->vap_out_vtx_fmt[1]);
735 OUT_CS_REG_SEQ(R300_GB_ENABLE, 1);
736 OUT_CS(rs->gb_enable);
737
738 if (r300->screen->caps.is_r500) {
739 OUT_CS_REG_SEQ(R500_RS_IP_0, count);
740 } else {
741 OUT_CS_REG_SEQ(R300_RS_IP_0, count);
742 }
743 OUT_CS_TABLE(rs->ip, count);
744
745 OUT_CS_REG_SEQ(R300_RS_COUNT, 2);
746 OUT_CS(rs->count);
747 OUT_CS(rs->inst_count);
748
749 if (r300->screen->caps.is_r500) {
750 OUT_CS_REG_SEQ(R500_RS_INST_0, count);
751 } else {
752 OUT_CS_REG_SEQ(R300_RS_INST_0, count);
753 }
754 OUT_CS_TABLE(rs->inst, count);
755 END_CS;
756 }
757
758 void r300_emit_scissor_state(struct r300_context* r300,
759 unsigned size, void* state)
760 {
761 struct pipe_scissor_state* scissor = (struct pipe_scissor_state*)state;
762 CS_LOCALS(r300);
763
764 BEGIN_CS(size);
765 OUT_CS_REG_SEQ(R300_SC_CLIPRECT_TL_0, 2);
766 if (r300->screen->caps.is_r500) {
767 OUT_CS((scissor->minx << R300_CLIPRECT_X_SHIFT) |
768 (scissor->miny << R300_CLIPRECT_Y_SHIFT));
769 OUT_CS(((scissor->maxx - 1) << R300_CLIPRECT_X_SHIFT) |
770 ((scissor->maxy - 1) << R300_CLIPRECT_Y_SHIFT));
771 } else {
772 OUT_CS(((scissor->minx + 1440) << R300_CLIPRECT_X_SHIFT) |
773 ((scissor->miny + 1440) << R300_CLIPRECT_Y_SHIFT));
774 OUT_CS(((scissor->maxx + 1440-1) << R300_CLIPRECT_X_SHIFT) |
775 ((scissor->maxy + 1440-1) << R300_CLIPRECT_Y_SHIFT));
776 }
777 END_CS;
778 }
779
780 void r300_emit_textures_state(struct r300_context *r300,
781 unsigned size, void *state)
782 {
783 struct r300_textures_state *allstate = (struct r300_textures_state*)state;
784 struct r300_texture_sampler_state *texstate;
785 struct r300_texture *tex;
786 unsigned i;
787 CS_LOCALS(r300);
788
789 BEGIN_CS(size);
790 OUT_CS_REG(R300_TX_ENABLE, allstate->tx_enable);
791
792 for (i = 0; i < allstate->count; i++) {
793 if ((1 << i) & allstate->tx_enable) {
794 texstate = &allstate->regs[i];
795 tex = r300_texture(allstate->sampler_views[i]->base.texture);
796
797 OUT_CS_REG(R300_TX_FILTER0_0 + (i * 4), texstate->filter0);
798 OUT_CS_REG(R300_TX_FILTER1_0 + (i * 4), texstate->filter1);
799 OUT_CS_REG(R300_TX_BORDER_COLOR_0 + (i * 4),
800 texstate->border_color);
801
802 OUT_CS_REG(R300_TX_FORMAT0_0 + (i * 4), texstate->format.format0);
803 OUT_CS_REG(R300_TX_FORMAT1_0 + (i * 4), texstate->format.format1);
804 OUT_CS_REG(R300_TX_FORMAT2_0 + (i * 4), texstate->format.format2);
805
806 OUT_CS_REG_SEQ(R300_TX_OFFSET_0 + (i * 4), 1);
807 OUT_CS_TEX_RELOC(tex, texstate->format.tile_config);
808 }
809 }
810 END_CS;
811 }
812
813 static void r300_update_vertex_arrays_cb(struct r300_context *r300, unsigned packet_size)
814 {
815 struct pipe_vertex_buffer *vb1, *vb2, *vbuf = r300->vertex_buffer;
816 struct pipe_vertex_element *velem = r300->velems->velem;
817 unsigned *hw_format_size = r300->velems->hw_format_size;
818 unsigned size1, size2, vertex_array_count = r300->velems->count;
819 int i;
820 CB_LOCALS;
821
822 BEGIN_CB(r300->vertex_arrays_cb, packet_size);
823 for (i = 0; i < vertex_array_count - 1; i += 2) {
824 vb1 = &vbuf[velem[i].vertex_buffer_index];
825 vb2 = &vbuf[velem[i+1].vertex_buffer_index];
826 size1 = hw_format_size[i];
827 size2 = hw_format_size[i+1];
828
829 OUT_CB(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(vb1->stride) |
830 R300_VBPNTR_SIZE1(size2) | R300_VBPNTR_STRIDE1(vb2->stride));
831 OUT_CB(vb1->buffer_offset + velem[i].src_offset);
832 OUT_CB(vb2->buffer_offset + velem[i+1].src_offset);
833 }
834
835 if (vertex_array_count & 1) {
836 vb1 = &vbuf[velem[i].vertex_buffer_index];
837 size1 = hw_format_size[i];
838
839 OUT_CB(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(vb1->stride));
840 OUT_CB(vb1->buffer_offset + velem[i].src_offset);
841 }
842 END_CB;
843
844 r300->vertex_arrays_dirty = FALSE;
845 }
846
847 void r300_emit_vertex_arrays(struct r300_context* r300, int offset, boolean indexed)
848 {
849 struct pipe_vertex_buffer *vbuf = r300->vertex_buffer;
850 struct pipe_resource **valid_vbuf = r300->real_vertex_buffer;
851 struct pipe_vertex_element *velem = r300->velems->velem;
852 struct r300_buffer *buf;
853 int i;
854 unsigned vertex_array_count = r300->velems->count;
855 unsigned packet_size = (vertex_array_count * 3 + 1) / 2;
856 CS_LOCALS(r300);
857
858 BEGIN_CS(2 + packet_size + vertex_array_count * 2);
859 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR, packet_size);
860 OUT_CS(vertex_array_count | (!indexed ? R300_VC_FORCE_PREFETCH : 0));
861
862 if (!offset) {
863 if (r300->vertex_arrays_dirty) {
864 r300_update_vertex_arrays_cb(r300, packet_size);
865 }
866 OUT_CS_TABLE(r300->vertex_arrays_cb, packet_size);
867 } else {
868 struct pipe_vertex_buffer *vb1, *vb2;
869 unsigned *hw_format_size = r300->velems->hw_format_size;
870 unsigned size1, size2;
871
872 for (i = 0; i < vertex_array_count - 1; i += 2) {
873 vb1 = &vbuf[velem[i].vertex_buffer_index];
874 vb2 = &vbuf[velem[i+1].vertex_buffer_index];
875 size1 = hw_format_size[i];
876 size2 = hw_format_size[i+1];
877
878 OUT_CS(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(vb1->stride) |
879 R300_VBPNTR_SIZE1(size2) | R300_VBPNTR_STRIDE1(vb2->stride));
880 OUT_CS(vb1->buffer_offset + velem[i].src_offset + offset * vb1->stride);
881 OUT_CS(vb2->buffer_offset + velem[i+1].src_offset + offset * vb2->stride);
882 }
883
884 if (vertex_array_count & 1) {
885 vb1 = &vbuf[velem[i].vertex_buffer_index];
886 size1 = hw_format_size[i];
887
888 OUT_CS(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(vb1->stride));
889 OUT_CS(vb1->buffer_offset + velem[i].src_offset + offset * vb1->stride);
890 }
891 }
892
893 for (i = 0; i < vertex_array_count; i++) {
894 buf = r300_buffer(valid_vbuf[velem[i].vertex_buffer_index]);
895 OUT_CS_BUF_RELOC_NO_OFFSET(&buf->b.b);
896 }
897 END_CS;
898 }
899
900 void r300_emit_vertex_arrays_swtcl(struct r300_context *r300, boolean indexed)
901 {
902 CS_LOCALS(r300);
903
904 DBG(r300, DBG_SWTCL, "r300: Preparing vertex buffer %p for render, "
905 "vertex size %d\n", r300->vbo,
906 r300->vertex_info.size);
907 /* Set the pointer to our vertex buffer. The emitted values are this:
908 * PACKET3 [3D_LOAD_VBPNTR]
909 * COUNT [1]
910 * FORMAT [size | stride << 8]
911 * OFFSET [offset into BO]
912 * VBPNTR [relocated BO]
913 */
914 BEGIN_CS(7);
915 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR, 3);
916 OUT_CS(1 | (!indexed ? R300_VC_FORCE_PREFETCH : 0));
917 OUT_CS(r300->vertex_info.size |
918 (r300->vertex_info.size << 8));
919 OUT_CS(r300->draw_vbo_offset);
920 OUT_CS_BUF_RELOC(r300->vbo, 0);
921 END_CS;
922 }
923
924 void r300_emit_vertex_stream_state(struct r300_context* r300,
925 unsigned size, void* state)
926 {
927 struct r300_vertex_stream_state *streams =
928 (struct r300_vertex_stream_state*)state;
929 unsigned i;
930 CS_LOCALS(r300);
931
932 if (DBG_ON(r300, DBG_PSC)) {
933 fprintf(stderr, "r300: PSC emit:\n");
934
935 for (i = 0; i < streams->count; i++) {
936 fprintf(stderr, " : prog_stream_cntl%d: 0x%08x\n", i,
937 streams->vap_prog_stream_cntl[i]);
938 }
939
940 for (i = 0; i < streams->count; i++) {
941 fprintf(stderr, " : prog_stream_cntl_ext%d: 0x%08x\n", i,
942 streams->vap_prog_stream_cntl_ext[i]);
943 }
944 }
945
946 BEGIN_CS(size);
947 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_0, streams->count);
948 OUT_CS_TABLE(streams->vap_prog_stream_cntl, streams->count);
949 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_EXT_0, streams->count);
950 OUT_CS_TABLE(streams->vap_prog_stream_cntl_ext, streams->count);
951 END_CS;
952 }
953
954 void r300_emit_pvs_flush(struct r300_context* r300, unsigned size, void* state)
955 {
956 CS_LOCALS(r300);
957
958 BEGIN_CS(size);
959 OUT_CS_REG(R300_VAP_PVS_STATE_FLUSH_REG, 0x0);
960 END_CS;
961 }
962
963 void r300_emit_vap_invariant_state(struct r300_context *r300,
964 unsigned size, void *state)
965 {
966 CS_LOCALS(r300);
967 WRITE_CS_TABLE(state, size);
968 }
969
970 void r300_emit_vs_state(struct r300_context* r300, unsigned size, void* state)
971 {
972 struct r300_vertex_shader* vs = (struct r300_vertex_shader*)state;
973 struct r300_vertex_program_code* code = &vs->code;
974 struct r300_screen* r300screen = r300->screen;
975 unsigned instruction_count = code->length / 4;
976
977 unsigned vtx_mem_size = r300screen->caps.is_r500 ? 128 : 72;
978 unsigned input_count = MAX2(util_bitcount(code->InputsRead), 1);
979 unsigned output_count = MAX2(util_bitcount(code->OutputsWritten), 1);
980 unsigned temp_count = MAX2(code->num_temporaries, 1);
981
982 unsigned pvs_num_slots = MIN3(vtx_mem_size / input_count,
983 vtx_mem_size / output_count, 10);
984 unsigned pvs_num_controllers = MIN2(vtx_mem_size / temp_count, 5);
985
986 CS_LOCALS(r300);
987
988 BEGIN_CS(size);
989
990 /* R300_VAP_PVS_CODE_CNTL_0
991 * R300_VAP_PVS_CONST_CNTL
992 * R300_VAP_PVS_CODE_CNTL_1
993 * See the r5xx docs for instructions on how to use these. */
994 OUT_CS_REG(R300_VAP_PVS_CODE_CNTL_0, R300_PVS_FIRST_INST(0) |
995 R300_PVS_XYZW_VALID_INST(instruction_count - 1) |
996 R300_PVS_LAST_INST(instruction_count - 1));
997 OUT_CS_REG(R300_VAP_PVS_CODE_CNTL_1, instruction_count - 1);
998
999 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG, 0);
1000 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, code->length);
1001 OUT_CS_TABLE(code->body.d, code->length);
1002
1003 OUT_CS_REG(R300_VAP_CNTL, R300_PVS_NUM_SLOTS(pvs_num_slots) |
1004 R300_PVS_NUM_CNTLRS(pvs_num_controllers) |
1005 R300_PVS_NUM_FPUS(r300screen->caps.num_vert_fpus) |
1006 R300_PVS_VF_MAX_VTX_NUM(12) |
1007 (r300screen->caps.is_r500 ? R500_TCL_STATE_OPTIMIZATION : 0));
1008
1009 /* Emit flow control instructions. */
1010 if (code->num_fc_ops) {
1011
1012 OUT_CS_REG(R300_VAP_PVS_FLOW_CNTL_OPC, code->fc_ops);
1013 if (r300screen->caps.is_r500) {
1014 OUT_CS_REG_SEQ(R500_VAP_PVS_FLOW_CNTL_ADDRS_LW_0, code->num_fc_ops * 2);
1015 OUT_CS_TABLE(code->fc_op_addrs.r500, code->num_fc_ops * 2);
1016 } else {
1017 OUT_CS_REG_SEQ(R300_VAP_PVS_FLOW_CNTL_ADDRS_0, code->num_fc_ops);
1018 OUT_CS_TABLE(code->fc_op_addrs.r300, code->num_fc_ops);
1019 }
1020 OUT_CS_REG_SEQ(R300_VAP_PVS_FLOW_CNTL_LOOP_INDEX_0, code->num_fc_ops);
1021 OUT_CS_TABLE(code->fc_loop_index, code->num_fc_ops);
1022 }
1023
1024 END_CS;
1025 }
1026
1027 void r300_emit_vs_constants(struct r300_context* r300,
1028 unsigned size, void *state)
1029 {
1030 unsigned count =
1031 ((struct r300_vertex_shader*)r300->vs_state.state)->externals_count;
1032 struct r300_constant_buffer *buf = (struct r300_constant_buffer*)state;
1033 struct r300_vertex_shader *vs = (struct r300_vertex_shader*)r300->vs_state.state;
1034 unsigned i;
1035 int imm_first = vs->externals_count;
1036 int imm_end = vs->code.constants.Count;
1037 int imm_count = vs->immediates_count;
1038 CS_LOCALS(r300);
1039
1040 BEGIN_CS(size);
1041 OUT_CS_REG(R300_VAP_PVS_CONST_CNTL,
1042 R300_PVS_CONST_BASE_OFFSET(buf->buffer_base) |
1043 R300_PVS_MAX_CONST_ADDR(MAX2(imm_end - 1, 0)));
1044 if (vs->externals_count) {
1045 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG,
1046 (r300->screen->caps.is_r500 ?
1047 R500_PVS_CONST_START : R300_PVS_CONST_START) + buf->buffer_base);
1048 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, count * 4);
1049 if (buf->remap_table){
1050 for (i = 0; i < count; i++) {
1051 uint32_t *data = &buf->ptr[buf->remap_table[i]*4];
1052 OUT_CS_TABLE(data, 4);
1053 }
1054 } else {
1055 OUT_CS_TABLE(buf->ptr, count * 4);
1056 }
1057 }
1058
1059 /* Emit immediates. */
1060 if (imm_count) {
1061 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG,
1062 (r300->screen->caps.is_r500 ?
1063 R500_PVS_CONST_START : R300_PVS_CONST_START) +
1064 buf->buffer_base + imm_first);
1065 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, imm_count * 4);
1066 for (i = imm_first; i < imm_end; i++) {
1067 const float *data = vs->code.constants.Constants[i].u.Immediate;
1068 OUT_CS_TABLE(data, 4);
1069 }
1070 }
1071 END_CS;
1072 }
1073
1074 void r300_emit_viewport_state(struct r300_context* r300,
1075 unsigned size, void* state)
1076 {
1077 struct r300_viewport_state* viewport = (struct r300_viewport_state*)state;
1078 CS_LOCALS(r300);
1079
1080 BEGIN_CS(size);
1081 OUT_CS_REG_SEQ(R300_SE_VPORT_XSCALE, 6);
1082 OUT_CS_TABLE(&viewport->xscale, 6);
1083 OUT_CS_REG(R300_VAP_VTE_CNTL, viewport->vte_control);
1084 END_CS;
1085 }
1086
1087 static void r300_emit_hiz_line_clear(struct r300_context *r300, int start, uint16_t count, uint32_t val)
1088 {
1089 CS_LOCALS(r300);
1090 BEGIN_CS(4);
1091 OUT_CS_PKT3(R300_PACKET3_3D_CLEAR_HIZ, 2);
1092 OUT_CS(start);
1093 OUT_CS(count);
1094 OUT_CS(val);
1095 END_CS;
1096 }
1097
1098 #define ALIGN_DIVUP(x, y) (((x) + (y) - 1) / (y))
1099
1100 void r300_emit_hiz_clear(struct r300_context *r300, unsigned size, void *state)
1101 {
1102 struct pipe_framebuffer_state *fb =
1103 (struct pipe_framebuffer_state*)r300->fb_state.state;
1104 struct r300_hyperz_state *z =
1105 (struct r300_hyperz_state*)r300->hyperz_state.state;
1106 struct r300_screen* r300screen = r300->screen;
1107 uint32_t stride, offset = 0, height, offset_shift;
1108 struct r300_texture* tex;
1109 int i;
1110
1111 tex = r300_texture(fb->zsbuf->texture);
1112
1113 offset = tex->hiz_mem[fb->zsbuf->u.tex.level]->ofs;
1114 stride = tex->desc.stride_in_pixels[fb->zsbuf->u.tex.level];
1115
1116 /* convert from pixels to 4x4 blocks */
1117 stride = ALIGN_DIVUP(stride, 4);
1118
1119 stride = ALIGN_DIVUP(stride, r300screen->caps.num_frag_pipes);
1120 /* there are 4 blocks per dwords */
1121 stride = ALIGN_DIVUP(stride, 4);
1122
1123 height = ALIGN_DIVUP(fb->zsbuf->height, 4);
1124
1125 offset_shift = 2;
1126 offset_shift += (r300screen->caps.num_frag_pipes / 2);
1127
1128 for (i = 0; i < height; i++) {
1129 offset = i * stride;
1130 offset <<= offset_shift;
1131 r300_emit_hiz_line_clear(r300, offset, stride, 0xffffffff);
1132 }
1133 z->current_func = -1;
1134
1135 /* Mark the current zbuffer's hiz ram as in use. */
1136 tex->hiz_in_use[fb->zsbuf->u.tex.level] = TRUE;
1137 }
1138
1139 void r300_emit_zmask_clear(struct r300_context *r300, unsigned size, void *state)
1140 {
1141 struct pipe_framebuffer_state *fb =
1142 (struct pipe_framebuffer_state*)r300->fb_state.state;
1143 struct r300_texture *tex;
1144 CS_LOCALS(r300);
1145
1146 tex = r300_texture(fb->zsbuf->texture);
1147
1148 BEGIN_CS(size);
1149 OUT_CS_PKT3(R300_PACKET3_3D_CLEAR_ZMASK, 2);
1150 OUT_CS(0);
1151 OUT_CS(tex->desc.zmask_dwords[fb->zsbuf->u.tex.level]);
1152 OUT_CS(0);
1153 END_CS;
1154
1155 /* Mark the current zbuffer's zmask as in use. */
1156 r300->zmask_in_use = TRUE;
1157 r300_mark_atom_dirty(r300, &r300->hyperz_state);
1158 }
1159
1160 void r300_emit_ztop_state(struct r300_context* r300,
1161 unsigned size, void* state)
1162 {
1163 struct r300_ztop_state* ztop = (struct r300_ztop_state*)state;
1164 CS_LOCALS(r300);
1165
1166 BEGIN_CS(size);
1167 OUT_CS_REG(R300_ZB_ZTOP, ztop->z_buffer_top);
1168 END_CS;
1169 }
1170
1171 void r300_emit_texture_cache_inval(struct r300_context* r300, unsigned size, void* state)
1172 {
1173 CS_LOCALS(r300);
1174
1175 BEGIN_CS(size);
1176 OUT_CS_REG(R300_TX_INVALTAGS, 0);
1177 END_CS;
1178 }
1179
1180 boolean r300_emit_buffer_validate(struct r300_context *r300,
1181 boolean do_validate_vertex_buffers,
1182 struct pipe_resource *index_buffer)
1183 {
1184 struct pipe_framebuffer_state *fb =
1185 (struct pipe_framebuffer_state*)r300->fb_state.state;
1186 struct r300_textures_state *texstate =
1187 (struct r300_textures_state*)r300->textures_state.state;
1188 struct r300_texture *tex;
1189 unsigned i;
1190 boolean flushed = FALSE;
1191
1192 validate:
1193 if (r300->fb_state.dirty) {
1194 /* Color buffers... */
1195 for (i = 0; i < fb->nr_cbufs; i++) {
1196 tex = r300_texture(fb->cbufs[i]->texture);
1197 assert(tex && tex->buffer && "cbuf is marked, but NULL!");
1198 r300->rws->cs_add_reloc(r300->cs, tex->cs_buffer, 0,
1199 r300_surface(fb->cbufs[i])->domain);
1200 }
1201 /* ...depth buffer... */
1202 if (fb->zsbuf) {
1203 tex = r300_texture(fb->zsbuf->texture);
1204 assert(tex && tex->buffer && "zsbuf is marked, but NULL!");
1205 r300->rws->cs_add_reloc(r300->cs, tex->cs_buffer, 0,
1206 r300_surface(fb->zsbuf)->domain);
1207 }
1208 }
1209 if (r300->textures_state.dirty) {
1210 /* ...textures... */
1211 for (i = 0; i < texstate->count; i++) {
1212 if (!(texstate->tx_enable & (1 << i))) {
1213 continue;
1214 }
1215
1216 tex = r300_texture(texstate->sampler_views[i]->base.texture);
1217 r300->rws->cs_add_reloc(r300->cs, tex->cs_buffer, tex->domain, 0);
1218 }
1219 }
1220 /* ...occlusion query buffer... */
1221 if (r300->query_current)
1222 r300->rws->cs_add_reloc(r300->cs, r300->query_current->cs_buffer,
1223 0, r300->query_current->domain);
1224 /* ...vertex buffer for SWTCL path... */
1225 if (r300->vbo)
1226 r300->rws->cs_add_reloc(r300->cs, r300_buffer(r300->vbo)->cs_buf,
1227 r300_buffer(r300->vbo)->domain, 0);
1228 /* ...vertex buffers for HWTCL path... */
1229 if (do_validate_vertex_buffers) {
1230 struct pipe_resource **buf = r300->real_vertex_buffer;
1231 struct pipe_resource **last = r300->real_vertex_buffer +
1232 r300->real_vertex_buffer_count;
1233 for (; buf != last; buf++) {
1234 if (!*buf)
1235 continue;
1236
1237 r300->rws->cs_add_reloc(r300->cs, r300_buffer(*buf)->cs_buf,
1238 r300_buffer(*buf)->domain, 0);
1239 }
1240 }
1241 /* ...and index buffer for HWTCL path. */
1242 if (index_buffer)
1243 r300->rws->cs_add_reloc(r300->cs, r300_buffer(index_buffer)->cs_buf,
1244 r300_buffer(index_buffer)->domain, 0);
1245
1246 /* Now do the validation. */
1247 if (!r300->rws->cs_validate(r300->cs)) {
1248 /* Ooops, an infinite loop, give up. */
1249 if (flushed)
1250 return FALSE;
1251
1252 r300->context.flush(&r300->context, 0, NULL);
1253 flushed = TRUE;
1254 goto validate;
1255 }
1256
1257 return TRUE;
1258 }
1259
1260 unsigned r300_get_num_dirty_dwords(struct r300_context *r300)
1261 {
1262 struct r300_atom* atom;
1263 unsigned dwords = 0;
1264
1265 foreach_dirty_atom(r300, atom) {
1266 if (atom->dirty) {
1267 dwords += atom->size;
1268 }
1269 }
1270
1271 /* let's reserve some more, just in case */
1272 dwords += 32;
1273
1274 return dwords;
1275 }
1276
1277 unsigned r300_get_num_cs_end_dwords(struct r300_context *r300)
1278 {
1279 unsigned dwords = 0;
1280
1281 /* Emitted in flush. */
1282 dwords += 26; /* emit_query_end */
1283 dwords += r300->hyperz_state.size + 2; /* emit_hyperz_end + zcache flush */
1284 if (r300->screen->caps.index_bias_supported)
1285 dwords += 2;
1286
1287 return dwords;
1288 }
1289
1290 /* Emit all dirty state. */
1291 void r300_emit_dirty_state(struct r300_context* r300)
1292 {
1293 struct r300_atom *atom;
1294
1295 foreach_dirty_atom(r300, atom) {
1296 if (atom->dirty) {
1297 atom->emit(r300, atom->size, atom->state);
1298 atom->dirty = FALSE;
1299 }
1300 }
1301
1302 r300->first_dirty = NULL;
1303 r300->last_dirty = NULL;
1304 r300->dirty_hw++;
1305 }