r300g: don't set sample positions to the pixel center if MSAA is disabled
[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 /* NUM_MULTIWRITES replicates COLOR[0] to all colorbuffers, which is not
403 * what we usually want. */
404 if (r300->screen->caps.is_r500) {
405 rb3d_cctl = R300_RB3D_CCTL_INDEPENDENT_COLORFORMAT_ENABLE_ENABLE;
406 }
407 if (fb->nr_cbufs && r300->fb_multiwrite) {
408 rb3d_cctl |= R300_RB3D_CCTL_NUM_MULTIWRITES(fb->nr_cbufs);
409 }
410
411 OUT_CS_REG(R300_RB3D_CCTL, rb3d_cctl);
412
413 /* Set up colorbuffers. */
414 for (i = 0; i < fb->nr_cbufs; i++) {
415 surf = r300_surface(fb->cbufs[i]);
416
417 OUT_CS_REG(R300_RB3D_COLOROFFSET0 + (4 * i), surf->offset);
418 OUT_CS_RELOC(surf);
419
420 OUT_CS_REG(R300_RB3D_COLORPITCH0 + (4 * i), surf->pitch);
421 OUT_CS_RELOC(surf);
422 }
423
424 /* Set up the ZB part of the CBZB clear. */
425 if (r300->cbzb_clear) {
426 surf = r300_surface(fb->cbufs[0]);
427
428 OUT_CS_REG(R300_ZB_FORMAT, surf->cbzb_format);
429
430 OUT_CS_REG(R300_ZB_DEPTHOFFSET, surf->cbzb_midpoint_offset);
431 OUT_CS_RELOC(surf);
432
433 OUT_CS_REG(R300_ZB_DEPTHPITCH, surf->cbzb_pitch);
434 OUT_CS_RELOC(surf);
435
436 DBG(r300, DBG_CBZB,
437 "CBZB clearing cbuf %08x %08x\n", surf->cbzb_format,
438 surf->cbzb_pitch);
439 }
440 /* Set up a zbuffer. */
441 else if (fb->zsbuf) {
442 surf = r300_surface(fb->zsbuf);
443
444 OUT_CS_REG(R300_ZB_FORMAT, surf->format);
445
446 OUT_CS_REG(R300_ZB_DEPTHOFFSET, surf->offset);
447 OUT_CS_RELOC(surf);
448
449 OUT_CS_REG(R300_ZB_DEPTHPITCH, surf->pitch);
450 OUT_CS_RELOC(surf);
451
452 if (r300->hyperz_enabled) {
453 /* HiZ RAM. */
454 OUT_CS_REG(R300_ZB_HIZ_OFFSET, 0);
455 OUT_CS_REG(R300_ZB_HIZ_PITCH, surf->pitch_hiz);
456 /* Z Mask RAM. (compressed zbuffer) */
457 OUT_CS_REG(R300_ZB_ZMASK_OFFSET, 0);
458 OUT_CS_REG(R300_ZB_ZMASK_PITCH, surf->pitch_zmask);
459 }
460 }
461
462 END_CS;
463 }
464
465 void r300_emit_hyperz_state(struct r300_context *r300,
466 unsigned size, void *state)
467 {
468 struct r300_hyperz_state *z = state;
469 CS_LOCALS(r300);
470
471 if (z->flush)
472 WRITE_CS_TABLE(&z->cb_flush_begin, size);
473 else
474 WRITE_CS_TABLE(&z->cb_begin, size - 2);
475 }
476
477 void r300_emit_hyperz_end(struct r300_context *r300)
478 {
479 struct r300_hyperz_state z =
480 *(struct r300_hyperz_state*)r300->hyperz_state.state;
481
482 z.flush = 1;
483 z.zb_bw_cntl = 0;
484 z.zb_depthclearvalue = 0;
485 z.sc_hyperz = R300_SC_HYPERZ_ADJ_2;
486 z.gb_z_peq_config = 0;
487
488 r300_emit_hyperz_state(r300, r300->hyperz_state.size, &z);
489 }
490
491 #define R300_NIBBLES(x0, y0, x1, y1, x2, y2, d0y, d0x) \
492 (((x0) & 0xf) | (((y0) & 0xf) << 4) | \
493 (((x1) & 0xf) << 8) | (((y1) & 0xf) << 12) | \
494 (((x2) & 0xf) << 16) | (((y2) & 0xf) << 20) | \
495 (((d0y) & 0xf) << 24) | (((d0x) & 0xf) << 28))
496
497 static unsigned r300_get_mspos(int index, unsigned *p)
498 {
499 unsigned reg, i, distx, disty, dist;
500
501 if (index == 0) {
502 /* MSPOS0 contains positions for samples 0,1,2 as (X,Y) pairs of nibbles,
503 * followed by a (Y,X) pair containing the minimum distance from the pixel
504 * edge:
505 * X0, Y0, X1, Y1, X2, Y2, D0_Y, D0_X
506 *
507 * There is a quirk when setting D0_X. The value represents the distance
508 * from the left edge of the pixel quad to the first sample in subpixels.
509 * All values less than eight should use the actual value, but „7‟ should
510 * be used for the distance „8‟. The hardware will convert 7 into 8 internally.
511 */
512 distx = 11;
513 for (i = 0; i < 12; i += 2) {
514 if (p[i] < distx)
515 distx = p[i];
516 }
517
518 disty = 11;
519 for (i = 1; i < 12; i += 2) {
520 if (p[i] < disty)
521 disty = p[i];
522 }
523
524 if (distx == 8)
525 distx = 7;
526
527 reg = R300_NIBBLES(p[0], p[1], p[2], p[3], p[4], p[5], disty, distx);
528 } else {
529 /* MSPOS1 contains positions for samples 3,4,5 as (X,Y) pairs of nibbles,
530 * followed by the minimum distance from the pixel edge (not sure if X or Y):
531 * X3, Y3, X4, Y4, X5, Y5, D1
532 */
533 dist = 11;
534 for (i = 0; i < 12; i++) {
535 if (p[i] < dist)
536 dist = p[i];
537 }
538
539 reg = R300_NIBBLES(p[6], p[7], p[8], p[9], p[10], p[11], dist, 0);
540 }
541 return reg;
542 }
543
544 void r300_emit_fb_state_pipelined(struct r300_context *r300,
545 unsigned size, void *state)
546 {
547 /* The sample coordinates are in the range [0,11], because
548 * GB_TILE_CONFIG.SUBPIXEL is set to the 1/12 subpixel precision.
549 *
550 * Some sample coordinates reach to neighboring pixels and should not be used.
551 * (e.g. Y=11)
552 *
553 * The unused samples must be set to the positions of other valid samples. */
554 static unsigned sample_locs_1x[12] = {
555 6,6, 6,6, 6,6, 6,6, 6,6, 6,6
556 };
557 static unsigned sample_locs_2x[12] = {
558 3,9, 9,3, 9,3, 9,3, 9,3, 9,3
559 };
560 static unsigned sample_locs_4x[12] = {
561 4,4, 8,8, 2,10, 10,2, 10,2, 10,2
562 };
563 static unsigned sample_locs_6x[12] = {
564 3,1, 7,3, 11,5, 1,7, 5,9, 9,10
565 };
566
567 struct pipe_framebuffer_state* fb =
568 (struct pipe_framebuffer_state*)r300->fb_state.state;
569 unsigned i, num_cbufs = fb->nr_cbufs;
570 unsigned mspos0, mspos1;
571 CS_LOCALS(r300);
572
573 /* If we use the multiwrite feature, the colorbuffers 2,3,4 must be
574 * marked as UNUSED in the US block. */
575 if (r300->fb_multiwrite) {
576 num_cbufs = MIN2(num_cbufs, 1);
577 }
578
579 BEGIN_CS(size);
580
581 /* Colorbuffer format in the US block.
582 * (must be written after unpipelined regs) */
583 OUT_CS_REG_SEQ(R300_US_OUT_FMT_0, 4);
584 for (i = 0; i < num_cbufs; i++) {
585 OUT_CS(r300_surface(fb->cbufs[i])->format);
586 }
587 for (; i < 1; i++) {
588 OUT_CS(R300_US_OUT_FMT_C4_8 |
589 R300_C0_SEL_B | R300_C1_SEL_G |
590 R300_C2_SEL_R | R300_C3_SEL_A);
591 }
592 for (; i < 4; i++) {
593 OUT_CS(R300_US_OUT_FMT_UNUSED);
594 }
595
596 /* Set sample positions. It depends on the framebuffer sample count.
597 * These are pipelined regs and as such cannot be moved to the AA state.
598 */
599 switch (r300->num_samples) {
600 default:
601 mspos0 = r300_get_mspos(0, sample_locs_1x);
602 mspos1 = r300_get_mspos(1, sample_locs_1x);
603 break;
604 case 2:
605 mspos0 = r300_get_mspos(0, sample_locs_2x);
606 mspos1 = r300_get_mspos(1, sample_locs_2x);
607 break;
608 case 4:
609 mspos0 = r300_get_mspos(0, sample_locs_4x);
610 mspos1 = r300_get_mspos(1, sample_locs_4x);
611 break;
612 case 6:
613 mspos0 = r300_get_mspos(0, sample_locs_6x);
614 mspos1 = r300_get_mspos(1, sample_locs_6x);
615 break;
616 }
617
618 OUT_CS_REG_SEQ(R300_GB_MSPOS0, 2);
619 OUT_CS(mspos0);
620 OUT_CS(mspos1);
621 END_CS;
622 }
623
624 void r300_emit_query_start(struct r300_context *r300, unsigned size, void*state)
625 {
626 struct r300_query *query = r300->query_current;
627 CS_LOCALS(r300);
628
629 if (!query)
630 return;
631
632 BEGIN_CS(size);
633 if (r300->screen->caps.family == CHIP_RV530) {
634 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL);
635 } else {
636 OUT_CS_REG(R300_SU_REG_DEST, R300_RASTER_PIPE_SELECT_ALL);
637 }
638 OUT_CS_REG(R300_ZB_ZPASS_DATA, 0);
639 END_CS;
640 query->begin_emitted = TRUE;
641 }
642
643 static void r300_emit_query_end_frag_pipes(struct r300_context *r300,
644 struct r300_query *query)
645 {
646 struct r300_capabilities* caps = &r300->screen->caps;
647 uint32_t gb_pipes = r300->screen->info.r300_num_gb_pipes;
648 CS_LOCALS(r300);
649
650 assert(gb_pipes);
651
652 BEGIN_CS(6 * gb_pipes + 2);
653 /* I'm not so sure I like this switch, but it's hard to be elegant
654 * when there's so many special cases...
655 *
656 * So here's the basic idea. For each pipe, enable writes to it only,
657 * then put out the relocation for ZPASS_ADDR, taking into account a
658 * 4-byte offset for each pipe. RV380 and older are special; they have
659 * only two pipes, and the second pipe's enable is on bit 3, not bit 1,
660 * so there's a chipset cap for that. */
661 switch (gb_pipes) {
662 case 4:
663 /* pipe 3 only */
664 OUT_CS_REG(R300_SU_REG_DEST, 1 << 3);
665 OUT_CS_REG(R300_ZB_ZPASS_ADDR, (query->num_results + 3) * 4);
666 OUT_CS_RELOC(r300->query_current);
667 case 3:
668 /* pipe 2 only */
669 OUT_CS_REG(R300_SU_REG_DEST, 1 << 2);
670 OUT_CS_REG(R300_ZB_ZPASS_ADDR, (query->num_results + 2) * 4);
671 OUT_CS_RELOC(r300->query_current);
672 case 2:
673 /* pipe 1 only */
674 /* As mentioned above, accomodate RV380 and older. */
675 OUT_CS_REG(R300_SU_REG_DEST,
676 1 << (caps->high_second_pipe ? 3 : 1));
677 OUT_CS_REG(R300_ZB_ZPASS_ADDR, (query->num_results + 1) * 4);
678 OUT_CS_RELOC(r300->query_current);
679 case 1:
680 /* pipe 0 only */
681 OUT_CS_REG(R300_SU_REG_DEST, 1 << 0);
682 OUT_CS_REG(R300_ZB_ZPASS_ADDR, (query->num_results + 0) * 4);
683 OUT_CS_RELOC(r300->query_current);
684 break;
685 default:
686 fprintf(stderr, "r300: Implementation error: Chipset reports %d"
687 " pixel pipes!\n", gb_pipes);
688 abort();
689 }
690
691 /* And, finally, reset it to normal... */
692 OUT_CS_REG(R300_SU_REG_DEST, 0xF);
693 END_CS;
694 }
695
696 static void rv530_emit_query_end_single_z(struct r300_context *r300,
697 struct r300_query *query)
698 {
699 CS_LOCALS(r300);
700
701 BEGIN_CS(8);
702 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_0);
703 OUT_CS_REG(R300_ZB_ZPASS_ADDR, query->num_results * 4);
704 OUT_CS_RELOC(r300->query_current);
705 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL);
706 END_CS;
707 }
708
709 static void rv530_emit_query_end_double_z(struct r300_context *r300,
710 struct r300_query *query)
711 {
712 CS_LOCALS(r300);
713
714 BEGIN_CS(14);
715 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_0);
716 OUT_CS_REG(R300_ZB_ZPASS_ADDR, (query->num_results + 0) * 4);
717 OUT_CS_RELOC(r300->query_current);
718 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_1);
719 OUT_CS_REG(R300_ZB_ZPASS_ADDR, (query->num_results + 1) * 4);
720 OUT_CS_RELOC(r300->query_current);
721 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL);
722 END_CS;
723 }
724
725 void r300_emit_query_end(struct r300_context* r300)
726 {
727 struct r300_capabilities *caps = &r300->screen->caps;
728 struct r300_query *query = r300->query_current;
729
730 if (!query)
731 return;
732
733 if (query->begin_emitted == FALSE)
734 return;
735
736 if (caps->family == CHIP_RV530) {
737 if (r300->screen->info.r300_num_z_pipes == 2)
738 rv530_emit_query_end_double_z(r300, query);
739 else
740 rv530_emit_query_end_single_z(r300, query);
741 } else
742 r300_emit_query_end_frag_pipes(r300, query);
743
744 query->begin_emitted = FALSE;
745 query->num_results += query->num_pipes;
746
747 /* XXX grab all the results and reset the counter. */
748 if (query->num_results >= query->buf->size / 4 - 4) {
749 query->num_results = (query->buf->size / 4) / 2;
750 fprintf(stderr, "r300: Rewinding OQBO...\n");
751 }
752 }
753
754 void r300_emit_invariant_state(struct r300_context *r300,
755 unsigned size, void *state)
756 {
757 CS_LOCALS(r300);
758 WRITE_CS_TABLE(state, size);
759 }
760
761 void r300_emit_rs_state(struct r300_context* r300, unsigned size, void* state)
762 {
763 struct r300_rs_state* rs = state;
764 CS_LOCALS(r300);
765
766 BEGIN_CS(size);
767 OUT_CS_TABLE(rs->cb_main, RS_STATE_MAIN_SIZE);
768 if (rs->polygon_offset_enable) {
769 if (r300->zbuffer_bpp == 16) {
770 OUT_CS_TABLE(rs->cb_poly_offset_zb16, 5);
771 } else {
772 OUT_CS_TABLE(rs->cb_poly_offset_zb24, 5);
773 }
774 }
775 END_CS;
776 }
777
778 void r300_emit_rs_block_state(struct r300_context* r300,
779 unsigned size, void* state)
780 {
781 struct r300_rs_block* rs = (struct r300_rs_block*)state;
782 unsigned i;
783 /* It's the same for both INST and IP tables */
784 unsigned count = (rs->inst_count & R300_RS_INST_COUNT_MASK) + 1;
785 CS_LOCALS(r300);
786
787 if (DBG_ON(r300, DBG_RS_BLOCK)) {
788 r500_dump_rs_block(rs);
789
790 fprintf(stderr, "r300: RS emit:\n");
791
792 for (i = 0; i < count; i++)
793 fprintf(stderr, " : ip %d: 0x%08x\n", i, rs->ip[i]);
794
795 for (i = 0; i < count; i++)
796 fprintf(stderr, " : inst %d: 0x%08x\n", i, rs->inst[i]);
797
798 fprintf(stderr, " : count: 0x%08x inst_count: 0x%08x\n",
799 rs->count, rs->inst_count);
800 }
801
802 BEGIN_CS(size);
803 OUT_CS_REG_SEQ(R300_VAP_VTX_STATE_CNTL, 2);
804 OUT_CS(rs->vap_vtx_state_cntl);
805 OUT_CS(rs->vap_vsm_vtx_assm);
806 OUT_CS_REG_SEQ(R300_VAP_OUTPUT_VTX_FMT_0, 2);
807 OUT_CS(rs->vap_out_vtx_fmt[0]);
808 OUT_CS(rs->vap_out_vtx_fmt[1]);
809 OUT_CS_REG_SEQ(R300_GB_ENABLE, 1);
810 OUT_CS(rs->gb_enable);
811
812 if (r300->screen->caps.is_r500) {
813 OUT_CS_REG_SEQ(R500_RS_IP_0, count);
814 } else {
815 OUT_CS_REG_SEQ(R300_RS_IP_0, count);
816 }
817 OUT_CS_TABLE(rs->ip, count);
818
819 OUT_CS_REG_SEQ(R300_RS_COUNT, 2);
820 OUT_CS(rs->count);
821 OUT_CS(rs->inst_count);
822
823 if (r300->screen->caps.is_r500) {
824 OUT_CS_REG_SEQ(R500_RS_INST_0, count);
825 } else {
826 OUT_CS_REG_SEQ(R300_RS_INST_0, count);
827 }
828 OUT_CS_TABLE(rs->inst, count);
829 END_CS;
830 }
831
832 void r300_emit_sample_mask(struct r300_context *r300,
833 unsigned size, void *state)
834 {
835 unsigned mask = (*(unsigned*)state) & ((1 << 6)-1);
836 CS_LOCALS(r300);
837
838 BEGIN_CS(size);
839 OUT_CS_REG(R300_SC_SCREENDOOR,
840 mask | (mask << 6) | (mask << 12) | (mask << 18));
841 END_CS;
842 }
843
844 void r300_emit_scissor_state(struct r300_context* r300,
845 unsigned size, void* state)
846 {
847 struct pipe_scissor_state* scissor = (struct pipe_scissor_state*)state;
848 CS_LOCALS(r300);
849
850 BEGIN_CS(size);
851 OUT_CS_REG_SEQ(R300_SC_CLIPRECT_TL_0, 2);
852 if (r300->screen->caps.is_r500) {
853 OUT_CS((scissor->minx << R300_CLIPRECT_X_SHIFT) |
854 (scissor->miny << R300_CLIPRECT_Y_SHIFT));
855 OUT_CS(((scissor->maxx - 1) << R300_CLIPRECT_X_SHIFT) |
856 ((scissor->maxy - 1) << R300_CLIPRECT_Y_SHIFT));
857 } else {
858 OUT_CS(((scissor->minx + 1440) << R300_CLIPRECT_X_SHIFT) |
859 ((scissor->miny + 1440) << R300_CLIPRECT_Y_SHIFT));
860 OUT_CS(((scissor->maxx + 1440-1) << R300_CLIPRECT_X_SHIFT) |
861 ((scissor->maxy + 1440-1) << R300_CLIPRECT_Y_SHIFT));
862 }
863 END_CS;
864 }
865
866 void r300_emit_textures_state(struct r300_context *r300,
867 unsigned size, void *state)
868 {
869 struct r300_textures_state *allstate = (struct r300_textures_state*)state;
870 struct r300_texture_sampler_state *texstate;
871 struct r300_resource *tex;
872 unsigned i;
873 boolean has_us_format = r300->screen->caps.has_us_format;
874 CS_LOCALS(r300);
875
876 BEGIN_CS(size);
877 OUT_CS_REG(R300_TX_ENABLE, allstate->tx_enable);
878
879 for (i = 0; i < allstate->count; i++) {
880 if ((1 << i) & allstate->tx_enable) {
881 texstate = &allstate->regs[i];
882 tex = r300_resource(allstate->sampler_views[i]->base.texture);
883
884 OUT_CS_REG(R300_TX_FILTER0_0 + (i * 4), texstate->filter0);
885 OUT_CS_REG(R300_TX_FILTER1_0 + (i * 4), texstate->filter1);
886 OUT_CS_REG(R300_TX_BORDER_COLOR_0 + (i * 4),
887 texstate->border_color);
888
889 OUT_CS_REG(R300_TX_FORMAT0_0 + (i * 4), texstate->format.format0);
890 OUT_CS_REG(R300_TX_FORMAT1_0 + (i * 4), texstate->format.format1);
891 OUT_CS_REG(R300_TX_FORMAT2_0 + (i * 4), texstate->format.format2);
892
893 OUT_CS_REG(R300_TX_OFFSET_0 + (i * 4), texstate->format.tile_config);
894 OUT_CS_RELOC(tex);
895
896 if (has_us_format) {
897 OUT_CS_REG(R500_US_FORMAT0_0 + (i * 4),
898 texstate->format.us_format0);
899 }
900 }
901 }
902 END_CS;
903 }
904
905 void r300_emit_vertex_arrays(struct r300_context* r300, int offset,
906 boolean indexed, int instance_id)
907 {
908 struct pipe_vertex_buffer *vbuf = r300->vertex_buffer;
909 struct pipe_vertex_element *velem = r300->velems->velem;
910 struct r300_resource *buf;
911 int i;
912 unsigned vertex_array_count = r300->velems->count;
913 unsigned packet_size = (vertex_array_count * 3 + 1) / 2;
914 struct pipe_vertex_buffer *vb1, *vb2;
915 unsigned *hw_format_size = r300->velems->format_size;
916 unsigned size1, size2, offset1, offset2, stride1, stride2;
917 CS_LOCALS(r300);
918
919 BEGIN_CS(2 + packet_size + vertex_array_count * 2);
920 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR, packet_size);
921 OUT_CS(vertex_array_count | (!indexed ? R300_VC_FORCE_PREFETCH : 0));
922
923 if (instance_id == -1) {
924 /* Non-instanced arrays. This ignores instance_divisor and instance_id. */
925 for (i = 0; i < vertex_array_count - 1; i += 2) {
926 vb1 = &vbuf[velem[i].vertex_buffer_index];
927 vb2 = &vbuf[velem[i+1].vertex_buffer_index];
928 size1 = hw_format_size[i];
929 size2 = hw_format_size[i+1];
930
931 OUT_CS(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(vb1->stride) |
932 R300_VBPNTR_SIZE1(size2) | R300_VBPNTR_STRIDE1(vb2->stride));
933 OUT_CS(vb1->buffer_offset + velem[i].src_offset + offset * vb1->stride);
934 OUT_CS(vb2->buffer_offset + velem[i+1].src_offset + offset * vb2->stride);
935 }
936
937 if (vertex_array_count & 1) {
938 vb1 = &vbuf[velem[i].vertex_buffer_index];
939 size1 = hw_format_size[i];
940
941 OUT_CS(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(vb1->stride));
942 OUT_CS(vb1->buffer_offset + velem[i].src_offset + offset * vb1->stride);
943 }
944
945 for (i = 0; i < vertex_array_count; i++) {
946 buf = r300_resource(vbuf[velem[i].vertex_buffer_index].buffer);
947 OUT_CS_RELOC(buf);
948 }
949 } else {
950 /* Instanced arrays. */
951 for (i = 0; i < vertex_array_count - 1; i += 2) {
952 vb1 = &vbuf[velem[i].vertex_buffer_index];
953 vb2 = &vbuf[velem[i+1].vertex_buffer_index];
954 size1 = hw_format_size[i];
955 size2 = hw_format_size[i+1];
956
957 if (velem[i].instance_divisor) {
958 stride1 = 0;
959 offset1 = vb1->buffer_offset + velem[i].src_offset +
960 (instance_id / velem[i].instance_divisor) * vb1->stride;
961 } else {
962 stride1 = vb1->stride;
963 offset1 = vb1->buffer_offset + velem[i].src_offset + offset * vb1->stride;
964 }
965 if (velem[i+1].instance_divisor) {
966 stride2 = 0;
967 offset2 = vb2->buffer_offset + velem[i+1].src_offset +
968 (instance_id / velem[i+1].instance_divisor) * vb2->stride;
969 } else {
970 stride2 = vb2->stride;
971 offset2 = vb2->buffer_offset + velem[i+1].src_offset + offset * vb2->stride;
972 }
973
974 OUT_CS(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(stride1) |
975 R300_VBPNTR_SIZE1(size2) | R300_VBPNTR_STRIDE1(stride2));
976 OUT_CS(offset1);
977 OUT_CS(offset2);
978 }
979
980 if (vertex_array_count & 1) {
981 vb1 = &vbuf[velem[i].vertex_buffer_index];
982 size1 = hw_format_size[i];
983
984 if (velem[i].instance_divisor) {
985 stride1 = 0;
986 offset1 = vb1->buffer_offset + velem[i].src_offset +
987 (instance_id / velem[i].instance_divisor) * vb1->stride;
988 } else {
989 stride1 = vb1->stride;
990 offset1 = vb1->buffer_offset + velem[i].src_offset + offset * vb1->stride;
991 }
992
993 OUT_CS(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(stride1));
994 OUT_CS(offset1);
995 }
996
997 for (i = 0; i < vertex_array_count; i++) {
998 buf = r300_resource(vbuf[velem[i].vertex_buffer_index].buffer);
999 OUT_CS_RELOC(buf);
1000 }
1001 }
1002 END_CS;
1003 }
1004
1005 void r300_emit_vertex_arrays_swtcl(struct r300_context *r300, boolean indexed)
1006 {
1007 CS_LOCALS(r300);
1008
1009 DBG(r300, DBG_SWTCL, "r300: Preparing vertex buffer %p for render, "
1010 "vertex size %d\n", r300->vbo,
1011 r300->vertex_info.size);
1012 /* Set the pointer to our vertex buffer. The emitted values are this:
1013 * PACKET3 [3D_LOAD_VBPNTR]
1014 * COUNT [1]
1015 * FORMAT [size | stride << 8]
1016 * OFFSET [offset into BO]
1017 * VBPNTR [relocated BO]
1018 */
1019 BEGIN_CS(7);
1020 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR, 3);
1021 OUT_CS(1 | (!indexed ? R300_VC_FORCE_PREFETCH : 0));
1022 OUT_CS(r300->vertex_info.size |
1023 (r300->vertex_info.size << 8));
1024 OUT_CS(r300->draw_vbo_offset);
1025 OUT_CS(0);
1026
1027 assert(r300->vbo_cs);
1028 cs_winsys->cs_write_reloc(cs_copy, r300->vbo_cs);
1029 CS_USED_DW(2);
1030 END_CS;
1031 }
1032
1033 void r300_emit_vertex_stream_state(struct r300_context* r300,
1034 unsigned size, void* state)
1035 {
1036 struct r300_vertex_stream_state *streams =
1037 (struct r300_vertex_stream_state*)state;
1038 unsigned i;
1039 CS_LOCALS(r300);
1040
1041 if (DBG_ON(r300, DBG_PSC)) {
1042 fprintf(stderr, "r300: PSC emit:\n");
1043
1044 for (i = 0; i < streams->count; i++) {
1045 fprintf(stderr, " : prog_stream_cntl%d: 0x%08x\n", i,
1046 streams->vap_prog_stream_cntl[i]);
1047 }
1048
1049 for (i = 0; i < streams->count; i++) {
1050 fprintf(stderr, " : prog_stream_cntl_ext%d: 0x%08x\n", i,
1051 streams->vap_prog_stream_cntl_ext[i]);
1052 }
1053 }
1054
1055 BEGIN_CS(size);
1056 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_0, streams->count);
1057 OUT_CS_TABLE(streams->vap_prog_stream_cntl, streams->count);
1058 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_EXT_0, streams->count);
1059 OUT_CS_TABLE(streams->vap_prog_stream_cntl_ext, streams->count);
1060 END_CS;
1061 }
1062
1063 void r300_emit_pvs_flush(struct r300_context* r300, unsigned size, void* state)
1064 {
1065 CS_LOCALS(r300);
1066
1067 BEGIN_CS(size);
1068 OUT_CS_REG(R300_VAP_PVS_STATE_FLUSH_REG, 0x0);
1069 END_CS;
1070 }
1071
1072 void r300_emit_vap_invariant_state(struct r300_context *r300,
1073 unsigned size, void *state)
1074 {
1075 CS_LOCALS(r300);
1076 WRITE_CS_TABLE(state, size);
1077 }
1078
1079 void r300_emit_vs_state(struct r300_context* r300, unsigned size, void* state)
1080 {
1081 struct r300_vertex_shader* vs = (struct r300_vertex_shader*)state;
1082 struct r300_vertex_program_code* code = &vs->code;
1083 struct r300_screen* r300screen = r300->screen;
1084 unsigned instruction_count = code->length / 4;
1085
1086 unsigned vtx_mem_size = r300screen->caps.is_r500 ? 128 : 72;
1087 unsigned input_count = MAX2(util_bitcount(code->InputsRead), 1);
1088 unsigned output_count = MAX2(util_bitcount(code->OutputsWritten), 1);
1089 unsigned temp_count = MAX2(code->num_temporaries, 1);
1090
1091 unsigned pvs_num_slots = MIN3(vtx_mem_size / input_count,
1092 vtx_mem_size / output_count, 10);
1093 unsigned pvs_num_controllers = MIN2(vtx_mem_size / temp_count, 5);
1094
1095 CS_LOCALS(r300);
1096
1097 BEGIN_CS(size);
1098
1099 /* R300_VAP_PVS_CODE_CNTL_0
1100 * R300_VAP_PVS_CONST_CNTL
1101 * R300_VAP_PVS_CODE_CNTL_1
1102 * See the r5xx docs for instructions on how to use these. */
1103 OUT_CS_REG(R300_VAP_PVS_CODE_CNTL_0, R300_PVS_FIRST_INST(0) |
1104 R300_PVS_XYZW_VALID_INST(instruction_count - 1) |
1105 R300_PVS_LAST_INST(instruction_count - 1));
1106 OUT_CS_REG(R300_VAP_PVS_CODE_CNTL_1, instruction_count - 1);
1107
1108 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG, 0);
1109 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, code->length);
1110 OUT_CS_TABLE(code->body.d, code->length);
1111
1112 OUT_CS_REG(R300_VAP_CNTL, R300_PVS_NUM_SLOTS(pvs_num_slots) |
1113 R300_PVS_NUM_CNTLRS(pvs_num_controllers) |
1114 R300_PVS_NUM_FPUS(r300screen->caps.num_vert_fpus) |
1115 R300_PVS_VF_MAX_VTX_NUM(12) |
1116 (r300screen->caps.is_r500 ? R500_TCL_STATE_OPTIMIZATION : 0));
1117
1118 /* Emit flow control instructions. Even if there are no fc instructions,
1119 * we still need to write the registers to make sure they are cleared. */
1120 OUT_CS_REG(R300_VAP_PVS_FLOW_CNTL_OPC, code->fc_ops);
1121 if (r300screen->caps.is_r500) {
1122 OUT_CS_REG_SEQ(R500_VAP_PVS_FLOW_CNTL_ADDRS_LW_0, R300_VS_MAX_FC_OPS * 2);
1123 OUT_CS_TABLE(code->fc_op_addrs.r500, R300_VS_MAX_FC_OPS * 2);
1124 } else {
1125 OUT_CS_REG_SEQ(R300_VAP_PVS_FLOW_CNTL_ADDRS_0, R300_VS_MAX_FC_OPS);
1126 OUT_CS_TABLE(code->fc_op_addrs.r300, R300_VS_MAX_FC_OPS);
1127 }
1128 OUT_CS_REG_SEQ(R300_VAP_PVS_FLOW_CNTL_LOOP_INDEX_0, R300_VS_MAX_FC_OPS);
1129 OUT_CS_TABLE(code->fc_loop_index, R300_VS_MAX_FC_OPS);
1130
1131 END_CS;
1132 }
1133
1134 void r300_emit_vs_constants(struct r300_context* r300,
1135 unsigned size, void *state)
1136 {
1137 unsigned count =
1138 ((struct r300_vertex_shader*)r300->vs_state.state)->externals_count;
1139 struct r300_constant_buffer *buf = (struct r300_constant_buffer*)state;
1140 struct r300_vertex_shader *vs = (struct r300_vertex_shader*)r300->vs_state.state;
1141 unsigned i;
1142 int imm_first = vs->externals_count;
1143 int imm_end = vs->code.constants.Count;
1144 int imm_count = vs->immediates_count;
1145 CS_LOCALS(r300);
1146
1147 BEGIN_CS(size);
1148 OUT_CS_REG(R300_VAP_PVS_CONST_CNTL,
1149 R300_PVS_CONST_BASE_OFFSET(buf->buffer_base) |
1150 R300_PVS_MAX_CONST_ADDR(MAX2(imm_end - 1, 0)));
1151 if (vs->externals_count) {
1152 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG,
1153 (r300->screen->caps.is_r500 ?
1154 R500_PVS_CONST_START : R300_PVS_CONST_START) + buf->buffer_base);
1155 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, count * 4);
1156 if (buf->remap_table){
1157 for (i = 0; i < count; i++) {
1158 uint32_t *data = &buf->ptr[buf->remap_table[i]*4];
1159 OUT_CS_TABLE(data, 4);
1160 }
1161 } else {
1162 OUT_CS_TABLE(buf->ptr, count * 4);
1163 }
1164 }
1165
1166 /* Emit immediates. */
1167 if (imm_count) {
1168 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG,
1169 (r300->screen->caps.is_r500 ?
1170 R500_PVS_CONST_START : R300_PVS_CONST_START) +
1171 buf->buffer_base + imm_first);
1172 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, imm_count * 4);
1173 for (i = imm_first; i < imm_end; i++) {
1174 const float *data = vs->code.constants.Constants[i].u.Immediate;
1175 OUT_CS_TABLE(data, 4);
1176 }
1177 }
1178 END_CS;
1179 }
1180
1181 void r300_emit_viewport_state(struct r300_context* r300,
1182 unsigned size, void* state)
1183 {
1184 struct r300_viewport_state* viewport = (struct r300_viewport_state*)state;
1185 CS_LOCALS(r300);
1186
1187 BEGIN_CS(size);
1188 OUT_CS_REG_SEQ(R300_SE_VPORT_XSCALE, 6);
1189 OUT_CS_TABLE(&viewport->xscale, 6);
1190 OUT_CS_REG(R300_VAP_VTE_CNTL, viewport->vte_control);
1191 END_CS;
1192 }
1193
1194 void r300_emit_hiz_clear(struct r300_context *r300, unsigned size, void *state)
1195 {
1196 struct pipe_framebuffer_state *fb =
1197 (struct pipe_framebuffer_state*)r300->fb_state.state;
1198 struct r300_resource* tex;
1199 CS_LOCALS(r300);
1200
1201 tex = r300_resource(fb->zsbuf->texture);
1202
1203 BEGIN_CS(size);
1204 OUT_CS_REG(R300_ZB_ZCACHE_CTLSTAT,
1205 R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE |
1206 R300_ZB_ZCACHE_CTLSTAT_ZC_FREE_FREE);
1207 OUT_CS_PKT3(R300_PACKET3_3D_CLEAR_HIZ, 2);
1208 OUT_CS(0);
1209 OUT_CS(tex->tex.hiz_dwords[fb->zsbuf->u.tex.level]);
1210 OUT_CS(r300->hiz_clear_value);
1211 END_CS;
1212
1213 /* Mark the current zbuffer's hiz ram as in use. */
1214 r300->hiz_in_use = TRUE;
1215 r300->hiz_func = HIZ_FUNC_NONE;
1216 r300_mark_atom_dirty(r300, &r300->hyperz_state);
1217 }
1218
1219 void r300_emit_zmask_clear(struct r300_context *r300, unsigned size, void *state)
1220 {
1221 struct pipe_framebuffer_state *fb =
1222 (struct pipe_framebuffer_state*)r300->fb_state.state;
1223 struct r300_resource *tex;
1224 CS_LOCALS(r300);
1225
1226 tex = r300_resource(fb->zsbuf->texture);
1227
1228 BEGIN_CS(size);
1229 OUT_CS_REG(R300_ZB_ZCACHE_CTLSTAT,
1230 R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE |
1231 R300_ZB_ZCACHE_CTLSTAT_ZC_FREE_FREE);
1232 OUT_CS_PKT3(R300_PACKET3_3D_CLEAR_ZMASK, 2);
1233 OUT_CS(0);
1234 OUT_CS(tex->tex.zmask_dwords[fb->zsbuf->u.tex.level]);
1235 OUT_CS(0);
1236 END_CS;
1237
1238 /* Mark the current zbuffer's zmask as in use. */
1239 r300->zmask_in_use = TRUE;
1240 r300_mark_atom_dirty(r300, &r300->hyperz_state);
1241 }
1242
1243 void r300_emit_ztop_state(struct r300_context* r300,
1244 unsigned size, void* state)
1245 {
1246 struct r300_ztop_state* ztop = (struct r300_ztop_state*)state;
1247 CS_LOCALS(r300);
1248
1249 BEGIN_CS(size);
1250 OUT_CS_REG(R300_ZB_ZTOP, ztop->z_buffer_top);
1251 END_CS;
1252 }
1253
1254 void r300_emit_texture_cache_inval(struct r300_context* r300, unsigned size, void* state)
1255 {
1256 CS_LOCALS(r300);
1257
1258 BEGIN_CS(size);
1259 OUT_CS_REG(R300_TX_INVALTAGS, 0);
1260 END_CS;
1261 }
1262
1263 boolean r300_emit_buffer_validate(struct r300_context *r300,
1264 boolean do_validate_vertex_buffers,
1265 struct pipe_resource *index_buffer)
1266 {
1267 struct pipe_framebuffer_state *fb =
1268 (struct pipe_framebuffer_state*)r300->fb_state.state;
1269 struct r300_aa_state *aa = (struct r300_aa_state*)r300->aa_state.state;
1270 struct r300_textures_state *texstate =
1271 (struct r300_textures_state*)r300->textures_state.state;
1272 struct r300_resource *tex;
1273 unsigned i;
1274 boolean flushed = FALSE;
1275
1276 validate:
1277 if (r300->fb_state.dirty) {
1278 /* Color buffers... */
1279 for (i = 0; i < fb->nr_cbufs; i++) {
1280 tex = r300_resource(fb->cbufs[i]->texture);
1281 assert(tex && tex->buf && "cbuf is marked, but NULL!");
1282 r300->rws->cs_add_reloc(r300->cs, tex->cs_buf,
1283 RADEON_USAGE_READWRITE,
1284 r300_surface(fb->cbufs[i])->domain);
1285 }
1286 /* ...depth buffer... */
1287 if (fb->zsbuf) {
1288 tex = r300_resource(fb->zsbuf->texture);
1289 assert(tex && tex->buf && "zsbuf is marked, but NULL!");
1290 r300->rws->cs_add_reloc(r300->cs, tex->cs_buf,
1291 RADEON_USAGE_READWRITE,
1292 r300_surface(fb->zsbuf)->domain);
1293 }
1294 }
1295 /* The AA resolve buffer. */
1296 if (r300->aa_state.dirty) {
1297 if (aa->dest) {
1298 r300->rws->cs_add_reloc(r300->cs, aa->dest->cs_buf,
1299 RADEON_USAGE_WRITE,
1300 aa->dest->domain);
1301 }
1302 }
1303 if (r300->textures_state.dirty) {
1304 /* ...textures... */
1305 for (i = 0; i < texstate->count; i++) {
1306 if (!(texstate->tx_enable & (1 << i))) {
1307 continue;
1308 }
1309
1310 tex = r300_resource(texstate->sampler_views[i]->base.texture);
1311 r300->rws->cs_add_reloc(r300->cs, tex->cs_buf, RADEON_USAGE_READ,
1312 tex->domain);
1313 }
1314 }
1315 /* ...occlusion query buffer... */
1316 if (r300->query_current)
1317 r300->rws->cs_add_reloc(r300->cs, r300->query_current->cs_buf,
1318 RADEON_USAGE_WRITE, RADEON_DOMAIN_GTT);
1319 /* ...vertex buffer for SWTCL path... */
1320 if (r300->vbo_cs)
1321 r300->rws->cs_add_reloc(r300->cs, r300->vbo_cs,
1322 RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
1323 /* ...vertex buffers for HWTCL path... */
1324 if (do_validate_vertex_buffers && r300->vertex_arrays_dirty) {
1325 struct pipe_vertex_buffer *vbuf = r300->vertex_buffer;
1326 struct pipe_vertex_buffer *last = r300->vertex_buffer +
1327 r300->nr_vertex_buffers;
1328 struct pipe_resource *buf;
1329
1330 for (; vbuf != last; vbuf++) {
1331 buf = vbuf->buffer;
1332 if (!buf)
1333 continue;
1334
1335 r300->rws->cs_add_reloc(r300->cs, r300_resource(buf)->cs_buf,
1336 RADEON_USAGE_READ,
1337 r300_resource(buf)->domain);
1338 }
1339 }
1340 /* ...and index buffer for HWTCL path. */
1341 if (index_buffer)
1342 r300->rws->cs_add_reloc(r300->cs, r300_resource(index_buffer)->cs_buf,
1343 RADEON_USAGE_READ,
1344 r300_resource(index_buffer)->domain);
1345
1346 /* Now do the validation (flush is called inside cs_validate on failure). */
1347 if (!r300->rws->cs_validate(r300->cs)) {
1348 /* Ooops, an infinite loop, give up. */
1349 if (flushed)
1350 return FALSE;
1351
1352 flushed = TRUE;
1353 goto validate;
1354 }
1355
1356 return TRUE;
1357 }
1358
1359 unsigned r300_get_num_dirty_dwords(struct r300_context *r300)
1360 {
1361 struct r300_atom* atom;
1362 unsigned dwords = 0;
1363
1364 foreach_dirty_atom(r300, atom) {
1365 if (atom->dirty) {
1366 dwords += atom->size;
1367 }
1368 }
1369
1370 /* let's reserve some more, just in case */
1371 dwords += 32;
1372
1373 return dwords;
1374 }
1375
1376 unsigned r300_get_num_cs_end_dwords(struct r300_context *r300)
1377 {
1378 unsigned dwords = 0;
1379
1380 /* Emitted in flush. */
1381 dwords += 26; /* emit_query_end */
1382 dwords += r300->hyperz_state.size + 2; /* emit_hyperz_end + zcache flush */
1383 if (r300->screen->caps.is_r500)
1384 dwords += 2; /* emit_index_bias */
1385 if (r300->screen->info.drm_minor >= 6)
1386 dwords += 3; /* MSPOS */
1387
1388 return dwords;
1389 }
1390
1391 /* Emit all dirty state. */
1392 void r300_emit_dirty_state(struct r300_context* r300)
1393 {
1394 struct r300_atom *atom;
1395
1396 foreach_dirty_atom(r300, atom) {
1397 if (atom->dirty) {
1398 atom->emit(r300, atom->size, atom->state);
1399 atom->dirty = FALSE;
1400 }
1401 }
1402
1403 r300->first_dirty = NULL;
1404 r300->last_dirty = NULL;
1405 r300->dirty_hw++;
1406 }