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
)
49 static const int prim_conv
[] = {
50 R300_VAP_VF_CNTL__PRIM_POINTS
,
51 R300_VAP_VF_CNTL__PRIM_LINES
,
52 R300_VAP_VF_CNTL__PRIM_LINE_LOOP
,
53 R300_VAP_VF_CNTL__PRIM_LINE_STRIP
,
54 R300_VAP_VF_CNTL__PRIM_TRIANGLES
,
55 R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP
,
56 R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN
,
57 R300_VAP_VF_CNTL__PRIM_QUADS
,
58 R300_VAP_VF_CNTL__PRIM_QUAD_STRIP
,
59 R300_VAP_VF_CNTL__PRIM_POLYGON
,
65 unsigned hwprim
= prim_conv
[prim
];
71 static uint32_t r300_provoking_vertex_fixes(struct r300_context
*r300
,
74 struct r300_rs_state
* rs
= (struct r300_rs_state
*)r300
->rs_state
.state
;
75 uint32_t color_control
= rs
->color_control
;
77 /* By default (see r300_state.c:r300_create_rs_state) color_control is
78 * initialized to provoking the first vertex.
80 * Triangle fans must be reduced to the second vertex, not the first, in
81 * Gallium flatshade-first mode, as per the GL spec.
82 * (http://www.opengl.org/registry/specs/ARB/provoking_vertex.txt)
84 * Quads never provoke correctly in flatshade-first mode. The first
85 * vertex is never considered as provoking, so only the second, third,
86 * and fourth vertices can be selected, and both "third" and "last" modes
87 * select the fourth vertex. This is probably due to D3D lacking quads.
89 * Similarly, polygons reduce to the first, not the last, vertex, when in
90 * "last" mode, and all other modes start from the second vertex.
95 if (rs
->rs
.flatshade_first
) {
97 case PIPE_PRIM_TRIANGLE_FAN
:
98 color_control
|= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND
;
100 case PIPE_PRIM_QUADS
:
101 case PIPE_PRIM_QUAD_STRIP
:
102 case PIPE_PRIM_POLYGON
:
103 color_control
|= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST
;
106 color_control
|= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST
;
110 color_control
|= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST
;
113 return color_control
;
116 void r500_emit_index_bias(struct r300_context
*r300
, int index_bias
)
121 OUT_CS_REG(R500_VAP_INDEX_OFFSET
,
122 (index_bias
& 0xFFFFFF) | (index_bias
< 0 ? 1<<24 : 0));
126 static void r300_emit_draw_init(struct r300_context
*r300
, unsigned mode
,
131 assert(max_index
< (1 << 24));
134 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
135 r300_provoking_vertex_fixes(r300
, mode
));
136 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
142 /* This function splits the index bias value into two parts:
143 * - buffer_offset: the value that can be safely added to buffer offsets
144 * in r300_emit_vertex_arrays (it must yield a positive offset when added to
145 * a vertex buffer offset)
146 * - index_offset: the value that must be manually subtracted from indices
147 * in an index buffer to achieve negative offsets. */
148 static void r300_split_index_bias(struct r300_context
*r300
, int index_bias
,
149 int *buffer_offset
, int *index_offset
)
151 struct pipe_vertex_buffer
*vb
, *vbufs
= r300
->vertex_buffer
;
152 struct pipe_vertex_element
*velem
= r300
->velems
->velem
;
156 if (index_bias
< 0) {
157 /* See how large index bias we may subtract. We must be careful
158 * here because negative buffer offsets are not allowed
160 max_neg_bias
= INT_MAX
;
161 for (i
= 0; i
< r300
->velems
->count
; i
++) {
162 vb
= &vbufs
[velem
[i
].vertex_buffer_index
];
163 size
= (vb
->buffer_offset
+ velem
[i
].src_offset
) / vb
->stride
;
164 max_neg_bias
= MIN2(max_neg_bias
, size
);
167 /* Now set the minimum allowed value. */
168 *buffer_offset
= MAX2(-max_neg_bias
, index_bias
);
170 /* A positive index bias is OK. */
171 *buffer_offset
= index_bias
;
174 *index_offset
= index_bias
- *buffer_offset
;
177 enum r300_prepare_flags
{
178 PREP_EMIT_STATES
= (1 << 0), /* call emit_dirty_state and friends? */
179 PREP_VALIDATE_VBOS
= (1 << 1), /* validate VBOs? */
180 PREP_EMIT_VARRAYS
= (1 << 2), /* call emit_vertex_arrays? */
181 PREP_EMIT_VARRAYS_SWTCL
= (1 << 3), /* call emit_vertex_arrays_swtcl? */
182 PREP_INDEXED
= (1 << 4) /* is this draw_elements? */
186 * Check if the requested number of dwords is available in the CS and
188 * \param r300 The context.
189 * \param flags See r300_prepare_flags.
190 * \param cs_dwords The number of dwords to reserve in CS.
191 * \return TRUE if the CS was flushed
193 static boolean
r300_reserve_cs_dwords(struct r300_context
*r300
,
194 enum r300_prepare_flags flags
,
197 boolean flushed
= FALSE
;
198 boolean emit_states
= flags
& PREP_EMIT_STATES
;
199 boolean emit_vertex_arrays
= flags
& PREP_EMIT_VARRAYS
;
200 boolean emit_vertex_arrays_swtcl
= flags
& PREP_EMIT_VARRAYS_SWTCL
;
202 /* Add dirty state, index offset, and AOS. */
204 cs_dwords
+= r300_get_num_dirty_dwords(r300
);
206 if (r300
->screen
->caps
.is_r500
)
207 cs_dwords
+= 2; /* emit_index_offset */
209 if (emit_vertex_arrays
)
210 cs_dwords
+= 55; /* emit_vertex_arrays */
212 if (emit_vertex_arrays_swtcl
)
213 cs_dwords
+= 7; /* emit_vertex_arrays_swtcl */
215 cs_dwords
+= r300_get_num_cs_end_dwords(r300
);
217 /* Reserve requested CS space. */
218 if (!r300
->rws
->cs_check_space(r300
->cs
, cs_dwords
)) {
219 r300_flush(&r300
->context
, RADEON_FLUSH_ASYNC
, NULL
);
227 * Validate buffers and emit dirty state.
228 * \param r300 The context.
229 * \param flags See r300_prepare_flags.
230 * \param index_buffer The index buffer to validate. The parameter may be NULL.
231 * \param buffer_offset The offset passed to emit_vertex_arrays.
232 * \param index_bias The index bias to emit.
233 * \param instance_id Index of instance to render
234 * \return TRUE if rendering should be skipped
236 static boolean
r300_emit_states(struct r300_context
*r300
,
237 enum r300_prepare_flags flags
,
238 struct pipe_resource
*index_buffer
,
240 int index_bias
, int instance_id
)
242 boolean emit_states
= flags
& PREP_EMIT_STATES
;
243 boolean emit_vertex_arrays
= flags
& PREP_EMIT_VARRAYS
;
244 boolean emit_vertex_arrays_swtcl
= flags
& PREP_EMIT_VARRAYS_SWTCL
;
245 boolean indexed
= flags
& PREP_INDEXED
;
246 boolean validate_vbos
= flags
& PREP_VALIDATE_VBOS
;
248 /* Validate buffers and emit dirty state if needed. */
249 if (emit_states
|| (emit_vertex_arrays
&& validate_vbos
)) {
250 if (!r300_emit_buffer_validate(r300
, validate_vbos
,
252 fprintf(stderr
, "r300: CS space validation failed. "
253 "(not enough memory?) Skipping rendering.\n");
259 r300_emit_dirty_state(r300
);
261 if (r300
->screen
->caps
.is_r500
) {
262 if (r300
->screen
->caps
.has_tcl
)
263 r500_emit_index_bias(r300
, index_bias
);
265 r500_emit_index_bias(r300
, 0);
268 if (emit_vertex_arrays
&&
269 (r300
->vertex_arrays_dirty
||
270 r300
->vertex_arrays_indexed
!= indexed
||
271 r300
->vertex_arrays_offset
!= buffer_offset
||
272 r300
->vertex_arrays_instance_id
!= instance_id
)) {
273 r300_emit_vertex_arrays(r300
, buffer_offset
, indexed
, instance_id
);
275 r300
->vertex_arrays_dirty
= FALSE
;
276 r300
->vertex_arrays_indexed
= indexed
;
277 r300
->vertex_arrays_offset
= buffer_offset
;
278 r300
->vertex_arrays_instance_id
= instance_id
;
281 if (emit_vertex_arrays_swtcl
)
282 r300_emit_vertex_arrays_swtcl(r300
, indexed
);
288 * Check if the requested number of dwords is available in the CS and
289 * if not, flush. Then validate buffers and emit dirty state.
290 * \param r300 The context.
291 * \param flags See r300_prepare_flags.
292 * \param index_buffer The index buffer to validate. The parameter may be NULL.
293 * \param cs_dwords The number of dwords to reserve in CS.
294 * \param buffer_offset The offset passed to emit_vertex_arrays.
295 * \param index_bias The index bias to emit.
296 * \param instance_id The instance to render.
297 * \return TRUE if rendering should be skipped
299 static boolean
r300_prepare_for_rendering(struct r300_context
*r300
,
300 enum r300_prepare_flags flags
,
301 struct pipe_resource
*index_buffer
,
307 /* Make sure there is enough space in the command stream and emit states. */
308 if (r300_reserve_cs_dwords(r300
, flags
, cs_dwords
))
309 flags
|= PREP_EMIT_STATES
;
311 return r300_emit_states(r300
, flags
, index_buffer
, buffer_offset
,
312 index_bias
, instance_id
);
315 static boolean
immd_is_good_idea(struct r300_context
*r300
,
318 if (DBG_ON(r300
, DBG_NO_IMMD
)) {
322 if (count
* r300
->velems
->vertex_size_dwords
> IMMD_DWORDS
) {
326 /* Buffers can only be used for read by r300 (except query buffers, but
327 * those can't be bound by a state tracker as vertex buffers). */
331 /*****************************************************************************
332 * The HWTCL draw functions. *
333 ****************************************************************************/
335 static void r300_draw_arrays_immediate(struct r300_context
*r300
,
336 const struct pipe_draw_info
*info
)
338 struct pipe_vertex_element
* velem
;
339 struct pipe_vertex_buffer
* vbuf
;
340 unsigned vertex_element_count
= r300
->velems
->count
;
343 /* Size of the vertex, in dwords. */
344 unsigned vertex_size
= r300
->velems
->vertex_size_dwords
;
346 /* The number of dwords for this draw operation. */
347 unsigned dwords
= 4 + info
->count
* vertex_size
;
349 /* Size of the vertex element, in dwords. */
350 unsigned size
[PIPE_MAX_ATTRIBS
];
352 /* Stride to the same attrib in the next vertex in the vertex buffer,
354 unsigned stride
[PIPE_MAX_ATTRIBS
];
356 /* Mapped vertex buffers. */
357 uint32_t* map
[PIPE_MAX_ATTRIBS
] = {0};
358 uint32_t* mapelem
[PIPE_MAX_ATTRIBS
];
362 if (!r300_prepare_for_rendering(r300
, PREP_EMIT_STATES
, NULL
, dwords
, 0, 0, -1))
365 /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
366 for (i
= 0; i
< vertex_element_count
; i
++) {
367 velem
= &r300
->velems
->velem
[i
];
368 size
[i
] = r300
->velems
->format_size
[i
] / 4;
369 vbi
= velem
->vertex_buffer_index
;
370 vbuf
= &r300
->vertex_buffer
[vbi
];
371 stride
[i
] = vbuf
->stride
/ 4;
373 /* Map the buffer. */
375 map
[vbi
] = (uint32_t*)r300
->rws
->buffer_map(
376 r300_resource(vbuf
->buffer
.resource
)->buf
,
377 r300
->cs
, PIPE_TRANSFER_READ
| PIPE_TRANSFER_UNSYNCHRONIZED
);
378 map
[vbi
] += (vbuf
->buffer_offset
/ 4) + stride
[i
] * info
->start
;
380 mapelem
[i
] = map
[vbi
] + (velem
->src_offset
/ 4);
383 r300_emit_draw_init(r300
, info
->mode
, info
->count
-1);
386 OUT_CS_REG(R300_VAP_VTX_SIZE
, vertex_size
);
387 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2
, info
->count
* vertex_size
);
388 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED
| (info
->count
<< 16) |
389 r300_translate_primitive(info
->mode
));
392 for (v
= 0; v
< info
->count
; v
++) {
393 for (i
= 0; i
< vertex_element_count
; i
++) {
394 OUT_CS_TABLE(&mapelem
[i
][stride
[i
] * v
], size
[i
]);
400 static void r300_emit_draw_arrays(struct r300_context
*r300
,
404 boolean alt_num_verts
= count
> 65535;
407 if (count
>= (1 << 24)) {
408 fprintf(stderr
, "r300: Got a huge number of vertices: %i, "
409 "refusing to render.\n", count
);
413 r300_emit_draw_init(r300
, mode
, count
-1);
415 BEGIN_CS(2 + (alt_num_verts
? 2 : 0));
417 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES
, count
);
419 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2
, 0);
420 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST
| (count
<< 16) |
421 r300_translate_primitive(mode
) |
422 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
426 static void r300_emit_draw_elements(struct r300_context
*r300
,
427 struct pipe_resource
* indexBuffer
,
433 uint16_t *imm_indices3
)
435 uint32_t count_dwords
, offset_dwords
;
436 boolean alt_num_verts
= count
> 65535;
439 if (count
>= (1 << 24)) {
440 fprintf(stderr
, "r300: Got a huge number of vertices: %i, "
441 "refusing to render (max_index: %i).\n", count
, max_index
);
445 DBG(r300
, DBG_DRAW
, "r300: Indexbuf of %u indices, max %u\n",
448 r300_emit_draw_init(r300
, mode
, max_index
);
450 /* If start is odd, render the first triangle with indices embedded
451 * in the command stream. This will increase start by 3 and make it
452 * even. We can then proceed without a fallback. */
453 if (indexSize
== 2 && (start
& 1) &&
454 mode
== PIPE_PRIM_TRIANGLES
) {
456 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, 2);
457 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (3 << 16) |
458 R300_VAP_VF_CNTL__PRIM_TRIANGLES
);
459 OUT_CS(imm_indices3
[1] << 16 | imm_indices3
[0]);
460 OUT_CS(imm_indices3
[2]);
469 offset_dwords
= indexSize
* start
/ sizeof(uint32_t);
471 BEGIN_CS(8 + (alt_num_verts
? 2 : 0));
473 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES
, count
);
475 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, 0);
476 if (indexSize
== 4) {
477 count_dwords
= count
;
478 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
479 R300_VAP_VF_CNTL__INDEX_SIZE_32bit
|
480 r300_translate_primitive(mode
) |
481 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
483 count_dwords
= (count
+ 1) / 2;
484 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
485 r300_translate_primitive(mode
) |
486 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
489 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER
, 2);
490 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR
| (R300_VAP_PORT_IDX0
>> 2) |
491 (0 << R300_INDX_BUFFER_SKIP_SHIFT
));
492 OUT_CS(offset_dwords
<< 2);
493 OUT_CS(count_dwords
);
494 OUT_CS_RELOC(r300_resource(indexBuffer
));
498 static void r300_draw_elements_immediate(struct r300_context
*r300
,
499 const struct pipe_draw_info
*info
)
502 const uint16_t *ptr2
;
503 const uint32_t *ptr4
;
504 unsigned index_size
= info
->index_size
;
505 unsigned i
, count_dwords
= index_size
== 4 ? info
->count
:
506 (info
->count
+ 1) / 2;
509 /* 19 dwords for r300_draw_elements_immediate. Give up if the function fails. */
510 if (!r300_prepare_for_rendering(r300
,
511 PREP_EMIT_STATES
| PREP_VALIDATE_VBOS
| PREP_EMIT_VARRAYS
|
512 PREP_INDEXED
, NULL
, 2+count_dwords
, 0, info
->index_bias
, -1))
515 r300_emit_draw_init(r300
, info
->mode
, info
->max_index
);
517 BEGIN_CS(2 + count_dwords
);
518 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, count_dwords
);
520 switch (index_size
) {
522 ptr1
= (uint8_t*)info
->index
.user
;
525 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (info
->count
<< 16) |
526 r300_translate_primitive(info
->mode
));
528 if (info
->index_bias
&& !r300
->screen
->caps
.is_r500
) {
529 for (i
= 0; i
< info
->count
-1; i
+= 2)
530 OUT_CS(((ptr1
[i
+1] + info
->index_bias
) << 16) |
531 (ptr1
[i
] + info
->index_bias
));
534 OUT_CS(ptr1
[i
] + info
->index_bias
);
536 for (i
= 0; i
< info
->count
-1; i
+= 2)
537 OUT_CS(((ptr1
[i
+1]) << 16) |
546 ptr2
= (uint16_t*)info
->index
.user
;
549 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (info
->count
<< 16) |
550 r300_translate_primitive(info
->mode
));
552 if (info
->index_bias
&& !r300
->screen
->caps
.is_r500
) {
553 for (i
= 0; i
< info
->count
-1; i
+= 2)
554 OUT_CS(((ptr2
[i
+1] + info
->index_bias
) << 16) |
555 (ptr2
[i
] + info
->index_bias
));
558 OUT_CS(ptr2
[i
] + info
->index_bias
);
560 OUT_CS_TABLE(ptr2
, count_dwords
);
565 ptr4
= (uint32_t*)info
->index
.user
;
568 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (info
->count
<< 16) |
569 R300_VAP_VF_CNTL__INDEX_SIZE_32bit
|
570 r300_translate_primitive(info
->mode
));
572 if (info
->index_bias
&& !r300
->screen
->caps
.is_r500
) {
573 for (i
= 0; i
< info
->count
; i
++)
574 OUT_CS(ptr4
[i
] + info
->index_bias
);
576 OUT_CS_TABLE(ptr4
, count_dwords
);
583 static void r300_draw_elements(struct r300_context
*r300
,
584 const struct pipe_draw_info
*info
,
587 struct pipe_resource
*indexBuffer
=
588 info
->has_user_indices
? NULL
: info
->index
.resource
;
589 unsigned indexSize
= info
->index_size
;
590 struct pipe_resource
* orgIndexBuffer
= indexBuffer
;
591 unsigned start
= info
->start
;
592 unsigned count
= info
->count
;
593 boolean alt_num_verts
= r300
->screen
->caps
.is_r500
&&
595 unsigned short_count
;
596 int buffer_offset
= 0, index_offset
= 0; /* for index bias emulation */
597 uint16_t indices3
[3];
599 if (info
->index_bias
&& !r300
->screen
->caps
.is_r500
) {
600 r300_split_index_bias(r300
, info
->index_bias
, &buffer_offset
,
604 r300_translate_index_buffer(r300
, info
, &indexBuffer
,
605 &indexSize
, index_offset
, &start
, count
);
607 /* Fallback for misaligned ushort indices. */
608 if (indexSize
== 2 && (start
& 1) && indexBuffer
) {
609 /* If we got here, then orgIndexBuffer == indexBuffer. */
610 uint16_t *ptr
= r300
->rws
->buffer_map(r300_resource(orgIndexBuffer
)->buf
,
613 PIPE_TRANSFER_UNSYNCHRONIZED
);
615 if (info
->mode
== PIPE_PRIM_TRIANGLES
) {
616 memcpy(indices3
, ptr
+ start
, 6);
618 /* Copy the mapped index buffer directly to the upload buffer.
619 * The start index will be aligned simply from the fact that
620 * every sub-buffer in the upload buffer is aligned. */
621 r300_upload_index_buffer(r300
, &indexBuffer
, indexSize
, &start
,
622 count
, (uint8_t*)ptr
);
625 if (info
->has_user_indices
)
626 r300_upload_index_buffer(r300
, &indexBuffer
, indexSize
,
631 /* 19 dwords for emit_draw_elements. Give up if the function fails. */
632 if (!r300_prepare_for_rendering(r300
,
633 PREP_EMIT_STATES
| PREP_VALIDATE_VBOS
| PREP_EMIT_VARRAYS
|
634 PREP_INDEXED
, indexBuffer
, 19, buffer_offset
, info
->index_bias
,
638 if (alt_num_verts
|| count
<= 65535) {
639 r300_emit_draw_elements(r300
, indexBuffer
, indexSize
,
640 info
->max_index
, info
->mode
, start
, count
,
644 /* The maximum must be divisible by 4 and 3,
645 * so that quad and triangle lists are split correctly.
647 * Strips, loops, and fans won't work. */
648 short_count
= MIN2(count
, 65532);
650 r300_emit_draw_elements(r300
, indexBuffer
, indexSize
,
652 info
->mode
, start
, short_count
, indices3
);
654 start
+= short_count
;
655 count
-= short_count
;
657 /* 15 dwords for emit_draw_elements */
659 if (!r300_prepare_for_rendering(r300
,
660 PREP_VALIDATE_VBOS
| PREP_EMIT_VARRAYS
| PREP_INDEXED
,
661 indexBuffer
, 19, buffer_offset
, info
->index_bias
,
669 if (indexBuffer
!= orgIndexBuffer
) {
670 pipe_resource_reference( &indexBuffer
, NULL
);
674 static void r300_draw_arrays(struct r300_context
*r300
,
675 const struct pipe_draw_info
*info
,
678 boolean alt_num_verts
= r300
->screen
->caps
.is_r500
&&
680 unsigned start
= info
->start
;
681 unsigned count
= info
->count
;
682 unsigned short_count
;
684 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
685 if (!r300_prepare_for_rendering(r300
,
686 PREP_EMIT_STATES
| PREP_VALIDATE_VBOS
| PREP_EMIT_VARRAYS
,
687 NULL
, 9, start
, 0, instance_id
))
690 if (alt_num_verts
|| count
<= 65535) {
691 r300_emit_draw_arrays(r300
, info
->mode
, count
);
694 /* The maximum must be divisible by 4 and 3,
695 * so that quad and triangle lists are split correctly.
697 * Strips, loops, and fans won't work. */
698 short_count
= MIN2(count
, 65532);
699 r300_emit_draw_arrays(r300
, info
->mode
, short_count
);
701 start
+= short_count
;
702 count
-= short_count
;
704 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
706 if (!r300_prepare_for_rendering(r300
,
707 PREP_VALIDATE_VBOS
| PREP_EMIT_VARRAYS
, NULL
, 9,
708 start
, 0, instance_id
))
715 static void r300_draw_arrays_instanced(struct r300_context
*r300
,
716 const struct pipe_draw_info
*info
)
720 for (i
= 0; i
< info
->instance_count
; i
++)
721 r300_draw_arrays(r300
, info
, i
);
724 static void r300_draw_elements_instanced(struct r300_context
*r300
,
725 const struct pipe_draw_info
*info
)
729 for (i
= 0; i
< info
->instance_count
; i
++)
730 r300_draw_elements(r300
, info
, i
);
733 static unsigned r300_max_vertex_count(struct r300_context
*r300
)
735 unsigned i
, nr
= r300
->velems
->count
;
736 struct pipe_vertex_element
*velems
= r300
->velems
->velem
;
737 unsigned result
= ~0;
739 for (i
= 0; i
< nr
; i
++) {
740 struct pipe_vertex_buffer
*vb
=
741 &r300
->vertex_buffer
[velems
[i
].vertex_buffer_index
];
742 unsigned size
, max_count
, value
;
744 /* We're not interested in constant and per-instance attribs. */
745 if (!vb
->buffer
.resource
||
747 velems
[i
].instance_divisor
) {
751 size
= vb
->buffer
.resource
->width0
;
753 /* Subtract buffer_offset. */
754 value
= vb
->buffer_offset
;
760 /* Subtract src_offset. */
761 value
= velems
[i
].src_offset
;
767 /* Subtract format_size. */
768 value
= r300
->velems
->format_size
[i
];
774 /* Compute the max count. */
775 max_count
= 1 + size
/ vb
->stride
;
776 result
= MIN2(result
, max_count
);
782 static void r300_draw_vbo(struct pipe_context
* pipe
,
783 const struct pipe_draw_info
*dinfo
)
785 struct r300_context
* r300
= r300_context(pipe
);
786 struct pipe_draw_info info
= *dinfo
;
788 if (r300
->skip_rendering
||
789 !u_trim_pipe_prim(info
.mode
, &info
.count
)) {
793 r300_update_derived_state(r300
);
796 if (info
.index_size
) {
797 unsigned max_count
= r300_max_vertex_count(r300
);
800 fprintf(stderr
, "r300: Skipping a draw command. There is a buffer "
801 " which is too small to be used for rendering.\n");
805 if (max_count
== ~0) {
806 /* There are no per-vertex vertex elements. Use the hardware maximum. */
807 max_count
= 0xffffff;
810 info
.max_index
= max_count
- 1;
812 if (info
.instance_count
<= 1) {
813 if (info
.count
<= 8 && info
.has_user_indices
) {
814 r300_draw_elements_immediate(r300
, &info
);
816 r300_draw_elements(r300
, &info
, -1);
819 r300_draw_elements_instanced(r300
, &info
);
822 if (info
.instance_count
<= 1) {
823 if (immd_is_good_idea(r300
, info
.count
)) {
824 r300_draw_arrays_immediate(r300
, &info
);
826 r300_draw_arrays(r300
, &info
, -1);
829 r300_draw_arrays_instanced(r300
, &info
);
834 /****************************************************************************
835 * The rest of this file is for SW TCL rendering only. Please be polite and *
836 * keep these functions separated so that they are easier to locate. ~C. *
837 ***************************************************************************/
839 /* SW TCL elements, using Draw. */
840 static void r300_swtcl_draw_vbo(struct pipe_context
* pipe
,
841 const struct pipe_draw_info
*info
)
843 struct r300_context
* r300
= r300_context(pipe
);
845 if (r300
->skip_rendering
) {
849 if (!u_trim_pipe_prim(info
->mode
, (unsigned*)&info
->count
))
852 if (info
->index_size
) {
853 draw_set_indexes(r300
->draw
,
854 info
->has_user_indices
?
856 r300_resource(info
->index
.resource
)->malloced_buffer
,
857 info
->index_size
, ~0);
860 r300_update_derived_state(r300
);
862 draw_vbo(r300
->draw
, info
);
863 draw_flush(r300
->draw
);
866 /* Object for rendering using Draw. */
869 struct vbuf_render base
;
872 struct r300_context
* r300
;
874 /* Vertex information */
884 static inline struct r300_render
*
885 r300_render(struct vbuf_render
* render
)
887 return (struct r300_render
*)render
;
890 static const struct vertex_info
*
891 r300_render_get_vertex_info(struct vbuf_render
* render
)
893 struct r300_render
* r300render
= r300_render(render
);
894 struct r300_context
* r300
= r300render
->r300
;
896 return &r300
->vertex_info
;
899 static boolean
r300_render_allocate_vertices(struct vbuf_render
* render
,
903 struct r300_render
* r300render
= r300_render(render
);
904 struct r300_context
* r300
= r300render
->r300
;
905 struct radeon_winsys
*rws
= r300
->rws
;
906 size_t size
= (size_t)vertex_size
* (size_t)count
;
908 DBG(r300
, DBG_DRAW
, "r300: render_allocate_vertices (size: %d)\n", size
);
910 if (!r300
->vbo
|| size
+ r300
->draw_vbo_offset
> r300
->vbo
->size
) {
911 pb_reference(&r300
->vbo
, NULL
);
913 r300render
->vbo_ptr
= NULL
;
915 r300
->vbo
= rws
->buffer_create(rws
,
916 MAX2(R300_MAX_DRAW_VBO_SIZE
, size
),
917 R300_BUFFER_ALIGNMENT
,
918 RADEON_DOMAIN_GTT
, 0);
922 r300
->draw_vbo_offset
= 0;
923 r300render
->vbo_ptr
= rws
->buffer_map(r300
->vbo
, r300
->cs
,
924 PIPE_TRANSFER_WRITE
);
927 r300render
->vertex_size
= vertex_size
;
931 static void* r300_render_map_vertices(struct vbuf_render
* render
)
933 struct r300_render
* r300render
= r300_render(render
);
934 struct r300_context
* r300
= r300render
->r300
;
936 DBG(r300
, DBG_DRAW
, "r300: render_map_vertices\n");
938 assert(r300render
->vbo_ptr
);
939 return r300render
->vbo_ptr
+ r300
->draw_vbo_offset
;
942 static void r300_render_unmap_vertices(struct vbuf_render
* render
,
946 struct r300_render
* r300render
= r300_render(render
);
947 struct r300_context
* r300
= r300render
->r300
;
949 DBG(r300
, DBG_DRAW
, "r300: render_unmap_vertices\n");
951 r300render
->vbo_max_used
= MAX2(r300render
->vbo_max_used
,
952 r300render
->vertex_size
* (max
+ 1));
955 static void r300_render_release_vertices(struct vbuf_render
* render
)
957 struct r300_render
* r300render
= r300_render(render
);
958 struct r300_context
* r300
= r300render
->r300
;
960 DBG(r300
, DBG_DRAW
, "r300: render_release_vertices\n");
962 r300
->draw_vbo_offset
+= r300render
->vbo_max_used
;
963 r300render
->vbo_max_used
= 0;
966 static void r300_render_set_primitive(struct vbuf_render
* render
,
969 struct r300_render
* r300render
= r300_render(render
);
971 r300render
->prim
= prim
;
972 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
;
989 assert(count
< (1 << 16));
991 DBG(r300
, DBG_DRAW
, "r300: render_draw_arrays (count: %d)\n", count
);
993 if (!r300_prepare_for_rendering(r300
,
994 PREP_EMIT_STATES
| PREP_EMIT_VARRAYS_SWTCL
,
995 NULL
, dwords
, 0, 0, -1)) {
1000 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
1001 r300_provoking_vertex_fixes(r300
, r300render
->prim
));
1002 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX
, count
- 1);
1003 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2
, 0);
1004 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST
| (count
<< 16) |
1005 r300render
->hwprim
);
1009 static void r300_render_draw_elements(struct vbuf_render
* render
,
1010 const ushort
* indices
,
1013 struct r300_render
* r300render
= r300_render(render
);
1014 struct r300_context
* r300
= r300render
->r300
;
1015 unsigned max_index
= (r300
->vbo
->size
- r300
->draw_vbo_offset
) /
1016 (r300render
->r300
->vertex_info
.size
* 4) - 1;
1017 struct pipe_resource
*index_buffer
= NULL
;
1018 unsigned index_buffer_offset
;
1021 DBG(r300
, DBG_DRAW
, "r300: render_draw_elements (count: %d)\n", count
);
1023 u_upload_data(r300
->uploader
, 0, count
* 2, 4, indices
,
1024 &index_buffer_offset
, &index_buffer
);
1025 if (!index_buffer
) {
1029 if (!r300_prepare_for_rendering(r300
,
1031 PREP_EMIT_VARRAYS_SWTCL
| PREP_INDEXED
,
1032 index_buffer
, 12, 0, 0, -1)) {
1033 pipe_resource_reference(&index_buffer
, NULL
);
1038 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
1039 r300_provoking_vertex_fixes(r300
, r300render
->prim
));
1040 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX
, max_index
);
1042 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, 0);
1043 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
1044 r300render
->hwprim
);
1046 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER
, 2);
1047 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR
| (R300_VAP_PORT_IDX0
>> 2));
1048 OUT_CS(index_buffer_offset
);
1049 OUT_CS((count
+ 1) / 2);
1050 OUT_CS_RELOC(r300_resource(index_buffer
));
1053 pipe_resource_reference(&index_buffer
, NULL
);
1056 static void r300_render_destroy(struct vbuf_render
* render
)
1061 static struct vbuf_render
* r300_render_create(struct r300_context
* r300
)
1063 struct r300_render
* r300render
= CALLOC_STRUCT(r300_render
);
1065 r300render
->r300
= r300
;
1067 r300render
->base
.max_vertex_buffer_bytes
= R300_MAX_DRAW_VBO_SIZE
;
1068 r300render
->base
.max_indices
= 16 * 1024;
1070 r300render
->base
.get_vertex_info
= r300_render_get_vertex_info
;
1071 r300render
->base
.allocate_vertices
= r300_render_allocate_vertices
;
1072 r300render
->base
.map_vertices
= r300_render_map_vertices
;
1073 r300render
->base
.unmap_vertices
= r300_render_unmap_vertices
;
1074 r300render
->base
.set_primitive
= r300_render_set_primitive
;
1075 r300render
->base
.draw_elements
= r300_render_draw_elements
;
1076 r300render
->base
.draw_arrays
= r300_render_draw_arrays
;
1077 r300render
->base
.release_vertices
= r300_render_release_vertices
;
1078 r300render
->base
.destroy
= r300_render_destroy
;
1080 return &r300render
->base
;
1083 struct draw_stage
* r300_draw_stage(struct r300_context
* r300
)
1085 struct vbuf_render
* render
;
1086 struct draw_stage
* stage
;
1088 render
= r300_render_create(r300
);
1094 stage
= draw_vbuf_stage(r300
->draw
, render
);
1097 render
->destroy(render
);
1101 draw_set_render(r300
->draw
, render
);
1106 /****************************************************************************
1107 * End of SW TCL functions *
1108 ***************************************************************************/
1110 /* This functions is used to draw a rectangle for the blitter module.
1112 * If we rendered a quad, the pixels on the main diagonal
1113 * would be computed and stored twice, which makes the clear/copy codepaths
1114 * somewhat inefficient. Instead we use a rectangular point sprite. */
1115 void r300_blitter_draw_rectangle(struct blitter_context
*blitter
,
1116 int x1
, int y1
, int x2
, int y2
,
1118 enum blitter_attrib_type type
,
1119 const union blitter_attrib
*attrib
)
1121 struct r300_context
*r300
= r300_context(util_blitter_get_pipe(blitter
));
1122 unsigned last_sprite_coord_enable
= r300
->sprite_coord_enable
;
1123 unsigned width
= x2
- x1
;
1124 unsigned height
= y2
- y1
;
1125 unsigned vertex_size
=
1126 type
== UTIL_BLITTER_ATTRIB_COLOR
|| !r300
->draw
? 8 : 4;
1127 unsigned dwords
= 13 + vertex_size
+
1128 (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
? 7 : 0);
1129 static const union blitter_attrib zeros
;
1132 /* XXX workaround for a lockup in MSAA resolve on SWTCL chipsets, this
1133 * function most probably doesn't handle type=NONE correctly */
1134 if (!r300
->screen
->caps
.has_tcl
&& type
== UTIL_BLITTER_ATTRIB_NONE
) {
1135 util_blitter_draw_rectangle(blitter
, x1
, y1
, x2
, y2
, depth
, type
, attrib
);
1139 if (r300
->skip_rendering
)
1142 if (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
)
1143 r300
->sprite_coord_enable
= 1;
1145 r300_update_derived_state(r300
);
1147 /* Mark some states we don't care about as non-dirty. */
1148 r300
->viewport_state
.dirty
= FALSE
;
1150 if (!r300_prepare_for_rendering(r300
, PREP_EMIT_STATES
, NULL
, dwords
, 0, 0, -1))
1153 DBG(r300
, DBG_DRAW
, "r300: draw_rectangle\n");
1157 OUT_CS_REG(R300_GA_POINT_SIZE
, (height
* 6) | ((width
* 6) << 16));
1159 if (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
) {
1160 /* Set up the GA to generate texcoords. */
1161 OUT_CS_REG(R300_GB_ENABLE
, R300_GB_POINT_STUFF_ENABLE
|
1162 (R300_GB_TEX_STR
<< R300_GB_TEX0_SOURCE_SHIFT
));
1163 OUT_CS_REG_SEQ(R300_GA_POINT_S0
, 4);
1164 OUT_CS_32F(attrib
->texcoord
.x1
);
1165 OUT_CS_32F(attrib
->texcoord
.y2
);
1166 OUT_CS_32F(attrib
->texcoord
.x2
);
1167 OUT_CS_32F(attrib
->texcoord
.y1
);
1170 /* Set up VAP controls. */
1171 OUT_CS_REG(R300_VAP_CLIP_CNTL
, R300_CLIP_DISABLE
);
1172 OUT_CS_REG(R300_VAP_VTE_CNTL
, R300_VTX_XY_FMT
| R300_VTX_Z_FMT
);
1173 OUT_CS_REG(R300_VAP_VTX_SIZE
, vertex_size
);
1174 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
1179 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2
, vertex_size
);
1180 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED
| (1 << 16) |
1181 R300_VAP_VF_CNTL__PRIM_POINTS
);
1183 OUT_CS_32F(x1
+ width
* 0.5f
);
1184 OUT_CS_32F(y1
+ height
* 0.5f
);
1188 if (vertex_size
== 8) {
1191 OUT_CS_TABLE(attrib
->color
, 4);
1196 /* Restore the state. */
1197 r300_mark_atom_dirty(r300
, &r300
->rs_state
);
1198 r300_mark_atom_dirty(r300
, &r300
->viewport_state
);
1200 r300
->sprite_coord_enable
= last_sprite_coord_enable
;
1203 void r300_init_render_functions(struct r300_context
*r300
)
1205 /* Set draw functions based on presence of HW TCL. */
1206 if (r300
->screen
->caps
.has_tcl
) {
1207 r300
->context
.draw_vbo
= r300_draw_vbo
;
1209 r300
->context
.draw_vbo
= r300_swtcl_draw_vbo
;
1212 /* Plug in the two-sided stencil reference value fallback if needed. */
1213 if (!r300
->screen
->caps
.is_r500
)
1214 r300_plug_in_stencil_ref_fallback(r300
);