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