Merge remote branch 'origin/master' into pipe-video
[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, 0, aa->dest->domain);
357
358 OUT_CS_REG_SEQ(R300_RB3D_AARESOLVE_PITCH, 1);
359 OUT_CS_RELOC(aa->dest->cs_buffer, aa->dest->pitch, 0, aa->dest->domain);
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 CS_LOCALS(r300);
373
374 BEGIN_CS(size);
375
376 /* NUM_MULTIWRITES replicates COLOR[0] to all colorbuffers, which is not
377 * what we usually want. */
378 if (r300->screen->caps.is_r500) {
379 OUT_CS_REG(R300_RB3D_CCTL,
380 R300_RB3D_CCTL_INDEPENDENT_COLORFORMAT_ENABLE_ENABLE);
381 } else {
382 OUT_CS_REG(R300_RB3D_CCTL, 0);
383 }
384
385 /* Set up colorbuffers. */
386 for (i = 0; i < fb->nr_cbufs; i++) {
387 surf = r300_surface(fb->cbufs[i]);
388
389 OUT_CS_REG_SEQ(R300_RB3D_COLOROFFSET0 + (4 * i), 1);
390 OUT_CS_RELOC(surf->cs_buffer, surf->offset, 0, surf->domain);
391
392 OUT_CS_REG_SEQ(R300_RB3D_COLORPITCH0 + (4 * i), 1);
393 OUT_CS_RELOC(surf->cs_buffer, surf->pitch, 0, surf->domain);
394 }
395
396 /* Set up the ZB part of the CBZB clear. */
397 if (r300->cbzb_clear) {
398 surf = r300_surface(fb->cbufs[0]);
399
400 OUT_CS_REG(R300_ZB_FORMAT, surf->cbzb_format);
401
402 OUT_CS_REG_SEQ(R300_ZB_DEPTHOFFSET, 1);
403 OUT_CS_RELOC(surf->cs_buffer, surf->cbzb_midpoint_offset, 0, surf->domain);
404
405 OUT_CS_REG_SEQ(R300_ZB_DEPTHPITCH, 1);
406 OUT_CS_RELOC(surf->cs_buffer, surf->cbzb_pitch, 0, surf->domain);
407
408 DBG(r300, DBG_CBZB,
409 "CBZB clearing cbuf %08x %08x\n", surf->cbzb_format,
410 surf->cbzb_pitch);
411 }
412 /* Set up a zbuffer. */
413 else if (fb->zsbuf) {
414 surf = r300_surface(fb->zsbuf);
415
416 OUT_CS_REG(R300_ZB_FORMAT, surf->format);
417
418 OUT_CS_REG_SEQ(R300_ZB_DEPTHOFFSET, 1);
419 OUT_CS_RELOC(surf->cs_buffer, surf->offset, 0, surf->domain);
420
421 OUT_CS_REG_SEQ(R300_ZB_DEPTHPITCH, 1);
422 OUT_CS_RELOC(surf->cs_buffer, surf->pitch, 0, surf->domain);
423
424 if (can_hyperz) {
425 uint32_t surf_pitch;
426 struct r300_texture *tex;
427 int level = surf->base.u.tex.level;
428 tex = r300_texture(surf->base.texture);
429
430 surf_pitch = surf->pitch & R300_DEPTHPITCH_MASK;
431 /* HiZ RAM. */
432 if (r300->screen->caps.hiz_ram) {
433 if (tex->hiz_mem[level]) {
434 OUT_CS_REG(R300_ZB_HIZ_OFFSET, tex->hiz_mem[level]->ofs << 2);
435 OUT_CS_REG(R300_ZB_HIZ_PITCH, surf_pitch);
436 } else {
437 OUT_CS_REG(R300_ZB_HIZ_OFFSET, 0);
438 OUT_CS_REG(R300_ZB_HIZ_PITCH, 0);
439 }
440 }
441 /* Z Mask RAM. (compressed zbuffer) */
442 if (tex->zmask_mem[level]) {
443 OUT_CS_REG(R300_ZB_ZMASK_OFFSET, tex->zmask_mem[level]->ofs << 2);
444 OUT_CS_REG(R300_ZB_ZMASK_PITCH, surf_pitch);
445 } else {
446 OUT_CS_REG(R300_ZB_ZMASK_OFFSET, 0);
447 OUT_CS_REG(R300_ZB_ZMASK_PITCH, 0);
448 }
449 }
450 }
451
452 END_CS;
453 }
454
455 void r300_emit_hyperz_state(struct r300_context *r300,
456 unsigned size, void *state)
457 {
458 struct r300_hyperz_state *z = state;
459 CS_LOCALS(r300);
460 if (z->flush)
461 WRITE_CS_TABLE(&z->cb_flush_begin, size);
462 else
463 WRITE_CS_TABLE(&z->cb_begin, size - 2);
464 }
465
466 void r300_emit_hyperz_end(struct r300_context *r300)
467 {
468 struct r300_hyperz_state z =
469 *(struct r300_hyperz_state*)r300->hyperz_state.state;
470
471 z.flush = 1;
472 z.zb_bw_cntl = 0;
473 z.zb_depthclearvalue = 0;
474 z.sc_hyperz = R300_SC_HYPERZ_ADJ_2;
475 z.gb_z_peq_config = 0;
476
477 r300_emit_hyperz_state(r300, r300->hyperz_state.size, &z);
478 }
479
480 void r300_emit_fb_state_pipelined(struct r300_context *r300,
481 unsigned size, void *state)
482 {
483 struct pipe_framebuffer_state* fb =
484 (struct pipe_framebuffer_state*)r300->fb_state.state;
485 unsigned i;
486 CS_LOCALS(r300);
487
488 BEGIN_CS(size);
489
490 /* Colorbuffer format in the US block.
491 * (must be written after unpipelined regs) */
492 OUT_CS_REG_SEQ(R300_US_OUT_FMT_0, 4);
493 for (i = 0; i < fb->nr_cbufs; i++) {
494 OUT_CS(r300_surface(fb->cbufs[i])->format);
495 }
496 for (; i < 4; i++) {
497 OUT_CS(R300_US_OUT_FMT_UNUSED);
498 }
499
500 /* Multisampling. Depends on framebuffer sample count.
501 * These are pipelined regs and as such cannot be moved
502 * to the AA state. */
503 if (r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0)) {
504 unsigned mspos0 = 0x66666666;
505 unsigned mspos1 = 0x6666666;
506
507 if (fb->nr_cbufs && fb->cbufs[0]->texture->nr_samples > 1) {
508 /* Subsample placement. These may not be optimal. */
509 switch (fb->cbufs[0]->texture->nr_samples) {
510 case 2:
511 mspos0 = 0x33996633;
512 mspos1 = 0x6666663;
513 break;
514 case 3:
515 mspos0 = 0x33936933;
516 mspos1 = 0x6666663;
517 break;
518 case 4:
519 mspos0 = 0x33939933;
520 mspos1 = 0x3966663;
521 break;
522 case 6:
523 mspos0 = 0x22a2aa22;
524 mspos1 = 0x2a65672;
525 break;
526 default:
527 debug_printf("r300: Bad number of multisamples!\n");
528 }
529 }
530
531 OUT_CS_REG_SEQ(R300_GB_MSPOS0, 2);
532 OUT_CS(mspos0);
533 OUT_CS(mspos1);
534 }
535 END_CS;
536 }
537
538 void r300_emit_query_start(struct r300_context *r300, unsigned size, void*state)
539 {
540 struct r300_query *query = r300->query_current;
541 CS_LOCALS(r300);
542
543 if (!query)
544 return;
545
546 BEGIN_CS(size);
547 if (r300->screen->caps.family == CHIP_FAMILY_RV530) {
548 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL);
549 } else {
550 OUT_CS_REG(R300_SU_REG_DEST, R300_RASTER_PIPE_SELECT_ALL);
551 }
552 OUT_CS_REG(R300_ZB_ZPASS_DATA, 0);
553 END_CS;
554 query->begin_emitted = TRUE;
555 query->flushed = FALSE;
556 }
557
558 static void r300_emit_query_end_frag_pipes(struct r300_context *r300,
559 struct r300_query *query)
560 {
561 struct r300_capabilities* caps = &r300->screen->caps;
562 struct r300_winsys_cs_buffer *buf = r300->query_current->cs_buffer;
563 CS_LOCALS(r300);
564
565 assert(caps->num_frag_pipes);
566
567 BEGIN_CS(6 * caps->num_frag_pipes + 2);
568 /* I'm not so sure I like this switch, but it's hard to be elegant
569 * when there's so many special cases...
570 *
571 * So here's the basic idea. For each pipe, enable writes to it only,
572 * then put out the relocation for ZPASS_ADDR, taking into account a
573 * 4-byte offset for each pipe. RV380 and older are special; they have
574 * only two pipes, and the second pipe's enable is on bit 3, not bit 1,
575 * so there's a chipset cap for that. */
576 switch (caps->num_frag_pipes) {
577 case 4:
578 /* pipe 3 only */
579 OUT_CS_REG(R300_SU_REG_DEST, 1 << 3);
580 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
581 OUT_CS_RELOC(buf, (query->num_results + 3) * 4,
582 0, query->domain);
583 case 3:
584 /* pipe 2 only */
585 OUT_CS_REG(R300_SU_REG_DEST, 1 << 2);
586 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
587 OUT_CS_RELOC(buf, (query->num_results + 2) * 4,
588 0, query->domain);
589 case 2:
590 /* pipe 1 only */
591 /* As mentioned above, accomodate RV380 and older. */
592 OUT_CS_REG(R300_SU_REG_DEST,
593 1 << (caps->high_second_pipe ? 3 : 1));
594 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
595 OUT_CS_RELOC(buf, (query->num_results + 1) * 4,
596 0, query->domain);
597 case 1:
598 /* pipe 0 only */
599 OUT_CS_REG(R300_SU_REG_DEST, 1 << 0);
600 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
601 OUT_CS_RELOC(buf, (query->num_results + 0) * 4,
602 0, query->domain);
603 break;
604 default:
605 fprintf(stderr, "r300: Implementation error: Chipset reports %d"
606 " pixel pipes!\n", caps->num_frag_pipes);
607 abort();
608 }
609
610 /* And, finally, reset it to normal... */
611 OUT_CS_REG(R300_SU_REG_DEST, 0xF);
612 END_CS;
613 }
614
615 static void rv530_emit_query_end_single_z(struct r300_context *r300,
616 struct r300_query *query)
617 {
618 struct r300_winsys_cs_buffer *buf = r300->query_current->cs_buffer;
619 CS_LOCALS(r300);
620
621 BEGIN_CS(8);
622 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_0);
623 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
624 OUT_CS_RELOC(buf, query->num_results * 4, 0, query->domain);
625 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL);
626 END_CS;
627 }
628
629 static void rv530_emit_query_end_double_z(struct r300_context *r300,
630 struct r300_query *query)
631 {
632 struct r300_winsys_cs_buffer *buf = r300->query_current->cs_buffer;
633 CS_LOCALS(r300);
634
635 BEGIN_CS(14);
636 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_0);
637 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
638 OUT_CS_RELOC(buf, (query->num_results + 0) * 4, 0, query->domain);
639 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_1);
640 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
641 OUT_CS_RELOC(buf, (query->num_results + 1) * 4, 0, query->domain);
642 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL);
643 END_CS;
644 }
645
646 void r300_emit_query_end(struct r300_context* r300)
647 {
648 struct r300_capabilities *caps = &r300->screen->caps;
649 struct r300_query *query = r300->query_current;
650
651 if (!query)
652 return;
653
654 if (query->begin_emitted == FALSE)
655 return;
656
657 if (caps->family == CHIP_FAMILY_RV530) {
658 if (caps->num_z_pipes == 2)
659 rv530_emit_query_end_double_z(r300, query);
660 else
661 rv530_emit_query_end_single_z(r300, query);
662 } else
663 r300_emit_query_end_frag_pipes(r300, query);
664
665 query->begin_emitted = FALSE;
666 query->num_results += query->num_pipes;
667
668 /* XXX grab all the results and reset the counter. */
669 if (query->num_results >= query->buffer_size / 4 - 4) {
670 query->num_results = (query->buffer_size / 4) / 2;
671 fprintf(stderr, "r300: Rewinding OQBO...\n");
672 }
673 }
674
675 void r300_emit_invariant_state(struct r300_context *r300,
676 unsigned size, void *state)
677 {
678 CS_LOCALS(r300);
679 WRITE_CS_TABLE(state, size);
680 }
681
682 void r300_emit_rs_state(struct r300_context* r300, unsigned size, void* state)
683 {
684 struct r300_rs_state* rs = state;
685 CS_LOCALS(r300);
686
687 BEGIN_CS(size);
688 OUT_CS_TABLE(rs->cb_main, RS_STATE_MAIN_SIZE);
689 if (rs->polygon_offset_enable) {
690 if (r300->zbuffer_bpp == 16) {
691 OUT_CS_TABLE(rs->cb_poly_offset_zb16, 5);
692 } else {
693 OUT_CS_TABLE(rs->cb_poly_offset_zb24, 5);
694 }
695 }
696 END_CS;
697 }
698
699 void r300_emit_rs_block_state(struct r300_context* r300,
700 unsigned size, void* state)
701 {
702 struct r300_rs_block* rs = (struct r300_rs_block*)state;
703 unsigned i;
704 /* It's the same for both INST and IP tables */
705 unsigned count = (rs->inst_count & R300_RS_INST_COUNT_MASK) + 1;
706 CS_LOCALS(r300);
707
708 if (DBG_ON(r300, DBG_RS_BLOCK)) {
709 r500_dump_rs_block(rs);
710
711 fprintf(stderr, "r300: RS emit:\n");
712
713 for (i = 0; i < count; i++)
714 fprintf(stderr, " : ip %d: 0x%08x\n", i, rs->ip[i]);
715
716 for (i = 0; i < count; i++)
717 fprintf(stderr, " : inst %d: 0x%08x\n", i, rs->inst[i]);
718
719 fprintf(stderr, " : count: 0x%08x inst_count: 0x%08x\n",
720 rs->count, rs->inst_count);
721 }
722
723 BEGIN_CS(size);
724 OUT_CS_REG_SEQ(R300_VAP_VTX_STATE_CNTL, 2);
725 OUT_CS(rs->vap_vtx_state_cntl);
726 OUT_CS(rs->vap_vsm_vtx_assm);
727 OUT_CS_REG_SEQ(R300_VAP_OUTPUT_VTX_FMT_0, 2);
728 OUT_CS(rs->vap_out_vtx_fmt[0]);
729 OUT_CS(rs->vap_out_vtx_fmt[1]);
730 OUT_CS_REG_SEQ(R300_GB_ENABLE, 1);
731 OUT_CS(rs->gb_enable);
732
733 if (r300->screen->caps.is_r500) {
734 OUT_CS_REG_SEQ(R500_RS_IP_0, count);
735 } else {
736 OUT_CS_REG_SEQ(R300_RS_IP_0, count);
737 }
738 OUT_CS_TABLE(rs->ip, count);
739
740 OUT_CS_REG_SEQ(R300_RS_COUNT, 2);
741 OUT_CS(rs->count);
742 OUT_CS(rs->inst_count);
743
744 if (r300->screen->caps.is_r500) {
745 OUT_CS_REG_SEQ(R500_RS_INST_0, count);
746 } else {
747 OUT_CS_REG_SEQ(R300_RS_INST_0, count);
748 }
749 OUT_CS_TABLE(rs->inst, count);
750 END_CS;
751 }
752
753 void r300_emit_scissor_state(struct r300_context* r300,
754 unsigned size, void* state)
755 {
756 struct pipe_scissor_state* scissor = (struct pipe_scissor_state*)state;
757 CS_LOCALS(r300);
758
759 BEGIN_CS(size);
760 OUT_CS_REG_SEQ(R300_SC_CLIPRECT_TL_0, 2);
761 if (r300->screen->caps.is_r500) {
762 OUT_CS((scissor->minx << R300_CLIPRECT_X_SHIFT) |
763 (scissor->miny << R300_CLIPRECT_Y_SHIFT));
764 OUT_CS(((scissor->maxx - 1) << R300_CLIPRECT_X_SHIFT) |
765 ((scissor->maxy - 1) << R300_CLIPRECT_Y_SHIFT));
766 } else {
767 OUT_CS(((scissor->minx + 1440) << R300_CLIPRECT_X_SHIFT) |
768 ((scissor->miny + 1440) << R300_CLIPRECT_Y_SHIFT));
769 OUT_CS(((scissor->maxx + 1440-1) << R300_CLIPRECT_X_SHIFT) |
770 ((scissor->maxy + 1440-1) << R300_CLIPRECT_Y_SHIFT));
771 }
772 END_CS;
773 }
774
775 void r300_emit_textures_state(struct r300_context *r300,
776 unsigned size, void *state)
777 {
778 struct r300_textures_state *allstate = (struct r300_textures_state*)state;
779 struct r300_texture_sampler_state *texstate;
780 struct r300_texture *tex;
781 unsigned i;
782 CS_LOCALS(r300);
783
784 BEGIN_CS(size);
785 OUT_CS_REG(R300_TX_ENABLE, allstate->tx_enable);
786
787 for (i = 0; i < allstate->count; i++) {
788 if ((1 << i) & allstate->tx_enable) {
789 texstate = &allstate->regs[i];
790 tex = r300_texture(allstate->sampler_views[i]->base.texture);
791
792 OUT_CS_REG(R300_TX_FILTER0_0 + (i * 4), texstate->filter0);
793 OUT_CS_REG(R300_TX_FILTER1_0 + (i * 4), texstate->filter1);
794 OUT_CS_REG(R300_TX_BORDER_COLOR_0 + (i * 4),
795 texstate->border_color);
796
797 OUT_CS_REG(R300_TX_FORMAT0_0 + (i * 4), texstate->format.format0);
798 OUT_CS_REG(R300_TX_FORMAT1_0 + (i * 4), texstate->format.format1);
799 OUT_CS_REG(R300_TX_FORMAT2_0 + (i * 4), texstate->format.format2);
800
801 OUT_CS_REG_SEQ(R300_TX_OFFSET_0 + (i * 4), 1);
802 OUT_CS_TEX_RELOC(tex, texstate->format.tile_config, tex->domain,
803 0);
804 }
805 }
806 END_CS;
807 }
808
809 static void r300_update_aos_cb(struct r300_context *r300, unsigned packet_size)
810 {
811 struct pipe_vertex_buffer *vb1, *vb2, *vbuf = r300->vertex_buffer;
812 struct pipe_vertex_element *velem = r300->velems->velem;
813 unsigned *hw_format_size = r300->velems->hw_format_size;
814 unsigned size1, size2, aos_count = r300->velems->count;
815 int i;
816 CB_LOCALS;
817
818 BEGIN_CB(r300->aos_cb, packet_size);
819 for (i = 0; i < aos_count - 1; i += 2) {
820 vb1 = &vbuf[velem[i].vertex_buffer_index];
821 vb2 = &vbuf[velem[i+1].vertex_buffer_index];
822 size1 = hw_format_size[i];
823 size2 = hw_format_size[i+1];
824
825 OUT_CB(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(vb1->stride) |
826 R300_VBPNTR_SIZE1(size2) | R300_VBPNTR_STRIDE1(vb2->stride));
827 OUT_CB(vb1->buffer_offset + velem[i].src_offset);
828 OUT_CB(vb2->buffer_offset + velem[i+1].src_offset);
829 }
830
831 if (aos_count & 1) {
832 vb1 = &vbuf[velem[i].vertex_buffer_index];
833 size1 = hw_format_size[i];
834
835 OUT_CB(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(vb1->stride));
836 OUT_CB(vb1->buffer_offset + velem[i].src_offset);
837 }
838 END_CB;
839
840 r300->aos_dirty = FALSE;
841 }
842
843 void r300_emit_aos(struct r300_context* r300, int offset, boolean indexed)
844 {
845 struct pipe_vertex_buffer *vbuf = r300->vertex_buffer;
846 struct pipe_vertex_element *velem = r300->velems->velem;
847 struct r300_buffer *buf;
848 int i;
849 unsigned aos_count = r300->velems->count;
850 unsigned packet_size = (aos_count * 3 + 1) / 2;
851 CS_LOCALS(r300);
852
853 BEGIN_CS(2 + packet_size + aos_count * 2);
854 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR, packet_size);
855 OUT_CS(aos_count | (!indexed ? R300_VC_FORCE_PREFETCH : 0));
856
857 if (!offset) {
858 if (r300->aos_dirty) {
859 r300_update_aos_cb(r300, packet_size);
860 }
861 OUT_CS_TABLE(r300->aos_cb, packet_size);
862 } else {
863 struct pipe_vertex_buffer *vb1, *vb2;
864 unsigned *hw_format_size = r300->velems->hw_format_size;
865 unsigned size1, size2;
866
867 for (i = 0; i < aos_count - 1; i += 2) {
868 vb1 = &vbuf[velem[i].vertex_buffer_index];
869 vb2 = &vbuf[velem[i+1].vertex_buffer_index];
870 size1 = hw_format_size[i];
871 size2 = hw_format_size[i+1];
872
873 OUT_CS(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(vb1->stride) |
874 R300_VBPNTR_SIZE1(size2) | R300_VBPNTR_STRIDE1(vb2->stride));
875 OUT_CS(vb1->buffer_offset + velem[i].src_offset + offset * vb1->stride);
876 OUT_CS(vb2->buffer_offset + velem[i+1].src_offset + offset * vb2->stride);
877 }
878
879 if (aos_count & 1) {
880 vb1 = &vbuf[velem[i].vertex_buffer_index];
881 size1 = hw_format_size[i];
882
883 OUT_CS(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(vb1->stride));
884 OUT_CS(vb1->buffer_offset + velem[i].src_offset + offset * vb1->stride);
885 }
886 }
887
888 for (i = 0; i < aos_count; i++) {
889 buf = r300_buffer(vbuf[velem[i].vertex_buffer_index].buffer);
890 OUT_CS_BUF_RELOC_NO_OFFSET(&buf->b.b, buf->domain, 0);
891 }
892 END_CS;
893 }
894
895 void r300_emit_aos_swtcl(struct r300_context *r300, boolean indexed)
896 {
897 CS_LOCALS(r300);
898
899 DBG(r300, DBG_SWTCL, "r300: Preparing vertex buffer %p for render, "
900 "vertex size %d\n", r300->vbo,
901 r300->vertex_info.size);
902 /* Set the pointer to our vertex buffer. The emitted values are this:
903 * PACKET3 [3D_LOAD_VBPNTR]
904 * COUNT [1]
905 * FORMAT [size | stride << 8]
906 * OFFSET [offset into BO]
907 * VBPNTR [relocated BO]
908 */
909 BEGIN_CS(7);
910 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR, 3);
911 OUT_CS(1 | (!indexed ? R300_VC_FORCE_PREFETCH : 0));
912 OUT_CS(r300->vertex_info.size |
913 (r300->vertex_info.size << 8));
914 OUT_CS(r300->draw_vbo_offset);
915 OUT_CS_BUF_RELOC(r300->vbo, 0, r300_buffer(r300->vbo)->domain, 0);
916 END_CS;
917 }
918
919 void r300_emit_vertex_stream_state(struct r300_context* r300,
920 unsigned size, void* state)
921 {
922 struct r300_vertex_stream_state *streams =
923 (struct r300_vertex_stream_state*)state;
924 unsigned i;
925 CS_LOCALS(r300);
926
927 if (DBG_ON(r300, DBG_PSC)) {
928 fprintf(stderr, "r300: PSC emit:\n");
929
930 for (i = 0; i < streams->count; i++) {
931 fprintf(stderr, " : prog_stream_cntl%d: 0x%08x\n", i,
932 streams->vap_prog_stream_cntl[i]);
933 }
934
935 for (i = 0; i < streams->count; i++) {
936 fprintf(stderr, " : prog_stream_cntl_ext%d: 0x%08x\n", i,
937 streams->vap_prog_stream_cntl_ext[i]);
938 }
939 }
940
941 BEGIN_CS(size);
942 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_0, streams->count);
943 OUT_CS_TABLE(streams->vap_prog_stream_cntl, streams->count);
944 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_EXT_0, streams->count);
945 OUT_CS_TABLE(streams->vap_prog_stream_cntl_ext, streams->count);
946 END_CS;
947 }
948
949 void r300_emit_pvs_flush(struct r300_context* r300, unsigned size, void* state)
950 {
951 CS_LOCALS(r300);
952
953 BEGIN_CS(size);
954 OUT_CS_REG(R300_VAP_PVS_STATE_FLUSH_REG, 0x0);
955 END_CS;
956 }
957
958 void r300_emit_vap_invariant_state(struct r300_context *r300,
959 unsigned size, void *state)
960 {
961 CS_LOCALS(r300);
962 WRITE_CS_TABLE(state, size);
963 }
964
965 void r300_emit_vs_state(struct r300_context* r300, unsigned size, void* state)
966 {
967 struct r300_vertex_shader* vs = (struct r300_vertex_shader*)state;
968 struct r300_vertex_program_code* code = &vs->code;
969 struct r300_screen* r300screen = r300->screen;
970 unsigned instruction_count = code->length / 4;
971
972 unsigned vtx_mem_size = r300screen->caps.is_r500 ? 128 : 72;
973 unsigned input_count = MAX2(util_bitcount(code->InputsRead), 1);
974 unsigned output_count = MAX2(util_bitcount(code->OutputsWritten), 1);
975 unsigned temp_count = MAX2(code->num_temporaries, 1);
976
977 unsigned pvs_num_slots = MIN3(vtx_mem_size / input_count,
978 vtx_mem_size / output_count, 10);
979 unsigned pvs_num_controllers = MIN2(vtx_mem_size / temp_count, 5);
980
981 CS_LOCALS(r300);
982
983 BEGIN_CS(size);
984
985 /* R300_VAP_PVS_CODE_CNTL_0
986 * R300_VAP_PVS_CONST_CNTL
987 * R300_VAP_PVS_CODE_CNTL_1
988 * See the r5xx docs for instructions on how to use these. */
989 OUT_CS_REG(R300_VAP_PVS_CODE_CNTL_0, R300_PVS_FIRST_INST(0) |
990 R300_PVS_XYZW_VALID_INST(instruction_count - 1) |
991 R300_PVS_LAST_INST(instruction_count - 1));
992 OUT_CS_REG(R300_VAP_PVS_CODE_CNTL_1, instruction_count - 1);
993
994 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG, 0);
995 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, code->length);
996 OUT_CS_TABLE(code->body.d, code->length);
997
998 OUT_CS_REG(R300_VAP_CNTL, R300_PVS_NUM_SLOTS(pvs_num_slots) |
999 R300_PVS_NUM_CNTLRS(pvs_num_controllers) |
1000 R300_PVS_NUM_FPUS(r300screen->caps.num_vert_fpus) |
1001 R300_PVS_VF_MAX_VTX_NUM(12) |
1002 (r300screen->caps.is_r500 ? R500_TCL_STATE_OPTIMIZATION : 0));
1003
1004 /* Emit flow control instructions. */
1005 if (code->num_fc_ops) {
1006
1007 OUT_CS_REG(R300_VAP_PVS_FLOW_CNTL_OPC, code->fc_ops);
1008 if (r300screen->caps.is_r500) {
1009 OUT_CS_REG_SEQ(R500_VAP_PVS_FLOW_CNTL_ADDRS_LW_0, code->num_fc_ops * 2);
1010 OUT_CS_TABLE(code->fc_op_addrs.r500, code->num_fc_ops * 2);
1011 } else {
1012 OUT_CS_REG_SEQ(R300_VAP_PVS_FLOW_CNTL_ADDRS_0, code->num_fc_ops);
1013 OUT_CS_TABLE(code->fc_op_addrs.r300, code->num_fc_ops);
1014 }
1015 OUT_CS_REG_SEQ(R300_VAP_PVS_FLOW_CNTL_LOOP_INDEX_0, code->num_fc_ops);
1016 OUT_CS_TABLE(code->fc_loop_index, code->num_fc_ops);
1017 }
1018
1019 END_CS;
1020 }
1021
1022 void r300_emit_vs_constants(struct r300_context* r300,
1023 unsigned size, void *state)
1024 {
1025 unsigned count =
1026 ((struct r300_vertex_shader*)r300->vs_state.state)->externals_count;
1027 struct r300_constant_buffer *buf = (struct r300_constant_buffer*)state;
1028 struct r300_vertex_shader *vs = (struct r300_vertex_shader*)r300->vs_state.state;
1029 unsigned i;
1030 int imm_first = vs->externals_count;
1031 int imm_end = vs->code.constants.Count;
1032 int imm_count = vs->immediates_count;
1033 CS_LOCALS(r300);
1034
1035 BEGIN_CS(size);
1036 OUT_CS_REG(R300_VAP_PVS_CONST_CNTL,
1037 R300_PVS_CONST_BASE_OFFSET(buf->buffer_base) |
1038 R300_PVS_MAX_CONST_ADDR(MAX2(imm_end - 1, 0)));
1039 if (vs->externals_count) {
1040 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG,
1041 (r300->screen->caps.is_r500 ?
1042 R500_PVS_CONST_START : R300_PVS_CONST_START) + buf->buffer_base);
1043 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, count * 4);
1044 if (buf->remap_table){
1045 for (i = 0; i < count; i++) {
1046 uint32_t *data = &buf->ptr[buf->remap_table[i]*4];
1047 OUT_CS_TABLE(data, 4);
1048 }
1049 } else {
1050 OUT_CS_TABLE(buf->ptr, count * 4);
1051 }
1052 }
1053
1054 /* Emit immediates. */
1055 if (imm_count) {
1056 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG,
1057 (r300->screen->caps.is_r500 ?
1058 R500_PVS_CONST_START : R300_PVS_CONST_START) +
1059 buf->buffer_base + imm_first);
1060 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, imm_count * 4);
1061 for (i = imm_first; i < imm_end; i++) {
1062 const float *data = vs->code.constants.Constants[i].u.Immediate;
1063 OUT_CS_TABLE(data, 4);
1064 }
1065 }
1066 END_CS;
1067 }
1068
1069 void r300_emit_viewport_state(struct r300_context* r300,
1070 unsigned size, void* state)
1071 {
1072 struct r300_viewport_state* viewport = (struct r300_viewport_state*)state;
1073 CS_LOCALS(r300);
1074
1075 BEGIN_CS(size);
1076 OUT_CS_REG_SEQ(R300_SE_VPORT_XSCALE, 6);
1077 OUT_CS_TABLE(&viewport->xscale, 6);
1078 OUT_CS_REG(R300_VAP_VTE_CNTL, viewport->vte_control);
1079 END_CS;
1080 }
1081
1082 static void r300_emit_hiz_line_clear(struct r300_context *r300, int start, uint16_t count, uint32_t val)
1083 {
1084 CS_LOCALS(r300);
1085 BEGIN_CS(4);
1086 OUT_CS_PKT3(R300_PACKET3_3D_CLEAR_HIZ, 2);
1087 OUT_CS(start);
1088 OUT_CS(count);
1089 OUT_CS(val);
1090 END_CS;
1091 }
1092
1093 static void r300_emit_zmask_line_clear(struct r300_context *r300, int start, uint16_t count, uint32_t val)
1094 {
1095 CS_LOCALS(r300);
1096 BEGIN_CS(4);
1097 OUT_CS_PKT3(R300_PACKET3_3D_CLEAR_ZMASK, 2);
1098 OUT_CS(start);
1099 OUT_CS(count);
1100 OUT_CS(val);
1101 END_CS;
1102 }
1103
1104 #define ALIGN_DIVUP(x, y) (((x) + (y) - 1) / (y))
1105
1106 void r300_emit_hiz_clear(struct r300_context *r300, unsigned size, void *state)
1107 {
1108 struct pipe_framebuffer_state *fb =
1109 (struct pipe_framebuffer_state*)r300->fb_state.state;
1110 struct r300_hyperz_state *z =
1111 (struct r300_hyperz_state*)r300->hyperz_state.state;
1112 struct r300_screen* r300screen = r300->screen;
1113 uint32_t stride, offset = 0, height, offset_shift;
1114 struct r300_texture* tex;
1115 int i;
1116
1117 tex = r300_texture(fb->zsbuf->texture);
1118
1119 offset = tex->hiz_mem[fb->zsbuf->u.tex.level]->ofs;
1120 stride = tex->desc.stride_in_pixels[fb->zsbuf->u.tex.level];
1121
1122 /* convert from pixels to 4x4 blocks */
1123 stride = ALIGN_DIVUP(stride, 4);
1124
1125 stride = ALIGN_DIVUP(stride, r300screen->caps.num_frag_pipes);
1126 /* there are 4 blocks per dwords */
1127 stride = ALIGN_DIVUP(stride, 4);
1128
1129 height = ALIGN_DIVUP(fb->zsbuf->height, 4);
1130
1131 offset_shift = 2;
1132 offset_shift += (r300screen->caps.num_frag_pipes / 2);
1133
1134 for (i = 0; i < height; i++) {
1135 offset = i * stride;
1136 offset <<= offset_shift;
1137 r300_emit_hiz_line_clear(r300, offset, stride, 0xffffffff);
1138 }
1139 z->current_func = -1;
1140
1141 /* Mark the current zbuffer's hiz ram as in use. */
1142 tex->hiz_in_use[fb->zsbuf->u.tex.level] = TRUE;
1143 }
1144
1145 void r300_emit_zmask_clear(struct r300_context *r300, unsigned size, void *state)
1146 {
1147 struct pipe_framebuffer_state *fb =
1148 (struct pipe_framebuffer_state*)r300->fb_state.state;
1149 struct r300_screen* r300screen = r300->screen;
1150 uint32_t stride, offset = 0;
1151 struct r300_texture* tex;
1152 uint32_t i, height;
1153 int mult, offset_shift;
1154
1155 tex = r300_texture(fb->zsbuf->texture);
1156 stride = tex->desc.stride_in_pixels[fb->zsbuf->u.tex.level];
1157
1158 offset = tex->zmask_mem[fb->zsbuf->u.tex.level]->ofs;
1159
1160 if (r300->z_compression == RV350_Z_COMPRESS_88)
1161 mult = 8;
1162 else
1163 mult = 4;
1164
1165 height = ALIGN_DIVUP(fb->zsbuf->height, mult);
1166
1167 offset_shift = 4;
1168 offset_shift += (r300screen->caps.num_frag_pipes / 2);
1169 stride = ALIGN_DIVUP(stride, r300screen->caps.num_frag_pipes);
1170
1171 /* okay have width in pixels - divide by block width */
1172 stride = ALIGN_DIVUP(stride, mult);
1173 /* have width in blocks - divide by number of fragment pipes screen width */
1174 /* 16 blocks per dword */
1175 stride = ALIGN_DIVUP(stride, 16);
1176
1177 for (i = 0; i < height; i++) {
1178 offset = i * stride;
1179 offset <<= offset_shift;
1180 r300_emit_zmask_line_clear(r300, offset, stride, 0x0);//0xffffffff);
1181 }
1182
1183 /* Mark the current zbuffer's zmask as in use. */
1184 tex->zmask_in_use[fb->zsbuf->u.tex.level] = TRUE;
1185 }
1186
1187 void r300_emit_ztop_state(struct r300_context* r300,
1188 unsigned size, void* state)
1189 {
1190 struct r300_ztop_state* ztop = (struct r300_ztop_state*)state;
1191 CS_LOCALS(r300);
1192
1193 BEGIN_CS(size);
1194 OUT_CS_REG(R300_ZB_ZTOP, ztop->z_buffer_top);
1195 END_CS;
1196 }
1197
1198 void r300_emit_texture_cache_inval(struct r300_context* r300, unsigned size, void* state)
1199 {
1200 CS_LOCALS(r300);
1201
1202 BEGIN_CS(size);
1203 OUT_CS_REG(R300_TX_INVALTAGS, 0);
1204 END_CS;
1205 }
1206
1207 boolean r300_emit_buffer_validate(struct r300_context *r300,
1208 boolean do_validate_vertex_buffers,
1209 struct pipe_resource *index_buffer)
1210 {
1211 struct pipe_framebuffer_state* fb =
1212 (struct pipe_framebuffer_state*)r300->fb_state.state;
1213 struct r300_textures_state *texstate =
1214 (struct r300_textures_state*)r300->textures_state.state;
1215 struct r300_texture* tex;
1216 struct pipe_vertex_buffer *vbuf = r300->vertex_buffer;
1217 struct pipe_vertex_element *velem = r300->velems->velem;
1218 struct pipe_resource *pbuf;
1219 unsigned i;
1220
1221 /* Clean out BOs. */
1222 r300->rws->cs_reset_buffers(r300->cs);
1223
1224 /* Color buffers... */
1225 for (i = 0; i < fb->nr_cbufs; i++) {
1226 tex = r300_texture(fb->cbufs[i]->texture);
1227 assert(tex && tex->buffer && "cbuf is marked, but NULL!");
1228 r300->rws->cs_add_buffer(r300->cs, tex->cs_buffer, 0,
1229 r300_surface(fb->cbufs[i])->domain);
1230 }
1231 /* ...depth buffer... */
1232 if (fb->zsbuf) {
1233 tex = r300_texture(fb->zsbuf->texture);
1234 assert(tex && tex->buffer && "zsbuf is marked, but NULL!");
1235 r300->rws->cs_add_buffer(r300->cs, tex->cs_buffer, 0,
1236 r300_surface(fb->zsbuf)->domain);
1237 }
1238 /* ...textures... */
1239 for (i = 0; i < texstate->count; i++) {
1240 if (!(texstate->tx_enable & (1 << i))) {
1241 continue;
1242 }
1243
1244 tex = r300_texture(texstate->sampler_views[i]->base.texture);
1245 r300->rws->cs_add_buffer(r300->cs, tex->cs_buffer, tex->domain, 0);
1246 }
1247 /* ...occlusion query buffer... */
1248 if (r300->query_current)
1249 r300->rws->cs_add_buffer(r300->cs, r300->query_current->cs_buffer,
1250 0, r300->query_current->domain);
1251 /* ...vertex buffer for SWTCL path... */
1252 if (r300->vbo)
1253 r300->rws->cs_add_buffer(r300->cs, r300_buffer(r300->vbo)->cs_buf,
1254 r300_buffer(r300->vbo)->domain, 0);
1255 /* ...vertex buffers for HWTCL path... */
1256 if (do_validate_vertex_buffers) {
1257 for (i = 0; i < r300->velems->count; i++) {
1258 pbuf = vbuf[velem[i].vertex_buffer_index].buffer;
1259 if (!pbuf)
1260 continue;
1261
1262 r300->rws->cs_add_buffer(r300->cs, r300_buffer(pbuf)->cs_buf,
1263 r300_buffer(pbuf)->domain, 0);
1264 }
1265 }
1266 /* ...and index buffer for HWTCL path. */
1267 if (index_buffer)
1268 r300->rws->cs_add_buffer(r300->cs, r300_buffer(index_buffer)->cs_buf,
1269 r300_buffer(index_buffer)->domain, 0);
1270
1271 if (!r300->rws->cs_validate(r300->cs)) {
1272 return FALSE;
1273 }
1274
1275 return TRUE;
1276 }
1277
1278 unsigned r300_get_num_dirty_dwords(struct r300_context *r300)
1279 {
1280 struct r300_atom* atom;
1281 unsigned dwords = 0;
1282
1283 foreach_dirty_atom(r300, atom) {
1284 if (atom->dirty) {
1285 dwords += atom->size;
1286 }
1287 }
1288
1289 /* let's reserve some more, just in case */
1290 dwords += 32;
1291
1292 return dwords;
1293 }
1294
1295 unsigned r300_get_num_cs_end_dwords(struct r300_context *r300)
1296 {
1297 unsigned dwords = 0;
1298
1299 /* Emitted in flush. */
1300 dwords += 26; /* emit_query_end */
1301 dwords += r300->hyperz_state.size + 2; /* emit_hyperz_end + zcache flush */
1302 if (r300->screen->caps.index_bias_supported)
1303 dwords += 2;
1304
1305 return dwords;
1306 }
1307
1308 /* Emit all dirty state. */
1309 void r300_emit_dirty_state(struct r300_context* r300)
1310 {
1311 struct r300_atom *atom;
1312
1313 foreach_dirty_atom(r300, atom) {
1314 if (atom->dirty) {
1315 atom->emit(r300, atom->size, atom->state);
1316 atom->dirty = FALSE;
1317 }
1318 }
1319
1320 r300->first_dirty = NULL;
1321 r300->last_dirty = NULL;
1322 r300->dirty_hw++;
1323 }