gallium: add usage parameter to pipe_buffer_create
[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_FIRST_DRAW = (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_FIRST_DRAW;
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.index_bias_supported)
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 > (R300_MAX_CMDBUF_DWORDS - r300->cs->cdw)) {
222 r300->context.flush(&r300->context, 0, 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 * \return TRUE if rendering should be skipped
237 */
238 static boolean r300_emit_states(struct r300_context *r300,
239 enum r300_prepare_flags flags,
240 struct pipe_resource *index_buffer,
241 int buffer_offset,
242 int index_bias,
243 boolean user_buffers)
244 {
245 boolean first_draw = flags & PREP_FIRST_DRAW;
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->validate_buffers) {
254 if (!r300_emit_buffer_validate(r300, validate_vbos,
255 index_buffer)) {
256 fprintf(stderr, "r300: CS space validation failed. "
257 "(not enough memory?) Skipping rendering.\n");
258 return FALSE;
259 }
260
261 /* Consider the validation done only if everything was validated. */
262 if (validate_vbos) {
263 r300->validate_buffers = FALSE;
264 if (user_buffers)
265 r300->upload_vb_validated = TRUE;
266 if (r300->index_buffer.buffer &&
267 r300_resource(r300->index_buffer.buffer)->b.user_ptr) {
268 r300->upload_ib_validated = TRUE;
269 }
270 }
271 }
272
273 r300_emit_dirty_state(r300);
274 if (r300->screen->caps.index_bias_supported) {
275 if (r300->screen->caps.has_tcl)
276 r500_emit_index_bias(r300, index_bias);
277 else
278 r500_emit_index_bias(r300, 0);
279 }
280
281 if (emit_vertex_arrays &&
282 (r300->vertex_arrays_dirty ||
283 r300->vertex_arrays_indexed != indexed ||
284 r300->vertex_arrays_offset != buffer_offset)) {
285 r300_emit_vertex_arrays(r300, buffer_offset, indexed);
286
287 r300->vertex_arrays_dirty = FALSE;
288 r300->vertex_arrays_indexed = indexed;
289 r300->vertex_arrays_offset = buffer_offset;
290 }
291
292 if (emit_vertex_arrays_swtcl)
293 r300_emit_vertex_arrays_swtcl(r300, indexed);
294 }
295
296 return TRUE;
297 }
298
299 /**
300 * Check if the requested number of dwords is available in the CS and
301 * if not, flush. Then validate buffers and emit dirty state.
302 * \param r300 The context.
303 * \param flags See r300_prepare_flags.
304 * \param index_buffer The index buffer to validate. The parameter may be NULL.
305 * \param cs_dwords The number of dwords to reserve in CS.
306 * \param buffer_offset The offset passed to emit_vertex_arrays.
307 * \param index_bias The index bias to emit.
308 * \return TRUE if rendering should be skipped
309 */
310 static boolean r300_prepare_for_rendering(struct r300_context *r300,
311 enum r300_prepare_flags flags,
312 struct pipe_resource *index_buffer,
313 unsigned cs_dwords,
314 int buffer_offset,
315 int index_bias,
316 boolean user_buffers)
317 {
318 if (r300_reserve_cs_dwords(r300, flags, cs_dwords))
319 flags |= PREP_FIRST_DRAW;
320
321 return r300_emit_states(r300, flags, index_buffer, buffer_offset,
322 index_bias, user_buffers);
323 }
324
325 static boolean immd_is_good_idea(struct r300_context *r300,
326 unsigned count)
327 {
328 struct pipe_vertex_element* velem;
329 struct pipe_resource *buf;
330 boolean checked[PIPE_MAX_ATTRIBS] = {0};
331 unsigned vertex_element_count = r300->velems->count;
332 unsigned i, vbi;
333
334 if (DBG_ON(r300, DBG_NO_IMMD)) {
335 return FALSE;
336 }
337
338 if (r300->draw) {
339 return FALSE;
340 }
341
342 if (count * r300->velems->vertex_size_dwords > IMMD_DWORDS) {
343 return FALSE;
344 }
345
346 /* We shouldn't map buffers referenced by CS, busy buffers,
347 * and ones placed in VRAM. */
348 for (i = 0; i < vertex_element_count; i++) {
349 velem = &r300->velems->velem[i];
350 vbi = velem->vertex_buffer_index;
351
352 if (!checked[vbi]) {
353 buf = r300->vbuf_mgr->real_vertex_buffer[vbi];
354
355 if ((r300_resource(buf)->domain != R300_DOMAIN_GTT)) {
356 return FALSE;
357 }
358
359 checked[vbi] = TRUE;
360 }
361 }
362 return TRUE;
363 }
364
365 /*****************************************************************************
366 * The HWTCL draw functions. *
367 ****************************************************************************/
368
369 static void r300_draw_arrays_immediate(struct r300_context *r300,
370 unsigned mode, unsigned start,
371 unsigned count)
372 {
373 struct pipe_vertex_element* velem;
374 struct pipe_vertex_buffer* vbuf;
375 unsigned vertex_element_count = r300->velems->count;
376 unsigned i, v, vbi;
377
378 /* Size of the vertex, in dwords. */
379 unsigned vertex_size = r300->velems->vertex_size_dwords;
380
381 /* The number of dwords for this draw operation. */
382 unsigned dwords = 4 + count * vertex_size;
383
384 /* Size of the vertex element, in dwords. */
385 unsigned size[PIPE_MAX_ATTRIBS];
386
387 /* Stride to the same attrib in the next vertex in the vertex buffer,
388 * in dwords. */
389 unsigned stride[PIPE_MAX_ATTRIBS];
390
391 /* Mapped vertex buffers. */
392 uint32_t* map[PIPE_MAX_ATTRIBS] = {0};
393 uint32_t* mapelem[PIPE_MAX_ATTRIBS];
394
395 CS_LOCALS(r300);
396
397 if (!r300_prepare_for_rendering(r300, PREP_FIRST_DRAW, NULL, dwords, 0, 0,
398 FALSE))
399 return;
400
401 /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
402 for (i = 0; i < vertex_element_count; i++) {
403 velem = &r300->velems->velem[i];
404 size[i] = r300->velems->format_size[i] / 4;
405 vbi = velem->vertex_buffer_index;
406 vbuf = &r300->vbuf_mgr->vertex_buffer[vbi];
407 stride[i] = vbuf->stride / 4;
408
409 /* Map the buffer. */
410 if (!map[vbi]) {
411 map[vbi] = (uint32_t*)r300->rws->buffer_map(
412 r300_resource(r300->vbuf_mgr->real_vertex_buffer[vbi])->buf,
413 r300->cs, PIPE_TRANSFER_READ | PIPE_TRANSFER_UNSYNCHRONIZED);
414 map[vbi] += (vbuf->buffer_offset / 4) + stride[i] * start;
415 }
416 mapelem[i] = map[vbi] + (velem->src_offset / 4);
417 }
418
419 r300_emit_draw_init(r300, mode, 0, count-1);
420
421 BEGIN_CS(dwords);
422 OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
423 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, count * vertex_size);
424 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (count << 16) |
425 r300_translate_primitive(mode));
426
427 /* Emit vertices. */
428 for (v = 0; v < count; v++) {
429 for (i = 0; i < vertex_element_count; i++) {
430 OUT_CS_TABLE(&mapelem[i][stride[i] * v], size[i]);
431 }
432 }
433 END_CS;
434
435 /* Unmap buffers. */
436 for (i = 0; i < vertex_element_count; i++) {
437 vbi = r300->velems->velem[i].vertex_buffer_index;
438
439 if (map[vbi]) {
440 r300->rws->buffer_unmap(r300_resource(r300->vbuf_mgr->real_vertex_buffer[vbi])->buf);
441 map[vbi] = NULL;
442 }
443 }
444 }
445
446 static void r300_emit_draw_arrays(struct r300_context *r300,
447 unsigned mode,
448 unsigned count)
449 {
450 boolean alt_num_verts = count > 65535;
451 CS_LOCALS(r300);
452
453 if (count >= (1 << 24)) {
454 fprintf(stderr, "r300: Got a huge number of vertices: %i, "
455 "refusing to render.\n", count);
456 return;
457 }
458
459 r300_emit_draw_init(r300, mode, 0, count-1);
460
461 BEGIN_CS(2 + (alt_num_verts ? 2 : 0));
462 if (alt_num_verts) {
463 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
464 }
465 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
466 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
467 r300_translate_primitive(mode) |
468 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
469 END_CS;
470 }
471
472 static void r300_emit_draw_elements(struct r300_context *r300,
473 struct pipe_resource* indexBuffer,
474 unsigned indexSize,
475 unsigned minIndex,
476 unsigned maxIndex,
477 unsigned mode,
478 unsigned start,
479 unsigned count,
480 uint16_t *imm_indices3)
481 {
482 uint32_t count_dwords, offset_dwords;
483 boolean alt_num_verts = count > 65535;
484 CS_LOCALS(r300);
485
486 if (count >= (1 << 24) || maxIndex >= (1 << 24)) {
487 fprintf(stderr, "r300: Got a huge number of vertices: %i, "
488 "refusing to render (maxIndex: %i).\n", count, maxIndex);
489 return;
490 }
491
492 DBG(r300, DBG_DRAW, "r300: Indexbuf of %u indices, min %u max %u\n",
493 count, minIndex, maxIndex);
494
495 r300_emit_draw_init(r300, mode, minIndex, maxIndex);
496
497 /* If start is odd, render the first triangle with indices embedded
498 * in the command stream. This will increase start by 3 and make it
499 * even. We can then proceed without a fallback. */
500 if (indexSize == 2 && (start & 1) &&
501 mode == PIPE_PRIM_TRIANGLES) {
502 BEGIN_CS(4);
503 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 2);
504 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (3 << 16) |
505 R300_VAP_VF_CNTL__PRIM_TRIANGLES);
506 OUT_CS(imm_indices3[1] << 16 | imm_indices3[0]);
507 OUT_CS(imm_indices3[2]);
508 END_CS;
509
510 start += 3;
511 count -= 3;
512 if (!count)
513 return;
514 }
515
516 offset_dwords = indexSize * start / sizeof(uint32_t);
517
518 BEGIN_CS(8 + (alt_num_verts ? 2 : 0));
519 if (alt_num_verts) {
520 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
521 }
522 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);
523 if (indexSize == 4) {
524 count_dwords = count;
525 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
526 R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
527 r300_translate_primitive(mode) |
528 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
529 } else {
530 count_dwords = (count + 1) / 2;
531 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
532 r300_translate_primitive(mode) |
533 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
534 }
535
536 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2);
537 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) |
538 (0 << R300_INDX_BUFFER_SKIP_SHIFT));
539 OUT_CS(offset_dwords << 2);
540 OUT_CS(count_dwords);
541 OUT_CS_RELOC(r300_resource(indexBuffer));
542 END_CS;
543 }
544
545 static void r300_draw_elements_immediate(struct r300_context *r300,
546 int indexBias, unsigned minIndex,
547 unsigned maxIndex, unsigned mode,
548 unsigned start, unsigned count,
549 boolean user_buffers)
550 {
551 uint8_t *ptr1;
552 uint16_t *ptr2;
553 uint32_t *ptr4;
554 unsigned index_size = r300->index_buffer.index_size;
555 unsigned i, count_dwords = index_size == 4 ? count : (count + 1) / 2;
556 CS_LOCALS(r300);
557
558 /* 19 dwords for r300_draw_elements_immediate. Give up if the function fails. */
559 if (!r300_prepare_for_rendering(r300,
560 PREP_FIRST_DRAW | PREP_VALIDATE_VBOS | PREP_EMIT_AOS |
561 PREP_INDEXED, NULL, 2+count_dwords, 0, indexBias,
562 user_buffers))
563 return;
564
565 r300_emit_draw_init(r300, mode, minIndex, maxIndex);
566
567 BEGIN_CS(2 + count_dwords);
568 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, count_dwords);
569
570 switch (index_size) {
571 case 1:
572 ptr1 = r300_resource(r300->index_buffer.buffer)->b.user_ptr;
573 ptr1 += start;
574
575 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
576 r300_translate_primitive(mode));
577
578 if (indexBias && !r300->screen->caps.index_bias_supported) {
579 for (i = 0; i < count-1; i += 2)
580 OUT_CS(((ptr1[i+1] + indexBias) << 16) |
581 (ptr1[i] + indexBias));
582
583 if (count & 1)
584 OUT_CS(ptr1[i] + indexBias);
585 } else {
586 for (i = 0; i < count-1; i += 2)
587 OUT_CS(((ptr1[i+1]) << 16) |
588 (ptr1[i] ));
589
590 if (count & 1)
591 OUT_CS(ptr1[i]);
592 }
593 break;
594
595 case 2:
596 ptr2 = (uint16_t*)r300_resource(r300->index_buffer.buffer)->b.user_ptr;
597 ptr2 += start;
598
599 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
600 r300_translate_primitive(mode));
601
602 if (indexBias && !r300->screen->caps.index_bias_supported) {
603 for (i = 0; i < count-1; i += 2)
604 OUT_CS(((ptr2[i+1] + indexBias) << 16) |
605 (ptr2[i] + indexBias));
606
607 if (count & 1)
608 OUT_CS(ptr2[i] + indexBias);
609 } else {
610 OUT_CS_TABLE(ptr2, count_dwords);
611 }
612 break;
613
614 case 4:
615 ptr4 = (uint32_t*)r300_resource(r300->index_buffer.buffer)->b.user_ptr;
616 ptr4 += start;
617
618 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
619 R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
620 r300_translate_primitive(mode));
621
622 if (indexBias && !r300->screen->caps.index_bias_supported) {
623 for (i = 0; i < count; i++)
624 OUT_CS(ptr4[i] + indexBias);
625 } else {
626 OUT_CS_TABLE(ptr4, count_dwords);
627 }
628 break;
629 }
630 END_CS;
631 }
632
633 static void r300_draw_elements(struct r300_context *r300, int indexBias,
634 unsigned minIndex, unsigned maxIndex,
635 unsigned mode, unsigned start, unsigned count,
636 boolean user_buffers)
637 {
638 struct pipe_resource *indexBuffer = r300->index_buffer.buffer;
639 unsigned indexSize = r300->index_buffer.index_size;
640 struct pipe_resource* orgIndexBuffer = indexBuffer;
641 boolean alt_num_verts = r300->screen->caps.is_r500 &&
642 count > 65536 &&
643 r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0);
644 unsigned short_count;
645 int buffer_offset = 0, index_offset = 0; /* for index bias emulation */
646 uint16_t indices3[3];
647
648 if (indexBias && !r300->screen->caps.index_bias_supported) {
649 r300_split_index_bias(r300, indexBias, &buffer_offset, &index_offset);
650 }
651
652 r300_translate_index_buffer(r300, &indexBuffer, &indexSize, index_offset,
653 &start, count);
654
655 /* Fallback for misaligned ushort indices. */
656 if (indexSize == 2 && (start & 1) &&
657 !r300_resource(indexBuffer)->b.user_ptr) {
658 /* If we got here, then orgIndexBuffer == indexBuffer. */
659 uint16_t *ptr = r300->rws->buffer_map(r300_resource(orgIndexBuffer)->buf,
660 r300->cs,
661 PIPE_TRANSFER_READ |
662 PIPE_TRANSFER_UNSYNCHRONIZED);
663
664 if (mode == PIPE_PRIM_TRIANGLES) {
665 memcpy(indices3, ptr + start, 6);
666 } else {
667 /* Copy the mapped index buffer directly to the upload buffer.
668 * The start index will be aligned simply from the fact that
669 * every sub-buffer in the upload buffer is aligned. */
670 r300_upload_index_buffer(r300, &indexBuffer, indexSize, &start,
671 count, (uint8_t*)ptr);
672 }
673 r300->rws->buffer_unmap(r300_resource(orgIndexBuffer)->buf);
674 } else {
675 if (r300_resource(indexBuffer)->b.user_ptr)
676 r300_upload_index_buffer(r300, &indexBuffer, indexSize,
677 &start, count,
678 r300_resource(indexBuffer)->b.user_ptr);
679 }
680
681 /* 19 dwords for emit_draw_elements. Give up if the function fails. */
682 if (!r300_prepare_for_rendering(r300,
683 PREP_FIRST_DRAW | PREP_VALIDATE_VBOS | PREP_EMIT_AOS |
684 PREP_INDEXED, indexBuffer, 19, buffer_offset, indexBias,
685 user_buffers))
686 goto done;
687
688 if (alt_num_verts || count <= 65535) {
689 r300_emit_draw_elements(r300, indexBuffer, indexSize,
690 minIndex, maxIndex, mode, start, count, indices3);
691 } else {
692 do {
693 if (indexSize == 2 && (start & 1))
694 short_count = MIN2(count, 65535);
695 else
696 short_count = MIN2(count, 65534);
697
698 r300_emit_draw_elements(r300, indexBuffer, indexSize,
699 minIndex, maxIndex,
700 mode, start, short_count, indices3);
701
702 start += short_count;
703 count -= short_count;
704
705 /* 15 dwords for emit_draw_elements */
706 if (count) {
707 if (!r300_prepare_for_rendering(r300,
708 PREP_VALIDATE_VBOS | PREP_EMIT_AOS | PREP_INDEXED,
709 indexBuffer, 19, buffer_offset, indexBias, user_buffers))
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, unsigned mode,
722 unsigned start, unsigned count,
723 boolean user_buffers)
724 {
725 boolean alt_num_verts = r300->screen->caps.is_r500 &&
726 count > 65536 &&
727 r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0);
728 unsigned short_count;
729
730 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
731 if (!r300_prepare_for_rendering(r300,
732 PREP_FIRST_DRAW | PREP_VALIDATE_VBOS | PREP_EMIT_AOS,
733 NULL, 9, start, 0, user_buffers))
734 return;
735
736 if (alt_num_verts || count <= 65535) {
737 r300_emit_draw_arrays(r300, mode, count);
738 } else {
739 do {
740 short_count = MIN2(count, 65535);
741 r300_emit_draw_arrays(r300, mode, short_count);
742
743 start += short_count;
744 count -= short_count;
745
746 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
747 if (count) {
748 if (!r300_prepare_for_rendering(r300,
749 PREP_VALIDATE_VBOS | PREP_EMIT_AOS, NULL, 9,
750 start, 0, user_buffers))
751 return;
752 }
753 } while (count);
754 }
755 }
756
757 static void r300_draw_vbo(struct pipe_context* pipe,
758 const struct pipe_draw_info *info)
759 {
760 struct r300_context* r300 = r300_context(pipe);
761 unsigned count = info->count;
762 boolean buffers_updated, uploader_flushed;
763 boolean indexed = info->indexed && r300->index_buffer.buffer;
764 unsigned start_indexed = info->start + r300->index_buffer.offset;
765 int max_index = MIN2(r300->vbuf_mgr->max_index, info->max_index);
766
767 if (r300->skip_rendering ||
768 !u_trim_pipe_prim(info->mode, &count)) {
769 return;
770 }
771
772 /* Start the vbuf manager and update buffers if needed. */
773 u_vbuf_mgr_draw_begin(r300->vbuf_mgr, info,
774 &buffers_updated, &uploader_flushed);
775 if (buffers_updated) {
776 r300->vertex_arrays_dirty = TRUE;
777
778 if (uploader_flushed || !r300->upload_vb_validated) {
779 r300->upload_vb_validated = FALSE;
780 r300->validate_buffers = TRUE;
781 }
782 } else {
783 r300->upload_vb_validated = FALSE;
784 }
785
786 /* Draw. */
787 r300_update_derived_state(r300);
788
789 if (indexed) {
790 if (count <= 8 &&
791 r300_resource(r300->index_buffer.buffer)->b.user_ptr) {
792 r300_draw_elements_immediate(r300, info->index_bias,
793 info->min_index, max_index,
794 info->mode, start_indexed, count,
795 buffers_updated);
796 } else {
797 r300_draw_elements(r300, info->index_bias, info->min_index,
798 max_index, info->mode, start_indexed, count,
799 buffers_updated);
800 }
801 } else {
802 if (immd_is_good_idea(r300, count)) {
803 r300_draw_arrays_immediate(r300, info->mode, info->start, count);
804 } else {
805 r300_draw_arrays(r300, info->mode, info->start, count,
806 buffers_updated);
807 }
808 }
809
810 u_vbuf_mgr_draw_end(r300->vbuf_mgr);
811 }
812
813 /****************************************************************************
814 * The rest of this file is for SW TCL rendering only. Please be polite and *
815 * keep these functions separated so that they are easier to locate. ~C. *
816 ***************************************************************************/
817
818 /* SW TCL elements, using Draw. */
819 static void r300_swtcl_draw_vbo(struct pipe_context* pipe,
820 const struct pipe_draw_info *info)
821 {
822 struct r300_context* r300 = r300_context(pipe);
823 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS];
824 struct pipe_transfer *ib_transfer = NULL;
825 unsigned count = info->count;
826 int i;
827 void *indices = NULL;
828 boolean indexed = info->indexed && r300->index_buffer.buffer;
829
830 if (r300->skip_rendering) {
831 return;
832 }
833
834 if (!u_trim_pipe_prim(info->mode, &count)) {
835 return;
836 }
837
838 r300_update_derived_state(r300);
839
840 r300_reserve_cs_dwords(r300,
841 PREP_FIRST_DRAW | PREP_EMIT_AOS_SWTCL |
842 (indexed ? PREP_INDEXED : 0),
843 indexed ? 256 : 6);
844
845 for (i = 0; i < r300->vbuf_mgr->nr_vertex_buffers; i++) {
846 if (r300->vbuf_mgr->vertex_buffer[i].buffer) {
847 void *buf = pipe_buffer_map(pipe,
848 r300->vbuf_mgr->vertex_buffer[i].buffer,
849 PIPE_TRANSFER_READ |
850 PIPE_TRANSFER_UNSYNCHRONIZED,
851 &vb_transfer[i]);
852 draw_set_mapped_vertex_buffer(r300->draw, i, buf);
853 }
854 }
855
856 if (indexed) {
857 indices = pipe_buffer_map(pipe, r300->index_buffer.buffer,
858 PIPE_TRANSFER_READ |
859 PIPE_TRANSFER_UNSYNCHRONIZED, &ib_transfer);
860 }
861
862 draw_set_mapped_index_buffer(r300->draw, indices);
863
864 r300->draw_vbo_locked = TRUE;
865 r300->draw_first_emitted = FALSE;
866 draw_vbo(r300->draw, info);
867 draw_flush(r300->draw);
868 r300->draw_vbo_locked = FALSE;
869
870 for (i = 0; i < r300->vbuf_mgr->nr_vertex_buffers; i++) {
871 if (r300->vbuf_mgr->vertex_buffer[i].buffer) {
872 pipe_buffer_unmap(pipe, vb_transfer[i]);
873 draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
874 }
875 }
876
877 if (indexed) {
878 pipe_buffer_unmap(pipe, ib_transfer);
879 draw_set_mapped_index_buffer(r300->draw, NULL);
880 }
881 }
882
883 /* Object for rendering using Draw. */
884 struct r300_render {
885 /* Parent class */
886 struct vbuf_render base;
887
888 /* Pipe context */
889 struct r300_context* r300;
890
891 /* Vertex information */
892 size_t vertex_size;
893 unsigned prim;
894 unsigned hwprim;
895
896 /* VBO */
897 size_t vbo_max_used;
898 void * vbo_ptr;
899
900 struct pipe_transfer *vbo_transfer;
901 };
902
903 static INLINE struct r300_render*
904 r300_render(struct vbuf_render* render)
905 {
906 return (struct r300_render*)render;
907 }
908
909 static const struct vertex_info*
910 r300_render_get_vertex_info(struct vbuf_render* render)
911 {
912 struct r300_render* r300render = r300_render(render);
913 struct r300_context* r300 = r300render->r300;
914
915 return &r300->vertex_info;
916 }
917
918 static boolean r300_render_allocate_vertices(struct vbuf_render* render,
919 ushort vertex_size,
920 ushort count)
921 {
922 struct r300_render* r300render = r300_render(render);
923 struct r300_context* r300 = r300render->r300;
924 struct pipe_screen* screen = r300->context.screen;
925 size_t size = (size_t)vertex_size * (size_t)count;
926
927 DBG(r300, DBG_DRAW, "r300: render_allocate_vertices (size: %d)\n", size);
928
929 if (size + r300->draw_vbo_offset > r300->draw_vbo_size)
930 {
931 pipe_resource_reference(&r300->vbo, NULL);
932 r300->vbo = pipe_buffer_create(screen,
933 PIPE_BIND_VERTEX_BUFFER,
934 PIPE_USAGE_STREAM,
935 R300_MAX_DRAW_VBO_SIZE);
936 r300->draw_vbo_offset = 0;
937 r300->draw_vbo_size = R300_MAX_DRAW_VBO_SIZE;
938 r300->validate_buffers = TRUE;
939 }
940
941 r300render->vertex_size = vertex_size;
942
943 return (r300->vbo) ? TRUE : FALSE;
944 }
945
946 static void* r300_render_map_vertices(struct vbuf_render* render)
947 {
948 struct r300_render* r300render = r300_render(render);
949 struct r300_context* r300 = r300render->r300;
950
951 assert(!r300render->vbo_transfer);
952
953 DBG(r300, DBG_DRAW, "r300: render_map_vertices\n");
954
955 r300render->vbo_ptr = pipe_buffer_map(&r300render->r300->context,
956 r300->vbo,
957 PIPE_TRANSFER_WRITE |
958 PIPE_TRANSFER_UNSYNCHRONIZED,
959 &r300render->vbo_transfer);
960
961 assert(r300render->vbo_ptr);
962
963 return ((uint8_t*)r300render->vbo_ptr + r300->draw_vbo_offset);
964 }
965
966 static void r300_render_unmap_vertices(struct vbuf_render* render,
967 ushort min,
968 ushort max)
969 {
970 struct r300_render* r300render = r300_render(render);
971 struct pipe_context* context = &r300render->r300->context;
972 struct r300_context* r300 = r300render->r300;
973
974 assert(r300render->vbo_transfer);
975
976 DBG(r300, DBG_DRAW, "r300: render_unmap_vertices\n");
977
978 r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
979 r300render->vertex_size * (max + 1));
980 pipe_buffer_unmap(context, r300render->vbo_transfer);
981
982 r300render->vbo_transfer = NULL;
983 }
984
985 static void r300_render_release_vertices(struct vbuf_render* render)
986 {
987 struct r300_render* r300render = r300_render(render);
988 struct r300_context* r300 = r300render->r300;
989
990 DBG(r300, DBG_DRAW, "r300: render_release_vertices\n");
991
992 r300->draw_vbo_offset += r300render->vbo_max_used;
993 r300render->vbo_max_used = 0;
994 }
995
996 static boolean r300_render_set_primitive(struct vbuf_render* render,
997 unsigned prim)
998 {
999 struct r300_render* r300render = r300_render(render);
1000
1001 r300render->prim = prim;
1002 r300render->hwprim = r300_translate_primitive(prim);
1003
1004 return TRUE;
1005 }
1006
1007 static void r300_render_draw_arrays(struct vbuf_render* render,
1008 unsigned start,
1009 unsigned count)
1010 {
1011 struct r300_render* r300render = r300_render(render);
1012 struct r300_context* r300 = r300render->r300;
1013 uint8_t* ptr;
1014 unsigned i;
1015 unsigned dwords = 6;
1016
1017 CS_LOCALS(r300);
1018 (void) i; (void) ptr;
1019
1020 DBG(r300, DBG_DRAW, "r300: render_draw_arrays (count: %d)\n", count);
1021
1022 if (r300->draw_first_emitted) {
1023 if (!r300_prepare_for_rendering(r300,
1024 PREP_FIRST_DRAW | PREP_EMIT_AOS_SWTCL,
1025 NULL, dwords, 0, 0, FALSE))
1026 return;
1027 } else {
1028 if (!r300_emit_states(r300,
1029 PREP_FIRST_DRAW | PREP_EMIT_AOS_SWTCL,
1030 NULL, 0, 0, FALSE))
1031 return;
1032 }
1033
1034 BEGIN_CS(dwords);
1035 OUT_CS_REG(R300_GA_COLOR_CONTROL,
1036 r300_provoking_vertex_fixes(r300, r300render->prim));
1037 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);
1038 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
1039 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
1040 r300render->hwprim);
1041 END_CS;
1042
1043 r300->draw_first_emitted = TRUE;
1044 }
1045
1046 static void r300_render_draw_elements(struct vbuf_render* render,
1047 const ushort* indices,
1048 uint count)
1049 {
1050 struct r300_render* r300render = r300_render(render);
1051 struct r300_context* r300 = r300render->r300;
1052 int i;
1053 unsigned end_cs_dwords;
1054 unsigned max_index = (r300->draw_vbo_size - r300->draw_vbo_offset) /
1055 (r300render->r300->vertex_info.size * 4) - 1;
1056 unsigned short_count;
1057 unsigned free_dwords;
1058
1059 CS_LOCALS(r300);
1060 DBG(r300, DBG_DRAW, "r300: render_draw_elements (count: %d)\n", count);
1061
1062 if (r300->draw_first_emitted) {
1063 if (!r300_prepare_for_rendering(r300,
1064 PREP_FIRST_DRAW | PREP_EMIT_AOS_SWTCL | PREP_INDEXED,
1065 NULL, 256, 0, 0, FALSE))
1066 return;
1067 } else {
1068 if (!r300_emit_states(r300,
1069 PREP_FIRST_DRAW | PREP_EMIT_AOS_SWTCL | PREP_INDEXED,
1070 NULL, 0, 0, FALSE))
1071 return;
1072 }
1073
1074 /* Below we manage the CS space manually because there may be more
1075 * indices than it can fit in CS. */
1076
1077 end_cs_dwords = r300_get_num_cs_end_dwords(r300);
1078
1079 while (count) {
1080 free_dwords = R300_MAX_CMDBUF_DWORDS - r300->cs->cdw;
1081
1082 short_count = MIN2(count, (free_dwords - end_cs_dwords - 6) * 2);
1083
1084 BEGIN_CS(6 + (short_count+1)/2);
1085 OUT_CS_REG(R300_GA_COLOR_CONTROL,
1086 r300_provoking_vertex_fixes(r300, r300render->prim));
1087 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max_index);
1088 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (short_count+1)/2);
1089 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (short_count << 16) |
1090 r300render->hwprim);
1091 for (i = 0; i < short_count-1; i += 2) {
1092 OUT_CS(indices[i+1] << 16 | indices[i]);
1093 }
1094 if (short_count % 2) {
1095 OUT_CS(indices[short_count-1]);
1096 }
1097 END_CS;
1098
1099 /* OK now subtract the emitted indices and see if we need to emit
1100 * another draw packet. */
1101 indices += short_count;
1102 count -= short_count;
1103
1104 if (count) {
1105 if (!r300_prepare_for_rendering(r300,
1106 PREP_EMIT_AOS_SWTCL | PREP_INDEXED,
1107 NULL, 256, 0, 0, FALSE))
1108 return;
1109
1110 end_cs_dwords = r300_get_num_cs_end_dwords(r300);
1111 }
1112 }
1113
1114 r300->draw_first_emitted = TRUE;
1115 }
1116
1117 static void r300_render_destroy(struct vbuf_render* render)
1118 {
1119 FREE(render);
1120 }
1121
1122 static struct vbuf_render* r300_render_create(struct r300_context* r300)
1123 {
1124 struct r300_render* r300render = CALLOC_STRUCT(r300_render);
1125
1126 r300render->r300 = r300;
1127
1128 r300render->base.max_vertex_buffer_bytes = 1024 * 1024;
1129 r300render->base.max_indices = 16 * 1024;
1130
1131 r300render->base.get_vertex_info = r300_render_get_vertex_info;
1132 r300render->base.allocate_vertices = r300_render_allocate_vertices;
1133 r300render->base.map_vertices = r300_render_map_vertices;
1134 r300render->base.unmap_vertices = r300_render_unmap_vertices;
1135 r300render->base.set_primitive = r300_render_set_primitive;
1136 r300render->base.draw_elements = r300_render_draw_elements;
1137 r300render->base.draw_arrays = r300_render_draw_arrays;
1138 r300render->base.release_vertices = r300_render_release_vertices;
1139 r300render->base.destroy = r300_render_destroy;
1140
1141 return &r300render->base;
1142 }
1143
1144 struct draw_stage* r300_draw_stage(struct r300_context* r300)
1145 {
1146 struct vbuf_render* render;
1147 struct draw_stage* stage;
1148
1149 render = r300_render_create(r300);
1150
1151 if (!render) {
1152 return NULL;
1153 }
1154
1155 stage = draw_vbuf_stage(r300->draw, render);
1156
1157 if (!stage) {
1158 render->destroy(render);
1159 return NULL;
1160 }
1161
1162 draw_set_render(r300->draw, render);
1163
1164 return stage;
1165 }
1166
1167 void r300_draw_flush_vbuf(struct r300_context *r300)
1168 {
1169 pipe_resource_reference(&r300->vbo, NULL);
1170 r300->draw_vbo_size = 0;
1171 }
1172
1173 /****************************************************************************
1174 * End of SW TCL functions *
1175 ***************************************************************************/
1176
1177 /* This functions is used to draw a rectangle for the blitter module.
1178 *
1179 * If we rendered a quad, the pixels on the main diagonal
1180 * would be computed and stored twice, which makes the clear/copy codepaths
1181 * somewhat inefficient. Instead we use a rectangular point sprite. */
1182 static void r300_blitter_draw_rectangle(struct blitter_context *blitter,
1183 unsigned x1, unsigned y1,
1184 unsigned x2, unsigned y2,
1185 float depth,
1186 enum blitter_attrib_type type,
1187 const float attrib[4])
1188 {
1189 struct r300_context *r300 = r300_context(util_blitter_get_pipe(blitter));
1190 unsigned last_sprite_coord_enable = r300->sprite_coord_enable;
1191 unsigned width = x2 - x1;
1192 unsigned height = y2 - y1;
1193 unsigned vertex_size =
1194 type == UTIL_BLITTER_ATTRIB_COLOR || !r300->draw ? 8 : 4;
1195 unsigned dwords = 13 + vertex_size +
1196 (type == UTIL_BLITTER_ATTRIB_TEXCOORD ? 7 : 0);
1197 const float zeros[4] = {0, 0, 0, 0};
1198 CS_LOCALS(r300);
1199
1200 r300->context.set_vertex_buffers(&r300->context, 0, NULL);
1201
1202 if (type == UTIL_BLITTER_ATTRIB_TEXCOORD)
1203 r300->sprite_coord_enable = 1;
1204
1205 r300_update_derived_state(r300);
1206
1207 /* Mark some states we don't care about as non-dirty. */
1208 r300->clip_state.dirty = FALSE;
1209 r300->viewport_state.dirty = FALSE;
1210
1211 if (!r300_prepare_for_rendering(r300, PREP_FIRST_DRAW, NULL, dwords, 0, 0,
1212 FALSE))
1213 goto done;
1214
1215 DBG(r300, DBG_DRAW, "r300: draw_rectangle\n");
1216
1217 BEGIN_CS(dwords);
1218 /* Set up GA. */
1219 OUT_CS_REG(R300_GA_POINT_SIZE, (height * 6) | ((width * 6) << 16));
1220
1221 if (type == UTIL_BLITTER_ATTRIB_TEXCOORD) {
1222 /* Set up the GA to generate texcoords. */
1223 OUT_CS_REG(R300_GB_ENABLE, R300_GB_POINT_STUFF_ENABLE |
1224 (R300_GB_TEX_STR << R300_GB_TEX0_SOURCE_SHIFT));
1225 OUT_CS_REG_SEQ(R300_GA_POINT_S0, 4);
1226 OUT_CS_32F(attrib[0]);
1227 OUT_CS_32F(attrib[3]);
1228 OUT_CS_32F(attrib[2]);
1229 OUT_CS_32F(attrib[1]);
1230 }
1231
1232 /* Set up VAP controls. */
1233 OUT_CS_REG(R300_VAP_CLIP_CNTL, R300_CLIP_DISABLE);
1234 OUT_CS_REG(R300_VAP_VTE_CNTL, R300_VTX_XY_FMT | R300_VTX_Z_FMT);
1235 OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
1236 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
1237 OUT_CS(1);
1238 OUT_CS(0);
1239
1240 /* Draw. */
1241 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, vertex_size);
1242 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (1 << 16) |
1243 R300_VAP_VF_CNTL__PRIM_POINTS);
1244
1245 OUT_CS_32F(x1 + width * 0.5f);
1246 OUT_CS_32F(y1 + height * 0.5f);
1247 OUT_CS_32F(depth);
1248 OUT_CS_32F(1);
1249
1250 if (vertex_size == 8) {
1251 if (!attrib)
1252 attrib = zeros;
1253 OUT_CS_TABLE(attrib, 4);
1254 }
1255 END_CS;
1256
1257 done:
1258 /* Restore the state. */
1259 r300_mark_atom_dirty(r300, &r300->clip_state);
1260 r300_mark_atom_dirty(r300, &r300->rs_state);
1261 r300_mark_atom_dirty(r300, &r300->viewport_state);
1262
1263 r300->sprite_coord_enable = last_sprite_coord_enable;
1264 }
1265
1266 static void r300_resource_resolve(struct pipe_context* pipe,
1267 struct pipe_resource* dest,
1268 unsigned dst_layer,
1269 struct pipe_resource* src,
1270 unsigned src_layer)
1271 {
1272 struct r300_context* r300 = r300_context(pipe);
1273 struct pipe_surface* srcsurf, surf_tmpl;
1274 struct r300_aa_state *aa = (struct r300_aa_state*)r300->aa_state.state;
1275 float color[] = {0, 0, 0, 0};
1276
1277 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1278 surf_tmpl.format = src->format;
1279 surf_tmpl.usage = 0; /* not really a surface hence no bind flags */
1280 surf_tmpl.u.tex.level = 0; /* msaa resources cannot have mipmaps */
1281 surf_tmpl.u.tex.first_layer = src_layer;
1282 surf_tmpl.u.tex.last_layer = src_layer;
1283 srcsurf = pipe->create_surface(pipe, src, &surf_tmpl);
1284 surf_tmpl.format = dest->format;
1285 surf_tmpl.u.tex.first_layer = dst_layer;
1286 surf_tmpl.u.tex.last_layer = dst_layer;
1287
1288 DBG(r300, DBG_DRAW, "r300: Resolving resource...\n");
1289
1290 /* Enable AA resolve. */
1291 aa->dest = r300_surface(pipe->create_surface(pipe, dest, &surf_tmpl));
1292
1293 aa->aaresolve_ctl =
1294 R300_RB3D_AARESOLVE_CTL_AARESOLVE_MODE_RESOLVE |
1295 R300_RB3D_AARESOLVE_CTL_AARESOLVE_ALPHA_AVERAGE;
1296 r300->aa_state.size = 10;
1297 r300_mark_atom_dirty(r300, &r300->aa_state);
1298
1299 /* Resolve the surface. */
1300 r300->context.clear_render_target(pipe,
1301 srcsurf, color, 0, 0, src->width0, src->height0);
1302
1303 /* Disable AA resolve. */
1304 aa->aaresolve_ctl = 0;
1305 r300->aa_state.size = 4;
1306 r300_mark_atom_dirty(r300, &r300->aa_state);
1307
1308 pipe_surface_reference((struct pipe_surface**)&srcsurf, NULL);
1309 pipe_surface_reference((struct pipe_surface**)&aa->dest, NULL);
1310 }
1311
1312 void r300_init_render_functions(struct r300_context *r300)
1313 {
1314 /* Set draw functions based on presence of HW TCL. */
1315 if (r300->screen->caps.has_tcl) {
1316 r300->context.draw_vbo = r300_draw_vbo;
1317 } else {
1318 r300->context.draw_vbo = r300_swtcl_draw_vbo;
1319 }
1320
1321 r300->context.resource_resolve = r300_resource_resolve;
1322 r300->blitter->draw_rectangle = r300_blitter_draw_rectangle;
1323
1324 /* Plug in the two-sided stencil reference value fallback if needed. */
1325 if (!r300->screen->caps.is_r500)
1326 r300_plug_in_stencil_ref_fallback(r300);
1327 }