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