2 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2010 Marek Olšák <maraeo@gmail.com>
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:
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
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. */
24 /* r300_render: Vertex and index buffer primitive emission. Contains both
25 * HW TCL fastpath rendering, and SW TCL Draw-assisted rendering. */
27 #include "draw/draw_context.h"
28 #include "draw/draw_vbuf.h"
30 #include "util/u_inlines.h"
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"
38 #include "r300_context.h"
39 #include "r300_screen_buffer.h"
40 #include "r300_emit.h"
45 #define IMMD_DWORDS 32
47 static uint32_t r300_translate_primitive(unsigned prim
)
50 case PIPE_PRIM_POINTS
:
51 return R300_VAP_VF_CNTL__PRIM_POINTS
;
53 return R300_VAP_VF_CNTL__PRIM_LINES
;
54 case PIPE_PRIM_LINE_LOOP
:
55 return R300_VAP_VF_CNTL__PRIM_LINE_LOOP
;
56 case PIPE_PRIM_LINE_STRIP
:
57 return R300_VAP_VF_CNTL__PRIM_LINE_STRIP
;
58 case PIPE_PRIM_TRIANGLES
:
59 return R300_VAP_VF_CNTL__PRIM_TRIANGLES
;
60 case PIPE_PRIM_TRIANGLE_STRIP
:
61 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP
;
62 case PIPE_PRIM_TRIANGLE_FAN
:
63 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN
;
65 return R300_VAP_VF_CNTL__PRIM_QUADS
;
66 case PIPE_PRIM_QUAD_STRIP
:
67 return R300_VAP_VF_CNTL__PRIM_QUAD_STRIP
;
68 case PIPE_PRIM_POLYGON
:
69 return R300_VAP_VF_CNTL__PRIM_POLYGON
;
75 static uint32_t r300_provoking_vertex_fixes(struct r300_context
*r300
,
78 struct r300_rs_state
* rs
= (struct r300_rs_state
*)r300
->rs_state
.state
;
79 uint32_t color_control
= rs
->color_control
;
81 /* By default (see r300_state.c:r300_create_rs_state) color_control is
82 * initialized to provoking the first vertex.
84 * Triangle fans must be reduced to the second vertex, not the first, in
85 * Gallium flatshade-first mode, as per the GL spec.
86 * (http://www.opengl.org/registry/specs/ARB/provoking_vertex.txt)
88 * Quads never provoke correctly in flatshade-first mode. The first
89 * vertex is never considered as provoking, so only the second, third,
90 * and fourth vertices can be selected, and both "third" and "last" modes
91 * select the fourth vertex. This is probably due to D3D lacking quads.
93 * Similarly, polygons reduce to the first, not the last, vertex, when in
94 * "last" mode, and all other modes start from the second vertex.
99 if (rs
->rs
.flatshade_first
) {
101 case PIPE_PRIM_TRIANGLE_FAN
:
102 color_control
|= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND
;
104 case PIPE_PRIM_QUADS
:
105 case PIPE_PRIM_QUAD_STRIP
:
106 case PIPE_PRIM_POLYGON
:
107 color_control
|= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST
;
110 color_control
|= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST
;
114 color_control
|= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST
;
117 return color_control
;
120 void r500_emit_index_bias(struct r300_context
*r300
, int index_bias
)
125 OUT_CS_REG(R500_VAP_INDEX_OFFSET
,
126 (index_bias
& 0xFFFFFF) | (index_bias
< 0 ? 1<<24 : 0));
130 static void r300_emit_draw_init(struct r300_context
*r300
, unsigned mode
,
131 unsigned min_index
, unsigned max_index
)
136 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
137 r300_provoking_vertex_fixes(r300
, mode
));
138 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
144 /* This function splits the index bias value into two parts:
145 * - buffer_offset: the value that can be safely added to buffer offsets
146 * in r300_emit_vertex_arrays (it must yield a positive offset when added to
147 * a vertex buffer offset)
148 * - index_offset: the value that must be manually subtracted from indices
149 * in an index buffer to achieve negative offsets. */
150 static void r300_split_index_bias(struct r300_context
*r300
, int index_bias
,
151 int *buffer_offset
, int *index_offset
)
153 struct pipe_vertex_buffer
*vb
, *vbufs
= r300
->vbuf_mgr
->vertex_buffer
;
154 struct pipe_vertex_element
*velem
= r300
->velems
->velem
;
158 if (index_bias
< 0) {
159 /* See how large index bias we may subtract. We must be careful
160 * here because negative buffer offsets are not allowed
162 max_neg_bias
= INT_MAX
;
163 for (i
= 0; i
< r300
->velems
->count
; i
++) {
164 vb
= &vbufs
[velem
[i
].vertex_buffer_index
];
165 size
= (vb
->buffer_offset
+ velem
[i
].src_offset
) / vb
->stride
;
166 max_neg_bias
= MIN2(max_neg_bias
, size
);
169 /* Now set the minimum allowed value. */
170 *buffer_offset
= MAX2(-max_neg_bias
, index_bias
);
172 /* A positive index bias is OK. */
173 *buffer_offset
= index_bias
;
176 *index_offset
= index_bias
- *buffer_offset
;
179 enum r300_prepare_flags
{
180 PREP_FIRST_DRAW
= (1 << 0), /* call emit_dirty_state and friends? */
181 PREP_VALIDATE_VBOS
= (1 << 1), /* validate VBOs? */
182 PREP_EMIT_AOS
= (1 << 2), /* call emit_vertex_arrays? */
183 PREP_EMIT_AOS_SWTCL
= (1 << 3), /* call emit_vertex_arrays_swtcl? */
184 PREP_INDEXED
= (1 << 4) /* is this draw_elements? */
188 * Check if the requested number of dwords is available in the CS and
190 * \param r300 The context.
191 * \param flags See r300_prepare_flags.
192 * \param cs_dwords The number of dwords to reserve in CS.
193 * \return TRUE if the CS was flushed
195 static boolean
r300_reserve_cs_dwords(struct r300_context
*r300
,
196 enum r300_prepare_flags flags
,
199 boolean flushed
= FALSE
;
200 boolean first_draw
= flags
& PREP_FIRST_DRAW
;
201 boolean emit_vertex_arrays
= flags
& PREP_EMIT_AOS
;
202 boolean emit_vertex_arrays_swtcl
= flags
& PREP_EMIT_AOS_SWTCL
;
204 /* Add dirty state, index offset, and AOS. */
206 cs_dwords
+= r300_get_num_dirty_dwords(r300
);
208 if (r300
->screen
->caps
.index_bias_supported
)
209 cs_dwords
+= 2; /* emit_index_offset */
211 if (emit_vertex_arrays
)
212 cs_dwords
+= 55; /* emit_vertex_arrays */
214 if (emit_vertex_arrays_swtcl
)
215 cs_dwords
+= 7; /* emit_vertex_arrays_swtcl */
218 cs_dwords
+= r300_get_num_cs_end_dwords(r300
);
220 /* Reserve requested CS space. */
221 if (cs_dwords
> (R300_MAX_CMDBUF_DWORDS
- r300
->cs
->cdw
)) {
222 r300
->context
.flush(&r300
->context
, 0, NULL
);
230 * Validate buffers and emit dirty state.
231 * \param r300 The context.
232 * \param flags See r300_prepare_flags.
233 * \param index_buffer The index buffer to validate. The parameter may be NULL.
234 * \param buffer_offset The offset passed to emit_vertex_arrays.
235 * \param index_bias The index bias to emit.
236 * \return TRUE if rendering should be skipped
238 static boolean
r300_emit_states(struct r300_context
*r300
,
239 enum r300_prepare_flags flags
,
240 struct pipe_resource
*index_buffer
,
244 boolean first_draw
= flags
& PREP_FIRST_DRAW
;
245 boolean emit_vertex_arrays
= flags
& PREP_EMIT_AOS
;
246 boolean emit_vertex_arrays_swtcl
= flags
& PREP_EMIT_AOS_SWTCL
;
247 boolean indexed
= flags
& PREP_INDEXED
;
248 boolean validate_vbos
= flags
& PREP_VALIDATE_VBOS
;
250 /* Validate buffers and emit dirty state if needed. */
252 if (!r300_emit_buffer_validate(r300
, validate_vbos
,
254 fprintf(stderr
, "r300: CS space validation failed. "
255 "(not enough memory?) Skipping rendering.\n");
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
);
264 r500_emit_index_bias(r300
, 0);
267 if (emit_vertex_arrays
&&
268 (r300
->vertex_arrays_dirty
||
269 r300
->vertex_arrays_indexed
!= indexed
||
270 r300
->vertex_arrays_offset
!= buffer_offset
)) {
271 r300_emit_vertex_arrays(r300
, buffer_offset
, indexed
);
273 r300
->vertex_arrays_dirty
= FALSE
;
274 r300
->vertex_arrays_indexed
= indexed
;
275 r300
->vertex_arrays_offset
= buffer_offset
;
278 if (emit_vertex_arrays_swtcl
)
279 r300_emit_vertex_arrays_swtcl(r300
, indexed
);
286 * Check if the requested number of dwords is available in the CS and
287 * if not, flush. Then validate buffers and emit dirty state.
288 * \param r300 The context.
289 * \param flags See r300_prepare_flags.
290 * \param index_buffer The index buffer to validate. The parameter may be NULL.
291 * \param cs_dwords The number of dwords to reserve in CS.
292 * \param buffer_offset The offset passed to emit_vertex_arrays.
293 * \param index_bias The index bias to emit.
294 * \return TRUE if rendering should be skipped
296 static boolean
r300_prepare_for_rendering(struct r300_context
*r300
,
297 enum r300_prepare_flags flags
,
298 struct pipe_resource
*index_buffer
,
303 if (r300_reserve_cs_dwords(r300
, flags
, cs_dwords
))
304 flags
|= PREP_FIRST_DRAW
;
306 return r300_emit_states(r300
, flags
, index_buffer
, buffer_offset
,
310 static boolean
immd_is_good_idea(struct r300_context
*r300
,
313 struct pipe_vertex_element
* velem
;
314 struct pipe_resource
*buf
;
315 boolean checked
[PIPE_MAX_ATTRIBS
] = {0};
316 unsigned vertex_element_count
= r300
->velems
->count
;
319 if (DBG_ON(r300
, DBG_NO_IMMD
)) {
327 if (count
* r300
->velems
->vertex_size_dwords
> IMMD_DWORDS
) {
331 /* We shouldn't map buffers referenced by CS, busy buffers,
332 * and ones placed in VRAM. */
333 for (i
= 0; i
< vertex_element_count
; i
++) {
334 velem
= &r300
->velems
->velem
[i
];
335 vbi
= velem
->vertex_buffer_index
;
338 buf
= r300
->vbuf_mgr
->real_vertex_buffer
[vbi
];
340 if ((r300_resource(buf
)->domain
!= R300_DOMAIN_GTT
)) {
350 /*****************************************************************************
351 * The HWTCL draw functions. *
352 ****************************************************************************/
354 static void r300_draw_arrays_immediate(struct r300_context
*r300
,
355 unsigned mode
, unsigned start
,
358 struct pipe_vertex_element
* velem
;
359 struct pipe_vertex_buffer
* vbuf
;
360 unsigned vertex_element_count
= r300
->velems
->count
;
363 /* Size of the vertex, in dwords. */
364 unsigned vertex_size
= r300
->velems
->vertex_size_dwords
;
366 /* The number of dwords for this draw operation. */
367 unsigned dwords
= 4 + count
* vertex_size
;
369 /* Size of the vertex element, in dwords. */
370 unsigned size
[PIPE_MAX_ATTRIBS
];
372 /* Stride to the same attrib in the next vertex in the vertex buffer,
374 unsigned stride
[PIPE_MAX_ATTRIBS
];
376 /* Mapped vertex buffers. */
377 uint32_t* map
[PIPE_MAX_ATTRIBS
] = {0};
378 uint32_t* mapelem
[PIPE_MAX_ATTRIBS
];
382 if (!r300_prepare_for_rendering(r300
, PREP_FIRST_DRAW
, NULL
, dwords
, 0, 0))
385 /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
386 for (i
= 0; i
< vertex_element_count
; i
++) {
387 velem
= &r300
->velems
->velem
[i
];
388 size
[i
] = r300
->velems
->format_size
[i
] / 4;
389 vbi
= velem
->vertex_buffer_index
;
390 vbuf
= &r300
->vbuf_mgr
->vertex_buffer
[vbi
];
391 stride
[i
] = vbuf
->stride
/ 4;
393 /* Map the buffer. */
395 map
[vbi
] = (uint32_t*)r300
->rws
->buffer_map(
396 r300_resource(r300
->vbuf_mgr
->real_vertex_buffer
[vbi
])->buf
,
397 r300
->cs
, PIPE_TRANSFER_READ
| PIPE_TRANSFER_UNSYNCHRONIZED
);
398 map
[vbi
] += (vbuf
->buffer_offset
/ 4) + stride
[i
] * start
;
400 mapelem
[i
] = map
[vbi
] + (velem
->src_offset
/ 4);
403 r300_emit_draw_init(r300
, mode
, 0, count
-1);
406 OUT_CS_REG(R300_VAP_VTX_SIZE
, vertex_size
);
407 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2
, count
* vertex_size
);
408 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED
| (count
<< 16) |
409 r300_translate_primitive(mode
));
412 for (v
= 0; v
< count
; v
++) {
413 for (i
= 0; i
< vertex_element_count
; i
++) {
414 OUT_CS_TABLE(&mapelem
[i
][stride
[i
] * v
], size
[i
]);
420 for (i
= 0; i
< vertex_element_count
; i
++) {
421 vbi
= r300
->velems
->velem
[i
].vertex_buffer_index
;
424 r300
->rws
->buffer_unmap(r300_resource(r300
->vbuf_mgr
->real_vertex_buffer
[vbi
])->buf
);
430 static void r300_emit_draw_arrays(struct r300_context
*r300
,
434 boolean alt_num_verts
= count
> 65535;
437 if (count
>= (1 << 24)) {
438 fprintf(stderr
, "r300: Got a huge number of vertices: %i, "
439 "refusing to render.\n", count
);
443 r300_emit_draw_init(r300
, mode
, 0, count
-1);
445 BEGIN_CS(2 + (alt_num_verts
? 2 : 0));
447 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES
, count
);
449 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2
, 0);
450 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST
| (count
<< 16) |
451 r300_translate_primitive(mode
) |
452 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
456 static void r300_emit_draw_elements(struct r300_context
*r300
,
457 struct pipe_resource
* indexBuffer
,
464 uint16_t *imm_indices3
)
466 uint32_t count_dwords
, offset_dwords
;
467 boolean alt_num_verts
= count
> 65535;
470 if (count
>= (1 << 24) || maxIndex
>= (1 << 24)) {
471 fprintf(stderr
, "r300: Got a huge number of vertices: %i, "
472 "refusing to render (maxIndex: %i).\n", count
, maxIndex
);
476 DBG(r300
, DBG_DRAW
, "r300: Indexbuf of %u indices, min %u max %u\n",
477 count
, minIndex
, maxIndex
);
479 r300_emit_draw_init(r300
, mode
, minIndex
, maxIndex
);
481 /* If start is odd, render the first triangle with indices embedded
482 * in the command stream. This will increase start by 3 and make it
483 * even. We can then proceed without a fallback. */
484 if (indexSize
== 2 && (start
& 1) &&
485 mode
== PIPE_PRIM_TRIANGLES
) {
487 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, 2);
488 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (3 << 16) |
489 R300_VAP_VF_CNTL__PRIM_TRIANGLES
);
490 OUT_CS(imm_indices3
[1] << 16 | imm_indices3
[0]);
491 OUT_CS(imm_indices3
[2]);
500 offset_dwords
= indexSize
* start
/ sizeof(uint32_t);
502 BEGIN_CS(8 + (alt_num_verts
? 2 : 0));
504 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES
, count
);
506 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, 0);
507 if (indexSize
== 4) {
508 count_dwords
= count
;
509 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
510 R300_VAP_VF_CNTL__INDEX_SIZE_32bit
|
511 r300_translate_primitive(mode
) |
512 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
514 count_dwords
= (count
+ 1) / 2;
515 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
516 r300_translate_primitive(mode
) |
517 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
520 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER
, 2);
521 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR
| (R300_VAP_PORT_IDX0
>> 2) |
522 (0 << R300_INDX_BUFFER_SKIP_SHIFT
));
523 OUT_CS(offset_dwords
<< 2);
524 OUT_CS(count_dwords
);
525 OUT_CS_RELOC(r300_resource(indexBuffer
));
529 static void r300_draw_elements_immediate(struct r300_context
*r300
,
530 int indexBias
, unsigned minIndex
,
531 unsigned maxIndex
, unsigned mode
,
532 unsigned start
, unsigned count
)
537 unsigned index_size
= r300
->index_buffer
.index_size
;
538 unsigned i
, count_dwords
= index_size
== 4 ? count
: (count
+ 1) / 2;
541 /* 19 dwords for r300_draw_elements_immediate. Give up if the function fails. */
542 if (!r300_prepare_for_rendering(r300
,
543 PREP_FIRST_DRAW
| PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
|
544 PREP_INDEXED
, NULL
, 2+count_dwords
, 0, indexBias
))
547 r300_emit_draw_init(r300
, mode
, minIndex
, maxIndex
);
549 BEGIN_CS(2 + count_dwords
);
550 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, count_dwords
);
552 switch (index_size
) {
554 ptr1
= r300_resource(r300
->index_buffer
.buffer
)->b
.user_ptr
;
557 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
558 r300_translate_primitive(mode
));
560 if (indexBias
&& !r300
->screen
->caps
.index_bias_supported
) {
561 for (i
= 0; i
< count
-1; i
+= 2)
562 OUT_CS(((ptr1
[i
+1] + indexBias
) << 16) |
563 (ptr1
[i
] + indexBias
));
566 OUT_CS(ptr1
[i
] + indexBias
);
568 for (i
= 0; i
< count
-1; i
+= 2)
569 OUT_CS(((ptr1
[i
+1]) << 16) |
578 ptr2
= (uint16_t*)r300_resource(r300
->index_buffer
.buffer
)->b
.user_ptr
;
581 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
582 r300_translate_primitive(mode
));
584 if (indexBias
&& !r300
->screen
->caps
.index_bias_supported
) {
585 for (i
= 0; i
< count
-1; i
+= 2)
586 OUT_CS(((ptr2
[i
+1] + indexBias
) << 16) |
587 (ptr2
[i
] + indexBias
));
590 OUT_CS(ptr2
[i
] + indexBias
);
592 OUT_CS_TABLE(ptr2
, count_dwords
);
597 ptr4
= (uint32_t*)r300_resource(r300
->index_buffer
.buffer
)->b
.user_ptr
;
600 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
601 R300_VAP_VF_CNTL__INDEX_SIZE_32bit
|
602 r300_translate_primitive(mode
));
604 if (indexBias
&& !r300
->screen
->caps
.index_bias_supported
) {
605 for (i
= 0; i
< count
; i
++)
606 OUT_CS(ptr4
[i
] + indexBias
);
608 OUT_CS_TABLE(ptr4
, count_dwords
);
615 static void r300_draw_elements(struct r300_context
*r300
, int indexBias
,
616 unsigned minIndex
, unsigned maxIndex
,
617 unsigned mode
, unsigned start
, unsigned count
)
619 struct pipe_resource
*indexBuffer
= r300
->index_buffer
.buffer
;
620 unsigned indexSize
= r300
->index_buffer
.index_size
;
621 struct pipe_resource
* orgIndexBuffer
= indexBuffer
;
622 boolean alt_num_verts
= r300
->screen
->caps
.is_r500
&&
624 r300
->rws
->get_value(r300
->rws
, R300_VID_DRM_2_3_0
);
625 unsigned short_count
;
626 int buffer_offset
= 0, index_offset
= 0; /* for index bias emulation */
627 uint16_t indices3
[3];
629 if (indexBias
&& !r300
->screen
->caps
.index_bias_supported
) {
630 r300_split_index_bias(r300
, indexBias
, &buffer_offset
, &index_offset
);
633 r300_translate_index_buffer(r300
, &indexBuffer
, &indexSize
, index_offset
,
636 /* Fallback for misaligned ushort indices. */
637 if (indexSize
== 2 && (start
& 1) &&
638 !r300_resource(indexBuffer
)->b
.user_ptr
) {
639 /* If we got here, then orgIndexBuffer == indexBuffer. */
640 uint16_t *ptr
= r300
->rws
->buffer_map(r300_resource(orgIndexBuffer
)->buf
,
643 PIPE_TRANSFER_UNSYNCHRONIZED
);
645 if (mode
== PIPE_PRIM_TRIANGLES
) {
646 memcpy(indices3
, ptr
+ start
, 6);
648 /* Copy the mapped index buffer directly to the upload buffer.
649 * The start index will be aligned simply from the fact that
650 * every sub-buffer in the upload buffer is aligned. */
651 r300_upload_index_buffer(r300
, &indexBuffer
, indexSize
, &start
,
652 count
, (uint8_t*)ptr
);
654 r300
->rws
->buffer_unmap(r300_resource(orgIndexBuffer
)->buf
);
656 if (r300_resource(indexBuffer
)->b
.user_ptr
)
657 r300_upload_index_buffer(r300
, &indexBuffer
, indexSize
,
659 r300_resource(indexBuffer
)->b
.user_ptr
);
662 /* 19 dwords for emit_draw_elements. Give up if the function fails. */
663 if (!r300_prepare_for_rendering(r300
,
664 PREP_FIRST_DRAW
| PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
|
665 PREP_INDEXED
, indexBuffer
, 19, buffer_offset
, indexBias
))
668 if (alt_num_verts
|| count
<= 65535) {
669 r300_emit_draw_elements(r300
, indexBuffer
, indexSize
,
670 minIndex
, maxIndex
, mode
, start
, count
, indices3
);
673 if (indexSize
== 2 && (start
& 1))
674 short_count
= MIN2(count
, 65535);
676 short_count
= MIN2(count
, 65534);
678 r300_emit_draw_elements(r300
, indexBuffer
, indexSize
,
680 mode
, start
, short_count
, indices3
);
682 start
+= short_count
;
683 count
-= short_count
;
685 /* 15 dwords for emit_draw_elements */
687 if (!r300_prepare_for_rendering(r300
,
688 PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
| PREP_INDEXED
,
689 indexBuffer
, 19, buffer_offset
, indexBias
))
696 if (indexBuffer
!= orgIndexBuffer
) {
697 pipe_resource_reference( &indexBuffer
, NULL
);
701 static void r300_draw_arrays(struct r300_context
*r300
, unsigned mode
,
702 unsigned start
, unsigned count
)
704 boolean alt_num_verts
= r300
->screen
->caps
.is_r500
&&
706 r300
->rws
->get_value(r300
->rws
, R300_VID_DRM_2_3_0
);
707 unsigned short_count
;
709 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
710 if (!r300_prepare_for_rendering(r300
,
711 PREP_FIRST_DRAW
| PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
,
715 if (alt_num_verts
|| count
<= 65535) {
716 r300_emit_draw_arrays(r300
, mode
, count
);
719 short_count
= MIN2(count
, 65535);
720 r300_emit_draw_arrays(r300
, mode
, short_count
);
722 start
+= short_count
;
723 count
-= short_count
;
725 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
727 if (!r300_prepare_for_rendering(r300
,
728 PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
, NULL
, 9,
736 static void r300_draw_vbo(struct pipe_context
* pipe
,
737 const struct pipe_draw_info
*info
)
739 struct r300_context
* r300
= r300_context(pipe
);
740 unsigned count
= info
->count
;
741 boolean buffers_updated
, uploader_flushed
;
742 boolean indexed
= info
->indexed
&& r300
->index_buffer
.buffer
;
743 unsigned start_indexed
= info
->start
+ r300
->index_buffer
.offset
;
744 int max_index
= MIN2(r300
->vbuf_mgr
->max_index
, info
->max_index
);
746 if (r300
->skip_rendering
||
747 !u_trim_pipe_prim(info
->mode
, &count
)) {
751 /* Start the vbuf manager and update buffers if needed. */
752 u_vbuf_mgr_draw_begin(r300
->vbuf_mgr
, info
,
753 &buffers_updated
, &uploader_flushed
);
754 if (buffers_updated
) {
755 r300
->vertex_arrays_dirty
= TRUE
;
759 r300_update_derived_state(r300
);
763 r300_resource(r300
->index_buffer
.buffer
)->b
.user_ptr
) {
764 r300_draw_elements_immediate(r300
, info
->index_bias
,
765 info
->min_index
, max_index
,
766 info
->mode
, start_indexed
, count
);
768 r300_draw_elements(r300
, info
->index_bias
, info
->min_index
,
769 max_index
, info
->mode
, start_indexed
, count
);
772 if (immd_is_good_idea(r300
, count
)) {
773 r300_draw_arrays_immediate(r300
, info
->mode
, info
->start
, count
);
775 r300_draw_arrays(r300
, info
->mode
, info
->start
, count
);
779 u_vbuf_mgr_draw_end(r300
->vbuf_mgr
);
782 /****************************************************************************
783 * The rest of this file is for SW TCL rendering only. Please be polite and *
784 * keep these functions separated so that they are easier to locate. ~C. *
785 ***************************************************************************/
787 /* SW TCL elements, using Draw. */
788 static void r300_swtcl_draw_vbo(struct pipe_context
* pipe
,
789 const struct pipe_draw_info
*info
)
791 struct r300_context
* r300
= r300_context(pipe
);
792 struct pipe_transfer
*vb_transfer
[PIPE_MAX_ATTRIBS
];
793 struct pipe_transfer
*ib_transfer
= NULL
;
794 unsigned count
= info
->count
;
796 void *indices
= NULL
;
797 boolean indexed
= info
->indexed
&& r300
->index_buffer
.buffer
;
799 if (r300
->skip_rendering
) {
803 if (!u_trim_pipe_prim(info
->mode
, &count
)) {
807 r300_update_derived_state(r300
);
809 r300_reserve_cs_dwords(r300
,
810 PREP_FIRST_DRAW
| PREP_EMIT_AOS_SWTCL
|
811 (indexed
? PREP_INDEXED
: 0),
814 for (i
= 0; i
< r300
->vbuf_mgr
->nr_vertex_buffers
; i
++) {
815 if (r300
->vbuf_mgr
->vertex_buffer
[i
].buffer
) {
816 void *buf
= pipe_buffer_map(pipe
,
817 r300
->vbuf_mgr
->vertex_buffer
[i
].buffer
,
819 PIPE_TRANSFER_UNSYNCHRONIZED
,
821 draw_set_mapped_vertex_buffer(r300
->draw
, i
, buf
);
826 indices
= pipe_buffer_map(pipe
, r300
->index_buffer
.buffer
,
828 PIPE_TRANSFER_UNSYNCHRONIZED
, &ib_transfer
);
831 draw_set_mapped_index_buffer(r300
->draw
, indices
);
833 r300
->draw_vbo_locked
= TRUE
;
834 r300
->draw_first_emitted
= FALSE
;
835 draw_vbo(r300
->draw
, info
);
836 draw_flush(r300
->draw
);
837 r300
->draw_vbo_locked
= FALSE
;
839 for (i
= 0; i
< r300
->vbuf_mgr
->nr_vertex_buffers
; i
++) {
840 if (r300
->vbuf_mgr
->vertex_buffer
[i
].buffer
) {
841 pipe_buffer_unmap(pipe
, vb_transfer
[i
]);
842 draw_set_mapped_vertex_buffer(r300
->draw
, i
, NULL
);
847 pipe_buffer_unmap(pipe
, ib_transfer
);
848 draw_set_mapped_index_buffer(r300
->draw
, NULL
);
852 /* Object for rendering using Draw. */
855 struct vbuf_render base
;
858 struct r300_context
* r300
;
860 /* Vertex information */
869 struct pipe_transfer
*vbo_transfer
;
872 static INLINE
struct r300_render
*
873 r300_render(struct vbuf_render
* render
)
875 return (struct r300_render
*)render
;
878 static const struct vertex_info
*
879 r300_render_get_vertex_info(struct vbuf_render
* render
)
881 struct r300_render
* r300render
= r300_render(render
);
882 struct r300_context
* r300
= r300render
->r300
;
884 return &r300
->vertex_info
;
887 static boolean
r300_render_allocate_vertices(struct vbuf_render
* render
,
891 struct r300_render
* r300render
= r300_render(render
);
892 struct r300_context
* r300
= r300render
->r300
;
893 struct pipe_screen
* screen
= r300
->context
.screen
;
894 size_t size
= (size_t)vertex_size
* (size_t)count
;
896 DBG(r300
, DBG_DRAW
, "r300: render_allocate_vertices (size: %d)\n", size
);
898 if (size
+ r300
->draw_vbo_offset
> r300
->draw_vbo_size
)
900 pipe_resource_reference(&r300
->vbo
, NULL
);
901 r300
->vbo
= pipe_buffer_create(screen
,
902 PIPE_BIND_VERTEX_BUFFER
,
904 R300_MAX_DRAW_VBO_SIZE
);
905 r300
->draw_vbo_offset
= 0;
906 r300
->draw_vbo_size
= R300_MAX_DRAW_VBO_SIZE
;
909 r300render
->vertex_size
= vertex_size
;
911 return (r300
->vbo
) ? TRUE
: FALSE
;
914 static void* r300_render_map_vertices(struct vbuf_render
* render
)
916 struct r300_render
* r300render
= r300_render(render
);
917 struct r300_context
* r300
= r300render
->r300
;
919 assert(!r300render
->vbo_transfer
);
921 DBG(r300
, DBG_DRAW
, "r300: render_map_vertices\n");
923 r300render
->vbo_ptr
= pipe_buffer_map(&r300render
->r300
->context
,
925 PIPE_TRANSFER_WRITE
|
926 PIPE_TRANSFER_UNSYNCHRONIZED
,
927 &r300render
->vbo_transfer
);
929 assert(r300render
->vbo_ptr
);
931 return ((uint8_t*)r300render
->vbo_ptr
+ r300
->draw_vbo_offset
);
934 static void r300_render_unmap_vertices(struct vbuf_render
* render
,
938 struct r300_render
* r300render
= r300_render(render
);
939 struct pipe_context
* context
= &r300render
->r300
->context
;
940 struct r300_context
* r300
= r300render
->r300
;
942 assert(r300render
->vbo_transfer
);
944 DBG(r300
, DBG_DRAW
, "r300: render_unmap_vertices\n");
946 r300render
->vbo_max_used
= MAX2(r300render
->vbo_max_used
,
947 r300render
->vertex_size
* (max
+ 1));
948 pipe_buffer_unmap(context
, r300render
->vbo_transfer
);
950 r300render
->vbo_transfer
= NULL
;
953 static void r300_render_release_vertices(struct vbuf_render
* render
)
955 struct r300_render
* r300render
= r300_render(render
);
956 struct r300_context
* r300
= r300render
->r300
;
958 DBG(r300
, DBG_DRAW
, "r300: render_release_vertices\n");
960 r300
->draw_vbo_offset
+= r300render
->vbo_max_used
;
961 r300render
->vbo_max_used
= 0;
964 static boolean
r300_render_set_primitive(struct vbuf_render
* render
,
967 struct r300_render
* r300render
= r300_render(render
);
969 r300render
->prim
= prim
;
970 r300render
->hwprim
= r300_translate_primitive(prim
);
975 static void r300_render_draw_arrays(struct vbuf_render
* render
,
979 struct r300_render
* r300render
= r300_render(render
);
980 struct r300_context
* r300
= r300render
->r300
;
986 (void) i
; (void) ptr
;
988 DBG(r300
, DBG_DRAW
, "r300: render_draw_arrays (count: %d)\n", count
);
990 if (r300
->draw_first_emitted
) {
991 if (!r300_prepare_for_rendering(r300
,
992 PREP_FIRST_DRAW
| PREP_EMIT_AOS_SWTCL
,
996 if (!r300_emit_states(r300
,
997 PREP_FIRST_DRAW
| PREP_EMIT_AOS_SWTCL
,
1003 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
1004 r300_provoking_vertex_fixes(r300
, r300render
->prim
));
1005 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX
, count
- 1);
1006 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2
, 0);
1007 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST
| (count
<< 16) |
1008 r300render
->hwprim
);
1011 r300
->draw_first_emitted
= TRUE
;
1014 static void r300_render_draw_elements(struct vbuf_render
* render
,
1015 const ushort
* indices
,
1018 struct r300_render
* r300render
= r300_render(render
);
1019 struct r300_context
* r300
= r300render
->r300
;
1021 unsigned end_cs_dwords
;
1022 unsigned max_index
= (r300
->draw_vbo_size
- r300
->draw_vbo_offset
) /
1023 (r300render
->r300
->vertex_info
.size
* 4) - 1;
1024 unsigned short_count
;
1025 unsigned free_dwords
;
1028 DBG(r300
, DBG_DRAW
, "r300: render_draw_elements (count: %d)\n", count
);
1030 if (r300
->draw_first_emitted
) {
1031 if (!r300_prepare_for_rendering(r300
,
1032 PREP_FIRST_DRAW
| PREP_EMIT_AOS_SWTCL
| PREP_INDEXED
,
1036 if (!r300_emit_states(r300
,
1037 PREP_FIRST_DRAW
| PREP_EMIT_AOS_SWTCL
| PREP_INDEXED
,
1042 /* Below we manage the CS space manually because there may be more
1043 * indices than it can fit in CS. */
1045 end_cs_dwords
= r300_get_num_cs_end_dwords(r300
);
1048 free_dwords
= R300_MAX_CMDBUF_DWORDS
- r300
->cs
->cdw
;
1050 short_count
= MIN2(count
, (free_dwords
- end_cs_dwords
- 6) * 2);
1052 BEGIN_CS(6 + (short_count
+1)/2);
1053 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
1054 r300_provoking_vertex_fixes(r300
, r300render
->prim
));
1055 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX
, max_index
);
1056 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, (short_count
+1)/2);
1057 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (short_count
<< 16) |
1058 r300render
->hwprim
);
1059 for (i
= 0; i
< short_count
-1; i
+= 2) {
1060 OUT_CS(indices
[i
+1] << 16 | indices
[i
]);
1062 if (short_count
% 2) {
1063 OUT_CS(indices
[short_count
-1]);
1067 /* OK now subtract the emitted indices and see if we need to emit
1068 * another draw packet. */
1069 indices
+= short_count
;
1070 count
-= short_count
;
1073 if (!r300_prepare_for_rendering(r300
,
1074 PREP_EMIT_AOS_SWTCL
| PREP_INDEXED
,
1078 end_cs_dwords
= r300_get_num_cs_end_dwords(r300
);
1082 r300
->draw_first_emitted
= TRUE
;
1085 static void r300_render_destroy(struct vbuf_render
* render
)
1090 static struct vbuf_render
* r300_render_create(struct r300_context
* r300
)
1092 struct r300_render
* r300render
= CALLOC_STRUCT(r300_render
);
1094 r300render
->r300
= r300
;
1096 r300render
->base
.max_vertex_buffer_bytes
= 1024 * 1024;
1097 r300render
->base
.max_indices
= 16 * 1024;
1099 r300render
->base
.get_vertex_info
= r300_render_get_vertex_info
;
1100 r300render
->base
.allocate_vertices
= r300_render_allocate_vertices
;
1101 r300render
->base
.map_vertices
= r300_render_map_vertices
;
1102 r300render
->base
.unmap_vertices
= r300_render_unmap_vertices
;
1103 r300render
->base
.set_primitive
= r300_render_set_primitive
;
1104 r300render
->base
.draw_elements
= r300_render_draw_elements
;
1105 r300render
->base
.draw_arrays
= r300_render_draw_arrays
;
1106 r300render
->base
.release_vertices
= r300_render_release_vertices
;
1107 r300render
->base
.destroy
= r300_render_destroy
;
1109 return &r300render
->base
;
1112 struct draw_stage
* r300_draw_stage(struct r300_context
* r300
)
1114 struct vbuf_render
* render
;
1115 struct draw_stage
* stage
;
1117 render
= r300_render_create(r300
);
1123 stage
= draw_vbuf_stage(r300
->draw
, render
);
1126 render
->destroy(render
);
1130 draw_set_render(r300
->draw
, render
);
1135 void r300_draw_flush_vbuf(struct r300_context
*r300
)
1137 pipe_resource_reference(&r300
->vbo
, NULL
);
1138 r300
->draw_vbo_size
= 0;
1141 /****************************************************************************
1142 * End of SW TCL functions *
1143 ***************************************************************************/
1145 /* This functions is used to draw a rectangle for the blitter module.
1147 * If we rendered a quad, the pixels on the main diagonal
1148 * would be computed and stored twice, which makes the clear/copy codepaths
1149 * somewhat inefficient. Instead we use a rectangular point sprite. */
1150 static void r300_blitter_draw_rectangle(struct blitter_context
*blitter
,
1151 unsigned x1
, unsigned y1
,
1152 unsigned x2
, unsigned y2
,
1154 enum blitter_attrib_type type
,
1155 const float attrib
[4])
1157 struct r300_context
*r300
= r300_context(util_blitter_get_pipe(blitter
));
1158 unsigned last_sprite_coord_enable
= r300
->sprite_coord_enable
;
1159 unsigned width
= x2
- x1
;
1160 unsigned height
= y2
- y1
;
1161 unsigned vertex_size
=
1162 type
== UTIL_BLITTER_ATTRIB_COLOR
|| !r300
->draw
? 8 : 4;
1163 unsigned dwords
= 13 + vertex_size
+
1164 (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
? 7 : 0);
1165 const float zeros
[4] = {0, 0, 0, 0};
1168 r300
->context
.set_vertex_buffers(&r300
->context
, 0, NULL
);
1170 if (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
)
1171 r300
->sprite_coord_enable
= 1;
1173 r300_update_derived_state(r300
);
1175 /* Mark some states we don't care about as non-dirty. */
1176 r300
->clip_state
.dirty
= FALSE
;
1177 r300
->viewport_state
.dirty
= FALSE
;
1179 if (!r300_prepare_for_rendering(r300
, PREP_FIRST_DRAW
, NULL
, dwords
, 0, 0))
1182 DBG(r300
, DBG_DRAW
, "r300: draw_rectangle\n");
1186 OUT_CS_REG(R300_GA_POINT_SIZE
, (height
* 6) | ((width
* 6) << 16));
1188 if (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
) {
1189 /* Set up the GA to generate texcoords. */
1190 OUT_CS_REG(R300_GB_ENABLE
, R300_GB_POINT_STUFF_ENABLE
|
1191 (R300_GB_TEX_STR
<< R300_GB_TEX0_SOURCE_SHIFT
));
1192 OUT_CS_REG_SEQ(R300_GA_POINT_S0
, 4);
1193 OUT_CS_32F(attrib
[0]);
1194 OUT_CS_32F(attrib
[3]);
1195 OUT_CS_32F(attrib
[2]);
1196 OUT_CS_32F(attrib
[1]);
1199 /* Set up VAP controls. */
1200 OUT_CS_REG(R300_VAP_CLIP_CNTL
, R300_CLIP_DISABLE
);
1201 OUT_CS_REG(R300_VAP_VTE_CNTL
, R300_VTX_XY_FMT
| R300_VTX_Z_FMT
);
1202 OUT_CS_REG(R300_VAP_VTX_SIZE
, vertex_size
);
1203 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
1208 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2
, vertex_size
);
1209 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED
| (1 << 16) |
1210 R300_VAP_VF_CNTL__PRIM_POINTS
);
1212 OUT_CS_32F(x1
+ width
* 0.5f
);
1213 OUT_CS_32F(y1
+ height
* 0.5f
);
1217 if (vertex_size
== 8) {
1220 OUT_CS_TABLE(attrib
, 4);
1225 /* Restore the state. */
1226 r300_mark_atom_dirty(r300
, &r300
->clip_state
);
1227 r300_mark_atom_dirty(r300
, &r300
->rs_state
);
1228 r300_mark_atom_dirty(r300
, &r300
->viewport_state
);
1230 r300
->sprite_coord_enable
= last_sprite_coord_enable
;
1233 static void r300_resource_resolve(struct pipe_context
* pipe
,
1234 struct pipe_resource
* dest
,
1236 struct pipe_resource
* src
,
1239 struct r300_context
* r300
= r300_context(pipe
);
1240 struct pipe_surface
* srcsurf
, surf_tmpl
;
1241 struct r300_aa_state
*aa
= (struct r300_aa_state
*)r300
->aa_state
.state
;
1242 float color
[] = {0, 0, 0, 0};
1244 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1245 surf_tmpl
.format
= src
->format
;
1246 surf_tmpl
.usage
= 0; /* not really a surface hence no bind flags */
1247 surf_tmpl
.u
.tex
.level
= 0; /* msaa resources cannot have mipmaps */
1248 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
1249 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
1250 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
1251 surf_tmpl
.format
= dest
->format
;
1252 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
1253 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
1255 DBG(r300
, DBG_DRAW
, "r300: Resolving resource...\n");
1257 /* Enable AA resolve. */
1258 aa
->dest
= r300_surface(pipe
->create_surface(pipe
, dest
, &surf_tmpl
));
1261 R300_RB3D_AARESOLVE_CTL_AARESOLVE_MODE_RESOLVE
|
1262 R300_RB3D_AARESOLVE_CTL_AARESOLVE_ALPHA_AVERAGE
;
1263 r300
->aa_state
.size
= 10;
1264 r300_mark_atom_dirty(r300
, &r300
->aa_state
);
1266 /* Resolve the surface. */
1267 r300
->context
.clear_render_target(pipe
,
1268 srcsurf
, color
, 0, 0, src
->width0
, src
->height0
);
1270 /* Disable AA resolve. */
1271 aa
->aaresolve_ctl
= 0;
1272 r300
->aa_state
.size
= 4;
1273 r300_mark_atom_dirty(r300
, &r300
->aa_state
);
1275 pipe_surface_reference((struct pipe_surface
**)&srcsurf
, NULL
);
1276 pipe_surface_reference((struct pipe_surface
**)&aa
->dest
, NULL
);
1279 void r300_init_render_functions(struct r300_context
*r300
)
1281 /* Set draw functions based on presence of HW TCL. */
1282 if (r300
->screen
->caps
.has_tcl
) {
1283 r300
->context
.draw_vbo
= r300_draw_vbo
;
1285 r300
->context
.draw_vbo
= r300_swtcl_draw_vbo
;
1288 r300
->context
.resource_resolve
= r300_resource_resolve
;
1289 r300
->blitter
->draw_rectangle
= r300_blitter_draw_rectangle
;
1291 /* Plug in the two-sided stencil reference value fallback if needed. */
1292 if (!r300
->screen
->caps
.is_r500
)
1293 r300_plug_in_stencil_ref_fallback(r300
);