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