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 /* This function splits the index bias value into two parts:
131 * - buffer_offset: the value that can be safely added to buffer offsets
132 * in r300_emit_vertex_arrays (it must yield a positive offset when added to
133 * a vertex buffer offset)
134 * - index_offset: the value that must be manually subtracted from indices
135 * in an index buffer to achieve negative offsets. */
136 static void r300_split_index_bias(struct r300_context
*r300
, int index_bias
,
137 int *buffer_offset
, int *index_offset
)
139 struct pipe_vertex_buffer
*vb
, *vbufs
= r300
->vbuf_mgr
->vertex_buffer
;
140 struct pipe_vertex_element
*velem
= r300
->velems
->velem
;
144 if (index_bias
< 0) {
145 /* See how large index bias we may subtract. We must be careful
146 * here because negative buffer offsets are not allowed
148 max_neg_bias
= INT_MAX
;
149 for (i
= 0; i
< r300
->velems
->count
; i
++) {
150 vb
= &vbufs
[velem
[i
].vertex_buffer_index
];
151 size
= (vb
->buffer_offset
+ velem
[i
].src_offset
) / vb
->stride
;
152 max_neg_bias
= MIN2(max_neg_bias
, size
);
155 /* Now set the minimum allowed value. */
156 *buffer_offset
= MAX2(-max_neg_bias
, index_bias
);
158 /* A positive index bias is OK. */
159 *buffer_offset
= index_bias
;
162 *index_offset
= index_bias
- *buffer_offset
;
165 enum r300_prepare_flags
{
166 PREP_FIRST_DRAW
= (1 << 0), /* call emit_dirty_state and friends? */
167 PREP_VALIDATE_VBOS
= (1 << 1), /* validate VBOs? */
168 PREP_EMIT_AOS
= (1 << 2), /* call emit_vertex_arrays? */
169 PREP_EMIT_AOS_SWTCL
= (1 << 3), /* call emit_vertex_arrays_swtcl? */
170 PREP_INDEXED
= (1 << 4) /* is this draw_elements? */
174 * Check if the requested number of dwords is available in the CS and
176 * \param r300 The context.
177 * \param flags See r300_prepare_flags.
178 * \param cs_dwords The number of dwords to reserve in CS.
179 * \return TRUE if the CS was flushed
181 static boolean
r300_reserve_cs_dwords(struct r300_context
*r300
,
182 enum r300_prepare_flags flags
,
185 boolean flushed
= FALSE
;
186 boolean first_draw
= flags
& PREP_FIRST_DRAW
;
187 boolean emit_vertex_arrays
= flags
& PREP_EMIT_AOS
;
188 boolean emit_vertex_arrays_swtcl
= flags
& PREP_EMIT_AOS_SWTCL
;
190 /* Add dirty state, index offset, and AOS. */
192 cs_dwords
+= r300_get_num_dirty_dwords(r300
);
194 if (r300
->screen
->caps
.index_bias_supported
)
195 cs_dwords
+= 2; /* emit_index_offset */
197 if (emit_vertex_arrays
)
198 cs_dwords
+= 55; /* emit_vertex_arrays */
200 if (emit_vertex_arrays_swtcl
)
201 cs_dwords
+= 7; /* emit_vertex_arrays_swtcl */
204 cs_dwords
+= r300_get_num_cs_end_dwords(r300
);
206 /* Reserve requested CS space. */
207 if (cs_dwords
> (R300_MAX_CMDBUF_DWORDS
- r300
->cs
->cdw
)) {
208 r300
->context
.flush(&r300
->context
, 0, NULL
);
216 * Validate buffers and emit dirty state.
217 * \param r300 The context.
218 * \param flags See r300_prepare_flags.
219 * \param index_buffer The index buffer to validate. The parameter may be NULL.
220 * \param buffer_offset The offset passed to emit_vertex_arrays.
221 * \param index_bias The index bias to emit.
222 * \return TRUE if rendering should be skipped
224 static boolean
r300_emit_states(struct r300_context
*r300
,
225 enum r300_prepare_flags flags
,
226 struct pipe_resource
*index_buffer
,
229 boolean user_buffers
)
231 boolean first_draw
= flags
& PREP_FIRST_DRAW
;
232 boolean emit_vertex_arrays
= flags
& PREP_EMIT_AOS
;
233 boolean emit_vertex_arrays_swtcl
= flags
& PREP_EMIT_AOS_SWTCL
;
234 boolean indexed
= flags
& PREP_INDEXED
;
235 boolean validate_vbos
= flags
& PREP_VALIDATE_VBOS
;
237 /* Validate buffers and emit dirty state if needed. */
239 if (r300
->validate_buffers
) {
240 if (!r300_emit_buffer_validate(r300
, validate_vbos
,
242 fprintf(stderr
, "r300: CS space validation failed. "
243 "(not enough memory?) Skipping rendering.\n");
247 /* Consider the validation done only if everything was validated. */
249 r300
->validate_buffers
= FALSE
;
251 r300
->upload_vb_validated
= TRUE
;
252 if (r300
->index_buffer
.buffer
&&
253 r300_buffer(r300
->index_buffer
.buffer
)->b
.user_ptr
) {
254 r300
->upload_ib_validated
= TRUE
;
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_emit_vertex_arrays(r300
, buffer_offset
, indexed
);
270 if (emit_vertex_arrays_swtcl
)
271 r300_emit_vertex_arrays_swtcl(r300
, indexed
);
278 * Check if the requested number of dwords is available in the CS and
279 * if not, flush. Then validate buffers and emit dirty state.
280 * \param r300 The context.
281 * \param flags See r300_prepare_flags.
282 * \param index_buffer The index buffer to validate. The parameter may be NULL.
283 * \param cs_dwords The number of dwords to reserve in CS.
284 * \param buffer_offset The offset passed to emit_vertex_arrays.
285 * \param index_bias The index bias to emit.
286 * \return TRUE if rendering should be skipped
288 static boolean
r300_prepare_for_rendering(struct r300_context
*r300
,
289 enum r300_prepare_flags flags
,
290 struct pipe_resource
*index_buffer
,
294 boolean user_buffers
)
296 if (r300_reserve_cs_dwords(r300
, flags
, cs_dwords
))
297 flags
|= PREP_FIRST_DRAW
;
299 return r300_emit_states(r300
, flags
, index_buffer
, buffer_offset
,
300 index_bias
, user_buffers
);
303 static boolean
immd_is_good_idea(struct r300_context
*r300
,
306 struct pipe_vertex_element
* velem
;
307 struct pipe_resource
*buf
;
308 boolean checked
[PIPE_MAX_ATTRIBS
] = {0};
309 unsigned vertex_element_count
= r300
->velems
->count
;
312 if (DBG_ON(r300
, DBG_NO_IMMD
)) {
320 if (count
* r300
->velems
->vertex_size_dwords
> IMMD_DWORDS
) {
324 /* We shouldn't map buffers referenced by CS, busy buffers,
325 * and ones placed in VRAM. */
326 for (i
= 0; i
< vertex_element_count
; i
++) {
327 velem
= &r300
->velems
->velem
[i
];
328 vbi
= velem
->vertex_buffer_index
;
331 buf
= r300
->vbuf_mgr
->real_vertex_buffer
[vbi
];
333 if (!(r300_buffer(buf
)->domain
& R300_DOMAIN_GTT
)) {
337 if (r300_buffer_is_referenced(&r300
->context
, buf
,
338 R300_REF_CS
| R300_REF_HW
)) {
339 /* It's a very bad idea to map it... */
348 /*****************************************************************************
349 * The HWTCL draw functions. *
350 ****************************************************************************/
352 static void r300_emit_draw_arrays_immediate(struct r300_context
*r300
,
357 struct pipe_vertex_element
* velem
;
358 struct pipe_vertex_buffer
* vbuf
;
359 unsigned vertex_element_count
= r300
->velems
->count
;
362 /* Size of the vertex, in dwords. */
363 unsigned vertex_size
= r300
->velems
->vertex_size_dwords
;
365 /* The number of dwords for this draw operation. */
366 unsigned dwords
= 9 + count
* vertex_size
;
368 /* Size of the vertex element, in dwords. */
369 unsigned size
[PIPE_MAX_ATTRIBS
];
371 /* Stride to the same attrib in the next vertex in the vertex buffer,
373 unsigned stride
[PIPE_MAX_ATTRIBS
];
375 /* Mapped vertex buffers. */
376 uint32_t* map
[PIPE_MAX_ATTRIBS
];
377 uint32_t* mapelem
[PIPE_MAX_ATTRIBS
];
378 struct pipe_transfer
* transfer
[PIPE_MAX_ATTRIBS
] = {0};
382 if (!r300_prepare_for_rendering(r300
, PREP_FIRST_DRAW
, NULL
, dwords
, 0, 0,
386 /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
387 for (i
= 0; i
< vertex_element_count
; i
++) {
388 velem
= &r300
->velems
->velem
[i
];
389 size
[i
] = r300
->velems
->format_size
[i
] / 4;
390 vbi
= velem
->vertex_buffer_index
;
391 vbuf
= &r300
->vbuf_mgr
->vertex_buffer
[vbi
];
392 stride
[i
] = vbuf
->stride
/ 4;
394 /* Map the buffer. */
395 if (!transfer
[vbi
]) {
396 map
[vbi
] = (uint32_t*)pipe_buffer_map(&r300
->context
,
397 r300
->vbuf_mgr
->real_vertex_buffer
[vbi
],
400 map
[vbi
] += (vbuf
->buffer_offset
/ 4) + stride
[i
] * start
;
402 mapelem
[i
] = map
[vbi
] + (velem
->src_offset
/ 4);
406 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
407 r300_provoking_vertex_fixes(r300
, mode
));
408 OUT_CS_REG(R300_VAP_VTX_SIZE
, vertex_size
);
409 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
412 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2
, count
* vertex_size
);
413 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED
| (count
<< 16) |
414 r300_translate_primitive(mode
));
417 for (v
= 0; v
< count
; v
++) {
418 for (i
= 0; i
< vertex_element_count
; i
++) {
419 OUT_CS_TABLE(&mapelem
[i
][stride
[i
] * v
], size
[i
]);
425 for (i
= 0; i
< vertex_element_count
; i
++) {
426 vbi
= r300
->velems
->velem
[i
].vertex_buffer_index
;
429 pipe_buffer_unmap(&r300
->context
, transfer
[vbi
]);
430 transfer
[vbi
] = NULL
;
435 static void r300_emit_draw_arrays(struct r300_context
*r300
,
439 boolean alt_num_verts
= count
> 65535;
442 if (count
>= (1 << 24)) {
443 fprintf(stderr
, "r300: Got a huge number of vertices: %i, "
444 "refusing to render.\n", count
);
448 BEGIN_CS(7 + (alt_num_verts
? 2 : 0));
450 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES
, count
);
452 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
453 r300_provoking_vertex_fixes(r300
, mode
));
454 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
457 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2
, 0);
458 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST
| (count
<< 16) |
459 r300_translate_primitive(mode
) |
460 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
464 static void r300_emit_draw_elements(struct r300_context
*r300
,
465 struct pipe_resource
* indexBuffer
,
472 uint16_t *imm_indices3
)
474 uint32_t count_dwords
, offset_dwords
;
475 boolean alt_num_verts
= count
> 65535;
478 if (count
>= (1 << 24)) {
479 fprintf(stderr
, "r300: Got a huge number of vertices: %i, "
480 "refusing to render.\n", count
);
484 DBG(r300
, DBG_DRAW
, "r300: Indexbuf of %u indices, min %u max %u\n",
485 count
, minIndex
, maxIndex
);
488 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
489 r300_provoking_vertex_fixes(r300
, mode
));
490 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
495 /* If start is odd, render the first triangle with indices embedded
496 * in the command stream. This will increase start by 3 and make it
497 * even. We can then proceed without a fallback. */
498 if (indexSize
== 2 && (start
& 1) &&
499 mode
== PIPE_PRIM_TRIANGLES
) {
501 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, 2);
502 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (3 << 16) |
503 R300_VAP_VF_CNTL__PRIM_TRIANGLES
);
504 OUT_CS(imm_indices3
[1] << 16 | imm_indices3
[0]);
505 OUT_CS(imm_indices3
[2]);
514 offset_dwords
= indexSize
* start
/ sizeof(uint32_t);
516 BEGIN_CS(8 + (alt_num_verts
? 2 : 0));
518 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES
, count
);
520 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, 0);
521 if (indexSize
== 4) {
522 count_dwords
= count
;
523 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
524 R300_VAP_VF_CNTL__INDEX_SIZE_32bit
|
525 r300_translate_primitive(mode
) |
526 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
528 count_dwords
= (count
+ 1) / 2;
529 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
530 r300_translate_primitive(mode
) |
531 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
534 /* INDX_BUFFER is a truly special packet3.
535 * Unlike most other packet3, where the offset is after the count,
536 * the order is reversed, so the relocation ends up carrying the
537 * size of the indexbuf instead of the offset.
539 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER
, 2);
540 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR
| (R300_VAP_PORT_IDX0
>> 2) |
541 (0 << R300_INDX_BUFFER_SKIP_SHIFT
));
542 OUT_CS(offset_dwords
<< 2);
543 OUT_CS_BUF_RELOC(indexBuffer
, count_dwords
);
548 /* This is the fast-path drawing & emission for HW TCL. */
549 static void r300_draw_range_elements(struct pipe_context
* pipe
,
556 boolean user_buffers
)
558 struct r300_context
* r300
= r300_context(pipe
);
559 struct pipe_resource
*indexBuffer
= r300
->index_buffer
.buffer
;
560 unsigned indexSize
= r300
->index_buffer
.index_size
;
561 struct pipe_resource
* orgIndexBuffer
= indexBuffer
;
562 boolean alt_num_verts
= r300
->screen
->caps
.is_r500
&&
564 r300
->rws
->get_value(r300
->rws
, R300_VID_DRM_2_3_0
);
565 unsigned short_count
;
566 int buffer_offset
= 0, index_offset
= 0; /* for index bias emulation */
567 uint16_t indices3
[3];
569 if (indexBias
&& !r300
->screen
->caps
.index_bias_supported
) {
570 r300_split_index_bias(r300
, indexBias
, &buffer_offset
, &index_offset
);
573 r300_translate_index_buffer(r300
, &indexBuffer
, &indexSize
, index_offset
,
576 /* Fallback for misaligned ushort indices. */
577 if (indexSize
== 2 && (start
& 1) &&
578 !r300_buffer(indexBuffer
)->b
.user_ptr
) {
579 struct pipe_transfer
*transfer
;
580 struct pipe_resource
*userbuf
;
582 uint16_t *ptr
= pipe_buffer_map(pipe
, indexBuffer
,
583 PIPE_TRANSFER_READ
, &transfer
);
585 if (mode
== PIPE_PRIM_TRIANGLES
) {
586 memcpy(indices3
, ptr
+ start
, 6);
588 /* Copy the mapped index buffer directly to the upload buffer.
589 * The start index will be aligned simply from the fact that
590 * every sub-buffer in u_upload_mgr is aligned. */
591 userbuf
= pipe
->screen
->user_buffer_create(pipe
->screen
,
593 PIPE_BIND_INDEX_BUFFER
);
594 indexBuffer
= userbuf
;
595 r300_upload_index_buffer(r300
, &indexBuffer
, indexSize
, &start
, count
);
596 pipe_resource_reference(&userbuf
, NULL
);
598 pipe_buffer_unmap(pipe
, transfer
);
600 if (r300_buffer(indexBuffer
)->b
.user_ptr
)
601 r300_upload_index_buffer(r300
, &indexBuffer
, indexSize
, &start
, count
);
604 /* 19 dwords for emit_draw_elements. Give up if the function fails. */
605 if (!r300_prepare_for_rendering(r300
,
606 PREP_FIRST_DRAW
| PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
|
607 PREP_INDEXED
, indexBuffer
, 19, buffer_offset
, indexBias
, user_buffers
))
610 if (alt_num_verts
|| count
<= 65535) {
611 r300_emit_draw_elements(r300
, indexBuffer
, indexSize
,
612 minIndex
, maxIndex
, mode
, start
, count
, indices3
);
615 short_count
= MIN2(count
, 65534);
616 r300_emit_draw_elements(r300
, indexBuffer
, indexSize
,
618 mode
, start
, short_count
, indices3
);
620 start
+= short_count
;
621 count
-= short_count
;
623 /* 15 dwords for emit_draw_elements */
625 if (!r300_prepare_for_rendering(r300
,
626 PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
| PREP_INDEXED
,
627 indexBuffer
, 19, buffer_offset
, indexBias
, user_buffers
))
634 if (indexBuffer
!= orgIndexBuffer
) {
635 pipe_resource_reference( &indexBuffer
, NULL
);
639 static void r300_draw_arrays(struct pipe_context
* pipe
, unsigned mode
,
640 unsigned start
, unsigned count
,
641 boolean user_buffers
)
643 struct r300_context
* r300
= r300_context(pipe
);
644 boolean alt_num_verts
= r300
->screen
->caps
.is_r500
&&
646 r300
->rws
->get_value(r300
->rws
, R300_VID_DRM_2_3_0
);
647 unsigned short_count
;
649 if (immd_is_good_idea(r300
, count
)) {
650 r300_emit_draw_arrays_immediate(r300
, mode
, start
, count
);
652 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
653 if (!r300_prepare_for_rendering(r300
,
654 PREP_FIRST_DRAW
| PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
,
655 NULL
, 9, start
, 0, user_buffers
))
658 if (alt_num_verts
|| count
<= 65535) {
659 r300_emit_draw_arrays(r300
, mode
, count
);
662 short_count
= MIN2(count
, 65535);
663 r300_emit_draw_arrays(r300
, mode
, short_count
);
665 start
+= short_count
;
666 count
-= short_count
;
668 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
670 if (!r300_prepare_for_rendering(r300
,
671 PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
, NULL
, 9,
672 start
, 0, user_buffers
))
680 static void r300_draw_vbo(struct pipe_context
* pipe
,
681 const struct pipe_draw_info
*info
)
683 struct r300_context
* r300
= r300_context(pipe
);
684 unsigned count
= info
->count
;
685 boolean buffers_updated
, uploader_flushed
;
686 boolean indexed
= info
->indexed
&& r300
->index_buffer
.buffer
;
688 if (r300
->skip_rendering
) {
692 if (!u_trim_pipe_prim(info
->mode
, &count
)) {
696 u_vbuf_mgr_draw_begin(r300
->vbuf_mgr
, info
,
697 &buffers_updated
, &uploader_flushed
);
699 if (buffers_updated
) {
700 r300
->vertex_arrays_dirty
= TRUE
;
702 if (uploader_flushed
|| !r300
->upload_vb_validated
) {
703 r300
->upload_vb_validated
= FALSE
;
704 r300
->validate_buffers
= TRUE
;
707 r300
->upload_vb_validated
= FALSE
;
711 /* Compute the start for draw_elements, taking the offset into account. */
712 unsigned start_indexed
=
714 (r300
->index_buffer
.offset
/ r300
->index_buffer
.index_size
);
715 int max_index
= MIN2(r300
->vbuf_mgr
->max_index
, info
->max_index
);
717 assert(r300
->index_buffer
.offset
% r300
->index_buffer
.index_size
== 0);
719 /* Index buffer range checking. */
720 if ((start_indexed
+ count
) * r300
->index_buffer
.index_size
>
721 r300
->index_buffer
.buffer
->width0
) {
722 fprintf(stderr
, "r300: Invalid index buffer range. Skipping rendering.\n");
726 if (max_index
>= (1 << 24) - 1) {
727 fprintf(stderr
, "r300: Invalid max_index: %i. Skipping rendering...\n", max_index
);
731 r300_update_derived_state(r300
);
732 r300_draw_range_elements(pipe
, info
->index_bias
, info
->min_index
,
733 max_index
, info
->mode
, start_indexed
, count
,
736 r300_update_derived_state(r300
);
737 r300_draw_arrays(pipe
, info
->mode
, info
->start
, count
, buffers_updated
);
740 u_vbuf_mgr_draw_end(r300
->vbuf_mgr
);
743 /****************************************************************************
744 * The rest of this file is for SW TCL rendering only. Please be polite and *
745 * keep these functions separated so that they are easier to locate. ~C. *
746 ***************************************************************************/
748 /* SW TCL elements, using Draw. */
749 static void r300_swtcl_draw_vbo(struct pipe_context
* pipe
,
750 const struct pipe_draw_info
*info
)
752 struct r300_context
* r300
= r300_context(pipe
);
753 struct pipe_transfer
*vb_transfer
[PIPE_MAX_ATTRIBS
];
754 struct pipe_transfer
*ib_transfer
= NULL
;
755 unsigned count
= info
->count
;
757 void *indices
= NULL
;
758 boolean indexed
= info
->indexed
&& r300
->index_buffer
.buffer
;
760 if (r300
->skip_rendering
) {
764 if (!u_trim_pipe_prim(info
->mode
, &count
)) {
768 r300_update_derived_state(r300
);
770 r300_reserve_cs_dwords(r300
,
771 PREP_FIRST_DRAW
| PREP_EMIT_AOS_SWTCL
|
772 (indexed
? PREP_INDEXED
: 0),
775 for (i
= 0; i
< r300
->vbuf_mgr
->nr_vertex_buffers
; i
++) {
776 if (r300
->vbuf_mgr
->vertex_buffer
[i
].buffer
) {
777 void *buf
= pipe_buffer_map(pipe
,
778 r300
->vbuf_mgr
->vertex_buffer
[i
].buffer
,
781 draw_set_mapped_vertex_buffer(r300
->draw
, i
, buf
);
786 indices
= pipe_buffer_map(pipe
, r300
->index_buffer
.buffer
,
787 PIPE_TRANSFER_READ
, &ib_transfer
);
790 draw_set_mapped_index_buffer(r300
->draw
, indices
);
792 r300
->draw_vbo_locked
= TRUE
;
793 r300
->draw_first_emitted
= FALSE
;
794 draw_vbo(r300
->draw
, info
);
795 draw_flush(r300
->draw
);
796 r300
->draw_vbo_locked
= FALSE
;
798 for (i
= 0; i
< r300
->vbuf_mgr
->nr_vertex_buffers
; i
++) {
799 if (r300
->vbuf_mgr
->vertex_buffer
[i
].buffer
) {
800 pipe_buffer_unmap(pipe
, vb_transfer
[i
]);
801 draw_set_mapped_vertex_buffer(r300
->draw
, i
, NULL
);
806 pipe_buffer_unmap(pipe
, ib_transfer
);
807 draw_set_mapped_index_buffer(r300
->draw
, NULL
);
811 /* Object for rendering using Draw. */
814 struct vbuf_render base
;
817 struct r300_context
* r300
;
819 /* Vertex information */
828 struct pipe_transfer
*vbo_transfer
;
831 static INLINE
struct r300_render
*
832 r300_render(struct vbuf_render
* render
)
834 return (struct r300_render
*)render
;
837 static const struct vertex_info
*
838 r300_render_get_vertex_info(struct vbuf_render
* render
)
840 struct r300_render
* r300render
= r300_render(render
);
841 struct r300_context
* r300
= r300render
->r300
;
843 return &r300
->vertex_info
;
846 static boolean
r300_render_allocate_vertices(struct vbuf_render
* render
,
850 struct r300_render
* r300render
= r300_render(render
);
851 struct r300_context
* r300
= r300render
->r300
;
852 struct pipe_screen
* screen
= r300
->context
.screen
;
853 size_t size
= (size_t)vertex_size
* (size_t)count
;
855 DBG(r300
, DBG_DRAW
, "r300: render_allocate_vertices (size: %d)\n", size
);
857 if (size
+ r300
->draw_vbo_offset
> r300
->draw_vbo_size
)
859 pipe_resource_reference(&r300
->vbo
, NULL
);
860 r300
->vbo
= pipe_buffer_create(screen
,
861 PIPE_BIND_VERTEX_BUFFER
,
862 R300_MAX_DRAW_VBO_SIZE
);
863 r300
->draw_vbo_offset
= 0;
864 r300
->draw_vbo_size
= R300_MAX_DRAW_VBO_SIZE
;
865 r300
->validate_buffers
= TRUE
;
868 r300render
->vertex_size
= vertex_size
;
870 return (r300
->vbo
) ? TRUE
: FALSE
;
873 static void* r300_render_map_vertices(struct vbuf_render
* render
)
875 struct r300_render
* r300render
= r300_render(render
);
876 struct r300_context
* r300
= r300render
->r300
;
878 assert(!r300render
->vbo_transfer
);
880 DBG(r300
, DBG_DRAW
, "r300: render_map_vertices\n");
882 r300render
->vbo_ptr
= pipe_buffer_map(&r300render
->r300
->context
,
885 &r300render
->vbo_transfer
);
887 assert(r300render
->vbo_ptr
);
889 return ((uint8_t*)r300render
->vbo_ptr
+ r300
->draw_vbo_offset
);
892 static void r300_render_unmap_vertices(struct vbuf_render
* render
,
896 struct r300_render
* r300render
= r300_render(render
);
897 struct pipe_context
* context
= &r300render
->r300
->context
;
898 struct r300_context
* r300
= r300render
->r300
;
900 assert(r300render
->vbo_transfer
);
902 DBG(r300
, DBG_DRAW
, "r300: render_unmap_vertices\n");
904 r300render
->vbo_max_used
= MAX2(r300render
->vbo_max_used
,
905 r300render
->vertex_size
* (max
+ 1));
906 pipe_buffer_unmap(context
, r300render
->vbo_transfer
);
908 r300render
->vbo_transfer
= NULL
;
911 static void r300_render_release_vertices(struct vbuf_render
* render
)
913 struct r300_render
* r300render
= r300_render(render
);
914 struct r300_context
* r300
= r300render
->r300
;
916 DBG(r300
, DBG_DRAW
, "r300: render_release_vertices\n");
918 r300
->draw_vbo_offset
+= r300render
->vbo_max_used
;
919 r300render
->vbo_max_used
= 0;
922 static boolean
r300_render_set_primitive(struct vbuf_render
* render
,
925 struct r300_render
* r300render
= r300_render(render
);
927 r300render
->prim
= prim
;
928 r300render
->hwprim
= r300_translate_primitive(prim
);
933 static void r300_render_draw_arrays(struct vbuf_render
* render
,
937 struct r300_render
* r300render
= r300_render(render
);
938 struct r300_context
* r300
= r300render
->r300
;
944 (void) i
; (void) ptr
;
946 DBG(r300
, DBG_DRAW
, "r300: render_draw_arrays (count: %d)\n", count
);
948 if (r300
->draw_first_emitted
) {
949 if (!r300_prepare_for_rendering(r300
,
950 PREP_FIRST_DRAW
| PREP_EMIT_AOS_SWTCL
,
951 NULL
, 6, 0, 0, FALSE
))
954 if (!r300_emit_states(r300
,
955 PREP_FIRST_DRAW
| PREP_EMIT_AOS_SWTCL
,
960 /* Uncomment to dump all VBOs rendered through this interface.
962 ptr = pipe_buffer_map(&r300render->r300->context,
963 r300render->vbo, PIPE_TRANSFER_READ,
964 &r300render->vbo_transfer);
966 for (i = 0; i < count; i++) {
967 printf("r300: Vertex %d\n", i);
968 draw_dump_emitted_vertex(&r300->vertex_info, ptr);
969 ptr += r300->vertex_info.size * 4;
973 pipe_buffer_unmap(&r300render->r300->context, r300render->vbo,
974 r300render->vbo_transfer);
978 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
979 r300_provoking_vertex_fixes(r300
, r300render
->prim
));
980 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX
, count
- 1);
981 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2
, 0);
982 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST
| (count
<< 16) |
986 r300
->draw_first_emitted
= TRUE
;
989 static void r300_render_draw_elements(struct vbuf_render
* render
,
990 const ushort
* indices
,
993 struct r300_render
* r300render
= r300_render(render
);
994 struct r300_context
* r300
= r300render
->r300
;
996 unsigned end_cs_dwords
;
997 unsigned max_index
= (r300
->draw_vbo_size
- r300
->draw_vbo_offset
) /
998 (r300render
->r300
->vertex_info
.size
* 4) - 1;
999 unsigned short_count
;
1000 unsigned free_dwords
;
1003 DBG(r300
, DBG_DRAW
, "r300: render_draw_elements (count: %d)\n", count
);
1005 if (r300
->draw_first_emitted
) {
1006 if (!r300_prepare_for_rendering(r300
,
1007 PREP_FIRST_DRAW
| PREP_EMIT_AOS_SWTCL
| PREP_INDEXED
,
1008 NULL
, 256, 0, 0, FALSE
))
1011 if (!r300_emit_states(r300
,
1012 PREP_FIRST_DRAW
| PREP_EMIT_AOS_SWTCL
| PREP_INDEXED
,
1017 /* Below we manage the CS space manually because there may be more
1018 * indices than it can fit in CS. */
1020 end_cs_dwords
= r300_get_num_cs_end_dwords(r300
);
1023 free_dwords
= R300_MAX_CMDBUF_DWORDS
- r300
->cs
->cdw
;
1025 short_count
= MIN2(count
, (free_dwords
- end_cs_dwords
- 6) * 2);
1027 BEGIN_CS(6 + (short_count
+1)/2);
1028 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
1029 r300_provoking_vertex_fixes(r300
, r300render
->prim
));
1030 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX
, max_index
);
1031 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, (short_count
+1)/2);
1032 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (short_count
<< 16) |
1033 r300render
->hwprim
);
1034 for (i
= 0; i
< short_count
-1; i
+= 2) {
1035 OUT_CS(indices
[i
+1] << 16 | indices
[i
]);
1037 if (short_count
% 2) {
1038 OUT_CS(indices
[short_count
-1]);
1042 /* OK now subtract the emitted indices and see if we need to emit
1043 * another draw packet. */
1044 indices
+= short_count
;
1045 count
-= short_count
;
1048 if (!r300_prepare_for_rendering(r300
,
1049 PREP_EMIT_AOS_SWTCL
| PREP_INDEXED
,
1050 NULL
, 256, 0, 0, FALSE
))
1053 end_cs_dwords
= r300_get_num_cs_end_dwords(r300
);
1057 r300
->draw_first_emitted
= TRUE
;
1060 static void r300_render_destroy(struct vbuf_render
* render
)
1065 static struct vbuf_render
* r300_render_create(struct r300_context
* r300
)
1067 struct r300_render
* r300render
= CALLOC_STRUCT(r300_render
);
1069 r300render
->r300
= r300
;
1071 r300render
->base
.max_vertex_buffer_bytes
= 1024 * 1024;
1072 r300render
->base
.max_indices
= 16 * 1024;
1074 r300render
->base
.get_vertex_info
= r300_render_get_vertex_info
;
1075 r300render
->base
.allocate_vertices
= r300_render_allocate_vertices
;
1076 r300render
->base
.map_vertices
= r300_render_map_vertices
;
1077 r300render
->base
.unmap_vertices
= r300_render_unmap_vertices
;
1078 r300render
->base
.set_primitive
= r300_render_set_primitive
;
1079 r300render
->base
.draw_elements
= r300_render_draw_elements
;
1080 r300render
->base
.draw_arrays
= r300_render_draw_arrays
;
1081 r300render
->base
.release_vertices
= r300_render_release_vertices
;
1082 r300render
->base
.destroy
= r300_render_destroy
;
1084 return &r300render
->base
;
1087 struct draw_stage
* r300_draw_stage(struct r300_context
* r300
)
1089 struct vbuf_render
* render
;
1090 struct draw_stage
* stage
;
1092 render
= r300_render_create(r300
);
1098 stage
= draw_vbuf_stage(r300
->draw
, render
);
1101 render
->destroy(render
);
1105 draw_set_render(r300
->draw
, render
);
1110 void r300_draw_flush_vbuf(struct r300_context
*r300
)
1112 pipe_resource_reference(&r300
->vbo
, NULL
);
1113 r300
->draw_vbo_size
= 0;
1116 /****************************************************************************
1117 * End of SW TCL functions *
1118 ***************************************************************************/
1120 /* This functions is used to draw a rectangle for the blitter module.
1122 * If we rendered a quad, the pixels on the main diagonal
1123 * would be computed and stored twice, which makes the clear/copy codepaths
1124 * somewhat inefficient. Instead we use a rectangular point sprite. */
1125 static void r300_blitter_draw_rectangle(struct blitter_context
*blitter
,
1126 unsigned x1
, unsigned y1
,
1127 unsigned x2
, unsigned y2
,
1129 enum blitter_attrib_type type
,
1130 const float attrib
[4])
1132 struct r300_context
*r300
= r300_context(util_blitter_get_pipe(blitter
));
1133 unsigned last_sprite_coord_enable
= r300
->sprite_coord_enable
;
1134 unsigned width
= x2
- x1
;
1135 unsigned height
= y2
- y1
;
1136 unsigned vertex_size
=
1137 type
== UTIL_BLITTER_ATTRIB_COLOR
|| !r300
->draw
? 8 : 4;
1138 unsigned dwords
= 13 + vertex_size
+
1139 (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
? 7 : 0);
1140 const float zeros
[4] = {0, 0, 0, 0};
1143 r300
->context
.set_vertex_buffers(&r300
->context
, 0, NULL
);
1145 if (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
)
1146 r300
->sprite_coord_enable
= 1;
1148 r300_update_derived_state(r300
);
1150 /* Mark some states we don't care about as non-dirty. */
1151 r300
->clip_state
.dirty
= FALSE
;
1152 r300
->viewport_state
.dirty
= FALSE
;
1154 if (!r300_prepare_for_rendering(r300
, PREP_FIRST_DRAW
, NULL
, dwords
, 0, 0,
1158 DBG(r300
, DBG_DRAW
, "r300: draw_rectangle\n");
1162 OUT_CS_REG(R300_GA_POINT_SIZE
, (height
* 6) | ((width
* 6) << 16));
1164 if (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
) {
1165 /* Set up the GA to generate texcoords. */
1166 OUT_CS_REG(R300_GB_ENABLE
, R300_GB_POINT_STUFF_ENABLE
|
1167 (R300_GB_TEX_STR
<< R300_GB_TEX0_SOURCE_SHIFT
));
1168 OUT_CS_REG_SEQ(R300_GA_POINT_S0
, 4);
1169 OUT_CS_32F(attrib
[0]);
1170 OUT_CS_32F(attrib
[3]);
1171 OUT_CS_32F(attrib
[2]);
1172 OUT_CS_32F(attrib
[1]);
1175 /* Set up VAP controls. */
1176 OUT_CS_REG(R300_VAP_CLIP_CNTL
, R300_CLIP_DISABLE
);
1177 OUT_CS_REG(R300_VAP_VTE_CNTL
, R300_VTX_XY_FMT
| R300_VTX_Z_FMT
);
1178 OUT_CS_REG(R300_VAP_VTX_SIZE
, vertex_size
);
1179 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
1184 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2
, vertex_size
);
1185 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED
| (1 << 16) |
1186 R300_VAP_VF_CNTL__PRIM_POINTS
);
1188 OUT_CS_32F(x1
+ width
* 0.5f
);
1189 OUT_CS_32F(y1
+ height
* 0.5f
);
1193 if (vertex_size
== 8) {
1196 OUT_CS_TABLE(attrib
, 4);
1201 /* Restore the state. */
1202 r300_mark_atom_dirty(r300
, &r300
->clip_state
);
1203 r300_mark_atom_dirty(r300
, &r300
->rs_state
);
1204 r300_mark_atom_dirty(r300
, &r300
->viewport_state
);
1206 r300
->sprite_coord_enable
= last_sprite_coord_enable
;
1209 static void r300_resource_resolve(struct pipe_context
* pipe
,
1210 struct pipe_resource
* dest
,
1212 struct pipe_resource
* src
,
1215 struct r300_context
* r300
= r300_context(pipe
);
1216 struct pipe_surface
* srcsurf
, surf_tmpl
;
1217 struct r300_aa_state
*aa
= (struct r300_aa_state
*)r300
->aa_state
.state
;
1218 float color
[] = {0, 0, 0, 0};
1220 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1221 surf_tmpl
.format
= src
->format
;
1222 surf_tmpl
.usage
= 0; /* not really a surface hence no bind flags */
1223 surf_tmpl
.u
.tex
.level
= 0; /* msaa resources cannot have mipmaps */
1224 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
1225 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
1226 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
1227 surf_tmpl
.format
= dest
->format
;
1228 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
1229 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
1231 DBG(r300
, DBG_DRAW
, "r300: Resolving resource...\n");
1233 /* Enable AA resolve. */
1234 aa
->dest
= r300_surface(pipe
->create_surface(pipe
, dest
, &surf_tmpl
));
1237 R300_RB3D_AARESOLVE_CTL_AARESOLVE_MODE_RESOLVE
|
1238 R300_RB3D_AARESOLVE_CTL_AARESOLVE_ALPHA_AVERAGE
;
1239 r300
->aa_state
.size
= 12;
1240 r300_mark_atom_dirty(r300
, &r300
->aa_state
);
1242 /* Resolve the surface. */
1243 r300
->context
.clear_render_target(pipe
,
1244 srcsurf
, color
, 0, 0, src
->width0
, src
->height0
);
1246 /* Disable AA resolve. */
1247 aa
->aaresolve_ctl
= 0;
1248 r300
->aa_state
.size
= 4;
1249 r300_mark_atom_dirty(r300
, &r300
->aa_state
);
1251 pipe_surface_reference((struct pipe_surface
**)&srcsurf
, NULL
);
1252 pipe_surface_reference((struct pipe_surface
**)&aa
->dest
, NULL
);
1255 void r300_init_render_functions(struct r300_context
*r300
)
1257 /* Set draw functions based on presence of HW TCL. */
1258 if (r300
->screen
->caps
.has_tcl
) {
1259 r300
->context
.draw_vbo
= r300_draw_vbo
;
1261 r300
->context
.draw_vbo
= r300_swtcl_draw_vbo
;
1264 r300
->context
.resource_resolve
= r300_resource_resolve
;
1265 r300
->blitter
->draw_rectangle
= r300_blitter_draw_rectangle
;
1267 /* Plug in the two-sided stencil reference value fallback if needed. */
1268 if (!r300
->screen
->caps
.is_r500
)
1269 r300_plug_in_stencil_ref_fallback(r300
);