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