3674edc975f158e1b24181b7185087fd71cf9190
[mesa.git] / src / gallium / drivers / r300 / r300_render.c
1 /*
2 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2010 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_render: Vertex and index buffer primitive emission. Contains both
25 * HW TCL fastpath rendering, and SW TCL Draw-assisted rendering. */
26
27 #include "draw/draw_context.h"
28 #include "draw/draw_vbuf.h"
29
30 #include "util/u_inlines.h"
31
32 #include "util/u_format.h"
33 #include "util/u_memory.h"
34 #include "util/u_upload_mgr.h"
35 #include "util/u_prim.h"
36
37 #include "r300_cs.h"
38 #include "r300_context.h"
39 #include "r300_screen_buffer.h"
40 #include "r300_emit.h"
41 #include "r300_reg.h"
42
43 #include <limits.h>
44
45 #define IMMD_DWORDS 32
46
47 static uint32_t r300_translate_primitive(unsigned prim)
48 {
49 switch (prim) {
50 case PIPE_PRIM_POINTS:
51 return R300_VAP_VF_CNTL__PRIM_POINTS;
52 case PIPE_PRIM_LINES:
53 return R300_VAP_VF_CNTL__PRIM_LINES;
54 case PIPE_PRIM_LINE_LOOP:
55 return R300_VAP_VF_CNTL__PRIM_LINE_LOOP;
56 case PIPE_PRIM_LINE_STRIP:
57 return R300_VAP_VF_CNTL__PRIM_LINE_STRIP;
58 case PIPE_PRIM_TRIANGLES:
59 return R300_VAP_VF_CNTL__PRIM_TRIANGLES;
60 case PIPE_PRIM_TRIANGLE_STRIP:
61 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP;
62 case PIPE_PRIM_TRIANGLE_FAN:
63 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN;
64 case PIPE_PRIM_QUADS:
65 return R300_VAP_VF_CNTL__PRIM_QUADS;
66 case PIPE_PRIM_QUAD_STRIP:
67 return R300_VAP_VF_CNTL__PRIM_QUAD_STRIP;
68 case PIPE_PRIM_POLYGON:
69 return R300_VAP_VF_CNTL__PRIM_POLYGON;
70 default:
71 return 0;
72 }
73 }
74
75 static uint32_t r300_provoking_vertex_fixes(struct r300_context *r300,
76 unsigned mode)
77 {
78 struct r300_rs_state* rs = (struct r300_rs_state*)r300->rs_state.state;
79 uint32_t color_control = rs->color_control;
80
81 /* By default (see r300_state.c:r300_create_rs_state) color_control is
82 * initialized to provoking the first vertex.
83 *
84 * Triangle fans must be reduced to the second vertex, not the first, in
85 * Gallium flatshade-first mode, as per the GL spec.
86 * (http://www.opengl.org/registry/specs/ARB/provoking_vertex.txt)
87 *
88 * Quads never provoke correctly in flatshade-first mode. The first
89 * vertex is never considered as provoking, so only the second, third,
90 * and fourth vertices can be selected, and both "third" and "last" modes
91 * select the fourth vertex. This is probably due to D3D lacking quads.
92 *
93 * Similarly, polygons reduce to the first, not the last, vertex, when in
94 * "last" mode, and all other modes start from the second vertex.
95 *
96 * ~ C.
97 */
98
99 if (rs->rs.flatshade_first) {
100 switch (mode) {
101 case PIPE_PRIM_TRIANGLE_FAN:
102 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND;
103 break;
104 case PIPE_PRIM_QUADS:
105 case PIPE_PRIM_QUAD_STRIP:
106 case PIPE_PRIM_POLYGON:
107 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
108 break;
109 default:
110 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST;
111 break;
112 }
113 } else {
114 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
115 }
116
117 return color_control;
118 }
119
120 void r500_emit_index_bias(struct r300_context *r300, int index_bias)
121 {
122 CS_LOCALS(r300);
123
124 BEGIN_CS(2);
125 OUT_CS_REG(R500_VAP_INDEX_OFFSET,
126 (index_bias & 0xFFFFFF) | (index_bias < 0 ? 1<<24 : 0));
127 END_CS;
128 }
129
130 static void r300_emit_draw_init(struct r300_context *r300, unsigned mode,
131 unsigned min_index, unsigned max_index)
132 {
133 CS_LOCALS(r300);
134
135 BEGIN_CS(5);
136 OUT_CS_REG(R300_GA_COLOR_CONTROL,
137 r300_provoking_vertex_fixes(r300, mode));
138 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
139 OUT_CS(max_index);
140 OUT_CS(min_index);
141 END_CS;
142 }
143
144 /* This function splits the index bias value into two parts:
145 * - buffer_offset: the value that can be safely added to buffer offsets
146 * in r300_emit_vertex_arrays (it must yield a positive offset when added to
147 * a vertex buffer offset)
148 * - index_offset: the value that must be manually subtracted from indices
149 * in an index buffer to achieve negative offsets. */
150 static void r300_split_index_bias(struct r300_context *r300, int index_bias,
151 int *buffer_offset, int *index_offset)
152 {
153 struct pipe_vertex_buffer *vb, *vbufs = r300->vbuf_mgr->vertex_buffer;
154 struct pipe_vertex_element *velem = r300->velems->velem;
155 unsigned i, size;
156 int max_neg_bias;
157
158 if (index_bias < 0) {
159 /* See how large index bias we may subtract. We must be careful
160 * here because negative buffer offsets are not allowed
161 * by the DRM API. */
162 max_neg_bias = INT_MAX;
163 for (i = 0; i < r300->velems->count; i++) {
164 vb = &vbufs[velem[i].vertex_buffer_index];
165 size = (vb->buffer_offset + velem[i].src_offset) / vb->stride;
166 max_neg_bias = MIN2(max_neg_bias, size);
167 }
168
169 /* Now set the minimum allowed value. */
170 *buffer_offset = MAX2(-max_neg_bias, index_bias);
171 } else {
172 /* A positive index bias is OK. */
173 *buffer_offset = index_bias;
174 }
175
176 *index_offset = index_bias - *buffer_offset;
177 }
178
179 enum r300_prepare_flags {
180 PREP_EMIT_STATES = (1 << 0), /* call emit_dirty_state and friends? */
181 PREP_VALIDATE_VBOS = (1 << 1), /* validate VBOs? */
182 PREP_EMIT_AOS = (1 << 2), /* call emit_vertex_arrays? */
183 PREP_EMIT_AOS_SWTCL = (1 << 3), /* call emit_vertex_arrays_swtcl? */
184 PREP_INDEXED = (1 << 4) /* is this draw_elements? */
185 };
186
187 /**
188 * Check if the requested number of dwords is available in the CS and
189 * if not, flush.
190 * \param r300 The context.
191 * \param flags See r300_prepare_flags.
192 * \param cs_dwords The number of dwords to reserve in CS.
193 * \return TRUE if the CS was flushed
194 */
195 static boolean r300_reserve_cs_dwords(struct r300_context *r300,
196 enum r300_prepare_flags flags,
197 unsigned cs_dwords)
198 {
199 boolean flushed = FALSE;
200 boolean first_draw = flags & PREP_EMIT_STATES;
201 boolean emit_vertex_arrays = flags & PREP_EMIT_AOS;
202 boolean emit_vertex_arrays_swtcl = flags & PREP_EMIT_AOS_SWTCL;
203
204 /* Add dirty state, index offset, and AOS. */
205 if (first_draw) {
206 cs_dwords += r300_get_num_dirty_dwords(r300);
207
208 if (r300->screen->caps.is_r500)
209 cs_dwords += 2; /* emit_index_offset */
210
211 if (emit_vertex_arrays)
212 cs_dwords += 55; /* emit_vertex_arrays */
213
214 if (emit_vertex_arrays_swtcl)
215 cs_dwords += 7; /* emit_vertex_arrays_swtcl */
216 }
217
218 cs_dwords += r300_get_num_cs_end_dwords(r300);
219
220 /* Reserve requested CS space. */
221 if (cs_dwords > (RADEON_MAX_CMDBUF_DWORDS - r300->cs->cdw)) {
222 r300_flush(&r300->context, RADEON_FLUSH_ASYNC, NULL);
223 flushed = TRUE;
224 }
225
226 return flushed;
227 }
228
229 /**
230 * Validate buffers and emit dirty state.
231 * \param r300 The context.
232 * \param flags See r300_prepare_flags.
233 * \param index_buffer The index buffer to validate. The parameter may be NULL.
234 * \param buffer_offset The offset passed to emit_vertex_arrays.
235 * \param index_bias The index bias to emit.
236 * \param instance_id Index of instance to render
237 * \return TRUE if rendering should be skipped
238 */
239 static boolean r300_emit_states(struct r300_context *r300,
240 enum r300_prepare_flags flags,
241 struct pipe_resource *index_buffer,
242 int buffer_offset,
243 int index_bias, int instance_id)
244 {
245 boolean first_draw = flags & PREP_EMIT_STATES;
246 boolean emit_vertex_arrays = flags & PREP_EMIT_AOS;
247 boolean emit_vertex_arrays_swtcl = flags & PREP_EMIT_AOS_SWTCL;
248 boolean indexed = flags & PREP_INDEXED;
249 boolean validate_vbos = flags & PREP_VALIDATE_VBOS;
250
251 /* Validate buffers and emit dirty state if needed. */
252 if (first_draw) {
253 if (!r300_emit_buffer_validate(r300, validate_vbos,
254 index_buffer)) {
255 fprintf(stderr, "r300: CS space validation failed. "
256 "(not enough memory?) Skipping rendering.\n");
257 return FALSE;
258 }
259
260 r300_emit_dirty_state(r300);
261 if (r300->screen->caps.is_r500) {
262 if (r300->screen->caps.has_tcl)
263 r500_emit_index_bias(r300, index_bias);
264 else
265 r500_emit_index_bias(r300, 0);
266 }
267
268 if (emit_vertex_arrays &&
269 (r300->vertex_arrays_dirty ||
270 r300->vertex_arrays_indexed != indexed ||
271 r300->vertex_arrays_offset != buffer_offset ||
272 r300->vertex_arrays_instance_id != instance_id)) {
273 r300_emit_vertex_arrays(r300, buffer_offset, indexed, instance_id);
274
275 r300->vertex_arrays_dirty = FALSE;
276 r300->vertex_arrays_indexed = indexed;
277 r300->vertex_arrays_offset = buffer_offset;
278 r300->vertex_arrays_instance_id = instance_id;
279 }
280
281 if (emit_vertex_arrays_swtcl)
282 r300_emit_vertex_arrays_swtcl(r300, indexed);
283 }
284
285 return TRUE;
286 }
287
288 /**
289 * Check if the requested number of dwords is available in the CS and
290 * if not, flush. Then validate buffers and emit dirty state.
291 * \param r300 The context.
292 * \param flags See r300_prepare_flags.
293 * \param index_buffer The index buffer to validate. The parameter may be NULL.
294 * \param cs_dwords The number of dwords to reserve in CS.
295 * \param buffer_offset The offset passed to emit_vertex_arrays.
296 * \param index_bias The index bias to emit.
297 * \param instance_id The instance to render.
298 * \return TRUE if rendering should be skipped
299 */
300 static boolean r300_prepare_for_rendering(struct r300_context *r300,
301 enum r300_prepare_flags flags,
302 struct pipe_resource *index_buffer,
303 unsigned cs_dwords,
304 int buffer_offset,
305 int index_bias,
306 int instance_id)
307 {
308 /* Update vertex elements for InstanceID here. */
309 boolean instancing_enabled = instance_id != -1;
310
311 if (r300->screen->caps.has_tcl &&
312 (flags & PREP_EMIT_AOS) &&
313 instancing_enabled != r300->instancing_enabled) {
314 r300->instancing_enabled = instancing_enabled;
315 r300_mark_atom_dirty(r300, &r300->vertex_stream_state);
316 r300->vertex_arrays_dirty = TRUE;
317 flags |= PREP_EMIT_STATES;
318 }
319
320 /* Make sure there is enough space in the command stream and emit states. */
321 if (r300_reserve_cs_dwords(r300, flags, cs_dwords))
322 flags |= PREP_EMIT_STATES;
323
324 return r300_emit_states(r300, flags, index_buffer, buffer_offset,
325 index_bias, instance_id);
326 }
327
328 static boolean immd_is_good_idea(struct r300_context *r300,
329 unsigned count)
330 {
331 struct pipe_vertex_element* velem;
332 struct pipe_resource *buf;
333 boolean checked[PIPE_MAX_ATTRIBS] = {0};
334 unsigned vertex_element_count = r300->velems->count;
335 unsigned i, vbi;
336
337 if (DBG_ON(r300, DBG_NO_IMMD)) {
338 return FALSE;
339 }
340
341 if (r300->draw) {
342 return FALSE;
343 }
344
345 if (count * r300->velems->vertex_size_dwords > IMMD_DWORDS) {
346 return FALSE;
347 }
348
349 /* We shouldn't map buffers referenced by CS, busy buffers,
350 * and ones placed in VRAM. */
351 for (i = 0; i < vertex_element_count; i++) {
352 velem = &r300->velems->velem[i];
353 vbi = velem->vertex_buffer_index;
354
355 if (!checked[vbi]) {
356 buf = r300->vbuf_mgr->real_vertex_buffer[vbi];
357
358 if ((r300_resource(buf)->domain != RADEON_DOMAIN_GTT)) {
359 return FALSE;
360 }
361
362 checked[vbi] = TRUE;
363 }
364 }
365 return TRUE;
366 }
367
368 /*****************************************************************************
369 * The HWTCL draw functions. *
370 ****************************************************************************/
371
372 static void r300_draw_arrays_immediate(struct r300_context *r300,
373 const struct pipe_draw_info *info)
374 {
375 struct pipe_vertex_element* velem;
376 struct pipe_vertex_buffer* vbuf;
377 unsigned vertex_element_count = r300->velems->count;
378 unsigned i, v, vbi;
379
380 /* Size of the vertex, in dwords. */
381 unsigned vertex_size = r300->velems->vertex_size_dwords;
382
383 /* The number of dwords for this draw operation. */
384 unsigned dwords = 4 + info->count * vertex_size;
385
386 /* Size of the vertex element, in dwords. */
387 unsigned size[PIPE_MAX_ATTRIBS];
388
389 /* Stride to the same attrib in the next vertex in the vertex buffer,
390 * in dwords. */
391 unsigned stride[PIPE_MAX_ATTRIBS];
392
393 /* Mapped vertex buffers. */
394 uint32_t* map[PIPE_MAX_ATTRIBS] = {0};
395 uint32_t* mapelem[PIPE_MAX_ATTRIBS];
396
397 CS_LOCALS(r300);
398
399 if (!r300_prepare_for_rendering(r300, PREP_EMIT_STATES, NULL, dwords, 0, 0, -1))
400 return;
401
402 /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
403 for (i = 0; i < vertex_element_count; i++) {
404 velem = &r300->velems->velem[i];
405 size[i] = r300->velems->format_size[i] / 4;
406 vbi = velem->vertex_buffer_index;
407 vbuf = &r300->vbuf_mgr->vertex_buffer[vbi];
408 stride[i] = vbuf->stride / 4;
409
410 /* Map the buffer. */
411 if (!map[vbi]) {
412 map[vbi] = (uint32_t*)r300->rws->buffer_map(
413 r300_resource(r300->vbuf_mgr->real_vertex_buffer[vbi])->buf,
414 r300->cs, PIPE_TRANSFER_READ | PIPE_TRANSFER_UNSYNCHRONIZED);
415 map[vbi] += (vbuf->buffer_offset / 4) + stride[i] * info->start;
416 }
417 mapelem[i] = map[vbi] + (velem->src_offset / 4);
418 }
419
420 r300_emit_draw_init(r300, info->mode, 0, info->count-1);
421
422 BEGIN_CS(dwords);
423 OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
424 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, info->count * vertex_size);
425 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (info->count << 16) |
426 r300_translate_primitive(info->mode));
427
428 /* Emit vertices. */
429 for (v = 0; v < info->count; v++) {
430 for (i = 0; i < vertex_element_count; i++) {
431 OUT_CS_TABLE(&mapelem[i][stride[i] * v], size[i]);
432 }
433 }
434 END_CS;
435
436 /* Unmap buffers. */
437 for (i = 0; i < vertex_element_count; i++) {
438 vbi = r300->velems->velem[i].vertex_buffer_index;
439
440 if (map[vbi]) {
441 r300->rws->buffer_unmap(r300_resource(r300->vbuf_mgr->real_vertex_buffer[vbi])->buf);
442 map[vbi] = NULL;
443 }
444 }
445 }
446
447 static void r300_emit_draw_arrays(struct r300_context *r300,
448 unsigned mode,
449 unsigned count)
450 {
451 boolean alt_num_verts = count > 65535;
452 CS_LOCALS(r300);
453
454 if (count >= (1 << 24)) {
455 fprintf(stderr, "r300: Got a huge number of vertices: %i, "
456 "refusing to render.\n", count);
457 return;
458 }
459
460 r300_emit_draw_init(r300, mode, 0, count-1);
461
462 BEGIN_CS(2 + (alt_num_verts ? 2 : 0));
463 if (alt_num_verts) {
464 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
465 }
466 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
467 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
468 r300_translate_primitive(mode) |
469 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
470 END_CS;
471 }
472
473 static void r300_emit_draw_elements(struct r300_context *r300,
474 struct pipe_resource* indexBuffer,
475 unsigned indexSize,
476 unsigned min_index,
477 unsigned max_index,
478 unsigned mode,
479 unsigned start,
480 unsigned count,
481 uint16_t *imm_indices3)
482 {
483 uint32_t count_dwords, offset_dwords;
484 boolean alt_num_verts = count > 65535;
485 CS_LOCALS(r300);
486
487 if (count >= (1 << 24) || max_index >= (1 << 24)) {
488 fprintf(stderr, "r300: Got a huge number of vertices: %i, "
489 "refusing to render (max_index: %i).\n", count, max_index);
490 return;
491 }
492
493 DBG(r300, DBG_DRAW, "r300: Indexbuf of %u indices, min %u max %u\n",
494 count, min_index, max_index);
495
496 r300_emit_draw_init(r300, mode, min_index, max_index);
497
498 /* If start is odd, render the first triangle with indices embedded
499 * in the command stream. This will increase start by 3 and make it
500 * even. We can then proceed without a fallback. */
501 if (indexSize == 2 && (start & 1) &&
502 mode == PIPE_PRIM_TRIANGLES) {
503 BEGIN_CS(4);
504 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 2);
505 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (3 << 16) |
506 R300_VAP_VF_CNTL__PRIM_TRIANGLES);
507 OUT_CS(imm_indices3[1] << 16 | imm_indices3[0]);
508 OUT_CS(imm_indices3[2]);
509 END_CS;
510
511 start += 3;
512 count -= 3;
513 if (!count)
514 return;
515 }
516
517 offset_dwords = indexSize * start / sizeof(uint32_t);
518
519 BEGIN_CS(8 + (alt_num_verts ? 2 : 0));
520 if (alt_num_verts) {
521 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
522 }
523 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);
524 if (indexSize == 4) {
525 count_dwords = count;
526 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
527 R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
528 r300_translate_primitive(mode) |
529 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
530 } else {
531 count_dwords = (count + 1) / 2;
532 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
533 r300_translate_primitive(mode) |
534 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
535 }
536
537 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2);
538 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) |
539 (0 << R300_INDX_BUFFER_SKIP_SHIFT));
540 OUT_CS(offset_dwords << 2);
541 OUT_CS(count_dwords);
542 OUT_CS_RELOC(r300_resource(indexBuffer));
543 END_CS;
544 }
545
546 static void r300_draw_elements_immediate(struct r300_context *r300,
547 const struct pipe_draw_info *info)
548 {
549 uint8_t *ptr1;
550 uint16_t *ptr2;
551 uint32_t *ptr4;
552 unsigned index_size = r300->index_buffer.index_size;
553 unsigned i, count_dwords = index_size == 4 ? info->count :
554 (info->count + 1) / 2;
555 CS_LOCALS(r300);
556
557 /* 19 dwords for r300_draw_elements_immediate. Give up if the function fails. */
558 if (!r300_prepare_for_rendering(r300,
559 PREP_EMIT_STATES | PREP_VALIDATE_VBOS | PREP_EMIT_AOS |
560 PREP_INDEXED, NULL, 2+count_dwords, 0, info->index_bias, -1))
561 return;
562
563 r300_emit_draw_init(r300, info->mode, info->min_index, info->max_index);
564
565 BEGIN_CS(2 + count_dwords);
566 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, count_dwords);
567
568 switch (index_size) {
569 case 1:
570 ptr1 = r300_resource(r300->index_buffer.buffer)->b.user_ptr;
571 ptr1 += info->start;
572
573 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (info->count << 16) |
574 r300_translate_primitive(info->mode));
575
576 if (info->index_bias && !r300->screen->caps.is_r500) {
577 for (i = 0; i < info->count-1; i += 2)
578 OUT_CS(((ptr1[i+1] + info->index_bias) << 16) |
579 (ptr1[i] + info->index_bias));
580
581 if (info->count & 1)
582 OUT_CS(ptr1[i] + info->index_bias);
583 } else {
584 for (i = 0; i < info->count-1; i += 2)
585 OUT_CS(((ptr1[i+1]) << 16) |
586 (ptr1[i] ));
587
588 if (info->count & 1)
589 OUT_CS(ptr1[i]);
590 }
591 break;
592
593 case 2:
594 ptr2 = (uint16_t*)r300_resource(r300->index_buffer.buffer)->b.user_ptr;
595 ptr2 += info->start;
596
597 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (info->count << 16) |
598 r300_translate_primitive(info->mode));
599
600 if (info->index_bias && !r300->screen->caps.is_r500) {
601 for (i = 0; i < info->count-1; i += 2)
602 OUT_CS(((ptr2[i+1] + info->index_bias) << 16) |
603 (ptr2[i] + info->index_bias));
604
605 if (info->count & 1)
606 OUT_CS(ptr2[i] + info->index_bias);
607 } else {
608 OUT_CS_TABLE(ptr2, count_dwords);
609 }
610 break;
611
612 case 4:
613 ptr4 = (uint32_t*)r300_resource(r300->index_buffer.buffer)->b.user_ptr;
614 ptr4 += info->start;
615
616 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (info->count << 16) |
617 R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
618 r300_translate_primitive(info->mode));
619
620 if (info->index_bias && !r300->screen->caps.is_r500) {
621 for (i = 0; i < info->count; i++)
622 OUT_CS(ptr4[i] + info->index_bias);
623 } else {
624 OUT_CS_TABLE(ptr4, count_dwords);
625 }
626 break;
627 }
628 END_CS;
629 }
630
631 static void r300_draw_elements(struct r300_context *r300,
632 const struct pipe_draw_info *info,
633 int instance_id)
634 {
635 struct pipe_resource *indexBuffer = r300->index_buffer.buffer;
636 unsigned indexSize = r300->index_buffer.index_size;
637 struct pipe_resource* orgIndexBuffer = indexBuffer;
638 unsigned start = info->start;
639 unsigned count = info->count;
640 boolean alt_num_verts = r300->screen->caps.is_r500 &&
641 count > 65536;
642 unsigned short_count;
643 int buffer_offset = 0, index_offset = 0; /* for index bias emulation */
644 uint16_t indices3[3];
645
646 if (info->index_bias && !r300->screen->caps.is_r500) {
647 r300_split_index_bias(r300, info->index_bias, &buffer_offset, &index_offset);
648 }
649
650 r300_translate_index_buffer(r300, &indexBuffer, &indexSize, index_offset,
651 &start, count);
652
653 /* Fallback for misaligned ushort indices. */
654 if (indexSize == 2 && (start & 1) &&
655 !r300_resource(indexBuffer)->b.user_ptr) {
656 /* If we got here, then orgIndexBuffer == indexBuffer. */
657 uint16_t *ptr = r300->rws->buffer_map(r300_resource(orgIndexBuffer)->buf,
658 r300->cs,
659 PIPE_TRANSFER_READ |
660 PIPE_TRANSFER_UNSYNCHRONIZED);
661
662 if (info->mode == PIPE_PRIM_TRIANGLES) {
663 memcpy(indices3, ptr + start, 6);
664 } else {
665 /* Copy the mapped index buffer directly to the upload buffer.
666 * The start index will be aligned simply from the fact that
667 * every sub-buffer in the upload buffer is aligned. */
668 r300_upload_index_buffer(r300, &indexBuffer, indexSize, &start,
669 count, (uint8_t*)ptr);
670 }
671 r300->rws->buffer_unmap(r300_resource(orgIndexBuffer)->buf);
672 } else {
673 if (r300_resource(indexBuffer)->b.user_ptr)
674 r300_upload_index_buffer(r300, &indexBuffer, indexSize,
675 &start, count,
676 r300_resource(indexBuffer)->b.user_ptr);
677 }
678
679 /* 19 dwords for emit_draw_elements. Give up if the function fails. */
680 if (!r300_prepare_for_rendering(r300,
681 PREP_EMIT_STATES | PREP_VALIDATE_VBOS | PREP_EMIT_AOS |
682 PREP_INDEXED, indexBuffer, 19, buffer_offset, info->index_bias,
683 instance_id))
684 goto done;
685
686 if (alt_num_verts || count <= 65535) {
687 r300_emit_draw_elements(r300, indexBuffer, indexSize, info->min_index,
688 info->max_index, info->mode, start, count,
689 indices3);
690 } else {
691 do {
692 if (indexSize == 2 && (start & 1))
693 short_count = MIN2(count, 65535);
694 else
695 short_count = MIN2(count, 65534);
696
697 r300_emit_draw_elements(r300, indexBuffer, indexSize,
698 info->min_index, info->max_index,
699 info->mode, start, short_count, indices3);
700
701 start += short_count;
702 count -= short_count;
703
704 /* 15 dwords for emit_draw_elements */
705 if (count) {
706 if (!r300_prepare_for_rendering(r300,
707 PREP_VALIDATE_VBOS | PREP_EMIT_AOS | PREP_INDEXED,
708 indexBuffer, 19, buffer_offset, info->index_bias,
709 instance_id))
710 goto done;
711 }
712 } while (count);
713 }
714
715 done:
716 if (indexBuffer != orgIndexBuffer) {
717 pipe_resource_reference( &indexBuffer, NULL );
718 }
719 }
720
721 static void r300_draw_arrays(struct r300_context *r300,
722 const struct pipe_draw_info *info,
723 int instance_id)
724 {
725 boolean alt_num_verts = r300->screen->caps.is_r500 &&
726 info->count > 65536;
727 unsigned start = info->start;
728 unsigned count = info->count;
729 unsigned short_count;
730
731 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
732 if (!r300_prepare_for_rendering(r300,
733 PREP_EMIT_STATES | PREP_VALIDATE_VBOS | PREP_EMIT_AOS,
734 NULL, 9, start, 0, instance_id))
735 return;
736
737 if (alt_num_verts || count <= 65535) {
738 r300_emit_draw_arrays(r300, info->mode, count);
739 } else {
740 do {
741 short_count = MIN2(count, 65535);
742 r300_emit_draw_arrays(r300, info->mode, short_count);
743
744 start += short_count;
745 count -= short_count;
746
747 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
748 if (count) {
749 if (!r300_prepare_for_rendering(r300,
750 PREP_VALIDATE_VBOS | PREP_EMIT_AOS, NULL, 9,
751 start, 0, instance_id))
752 return;
753 }
754 } while (count);
755 }
756 }
757
758 static void r300_draw_arrays_instanced(struct r300_context *r300,
759 const struct pipe_draw_info *info)
760 {
761 int i;
762
763 for (i = 0; i < info->instance_count; i++)
764 r300_draw_arrays(r300, info, i);
765 }
766
767 static void r300_draw_elements_instanced(struct r300_context *r300,
768 const struct pipe_draw_info *info)
769 {
770 int i;
771
772 for (i = 0; i < info->instance_count; i++)
773 r300_draw_elements(r300, info, i);
774 }
775
776 static void r300_draw_vbo(struct pipe_context* pipe,
777 const struct pipe_draw_info *dinfo)
778 {
779 struct r300_context* r300 = r300_context(pipe);
780 struct pipe_draw_info info = *dinfo;
781 boolean buffers_updated, uploader_flushed;
782
783 info.indexed = info.indexed && r300->index_buffer.buffer;
784
785 if (r300->skip_rendering ||
786 !u_trim_pipe_prim(info.mode, &info.count)) {
787 return;
788 }
789
790 r300_update_derived_state(r300);
791
792 /* Start the vbuf manager and update buffers if needed. */
793 u_vbuf_mgr_draw_begin(r300->vbuf_mgr, &info,
794 &buffers_updated, &uploader_flushed);
795 if (buffers_updated) {
796 r300->vertex_arrays_dirty = TRUE;
797 }
798
799 /* Draw. */
800 if (info.indexed) {
801 info.start += r300->index_buffer.offset;
802 info.max_index = MIN2(r300->vbuf_mgr->max_index, info.max_index);
803
804 if (info.instance_count <= 1) {
805 if (info.count <= 8 &&
806 r300_resource(r300->index_buffer.buffer)->b.user_ptr) {
807 r300_draw_elements_immediate(r300, &info);
808 } else {
809 r300_draw_elements(r300, &info, -1);
810 }
811 } else {
812 r300_draw_elements_instanced(r300, &info);
813 }
814 } else {
815 if (info.instance_count <= 1) {
816 if (immd_is_good_idea(r300, info.count)) {
817 r300_draw_arrays_immediate(r300, &info);
818 } else {
819 r300_draw_arrays(r300, &info, -1);
820 }
821 } else {
822 r300_draw_arrays_instanced(r300, &info);
823 }
824 }
825
826 u_vbuf_mgr_draw_end(r300->vbuf_mgr);
827 }
828
829 /****************************************************************************
830 * The rest of this file is for SW TCL rendering only. Please be polite and *
831 * keep these functions separated so that they are easier to locate. ~C. *
832 ***************************************************************************/
833
834 /* SW TCL elements, using Draw. */
835 static void r300_swtcl_draw_vbo(struct pipe_context* pipe,
836 const struct pipe_draw_info *info)
837 {
838 struct r300_context* r300 = r300_context(pipe);
839 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS];
840 struct pipe_transfer *ib_transfer = NULL;
841 unsigned count = info->count;
842 int i;
843 void *indices = NULL;
844 boolean indexed = info->indexed && r300->index_buffer.buffer;
845
846 if (r300->skip_rendering) {
847 return;
848 }
849
850 if (!u_trim_pipe_prim(info->mode, &count)) {
851 return;
852 }
853
854 r300_update_derived_state(r300);
855
856 r300_reserve_cs_dwords(r300,
857 PREP_EMIT_STATES | PREP_EMIT_AOS_SWTCL |
858 (indexed ? PREP_INDEXED : 0),
859 indexed ? 256 : 6);
860
861 for (i = 0; i < r300->vbuf_mgr->nr_vertex_buffers; i++) {
862 if (r300->vbuf_mgr->vertex_buffer[i].buffer) {
863 void *buf = pipe_buffer_map(pipe,
864 r300->vbuf_mgr->vertex_buffer[i].buffer,
865 PIPE_TRANSFER_READ |
866 PIPE_TRANSFER_UNSYNCHRONIZED,
867 &vb_transfer[i]);
868 draw_set_mapped_vertex_buffer(r300->draw, i, buf);
869 }
870 }
871
872 if (indexed) {
873 indices = pipe_buffer_map(pipe, r300->index_buffer.buffer,
874 PIPE_TRANSFER_READ |
875 PIPE_TRANSFER_UNSYNCHRONIZED, &ib_transfer);
876 }
877
878 draw_set_mapped_index_buffer(r300->draw, indices);
879
880 r300->draw_vbo_locked = TRUE;
881 r300->draw_first_emitted = FALSE;
882 draw_vbo(r300->draw, info);
883 draw_flush(r300->draw);
884 r300->draw_vbo_locked = FALSE;
885
886 for (i = 0; i < r300->vbuf_mgr->nr_vertex_buffers; i++) {
887 if (r300->vbuf_mgr->vertex_buffer[i].buffer) {
888 pipe_buffer_unmap(pipe, vb_transfer[i]);
889 draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
890 }
891 }
892
893 if (indexed) {
894 pipe_buffer_unmap(pipe, ib_transfer);
895 draw_set_mapped_index_buffer(r300->draw, NULL);
896 }
897 }
898
899 /* Object for rendering using Draw. */
900 struct r300_render {
901 /* Parent class */
902 struct vbuf_render base;
903
904 /* Pipe context */
905 struct r300_context* r300;
906
907 /* Vertex information */
908 size_t vertex_size;
909 unsigned prim;
910 unsigned hwprim;
911
912 /* VBO */
913 size_t vbo_max_used;
914 void * vbo_ptr;
915
916 struct pipe_transfer *vbo_transfer;
917 };
918
919 static INLINE struct r300_render*
920 r300_render(struct vbuf_render* render)
921 {
922 return (struct r300_render*)render;
923 }
924
925 static const struct vertex_info*
926 r300_render_get_vertex_info(struct vbuf_render* render)
927 {
928 struct r300_render* r300render = r300_render(render);
929 struct r300_context* r300 = r300render->r300;
930
931 return &r300->vertex_info;
932 }
933
934 static boolean r300_render_allocate_vertices(struct vbuf_render* render,
935 ushort vertex_size,
936 ushort count)
937 {
938 struct r300_render* r300render = r300_render(render);
939 struct r300_context* r300 = r300render->r300;
940 struct pipe_screen* screen = r300->context.screen;
941 size_t size = (size_t)vertex_size * (size_t)count;
942
943 DBG(r300, DBG_DRAW, "r300: render_allocate_vertices (size: %d)\n", size);
944
945 if (size + r300->draw_vbo_offset > r300->draw_vbo_size)
946 {
947 pipe_resource_reference(&r300->vbo, NULL);
948 r300->vbo = pipe_buffer_create(screen,
949 PIPE_BIND_VERTEX_BUFFER,
950 PIPE_USAGE_STREAM,
951 R300_MAX_DRAW_VBO_SIZE);
952 r300->draw_vbo_offset = 0;
953 r300->draw_vbo_size = R300_MAX_DRAW_VBO_SIZE;
954 }
955
956 r300render->vertex_size = vertex_size;
957
958 return (r300->vbo) ? TRUE : FALSE;
959 }
960
961 static void* r300_render_map_vertices(struct vbuf_render* render)
962 {
963 struct r300_render* r300render = r300_render(render);
964 struct r300_context* r300 = r300render->r300;
965
966 assert(!r300render->vbo_transfer);
967
968 DBG(r300, DBG_DRAW, "r300: render_map_vertices\n");
969
970 r300render->vbo_ptr = pipe_buffer_map(&r300render->r300->context,
971 r300->vbo,
972 PIPE_TRANSFER_WRITE |
973 PIPE_TRANSFER_UNSYNCHRONIZED,
974 &r300render->vbo_transfer);
975
976 assert(r300render->vbo_ptr);
977
978 return ((uint8_t*)r300render->vbo_ptr + r300->draw_vbo_offset);
979 }
980
981 static void r300_render_unmap_vertices(struct vbuf_render* render,
982 ushort min,
983 ushort max)
984 {
985 struct r300_render* r300render = r300_render(render);
986 struct pipe_context* context = &r300render->r300->context;
987 struct r300_context* r300 = r300render->r300;
988
989 assert(r300render->vbo_transfer);
990
991 DBG(r300, DBG_DRAW, "r300: render_unmap_vertices\n");
992
993 r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
994 r300render->vertex_size * (max + 1));
995 pipe_buffer_unmap(context, r300render->vbo_transfer);
996
997 r300render->vbo_transfer = NULL;
998 }
999
1000 static void r300_render_release_vertices(struct vbuf_render* render)
1001 {
1002 struct r300_render* r300render = r300_render(render);
1003 struct r300_context* r300 = r300render->r300;
1004
1005 DBG(r300, DBG_DRAW, "r300: render_release_vertices\n");
1006
1007 r300->draw_vbo_offset += r300render->vbo_max_used;
1008 r300render->vbo_max_used = 0;
1009 }
1010
1011 static boolean r300_render_set_primitive(struct vbuf_render* render,
1012 unsigned prim)
1013 {
1014 struct r300_render* r300render = r300_render(render);
1015
1016 r300render->prim = prim;
1017 r300render->hwprim = r300_translate_primitive(prim);
1018
1019 return TRUE;
1020 }
1021
1022 static void r300_render_draw_arrays(struct vbuf_render* render,
1023 unsigned start,
1024 unsigned count)
1025 {
1026 struct r300_render* r300render = r300_render(render);
1027 struct r300_context* r300 = r300render->r300;
1028 uint8_t* ptr;
1029 unsigned i;
1030 unsigned dwords = 6;
1031
1032 CS_LOCALS(r300);
1033 (void) i; (void) ptr;
1034
1035 DBG(r300, DBG_DRAW, "r300: render_draw_arrays (count: %d)\n", count);
1036
1037 if (r300->draw_first_emitted) {
1038 if (!r300_prepare_for_rendering(r300,
1039 PREP_EMIT_STATES | PREP_EMIT_AOS_SWTCL,
1040 NULL, dwords, 0, 0, -1))
1041 return;
1042 } else {
1043 if (!r300_emit_states(r300,
1044 PREP_EMIT_STATES | PREP_EMIT_AOS_SWTCL,
1045 NULL, 0, 0, -1))
1046 return;
1047 }
1048
1049 BEGIN_CS(dwords);
1050 OUT_CS_REG(R300_GA_COLOR_CONTROL,
1051 r300_provoking_vertex_fixes(r300, r300render->prim));
1052 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);
1053 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
1054 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
1055 r300render->hwprim);
1056 END_CS;
1057
1058 r300->draw_first_emitted = TRUE;
1059 }
1060
1061 static void r300_render_draw_elements(struct vbuf_render* render,
1062 const ushort* indices,
1063 uint count)
1064 {
1065 struct r300_render* r300render = r300_render(render);
1066 struct r300_context* r300 = r300render->r300;
1067 int i;
1068 unsigned end_cs_dwords;
1069 unsigned max_index = (r300->draw_vbo_size - r300->draw_vbo_offset) /
1070 (r300render->r300->vertex_info.size * 4) - 1;
1071 unsigned short_count;
1072 unsigned free_dwords;
1073
1074 CS_LOCALS(r300);
1075 DBG(r300, DBG_DRAW, "r300: render_draw_elements (count: %d)\n", count);
1076
1077 if (r300->draw_first_emitted) {
1078 if (!r300_prepare_for_rendering(r300,
1079 PREP_EMIT_STATES | PREP_EMIT_AOS_SWTCL | PREP_INDEXED,
1080 NULL, 256, 0, 0, -1))
1081 return;
1082 } else {
1083 if (!r300_emit_states(r300,
1084 PREP_EMIT_STATES | PREP_EMIT_AOS_SWTCL | PREP_INDEXED,
1085 NULL, 0, 0, -1))
1086 return;
1087 }
1088
1089 /* Below we manage the CS space manually because there may be more
1090 * indices than it can fit in CS. */
1091
1092 end_cs_dwords = r300_get_num_cs_end_dwords(r300);
1093
1094 while (count) {
1095 free_dwords = RADEON_MAX_CMDBUF_DWORDS - r300->cs->cdw;
1096
1097 short_count = MIN2(count, (free_dwords - end_cs_dwords - 6) * 2);
1098
1099 BEGIN_CS(6 + (short_count+1)/2);
1100 OUT_CS_REG(R300_GA_COLOR_CONTROL,
1101 r300_provoking_vertex_fixes(r300, r300render->prim));
1102 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max_index);
1103 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (short_count+1)/2);
1104 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (short_count << 16) |
1105 r300render->hwprim);
1106 for (i = 0; i < short_count-1; i += 2) {
1107 OUT_CS(indices[i+1] << 16 | indices[i]);
1108 }
1109 if (short_count % 2) {
1110 OUT_CS(indices[short_count-1]);
1111 }
1112 END_CS;
1113
1114 /* OK now subtract the emitted indices and see if we need to emit
1115 * another draw packet. */
1116 indices += short_count;
1117 count -= short_count;
1118
1119 if (count) {
1120 if (!r300_prepare_for_rendering(r300,
1121 PREP_EMIT_AOS_SWTCL | PREP_INDEXED,
1122 NULL, 256, 0, 0, -1))
1123 return;
1124
1125 end_cs_dwords = r300_get_num_cs_end_dwords(r300);
1126 }
1127 }
1128
1129 r300->draw_first_emitted = TRUE;
1130 }
1131
1132 static void r300_render_destroy(struct vbuf_render* render)
1133 {
1134 FREE(render);
1135 }
1136
1137 static struct vbuf_render* r300_render_create(struct r300_context* r300)
1138 {
1139 struct r300_render* r300render = CALLOC_STRUCT(r300_render);
1140
1141 r300render->r300 = r300;
1142
1143 r300render->base.max_vertex_buffer_bytes = 1024 * 1024;
1144 r300render->base.max_indices = 16 * 1024;
1145
1146 r300render->base.get_vertex_info = r300_render_get_vertex_info;
1147 r300render->base.allocate_vertices = r300_render_allocate_vertices;
1148 r300render->base.map_vertices = r300_render_map_vertices;
1149 r300render->base.unmap_vertices = r300_render_unmap_vertices;
1150 r300render->base.set_primitive = r300_render_set_primitive;
1151 r300render->base.draw_elements = r300_render_draw_elements;
1152 r300render->base.draw_arrays = r300_render_draw_arrays;
1153 r300render->base.release_vertices = r300_render_release_vertices;
1154 r300render->base.destroy = r300_render_destroy;
1155
1156 return &r300render->base;
1157 }
1158
1159 struct draw_stage* r300_draw_stage(struct r300_context* r300)
1160 {
1161 struct vbuf_render* render;
1162 struct draw_stage* stage;
1163
1164 render = r300_render_create(r300);
1165
1166 if (!render) {
1167 return NULL;
1168 }
1169
1170 stage = draw_vbuf_stage(r300->draw, render);
1171
1172 if (!stage) {
1173 render->destroy(render);
1174 return NULL;
1175 }
1176
1177 draw_set_render(r300->draw, render);
1178
1179 return stage;
1180 }
1181
1182 void r300_draw_flush_vbuf(struct r300_context *r300)
1183 {
1184 pipe_resource_reference(&r300->vbo, NULL);
1185 r300->draw_vbo_size = 0;
1186 }
1187
1188 /****************************************************************************
1189 * End of SW TCL functions *
1190 ***************************************************************************/
1191
1192 /* This functions is used to draw a rectangle for the blitter module.
1193 *
1194 * If we rendered a quad, the pixels on the main diagonal
1195 * would be computed and stored twice, which makes the clear/copy codepaths
1196 * somewhat inefficient. Instead we use a rectangular point sprite. */
1197 static void r300_blitter_draw_rectangle(struct blitter_context *blitter,
1198 unsigned x1, unsigned y1,
1199 unsigned x2, unsigned y2,
1200 float depth,
1201 enum blitter_attrib_type type,
1202 const float attrib[4])
1203 {
1204 struct r300_context *r300 = r300_context(util_blitter_get_pipe(blitter));
1205 unsigned last_sprite_coord_enable = r300->sprite_coord_enable;
1206 unsigned width = x2 - x1;
1207 unsigned height = y2 - y1;
1208 unsigned vertex_size =
1209 type == UTIL_BLITTER_ATTRIB_COLOR || !r300->draw ? 8 : 4;
1210 unsigned dwords = 13 + vertex_size +
1211 (type == UTIL_BLITTER_ATTRIB_TEXCOORD ? 7 : 0);
1212 const float zeros[4] = {0, 0, 0, 0};
1213 CS_LOCALS(r300);
1214
1215 r300->context.set_vertex_buffers(&r300->context, 0, NULL);
1216
1217 if (type == UTIL_BLITTER_ATTRIB_TEXCOORD)
1218 r300->sprite_coord_enable = 1;
1219
1220 r300_update_derived_state(r300);
1221
1222 /* Mark some states we don't care about as non-dirty. */
1223 r300->clip_state.dirty = FALSE;
1224 r300->viewport_state.dirty = FALSE;
1225
1226 if (!r300_prepare_for_rendering(r300, PREP_EMIT_STATES, NULL, dwords, 0, 0, -1))
1227 goto done;
1228
1229 DBG(r300, DBG_DRAW, "r300: draw_rectangle\n");
1230
1231 BEGIN_CS(dwords);
1232 /* Set up GA. */
1233 OUT_CS_REG(R300_GA_POINT_SIZE, (height * 6) | ((width * 6) << 16));
1234
1235 if (type == UTIL_BLITTER_ATTRIB_TEXCOORD) {
1236 /* Set up the GA to generate texcoords. */
1237 OUT_CS_REG(R300_GB_ENABLE, R300_GB_POINT_STUFF_ENABLE |
1238 (R300_GB_TEX_STR << R300_GB_TEX0_SOURCE_SHIFT));
1239 OUT_CS_REG_SEQ(R300_GA_POINT_S0, 4);
1240 OUT_CS_32F(attrib[0]);
1241 OUT_CS_32F(attrib[3]);
1242 OUT_CS_32F(attrib[2]);
1243 OUT_CS_32F(attrib[1]);
1244 }
1245
1246 /* Set up VAP controls. */
1247 OUT_CS_REG(R300_VAP_CLIP_CNTL, R300_CLIP_DISABLE);
1248 OUT_CS_REG(R300_VAP_VTE_CNTL, R300_VTX_XY_FMT | R300_VTX_Z_FMT);
1249 OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
1250 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
1251 OUT_CS(1);
1252 OUT_CS(0);
1253
1254 /* Draw. */
1255 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, vertex_size);
1256 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (1 << 16) |
1257 R300_VAP_VF_CNTL__PRIM_POINTS);
1258
1259 OUT_CS_32F(x1 + width * 0.5f);
1260 OUT_CS_32F(y1 + height * 0.5f);
1261 OUT_CS_32F(depth);
1262 OUT_CS_32F(1);
1263
1264 if (vertex_size == 8) {
1265 if (!attrib)
1266 attrib = zeros;
1267 OUT_CS_TABLE(attrib, 4);
1268 }
1269 END_CS;
1270
1271 done:
1272 /* Restore the state. */
1273 r300_mark_atom_dirty(r300, &r300->clip_state);
1274 r300_mark_atom_dirty(r300, &r300->rs_state);
1275 r300_mark_atom_dirty(r300, &r300->viewport_state);
1276
1277 r300->sprite_coord_enable = last_sprite_coord_enable;
1278 }
1279
1280 static void r300_resource_resolve(struct pipe_context* pipe,
1281 struct pipe_resource* dest,
1282 unsigned dst_layer,
1283 struct pipe_resource* src,
1284 unsigned src_layer)
1285 {
1286 struct r300_context* r300 = r300_context(pipe);
1287 struct pipe_surface* srcsurf, surf_tmpl;
1288 struct r300_aa_state *aa = (struct r300_aa_state*)r300->aa_state.state;
1289 float color[] = {0, 0, 0, 0};
1290
1291 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1292 surf_tmpl.format = src->format;
1293 surf_tmpl.usage = 0; /* not really a surface hence no bind flags */
1294 surf_tmpl.u.tex.level = 0; /* msaa resources cannot have mipmaps */
1295 surf_tmpl.u.tex.first_layer = src_layer;
1296 surf_tmpl.u.tex.last_layer = src_layer;
1297 srcsurf = pipe->create_surface(pipe, src, &surf_tmpl);
1298 surf_tmpl.format = dest->format;
1299 surf_tmpl.u.tex.first_layer = dst_layer;
1300 surf_tmpl.u.tex.last_layer = dst_layer;
1301
1302 DBG(r300, DBG_DRAW, "r300: Resolving resource...\n");
1303
1304 /* Enable AA resolve. */
1305 aa->dest = r300_surface(pipe->create_surface(pipe, dest, &surf_tmpl));
1306
1307 aa->aaresolve_ctl =
1308 R300_RB3D_AARESOLVE_CTL_AARESOLVE_MODE_RESOLVE |
1309 R300_RB3D_AARESOLVE_CTL_AARESOLVE_ALPHA_AVERAGE;
1310 r300->aa_state.size = 10;
1311 r300_mark_atom_dirty(r300, &r300->aa_state);
1312
1313 /* Resolve the surface. */
1314 r300->context.clear_render_target(pipe,
1315 srcsurf, color, 0, 0, src->width0, src->height0);
1316
1317 /* Disable AA resolve. */
1318 aa->aaresolve_ctl = 0;
1319 r300->aa_state.size = 4;
1320 r300_mark_atom_dirty(r300, &r300->aa_state);
1321
1322 pipe_surface_reference((struct pipe_surface**)&srcsurf, NULL);
1323 pipe_surface_reference((struct pipe_surface**)&aa->dest, NULL);
1324 }
1325
1326 void r300_init_render_functions(struct r300_context *r300)
1327 {
1328 /* Set draw functions based on presence of HW TCL. */
1329 if (r300->screen->caps.has_tcl) {
1330 r300->context.draw_vbo = r300_draw_vbo;
1331 } else {
1332 r300->context.draw_vbo = r300_swtcl_draw_vbo;
1333 }
1334
1335 r300->context.resource_resolve = r300_resource_resolve;
1336 r300->blitter->draw_rectangle = r300_blitter_draw_rectangle;
1337
1338 /* Plug in the two-sided stencil reference value fallback if needed. */
1339 if (!r300->screen->caps.is_r500)
1340 r300_plug_in_stencil_ref_fallback(r300);
1341 }