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