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