aa45ee3abfd0ce4ed6bd88229379c4251fe56870
[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 #include "r300_state_derived.h"
43
44 #include <limits.h>
45
46 #define IMMD_DWORDS 32
47
48 static uint32_t r300_translate_primitive(unsigned prim)
49 {
50 switch (prim) {
51 case PIPE_PRIM_POINTS:
52 return R300_VAP_VF_CNTL__PRIM_POINTS;
53 case PIPE_PRIM_LINES:
54 return R300_VAP_VF_CNTL__PRIM_LINES;
55 case PIPE_PRIM_LINE_LOOP:
56 return R300_VAP_VF_CNTL__PRIM_LINE_LOOP;
57 case PIPE_PRIM_LINE_STRIP:
58 return R300_VAP_VF_CNTL__PRIM_LINE_STRIP;
59 case PIPE_PRIM_TRIANGLES:
60 return R300_VAP_VF_CNTL__PRIM_TRIANGLES;
61 case PIPE_PRIM_TRIANGLE_STRIP:
62 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP;
63 case PIPE_PRIM_TRIANGLE_FAN:
64 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN;
65 case PIPE_PRIM_QUADS:
66 return R300_VAP_VF_CNTL__PRIM_QUADS;
67 case PIPE_PRIM_QUAD_STRIP:
68 return R300_VAP_VF_CNTL__PRIM_QUAD_STRIP;
69 case PIPE_PRIM_POLYGON:
70 return R300_VAP_VF_CNTL__PRIM_POLYGON;
71 default:
72 return 0;
73 }
74 }
75
76 static uint32_t r300_provoking_vertex_fixes(struct r300_context *r300,
77 unsigned mode)
78 {
79 struct r300_rs_state* rs = (struct r300_rs_state*)r300->rs_state.state;
80 uint32_t color_control = rs->color_control;
81
82 /* By default (see r300_state.c:r300_create_rs_state) color_control is
83 * initialized to provoking the first vertex.
84 *
85 * Triangle fans must be reduced to the second vertex, not the first, in
86 * Gallium flatshade-first mode, as per the GL spec.
87 * (http://www.opengl.org/registry/specs/ARB/provoking_vertex.txt)
88 *
89 * Quads never provoke correctly in flatshade-first mode. The first
90 * vertex is never considered as provoking, so only the second, third,
91 * and fourth vertices can be selected, and both "third" and "last" modes
92 * select the fourth vertex. This is probably due to D3D lacking quads.
93 *
94 * Similarly, polygons reduce to the first, not the last, vertex, when in
95 * "last" mode, and all other modes start from the second vertex.
96 *
97 * ~ C.
98 */
99
100 if (rs->rs.flatshade_first) {
101 switch (mode) {
102 case PIPE_PRIM_TRIANGLE_FAN:
103 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND;
104 break;
105 case PIPE_PRIM_QUADS:
106 case PIPE_PRIM_QUAD_STRIP:
107 case PIPE_PRIM_POLYGON:
108 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
109 break;
110 default:
111 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST;
112 break;
113 }
114 } else {
115 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
116 }
117
118 return color_control;
119 }
120
121 boolean r500_index_bias_supported(struct r300_context *r300)
122 {
123 return r300->screen->caps.is_r500 &&
124 r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0);
125 }
126
127 void r500_emit_index_bias(struct r300_context *r300, int index_bias)
128 {
129 CS_LOCALS(r300);
130
131 BEGIN_CS(2);
132 OUT_CS_REG(R500_VAP_INDEX_OFFSET,
133 (index_bias & 0xFFFFFF) | (index_bias < 0 ? 1<<24 : 0));
134 END_CS;
135 }
136
137 /* This function splits the index bias value into two parts:
138 * - buffer_offset: the value that can be safely added to buffer offsets
139 * in r300_emit_aos (it must yield a positive offset when added to
140 * a vertex buffer offset)
141 * - index_offset: the value that must be manually subtracted from indices
142 * in an index buffer to achieve negative offsets. */
143 static void r300_split_index_bias(struct r300_context *r300, int index_bias,
144 int *buffer_offset, int *index_offset)
145 {
146 struct pipe_vertex_buffer *vb, *vbufs = r300->vertex_buffer;
147 struct pipe_vertex_element *velem = r300->velems->velem;
148 unsigned i, size;
149 int max_neg_bias;
150
151 if (index_bias < 0) {
152 /* See how large index bias we may subtract. We must be careful
153 * here because negative buffer offsets are not allowed
154 * by the DRM API. */
155 max_neg_bias = INT_MAX;
156 for (i = 0; i < r300->velems->count; i++) {
157 vb = &vbufs[velem[i].vertex_buffer_index];
158 size = (vb->buffer_offset + velem[i].src_offset) / vb->stride;
159 max_neg_bias = MIN2(max_neg_bias, size);
160 }
161
162 /* Now set the minimum allowed value. */
163 *buffer_offset = MAX2(-max_neg_bias, index_bias);
164 } else {
165 /* A positive index bias is OK. */
166 *buffer_offset = index_bias;
167 }
168
169 *index_offset = index_bias - *buffer_offset;
170 }
171
172 enum r300_prepare_flags {
173 PREP_FIRST_DRAW = (1 << 0), /* call emit_dirty_state and friends? */
174 PREP_VALIDATE_VBOS = (1 << 1), /* validate VBOs? */
175 PREP_EMIT_AOS = (1 << 2), /* call emit_aos? */
176 PREP_EMIT_AOS_SWTCL = (1 << 3), /* call emit_aos_swtcl? */
177 PREP_INDEXED = (1 << 4) /* is this draw_elements? */
178 };
179
180 /**
181 * Check if the requested number of dwords is available in the CS and
182 * if not, flush. Then validate buffers and emit dirty state.
183 * \param r300 The context.
184 * \param flags See r300_prepare_flags.
185 * \param index_buffer The index buffer to validate. The parameter may be NULL.
186 * \param cs_dwords The number of dwords to reserve in CS.
187 * \param aos_offset The offset passed to emit_aos.
188 * \param index_bias The index bias to emit.
189 */
190 static boolean r300_prepare_for_rendering(struct r300_context *r300,
191 enum r300_prepare_flags flags,
192 struct pipe_resource *index_buffer,
193 unsigned cs_dwords,
194 int aos_offset,
195 int index_bias)
196 {
197 boolean flushed = FALSE;
198 boolean first_draw = flags & PREP_FIRST_DRAW;
199 boolean emit_aos = flags & PREP_EMIT_AOS;
200 boolean emit_aos_swtcl = flags & PREP_EMIT_AOS_SWTCL;
201 boolean indexed = flags & PREP_INDEXED;
202 boolean hw_index_bias = r500_index_bias_supported(r300);
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 (hw_index_bias)
209 cs_dwords += 2; /* emit_index_offset */
210
211 if (emit_aos)
212 cs_dwords += 55; /* emit_aos */
213
214 if (emit_aos_swtcl)
215 cs_dwords += 7; /* emit_aos_swtcl */
216 }
217
218 cs_dwords += r300_get_num_cs_end_dwords(r300);
219
220 /* Reserve requested CS space. */
221 if (cs_dwords > (r300->cs->ndw - r300->cs->cdw)) {
222 r300->context.flush(&r300->context, 0, NULL);
223 flushed = TRUE;
224 }
225
226 /* Validate buffers and emit dirty state if needed. */
227 if (first_draw || flushed) {
228 if (!r300_emit_buffer_validate(r300, flags & PREP_VALIDATE_VBOS,
229 index_buffer)) {
230 fprintf(stderr, "r300: CS space validation failed. "
231 "(not enough memory?) Skipping rendering.\n");
232 return FALSE;
233 }
234
235 r300_emit_dirty_state(r300);
236 if (hw_index_bias) {
237 if (r300->screen->caps.has_tcl)
238 r500_emit_index_bias(r300, index_bias);
239 else
240 r500_emit_index_bias(r300, 0);
241 }
242
243 if (emit_aos)
244 r300_emit_aos(r300, aos_offset, indexed);
245
246 if (emit_aos_swtcl)
247 r300_emit_aos_swtcl(r300, indexed);
248 }
249
250 return TRUE;
251 }
252
253 static boolean immd_is_good_idea(struct r300_context *r300,
254 unsigned count)
255 {
256 struct pipe_vertex_element* velem;
257 struct pipe_vertex_buffer* vbuf;
258 boolean checked[PIPE_MAX_ATTRIBS] = {0};
259 unsigned vertex_element_count = r300->velems->count;
260 unsigned i, vbi;
261
262 if (DBG_ON(r300, DBG_NO_IMMD)) {
263 return FALSE;
264 }
265
266 if (r300->draw) {
267 return FALSE;
268 }
269
270 if (count * r300->velems->vertex_size_dwords > IMMD_DWORDS) {
271 return FALSE;
272 }
273
274 /* We shouldn't map buffers referenced by CS, busy buffers,
275 * and ones placed in VRAM. */
276 for (i = 0; i < vertex_element_count; i++) {
277 velem = &r300->velems->velem[i];
278 vbi = velem->vertex_buffer_index;
279
280 if (!checked[vbi]) {
281 vbuf = &r300->vertex_buffer[vbi];
282
283 if (!(r300_buffer(vbuf->buffer)->domain & R300_DOMAIN_GTT)) {
284 return FALSE;
285 }
286
287 if (r300_buffer_is_referenced(&r300->context,
288 vbuf->buffer,
289 R300_REF_CS | R300_REF_HW)) {
290 /* It's a very bad idea to map it... */
291 return FALSE;
292 }
293 checked[vbi] = TRUE;
294 }
295 }
296 return TRUE;
297 }
298
299 /*****************************************************************************
300 * The HWTCL draw functions. *
301 ****************************************************************************/
302
303 static void r300_emit_draw_arrays_immediate(struct r300_context *r300,
304 unsigned mode,
305 unsigned start,
306 unsigned count)
307 {
308 struct pipe_vertex_element* velem;
309 struct pipe_vertex_buffer* vbuf;
310 unsigned vertex_element_count = r300->velems->count;
311 unsigned i, v, vbi;
312
313 /* Size of the vertex, in dwords. */
314 unsigned vertex_size = r300->velems->vertex_size_dwords;
315
316 /* The number of dwords for this draw operation. */
317 unsigned dwords = 9 + count * vertex_size;
318
319 /* Size of the vertex element, in dwords. */
320 unsigned size[PIPE_MAX_ATTRIBS];
321
322 /* Stride to the same attrib in the next vertex in the vertex buffer,
323 * in dwords. */
324 unsigned stride[PIPE_MAX_ATTRIBS];
325
326 /* Mapped vertex buffers. */
327 uint32_t* map[PIPE_MAX_ATTRIBS];
328 uint32_t* mapelem[PIPE_MAX_ATTRIBS];
329 struct pipe_transfer* transfer[PIPE_MAX_ATTRIBS] = {0};
330
331 CS_LOCALS(r300);
332
333 if (!r300_prepare_for_rendering(r300, PREP_FIRST_DRAW, NULL, dwords, 0, 0))
334 return;
335
336 /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
337 for (i = 0; i < vertex_element_count; i++) {
338 velem = &r300->velems->velem[i];
339 size[i] = r300->velems->hw_format_size[i] / 4;
340 vbi = velem->vertex_buffer_index;
341 vbuf = &r300->vertex_buffer[vbi];
342 stride[i] = vbuf->stride / 4;
343
344 /* Map the buffer. */
345 if (!transfer[vbi]) {
346 map[vbi] = (uint32_t*)pipe_buffer_map(&r300->context,
347 vbuf->buffer,
348 PIPE_TRANSFER_READ,
349 &transfer[vbi]);
350 map[vbi] += (vbuf->buffer_offset / 4) + stride[i] * start;
351 }
352 mapelem[i] = map[vbi] + (velem->src_offset / 4);
353 }
354
355 BEGIN_CS(dwords);
356 OUT_CS_REG(R300_GA_COLOR_CONTROL,
357 r300_provoking_vertex_fixes(r300, mode));
358 OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
359 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
360 OUT_CS(count - 1);
361 OUT_CS(0);
362 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, count * vertex_size);
363 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (count << 16) |
364 r300_translate_primitive(mode));
365
366 /* Emit vertices. */
367 for (v = 0; v < count; v++) {
368 for (i = 0; i < vertex_element_count; i++) {
369 OUT_CS_TABLE(&mapelem[i][stride[i] * v], size[i]);
370 }
371 }
372 END_CS;
373
374 /* Unmap buffers. */
375 for (i = 0; i < vertex_element_count; i++) {
376 vbi = r300->velems->velem[i].vertex_buffer_index;
377
378 if (transfer[vbi]) {
379 vbuf = &r300->vertex_buffer[vbi];
380 pipe_buffer_unmap(&r300->context, vbuf->buffer, transfer[vbi]);
381 transfer[vbi] = NULL;
382 }
383 }
384 }
385
386 static void r300_emit_draw_arrays(struct r300_context *r300,
387 unsigned mode,
388 unsigned count)
389 {
390 boolean alt_num_verts = count > 65535;
391 CS_LOCALS(r300);
392
393 if (count >= (1 << 24)) {
394 fprintf(stderr, "r300: Got a huge number of vertices: %i, "
395 "refusing to render.\n", count);
396 return;
397 }
398
399 BEGIN_CS(7 + (alt_num_verts ? 2 : 0));
400 if (alt_num_verts) {
401 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
402 }
403 OUT_CS_REG(R300_GA_COLOR_CONTROL,
404 r300_provoking_vertex_fixes(r300, mode));
405 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
406 OUT_CS(count - 1);
407 OUT_CS(0);
408 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
409 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
410 r300_translate_primitive(mode) |
411 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
412 END_CS;
413 }
414
415 static void r300_emit_draw_elements(struct r300_context *r300,
416 struct pipe_resource* indexBuffer,
417 unsigned indexSize,
418 unsigned minIndex,
419 unsigned maxIndex,
420 unsigned mode,
421 unsigned start,
422 unsigned count)
423 {
424 uint32_t count_dwords;
425 uint32_t offset_dwords = indexSize * start / sizeof(uint32_t);
426 boolean alt_num_verts = count > 65535;
427 CS_LOCALS(r300);
428
429 if (count >= (1 << 24)) {
430 fprintf(stderr, "r300: Got a huge number of vertices: %i, "
431 "refusing to render.\n", count);
432 return;
433 }
434
435 maxIndex = MIN2(maxIndex, r300->vertex_buffer_max_index);
436
437 DBG(r300, DBG_DRAW, "r300: Indexbuf of %u indices, min %u max %u\n",
438 count, minIndex, maxIndex);
439
440 BEGIN_CS(13 + (alt_num_verts ? 2 : 0));
441 if (alt_num_verts) {
442 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
443 }
444 OUT_CS_REG(R300_GA_COLOR_CONTROL,
445 r300_provoking_vertex_fixes(r300, mode));
446 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
447 OUT_CS(maxIndex);
448 OUT_CS(minIndex);
449 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);
450 if (indexSize == 4) {
451 count_dwords = count;
452 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
453 R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
454 r300_translate_primitive(mode) |
455 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
456 } else {
457 count_dwords = (count + 1) / 2;
458 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
459 r300_translate_primitive(mode) |
460 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
461 }
462
463 /* INDX_BUFFER is a truly special packet3.
464 * Unlike most other packet3, where the offset is after the count,
465 * the order is reversed, so the relocation ends up carrying the
466 * size of the indexbuf instead of the offset.
467 */
468 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2);
469 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) |
470 (0 << R300_INDX_BUFFER_SKIP_SHIFT));
471 OUT_CS(offset_dwords << 2);
472 OUT_CS_BUF_RELOC(indexBuffer, count_dwords,
473 r300_buffer(indexBuffer)->domain, 0);
474
475 END_CS;
476 }
477
478 /* This is the fast-path drawing & emission for HW TCL. */
479 static void r300_draw_range_elements(struct pipe_context* pipe,
480 struct pipe_resource* indexBuffer,
481 unsigned indexSize,
482 int indexBias,
483 unsigned minIndex,
484 unsigned maxIndex,
485 unsigned mode,
486 unsigned start,
487 unsigned count)
488 {
489 struct r300_context* r300 = r300_context(pipe);
490 struct pipe_resource* orgIndexBuffer = indexBuffer;
491 boolean alt_num_verts = r300->screen->caps.is_r500 &&
492 count > 65536 &&
493 r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0);
494 unsigned short_count;
495 int buffer_offset = 0, index_offset = 0; /* for index bias emulation */
496 boolean translate = FALSE;
497 unsigned new_offset;
498
499 if (r300->skip_rendering) {
500 return;
501 }
502
503 if (!u_trim_pipe_prim(mode, &count)) {
504 return;
505 }
506
507 /* Index buffer range checking. */
508 if ((start + count) * indexSize > indexBuffer->width0) {
509 fprintf(stderr, "r300: Invalid index buffer range. Skipping rendering.\n");
510 return;
511 }
512
513 /* Set up fallback for incompatible vertex layout if needed. */
514 if (r300->incompatible_vb_layout || r300->velems->incompatible_layout) {
515 r300_begin_vertex_translate(r300);
516 translate = TRUE;
517 }
518
519 if (indexBias && !r500_index_bias_supported(r300)) {
520 r300_split_index_bias(r300, indexBias, &buffer_offset, &index_offset);
521 }
522
523 r300_translate_index_buffer(r300, &indexBuffer, &indexSize, index_offset,
524 &start, count);
525
526 r300_update_derived_state(r300);
527 r300_upload_index_buffer(r300, &indexBuffer, indexSize, start, count, &new_offset);
528
529 start = new_offset;
530
531 /* 15 dwords for emit_draw_elements. Give up if the function fails. */
532 if (!r300_prepare_for_rendering(r300,
533 PREP_FIRST_DRAW | PREP_VALIDATE_VBOS | PREP_EMIT_AOS |
534 PREP_INDEXED, indexBuffer, 15, buffer_offset, indexBias))
535 goto done;
536
537 if (alt_num_verts || count <= 65535) {
538 r300_emit_draw_elements(r300, indexBuffer, indexSize,
539 minIndex, maxIndex, mode, start, count);
540 } else {
541 do {
542 short_count = MIN2(count, 65534);
543 r300_emit_draw_elements(r300, indexBuffer, indexSize,
544 minIndex, maxIndex,
545 mode, start, short_count);
546
547 start += short_count;
548 count -= short_count;
549
550 /* 15 dwords for emit_draw_elements */
551 if (count) {
552 if (!r300_prepare_for_rendering(r300,
553 PREP_VALIDATE_VBOS | PREP_EMIT_AOS | PREP_INDEXED,
554 indexBuffer, 15, buffer_offset, indexBias))
555 goto done;
556 }
557 } while (count);
558 }
559
560 done:
561 if (indexBuffer != orgIndexBuffer) {
562 pipe_resource_reference( &indexBuffer, NULL );
563 }
564
565 if (translate) {
566 r300_end_vertex_translate(r300);
567 }
568 }
569
570 static void r300_draw_arrays(struct pipe_context* pipe, unsigned mode,
571 unsigned start, unsigned count)
572 {
573 struct r300_context* r300 = r300_context(pipe);
574 boolean alt_num_verts = r300->screen->caps.is_r500 &&
575 count > 65536 &&
576 r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0);
577 unsigned short_count;
578 boolean translate = FALSE;
579
580 if (r300->skip_rendering) {
581 return;
582 }
583
584 if (!u_trim_pipe_prim(mode, &count)) {
585 return;
586 }
587
588 /* Set up fallback for incompatible vertex layout if needed. */
589 if (r300->incompatible_vb_layout || r300->velems->incompatible_layout) {
590 r300_begin_vertex_translate(r300);
591 translate = TRUE;
592 }
593
594 r300_update_derived_state(r300);
595
596 if (immd_is_good_idea(r300, count)) {
597 r300_emit_draw_arrays_immediate(r300, mode, start, count);
598 } else {
599 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
600 if (!r300_prepare_for_rendering(r300,
601 PREP_FIRST_DRAW | PREP_VALIDATE_VBOS | PREP_EMIT_AOS,
602 NULL, 9, start, 0))
603 goto done;
604
605 if (alt_num_verts || count <= 65535) {
606 r300_emit_draw_arrays(r300, mode, count);
607 } else {
608 do {
609 short_count = MIN2(count, 65535);
610 r300_emit_draw_arrays(r300, mode, short_count);
611
612 start += short_count;
613 count -= short_count;
614
615 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
616 if (count) {
617 if (!r300_prepare_for_rendering(r300,
618 PREP_VALIDATE_VBOS | PREP_EMIT_AOS, NULL, 9,
619 start, 0))
620 goto done;
621 }
622 } while (count);
623 }
624 }
625
626 done:
627 if (translate) {
628 r300_end_vertex_translate(r300);
629 }
630 }
631
632 static void r300_draw_vbo(struct pipe_context* pipe,
633 const struct pipe_draw_info *info)
634 {
635 struct r300_context* r300 = r300_context(pipe);
636
637 if (!r300->velems->count || !r300->vertex_buffer_count)
638 return;
639
640 if (info->indexed && r300->index_buffer.buffer) {
641 unsigned offset;
642
643 assert(r300->index_buffer.offset % r300->index_buffer.index_size == 0);
644 offset = r300->index_buffer.offset / r300->index_buffer.index_size;
645
646 r300_draw_range_elements(pipe,
647 r300->index_buffer.buffer,
648 r300->index_buffer.index_size,
649 info->index_bias,
650 info->min_index,
651 info->max_index,
652 info->mode,
653 info->start + offset,
654 info->count);
655 }
656 else {
657 r300_draw_arrays(pipe,
658 info->mode,
659 info->start,
660 info->count);
661 }
662 }
663
664 /****************************************************************************
665 * The rest of this file is for SW TCL rendering only. Please be polite and *
666 * keep these functions separated so that they are easier to locate. ~C. *
667 ***************************************************************************/
668
669 /* SW TCL elements, using Draw. */
670 static void r300_swtcl_draw_vbo(struct pipe_context* pipe,
671 const struct pipe_draw_info *info)
672 {
673 struct r300_context* r300 = r300_context(pipe);
674 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS];
675 struct pipe_transfer *ib_transfer = NULL;
676 unsigned count = info->count;
677 int i;
678 void* indices = NULL;
679
680 if (r300->skip_rendering) {
681 return;
682 }
683
684 if (!u_trim_pipe_prim(info->mode, &count)) {
685 return;
686 }
687
688 r300_update_derived_state(r300);
689
690 for (i = 0; i < r300->vertex_buffer_count; i++) {
691 if (r300->vertex_buffer[i].buffer) {
692 void *buf = pipe_buffer_map(pipe,
693 r300->vertex_buffer[i].buffer,
694 PIPE_TRANSFER_READ,
695 &vb_transfer[i]);
696 draw_set_mapped_vertex_buffer(r300->draw, i, buf);
697 }
698 }
699
700 if (info->indexed && r300->index_buffer.buffer) {
701 indices = pipe_buffer_map(pipe, r300->index_buffer.buffer,
702 PIPE_TRANSFER_READ, &ib_transfer);
703 }
704
705 draw_set_mapped_index_buffer(r300->draw, indices);
706
707 draw_vbo(r300->draw, info);
708
709 /* XXX Not sure whether this is the best fix.
710 * It prevents CS from being rejected and weird assertion failures. */
711 draw_flush(r300->draw);
712
713 for (i = 0; i < r300->vertex_buffer_count; i++) {
714 if (r300->vertex_buffer[i].buffer) {
715 pipe_buffer_unmap(pipe, r300->vertex_buffer[i].buffer,
716 vb_transfer[i]);
717 draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
718 }
719 }
720
721 if (ib_transfer) {
722 pipe_buffer_unmap(pipe, r300->index_buffer.buffer, ib_transfer);
723 draw_set_mapped_index_buffer(r300->draw, NULL);
724 }
725 }
726
727 /* Object for rendering using Draw. */
728 struct r300_render {
729 /* Parent class */
730 struct vbuf_render base;
731
732 /* Pipe context */
733 struct r300_context* r300;
734
735 /* Vertex information */
736 size_t vertex_size;
737 unsigned prim;
738 unsigned hwprim;
739
740 /* VBO */
741 size_t vbo_offset;
742 size_t vbo_max_used;
743 void * vbo_ptr;
744
745 struct pipe_transfer *vbo_transfer;
746 };
747
748 static INLINE struct r300_render*
749 r300_render(struct vbuf_render* render)
750 {
751 return (struct r300_render*)render;
752 }
753
754 static const struct vertex_info*
755 r300_render_get_vertex_info(struct vbuf_render* render)
756 {
757 struct r300_render* r300render = r300_render(render);
758 struct r300_context* r300 = r300render->r300;
759
760 return &r300->vertex_info;
761 }
762
763 static boolean r300_render_allocate_vertices(struct vbuf_render* render,
764 ushort vertex_size,
765 ushort count)
766 {
767 struct r300_render* r300render = r300_render(render);
768 struct r300_context* r300 = r300render->r300;
769 struct pipe_screen* screen = r300->context.screen;
770 size_t size = (size_t)vertex_size * (size_t)count;
771
772 if (size + r300render->vbo_offset > r300->draw_vbo_size)
773 {
774 pipe_resource_reference(&r300->vbo, NULL);
775 r300->vbo = pipe_buffer_create(screen,
776 PIPE_BIND_VERTEX_BUFFER,
777 R300_MAX_DRAW_VBO_SIZE);
778 r300render->vbo_offset = 0;
779 r300->draw_vbo_size = R300_MAX_DRAW_VBO_SIZE;
780 }
781
782 r300render->vertex_size = vertex_size;
783 r300->vbo_offset = r300render->vbo_offset;
784
785 return (r300->vbo) ? TRUE : FALSE;
786 }
787
788 static void* r300_render_map_vertices(struct vbuf_render* render)
789 {
790 struct r300_render* r300render = r300_render(render);
791 struct r300_context* r300 = r300render->r300;
792
793 assert(!r300render->vbo_transfer);
794
795 r300render->vbo_ptr = pipe_buffer_map(&r300render->r300->context,
796 r300->vbo,
797 PIPE_TRANSFER_WRITE,
798 &r300render->vbo_transfer);
799
800 assert(r300render->vbo_ptr);
801
802 return ((uint8_t*)r300render->vbo_ptr + r300render->vbo_offset);
803 }
804
805 static void r300_render_unmap_vertices(struct vbuf_render* render,
806 ushort min,
807 ushort max)
808 {
809 struct r300_render* r300render = r300_render(render);
810 struct pipe_context* context = &r300render->r300->context;
811 struct r300_context* r300 = r300render->r300;
812
813 assert(r300render->vbo_transfer);
814
815 r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
816 r300render->vertex_size * (max + 1));
817 pipe_buffer_unmap(context, r300->vbo, r300render->vbo_transfer);
818
819 r300render->vbo_transfer = NULL;
820 }
821
822 static void r300_render_release_vertices(struct vbuf_render* render)
823 {
824 struct r300_render* r300render = r300_render(render);
825
826 r300render->vbo_offset += r300render->vbo_max_used;
827 r300render->vbo_max_used = 0;
828 }
829
830 static boolean r300_render_set_primitive(struct vbuf_render* render,
831 unsigned prim)
832 {
833 struct r300_render* r300render = r300_render(render);
834
835 r300render->prim = prim;
836 r300render->hwprim = r300_translate_primitive(prim);
837
838 return TRUE;
839 }
840
841 static void r300_render_draw_arrays(struct vbuf_render* render,
842 unsigned start,
843 unsigned count)
844 {
845 struct r300_render* r300render = r300_render(render);
846 struct r300_context* r300 = r300render->r300;
847 uint8_t* ptr;
848 unsigned i;
849 unsigned dwords = 6;
850
851 CS_LOCALS(r300);
852 (void) i; (void) ptr;
853
854 if (!r300_prepare_for_rendering(r300,
855 PREP_FIRST_DRAW | PREP_EMIT_AOS_SWTCL,
856 NULL, dwords, 0, 0))
857 return;
858
859 DBG(r300, DBG_DRAW, "r300: render_draw_arrays (count: %d)\n", count);
860
861 /* Uncomment to dump all VBOs rendered through this interface.
862 * Slow and noisy!
863 ptr = pipe_buffer_map(&r300render->r300->context,
864 r300render->vbo, PIPE_TRANSFER_READ,
865 &r300render->vbo_transfer);
866
867 for (i = 0; i < count; i++) {
868 printf("r300: Vertex %d\n", i);
869 draw_dump_emitted_vertex(&r300->vertex_info, ptr);
870 ptr += r300->vertex_info.size * 4;
871 printf("\n");
872 }
873
874 pipe_buffer_unmap(&r300render->r300->context, r300render->vbo,
875 r300render->vbo_transfer);
876 */
877
878 BEGIN_CS(dwords);
879 OUT_CS_REG(R300_GA_COLOR_CONTROL,
880 r300_provoking_vertex_fixes(r300, r300render->prim));
881 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);
882 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
883 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
884 r300render->hwprim);
885 END_CS;
886 }
887
888 static void r300_render_draw_elements(struct vbuf_render* render,
889 const ushort* indices,
890 uint count)
891 {
892 struct r300_render* r300render = r300_render(render);
893 struct r300_context* r300 = r300render->r300;
894 int i;
895 unsigned end_cs_dwords;
896 unsigned max_index = (r300->draw_vbo_size - r300render->vbo_offset) /
897 (r300render->r300->vertex_info.size * 4) - 1;
898 unsigned short_count;
899 unsigned free_dwords;
900
901 CS_LOCALS(r300);
902 DBG(r300, DBG_DRAW, "r300: render_draw_elements (count: %d)\n", count);
903
904 /* Reserve at least 256 dwords.
905 *
906 * Below we manage the CS space manually because there may be more
907 * indices than it can fit in CS. */
908 if (!r300_prepare_for_rendering(r300,
909 PREP_FIRST_DRAW | PREP_EMIT_AOS_SWTCL | PREP_INDEXED,
910 NULL, 256, 0, 0))
911 return;
912
913 end_cs_dwords = r300_get_num_cs_end_dwords(r300);
914
915 while (count) {
916 free_dwords = r300->cs->ndw - r300->cs->cdw;
917
918 short_count = MIN2(count, (free_dwords - end_cs_dwords - 6) * 2);
919
920 BEGIN_CS(6 + (short_count+1)/2);
921 OUT_CS_REG(R300_GA_COLOR_CONTROL,
922 r300_provoking_vertex_fixes(r300, r300render->prim));
923 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max_index);
924 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (short_count+1)/2);
925 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (short_count << 16) |
926 r300render->hwprim);
927 for (i = 0; i < short_count-1; i += 2) {
928 OUT_CS(indices[i+1] << 16 | indices[i]);
929 }
930 if (short_count % 2) {
931 OUT_CS(indices[short_count-1]);
932 }
933 END_CS;
934
935 /* OK now subtract the emitted indices and see if we need to emit
936 * another draw packet. */
937 indices += short_count;
938 count -= short_count;
939
940 if (count) {
941 if (!r300_prepare_for_rendering(r300,
942 PREP_EMIT_AOS_SWTCL | PREP_INDEXED,
943 NULL, 256, 0, 0))
944 return;
945
946 end_cs_dwords = r300_get_num_cs_end_dwords(r300);
947 }
948 }
949 }
950
951 static void r300_render_destroy(struct vbuf_render* render)
952 {
953 FREE(render);
954 }
955
956 static struct vbuf_render* r300_render_create(struct r300_context* r300)
957 {
958 struct r300_render* r300render = CALLOC_STRUCT(r300_render);
959
960 r300render->r300 = r300;
961
962 /* XXX find real numbers plz */
963 r300render->base.max_vertex_buffer_bytes = 128 * 1024;
964 r300render->base.max_indices = 16 * 1024;
965
966 r300render->base.get_vertex_info = r300_render_get_vertex_info;
967 r300render->base.allocate_vertices = r300_render_allocate_vertices;
968 r300render->base.map_vertices = r300_render_map_vertices;
969 r300render->base.unmap_vertices = r300_render_unmap_vertices;
970 r300render->base.set_primitive = r300_render_set_primitive;
971 r300render->base.draw_elements = r300_render_draw_elements;
972 r300render->base.draw_arrays = r300_render_draw_arrays;
973 r300render->base.release_vertices = r300_render_release_vertices;
974 r300render->base.destroy = r300_render_destroy;
975
976 r300render->vbo_offset = 0;
977
978 return &r300render->base;
979 }
980
981 struct draw_stage* r300_draw_stage(struct r300_context* r300)
982 {
983 struct vbuf_render* render;
984 struct draw_stage* stage;
985
986 render = r300_render_create(r300);
987
988 if (!render) {
989 return NULL;
990 }
991
992 stage = draw_vbuf_stage(r300->draw, render);
993
994 if (!stage) {
995 render->destroy(render);
996 return NULL;
997 }
998
999 draw_set_render(r300->draw, render);
1000
1001 return stage;
1002 }
1003
1004 void r300_draw_flush_vbuf(struct r300_context *r300)
1005 {
1006 pipe_resource_reference(&r300->vbo, NULL);
1007 r300->draw_vbo_size = 0;
1008 }
1009
1010 /****************************************************************************
1011 * End of SW TCL functions *
1012 ***************************************************************************/
1013
1014 /* This functions is used to draw a rectangle for the blitter module.
1015 *
1016 * If we rendered a quad, the pixels on the main diagonal
1017 * would be computed and stored twice, which makes the clear/copy codepaths
1018 * somewhat inefficient. Instead we use a rectangular point sprite. */
1019 static void r300_blitter_draw_rectangle(struct blitter_context *blitter,
1020 unsigned x1, unsigned y1,
1021 unsigned x2, unsigned y2,
1022 float depth,
1023 enum blitter_attrib_type type,
1024 const float attrib[4])
1025 {
1026 struct r300_context *r300 = r300_context(util_blitter_get_pipe(blitter));
1027 unsigned last_sprite_coord_enable = r300->sprite_coord_enable;
1028 unsigned width = x2 - x1;
1029 unsigned height = y2 - y1;
1030 unsigned vertex_size =
1031 type == UTIL_BLITTER_ATTRIB_COLOR || !r300->draw ? 8 : 4;
1032 unsigned dwords = 13 + vertex_size +
1033 (type == UTIL_BLITTER_ATTRIB_TEXCOORD ? 7 : 0);
1034 const float zeros[4] = {0, 0, 0, 0};
1035 CS_LOCALS(r300);
1036
1037 if (type == UTIL_BLITTER_ATTRIB_TEXCOORD)
1038 r300->sprite_coord_enable = 1;
1039
1040 r300_update_derived_state(r300);
1041
1042 /* Mark some states we don't care about as non-dirty. */
1043 r300->clip_state.dirty = FALSE;
1044 r300->viewport_state.dirty = FALSE;
1045
1046 if (!r300_prepare_for_rendering(r300, PREP_FIRST_DRAW, NULL, dwords, 0, 0))
1047 goto done;
1048
1049 DBG(r300, DBG_DRAW, "r300: draw_rectangle\n");
1050
1051 BEGIN_CS(dwords);
1052 /* Set up GA. */
1053 OUT_CS_REG(R300_GA_POINT_SIZE, (height * 6) | ((width * 6) << 16));
1054
1055 if (type == UTIL_BLITTER_ATTRIB_TEXCOORD) {
1056 /* Set up the GA to generate texcoords. */
1057 OUT_CS_REG(R300_GB_ENABLE, R300_GB_POINT_STUFF_ENABLE |
1058 (R300_GB_TEX_STR << R300_GB_TEX0_SOURCE_SHIFT));
1059 OUT_CS_REG_SEQ(R300_GA_POINT_S0, 4);
1060 OUT_CS_32F(attrib[0]);
1061 OUT_CS_32F(attrib[3]);
1062 OUT_CS_32F(attrib[2]);
1063 OUT_CS_32F(attrib[1]);
1064 }
1065
1066 /* Set up VAP controls. */
1067 OUT_CS_REG(R300_VAP_CLIP_CNTL, R300_CLIP_DISABLE);
1068 OUT_CS_REG(R300_VAP_VTE_CNTL, R300_VTX_XY_FMT | R300_VTX_Z_FMT);
1069 OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
1070 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
1071 OUT_CS(1);
1072 OUT_CS(0);
1073
1074 /* Draw. */
1075 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, vertex_size);
1076 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (1 << 16) |
1077 R300_VAP_VF_CNTL__PRIM_POINTS);
1078
1079 OUT_CS_32F(x1 + width * 0.5f);
1080 OUT_CS_32F(y1 + height * 0.5f);
1081 OUT_CS_32F(depth);
1082 OUT_CS_32F(1);
1083
1084 if (vertex_size == 8) {
1085 if (!attrib)
1086 attrib = zeros;
1087 OUT_CS_TABLE(attrib, 4);
1088 }
1089 END_CS;
1090
1091 done:
1092 /* Restore the state. */
1093 r300->clip_state.dirty = TRUE;
1094 r300->rs_state.dirty = TRUE;
1095 r300->viewport_state.dirty = TRUE;
1096
1097 r300->sprite_coord_enable = last_sprite_coord_enable;
1098 }
1099
1100 static void r300_resource_resolve(struct pipe_context* pipe,
1101 struct pipe_resource* dest,
1102 struct pipe_subresource subdest,
1103 struct pipe_resource* src,
1104 struct pipe_subresource subsrc)
1105 {
1106 struct r300_context* r300 = r300_context(pipe);
1107 struct r300_aa_state *aa = (struct r300_aa_state*)r300->aa_state.state;
1108 struct pipe_surface* srcsurf = src->screen->get_tex_surface(src->screen,
1109 src, subsrc.face, subsrc.level, 0, 0);
1110 float color[] = {0, 0, 0, 0};
1111
1112 DBG(r300, DBG_DRAW, "r300: Resolving resource...\n");
1113
1114 /* Enable AA resolve. */
1115 aa->dest = r300_surface(
1116 dest->screen->get_tex_surface(dest->screen, dest, subdest.face,
1117 subdest.level, 0, 0));
1118
1119 aa->aaresolve_ctl =
1120 R300_RB3D_AARESOLVE_CTL_AARESOLVE_MODE_RESOLVE |
1121 R300_RB3D_AARESOLVE_CTL_AARESOLVE_ALPHA_AVERAGE;
1122 r300->aa_state.size = 12;
1123 r300->aa_state.dirty = TRUE;
1124
1125 /* Resolve the surface. */
1126 r300->context.clear_render_target(pipe,
1127 srcsurf, color, 0, 0, src->width0, src->height0);
1128
1129 /* Disable AA resolve. */
1130 aa->aaresolve_ctl = 0;
1131 r300->aa_state.size = 4;
1132 r300->aa_state.dirty = TRUE;
1133
1134 pipe_surface_reference((struct pipe_surface**)&srcsurf, NULL);
1135 pipe_surface_reference((struct pipe_surface**)&aa->dest, NULL);
1136 }
1137
1138 void r300_init_render_functions(struct r300_context *r300)
1139 {
1140 /* Set draw functions based on presence of HW TCL. */
1141 if (r300->screen->caps.has_tcl) {
1142 r300->context.draw_vbo = r300_draw_vbo;
1143 } else {
1144 r300->context.draw_vbo = r300_swtcl_draw_vbo;
1145 }
1146
1147 r300->context.resource_resolve = r300_resource_resolve;
1148 r300->blitter->draw_rectangle = r300_blitter_draw_rectangle;
1149
1150 /* Plug in the two-sided stencil reference value fallback if needed. */
1151 if (!r300->screen->caps.is_r500)
1152 r300_plug_in_stencil_ref_fallback(r300);
1153 }