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
,
127 unsigned min_index
, unsigned max_index
)
132 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
133 r300_provoking_vertex_fixes(r300
, mode
));
134 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
140 /* This function splits the index bias value into two parts:
141 * - buffer_offset: the value that can be safely added to buffer offsets
142 * in r300_emit_vertex_arrays (it must yield a positive offset when added to
143 * a vertex buffer offset)
144 * - index_offset: the value that must be manually subtracted from indices
145 * in an index buffer to achieve negative offsets. */
146 static void r300_split_index_bias(struct r300_context
*r300
, int index_bias
,
147 int *buffer_offset
, int *index_offset
)
149 struct pipe_vertex_buffer
*vb
, *vbufs
= r300
->vbuf_mgr
->real_vertex_buffer
;
150 struct pipe_vertex_element
*velem
= r300
->velems
->velem
;
154 if (index_bias
< 0) {
155 /* See how large index bias we may subtract. We must be careful
156 * here because negative buffer offsets are not allowed
158 max_neg_bias
= INT_MAX
;
159 for (i
= 0; i
< r300
->velems
->count
; i
++) {
160 vb
= &vbufs
[velem
[i
].vertex_buffer_index
];
161 size
= (vb
->buffer_offset
+ velem
[i
].src_offset
) / vb
->stride
;
162 max_neg_bias
= MIN2(max_neg_bias
, size
);
165 /* Now set the minimum allowed value. */
166 *buffer_offset
= MAX2(-max_neg_bias
, index_bias
);
168 /* A positive index bias is OK. */
169 *buffer_offset
= index_bias
;
172 *index_offset
= index_bias
- *buffer_offset
;
175 enum r300_prepare_flags
{
176 PREP_EMIT_STATES
= (1 << 0), /* call emit_dirty_state and friends? */
177 PREP_VALIDATE_VBOS
= (1 << 1), /* validate VBOs? */
178 PREP_EMIT_VARRAYS
= (1 << 2), /* call emit_vertex_arrays? */
179 PREP_EMIT_VARRAYS_SWTCL
= (1 << 3), /* call emit_vertex_arrays_swtcl? */
180 PREP_INDEXED
= (1 << 4) /* is this draw_elements? */
184 * Check if the requested number of dwords is available in the CS and
186 * \param r300 The context.
187 * \param flags See r300_prepare_flags.
188 * \param cs_dwords The number of dwords to reserve in CS.
189 * \return TRUE if the CS was flushed
191 static boolean
r300_reserve_cs_dwords(struct r300_context
*r300
,
192 enum r300_prepare_flags flags
,
195 boolean flushed
= FALSE
;
196 boolean emit_states
= flags
& PREP_EMIT_STATES
;
197 boolean emit_vertex_arrays
= flags
& PREP_EMIT_VARRAYS
;
198 boolean emit_vertex_arrays_swtcl
= flags
& PREP_EMIT_VARRAYS_SWTCL
;
200 /* Add dirty state, index offset, and AOS. */
202 cs_dwords
+= r300_get_num_dirty_dwords(r300
);
204 if (r300
->screen
->caps
.is_r500
)
205 cs_dwords
+= 2; /* emit_index_offset */
207 if (emit_vertex_arrays
)
208 cs_dwords
+= 55; /* emit_vertex_arrays */
210 if (emit_vertex_arrays_swtcl
)
211 cs_dwords
+= 7; /* emit_vertex_arrays_swtcl */
213 cs_dwords
+= r300_get_num_cs_end_dwords(r300
);
215 /* Reserve requested CS space. */
216 if (cs_dwords
> (RADEON_MAX_CMDBUF_DWORDS
- r300
->cs
->cdw
)) {
217 r300_flush(&r300
->context
, RADEON_FLUSH_ASYNC
, NULL
);
225 * Validate buffers and emit dirty state.
226 * \param r300 The context.
227 * \param flags See r300_prepare_flags.
228 * \param index_buffer The index buffer to validate. The parameter may be NULL.
229 * \param buffer_offset The offset passed to emit_vertex_arrays.
230 * \param index_bias The index bias to emit.
231 * \param instance_id Index of instance to render
232 * \return TRUE if rendering should be skipped
234 static boolean
r300_emit_states(struct r300_context
*r300
,
235 enum r300_prepare_flags flags
,
236 struct pipe_resource
*index_buffer
,
238 int index_bias
, int instance_id
)
240 boolean emit_states
= flags
& PREP_EMIT_STATES
;
241 boolean emit_vertex_arrays
= flags
& PREP_EMIT_VARRAYS
;
242 boolean emit_vertex_arrays_swtcl
= flags
& PREP_EMIT_VARRAYS_SWTCL
;
243 boolean indexed
= flags
& PREP_INDEXED
;
244 boolean validate_vbos
= flags
& PREP_VALIDATE_VBOS
;
246 /* Validate buffers and emit dirty state if needed. */
247 if (emit_states
|| (emit_vertex_arrays
&& validate_vbos
)) {
248 if (!r300_emit_buffer_validate(r300
, validate_vbos
,
250 fprintf(stderr
, "r300: CS space validation failed. "
251 "(not enough memory?) Skipping rendering.\n");
257 r300_emit_dirty_state(r300
);
259 if (r300
->screen
->caps
.is_r500
) {
260 if (r300
->screen
->caps
.has_tcl
)
261 r500_emit_index_bias(r300
, index_bias
);
263 r500_emit_index_bias(r300
, 0);
266 if (emit_vertex_arrays
&&
267 (r300
->vertex_arrays_dirty
||
268 r300
->vertex_arrays_indexed
!= indexed
||
269 r300
->vertex_arrays_offset
!= buffer_offset
||
270 r300
->vertex_arrays_instance_id
!= instance_id
)) {
271 r300_emit_vertex_arrays(r300
, buffer_offset
, indexed
, instance_id
);
273 r300
->vertex_arrays_dirty
= FALSE
;
274 r300
->vertex_arrays_indexed
= indexed
;
275 r300
->vertex_arrays_offset
= buffer_offset
;
276 r300
->vertex_arrays_instance_id
= instance_id
;
279 if (emit_vertex_arrays_swtcl
)
280 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 * \param instance_id The instance to render.
295 * \return TRUE if rendering should be skipped
297 static boolean
r300_prepare_for_rendering(struct r300_context
*r300
,
298 enum r300_prepare_flags flags
,
299 struct pipe_resource
*index_buffer
,
305 /* Make sure there is enough space in the command stream and emit states. */
306 if (r300_reserve_cs_dwords(r300
, flags
, cs_dwords
))
307 flags
|= PREP_EMIT_STATES
;
309 return r300_emit_states(r300
, flags
, index_buffer
, buffer_offset
,
310 index_bias
, instance_id
);
313 static boolean
immd_is_good_idea(struct r300_context
*r300
,
316 if (DBG_ON(r300
, DBG_NO_IMMD
)) {
320 if (count
* r300
->velems
->vertex_size_dwords
> IMMD_DWORDS
) {
324 /* Buffers can only be used for read by r300 (except query buffers, but
325 * those can't be bound by a state tracker as vertex buffers). */
329 /*****************************************************************************
330 * The HWTCL draw functions. *
331 ****************************************************************************/
333 static void r300_draw_arrays_immediate(struct r300_context
*r300
,
334 const struct pipe_draw_info
*info
)
336 struct pipe_vertex_element
* velem
;
337 struct pipe_vertex_buffer
* vbuf
;
338 unsigned vertex_element_count
= r300
->velems
->count
;
341 /* Size of the vertex, in dwords. */
342 unsigned vertex_size
= r300
->velems
->vertex_size_dwords
;
344 /* The number of dwords for this draw operation. */
345 unsigned dwords
= 4 + info
->count
* vertex_size
;
347 /* Size of the vertex element, in dwords. */
348 unsigned size
[PIPE_MAX_ATTRIBS
];
350 /* Stride to the same attrib in the next vertex in the vertex buffer,
352 unsigned stride
[PIPE_MAX_ATTRIBS
];
354 /* Mapped vertex buffers. */
355 uint32_t* map
[PIPE_MAX_ATTRIBS
] = {0};
356 uint32_t* mapelem
[PIPE_MAX_ATTRIBS
];
360 if (!r300_prepare_for_rendering(r300
, PREP_EMIT_STATES
, NULL
, dwords
, 0, 0, -1))
363 /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
364 for (i
= 0; i
< vertex_element_count
; i
++) {
365 velem
= &r300
->velems
->velem
[i
];
366 size
[i
] = r300
->velems
->format_size
[i
] / 4;
367 vbi
= velem
->vertex_buffer_index
;
368 vbuf
= &r300
->vbuf_mgr
->real_vertex_buffer
[vbi
];
369 stride
[i
] = vbuf
->stride
/ 4;
371 /* Map the buffer. */
373 map
[vbi
] = (uint32_t*)r300
->rws
->buffer_map(
374 r300_resource(vbuf
->buffer
)->buf
,
375 r300
->cs
, PIPE_TRANSFER_READ
| PIPE_TRANSFER_UNSYNCHRONIZED
);
376 map
[vbi
] += (vbuf
->buffer_offset
/ 4) + stride
[i
] * info
->start
;
378 mapelem
[i
] = map
[vbi
] + (velem
->src_offset
/ 4);
381 r300_emit_draw_init(r300
, info
->mode
, 0, info
->count
-1);
384 OUT_CS_REG(R300_VAP_VTX_SIZE
, vertex_size
);
385 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2
, info
->count
* vertex_size
);
386 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED
| (info
->count
<< 16) |
387 r300_translate_primitive(info
->mode
));
390 for (v
= 0; v
< info
->count
; v
++) {
391 for (i
= 0; i
< vertex_element_count
; i
++) {
392 OUT_CS_TABLE(&mapelem
[i
][stride
[i
] * v
], size
[i
]);
398 for (i
= 0; i
< vertex_element_count
; i
++) {
399 vbi
= r300
->velems
->velem
[i
].vertex_buffer_index
;
402 r300
->rws
->buffer_unmap(r300_resource(r300
->vbuf_mgr
->real_vertex_buffer
[vbi
].buffer
)->buf
);
408 static void r300_emit_draw_arrays(struct r300_context
*r300
,
412 boolean alt_num_verts
= count
> 65535;
415 if (count
>= (1 << 24)) {
416 fprintf(stderr
, "r300: Got a huge number of vertices: %i, "
417 "refusing to render.\n", count
);
421 r300_emit_draw_init(r300
, mode
, 0, count
-1);
423 BEGIN_CS(2 + (alt_num_verts
? 2 : 0));
425 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES
, count
);
427 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2
, 0);
428 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST
| (count
<< 16) |
429 r300_translate_primitive(mode
) |
430 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
434 static void r300_emit_draw_elements(struct r300_context
*r300
,
435 struct pipe_resource
* indexBuffer
,
442 uint16_t *imm_indices3
)
444 uint32_t count_dwords
, offset_dwords
;
445 boolean alt_num_verts
= count
> 65535;
448 if (count
>= (1 << 24) || max_index
>= (1 << 24)) {
449 fprintf(stderr
, "r300: Got a huge number of vertices: %i, "
450 "refusing to render (max_index: %i).\n", count
, max_index
);
454 DBG(r300
, DBG_DRAW
, "r300: Indexbuf of %u indices, min %u max %u\n",
455 count
, min_index
, max_index
);
457 r300_emit_draw_init(r300
, mode
, min_index
, max_index
);
459 /* If start is odd, render the first triangle with indices embedded
460 * in the command stream. This will increase start by 3 and make it
461 * even. We can then proceed without a fallback. */
462 if (indexSize
== 2 && (start
& 1) &&
463 mode
== PIPE_PRIM_TRIANGLES
) {
465 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, 2);
466 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (3 << 16) |
467 R300_VAP_VF_CNTL__PRIM_TRIANGLES
);
468 OUT_CS(imm_indices3
[1] << 16 | imm_indices3
[0]);
469 OUT_CS(imm_indices3
[2]);
478 offset_dwords
= indexSize
* start
/ sizeof(uint32_t);
480 BEGIN_CS(8 + (alt_num_verts
? 2 : 0));
482 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES
, count
);
484 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, 0);
485 if (indexSize
== 4) {
486 count_dwords
= count
;
487 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
488 R300_VAP_VF_CNTL__INDEX_SIZE_32bit
|
489 r300_translate_primitive(mode
) |
490 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
492 count_dwords
= (count
+ 1) / 2;
493 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
494 r300_translate_primitive(mode
) |
495 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
498 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER
, 2);
499 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR
| (R300_VAP_PORT_IDX0
>> 2) |
500 (0 << R300_INDX_BUFFER_SKIP_SHIFT
));
501 OUT_CS(offset_dwords
<< 2);
502 OUT_CS(count_dwords
);
503 OUT_CS_RELOC(r300_resource(indexBuffer
));
507 static void r300_draw_elements_immediate(struct r300_context
*r300
,
508 const struct pipe_draw_info
*info
)
513 unsigned index_size
= r300
->index_buffer
.index_size
;
514 unsigned i
, count_dwords
= index_size
== 4 ? info
->count
:
515 (info
->count
+ 1) / 2;
518 /* 19 dwords for r300_draw_elements_immediate. Give up if the function fails. */
519 if (!r300_prepare_for_rendering(r300
,
520 PREP_EMIT_STATES
| PREP_VALIDATE_VBOS
| PREP_EMIT_VARRAYS
|
521 PREP_INDEXED
, NULL
, 2+count_dwords
, 0, info
->index_bias
, -1))
524 r300_emit_draw_init(r300
, info
->mode
, info
->min_index
, info
->max_index
);
526 BEGIN_CS(2 + count_dwords
);
527 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, count_dwords
);
529 switch (index_size
) {
531 ptr1
= r300_resource(r300
->index_buffer
.buffer
)->b
.user_ptr
;
534 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (info
->count
<< 16) |
535 r300_translate_primitive(info
->mode
));
537 if (info
->index_bias
&& !r300
->screen
->caps
.is_r500
) {
538 for (i
= 0; i
< info
->count
-1; i
+= 2)
539 OUT_CS(((ptr1
[i
+1] + info
->index_bias
) << 16) |
540 (ptr1
[i
] + info
->index_bias
));
543 OUT_CS(ptr1
[i
] + info
->index_bias
);
545 for (i
= 0; i
< info
->count
-1; i
+= 2)
546 OUT_CS(((ptr1
[i
+1]) << 16) |
555 ptr2
= (uint16_t*)r300_resource(r300
->index_buffer
.buffer
)->b
.user_ptr
;
558 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (info
->count
<< 16) |
559 r300_translate_primitive(info
->mode
));
561 if (info
->index_bias
&& !r300
->screen
->caps
.is_r500
) {
562 for (i
= 0; i
< info
->count
-1; i
+= 2)
563 OUT_CS(((ptr2
[i
+1] + info
->index_bias
) << 16) |
564 (ptr2
[i
] + info
->index_bias
));
567 OUT_CS(ptr2
[i
] + info
->index_bias
);
569 OUT_CS_TABLE(ptr2
, count_dwords
);
574 ptr4
= (uint32_t*)r300_resource(r300
->index_buffer
.buffer
)->b
.user_ptr
;
577 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (info
->count
<< 16) |
578 R300_VAP_VF_CNTL__INDEX_SIZE_32bit
|
579 r300_translate_primitive(info
->mode
));
581 if (info
->index_bias
&& !r300
->screen
->caps
.is_r500
) {
582 for (i
= 0; i
< info
->count
; i
++)
583 OUT_CS(ptr4
[i
] + info
->index_bias
);
585 OUT_CS_TABLE(ptr4
, count_dwords
);
592 static void r300_draw_elements(struct r300_context
*r300
,
593 const struct pipe_draw_info
*info
,
596 struct pipe_resource
*indexBuffer
= r300
->index_buffer
.buffer
;
597 unsigned indexSize
= r300
->index_buffer
.index_size
;
598 struct pipe_resource
* orgIndexBuffer
= indexBuffer
;
599 unsigned start
= info
->start
;
600 unsigned count
= info
->count
;
601 boolean alt_num_verts
= r300
->screen
->caps
.is_r500
&&
603 unsigned short_count
;
604 int buffer_offset
= 0, index_offset
= 0; /* for index bias emulation */
605 uint16_t indices3
[3];
607 if (info
->index_bias
&& !r300
->screen
->caps
.is_r500
) {
608 r300_split_index_bias(r300
, info
->index_bias
, &buffer_offset
, &index_offset
);
611 r300_translate_index_buffer(r300
, &indexBuffer
, &indexSize
, index_offset
,
614 /* Fallback for misaligned ushort indices. */
615 if (indexSize
== 2 && (start
& 1) &&
616 !r300_resource(indexBuffer
)->b
.user_ptr
) {
617 /* If we got here, then orgIndexBuffer == indexBuffer. */
618 uint16_t *ptr
= r300
->rws
->buffer_map(r300_resource(orgIndexBuffer
)->buf
,
621 PIPE_TRANSFER_UNSYNCHRONIZED
);
623 if (info
->mode
== PIPE_PRIM_TRIANGLES
) {
624 memcpy(indices3
, ptr
+ start
, 6);
626 /* Copy the mapped index buffer directly to the upload buffer.
627 * The start index will be aligned simply from the fact that
628 * every sub-buffer in the upload buffer is aligned. */
629 r300_upload_index_buffer(r300
, &indexBuffer
, indexSize
, &start
,
630 count
, (uint8_t*)ptr
);
632 r300
->rws
->buffer_unmap(r300_resource(orgIndexBuffer
)->buf
);
634 if (r300_resource(indexBuffer
)->b
.user_ptr
)
635 r300_upload_index_buffer(r300
, &indexBuffer
, indexSize
,
637 r300_resource(indexBuffer
)->b
.user_ptr
);
640 /* 19 dwords for emit_draw_elements. Give up if the function fails. */
641 if (!r300_prepare_for_rendering(r300
,
642 PREP_EMIT_STATES
| PREP_VALIDATE_VBOS
| PREP_EMIT_VARRAYS
|
643 PREP_INDEXED
, indexBuffer
, 19, buffer_offset
, info
->index_bias
,
647 if (alt_num_verts
|| count
<= 65535) {
648 r300_emit_draw_elements(r300
, indexBuffer
, indexSize
, info
->min_index
,
649 info
->max_index
, info
->mode
, start
, count
,
653 /* The maximum must be divisible by 4 and 3,
654 * so that quad and triangle lists are split correctly.
656 * Strips, loops, and fans won't work. */
657 short_count
= MIN2(count
, 65532);
659 r300_emit_draw_elements(r300
, indexBuffer
, indexSize
,
660 info
->min_index
, info
->max_index
,
661 info
->mode
, start
, short_count
, indices3
);
663 start
+= short_count
;
664 count
-= short_count
;
666 /* 15 dwords for emit_draw_elements */
668 if (!r300_prepare_for_rendering(r300
,
669 PREP_VALIDATE_VBOS
| PREP_EMIT_VARRAYS
| PREP_INDEXED
,
670 indexBuffer
, 19, buffer_offset
, info
->index_bias
,
678 if (indexBuffer
!= orgIndexBuffer
) {
679 pipe_resource_reference( &indexBuffer
, NULL
);
683 static void r300_draw_arrays(struct r300_context
*r300
,
684 const struct pipe_draw_info
*info
,
687 boolean alt_num_verts
= r300
->screen
->caps
.is_r500
&&
689 unsigned start
= info
->start
;
690 unsigned count
= info
->count
;
691 unsigned short_count
;
693 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
694 if (!r300_prepare_for_rendering(r300
,
695 PREP_EMIT_STATES
| PREP_VALIDATE_VBOS
| PREP_EMIT_VARRAYS
,
696 NULL
, 9, start
, 0, instance_id
))
699 if (alt_num_verts
|| count
<= 65535) {
700 r300_emit_draw_arrays(r300
, info
->mode
, count
);
703 /* The maximum must be divisible by 4 and 3,
704 * so that quad and triangle lists are split correctly.
706 * Strips, loops, and fans won't work. */
707 short_count
= MIN2(count
, 65532);
708 r300_emit_draw_arrays(r300
, info
->mode
, short_count
);
710 start
+= short_count
;
711 count
-= short_count
;
713 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
715 if (!r300_prepare_for_rendering(r300
,
716 PREP_VALIDATE_VBOS
| PREP_EMIT_VARRAYS
, NULL
, 9,
717 start
, 0, instance_id
))
724 static void r300_draw_arrays_instanced(struct r300_context
*r300
,
725 const struct pipe_draw_info
*info
)
729 for (i
= 0; i
< info
->instance_count
; i
++)
730 r300_draw_arrays(r300
, info
, i
);
733 static void r300_draw_elements_instanced(struct r300_context
*r300
,
734 const struct pipe_draw_info
*info
)
738 for (i
= 0; i
< info
->instance_count
; i
++)
739 r300_draw_elements(r300
, info
, i
);
742 static void r300_draw_vbo(struct pipe_context
* pipe
,
743 const struct pipe_draw_info
*dinfo
)
745 struct r300_context
* r300
= r300_context(pipe
);
746 struct pipe_draw_info info
= *dinfo
;
748 info
.indexed
= info
.indexed
&& r300
->index_buffer
.buffer
;
750 if (r300
->skip_rendering
||
751 !u_trim_pipe_prim(info
.mode
, &info
.count
)) {
755 r300_update_derived_state(r300
);
757 /* Start the vbuf manager and update buffers if needed. */
758 if (u_vbuf_draw_begin(r300
->vbuf_mgr
, &info
) & U_VBUF_BUFFERS_UPDATED
) {
759 r300
->vertex_arrays_dirty
= TRUE
;
764 info
.start
+= r300
->index_buffer
.offset
;
765 info
.max_index
= MIN2(r300
->vbuf_mgr
->max_index
, info
.max_index
);
767 if (info
.instance_count
<= 1) {
768 if (info
.count
<= 8 &&
769 r300_resource(r300
->index_buffer
.buffer
)->b
.user_ptr
) {
770 r300_draw_elements_immediate(r300
, &info
);
772 r300_draw_elements(r300
, &info
, -1);
775 r300_draw_elements_instanced(r300
, &info
);
778 if (info
.instance_count
<= 1) {
779 if (immd_is_good_idea(r300
, info
.count
)) {
780 r300_draw_arrays_immediate(r300
, &info
);
782 r300_draw_arrays(r300
, &info
, -1);
785 r300_draw_arrays_instanced(r300
, &info
);
789 u_vbuf_draw_end(r300
->vbuf_mgr
);
792 /****************************************************************************
793 * The rest of this file is for SW TCL rendering only. Please be polite and *
794 * keep these functions separated so that they are easier to locate. ~C. *
795 ***************************************************************************/
797 /* SW TCL elements, using Draw. */
798 static void r300_swtcl_draw_vbo(struct pipe_context
* pipe
,
799 const struct pipe_draw_info
*info
)
801 struct r300_context
* r300
= r300_context(pipe
);
802 struct pipe_transfer
*vb_transfer
[PIPE_MAX_ATTRIBS
];
803 struct pipe_transfer
*ib_transfer
= NULL
;
805 void *indices
= NULL
;
806 boolean indexed
= info
->indexed
&& r300
->index_buffer
.buffer
;
808 if (r300
->skip_rendering
) {
812 r300_update_derived_state(r300
);
814 r300_reserve_cs_dwords(r300
,
815 PREP_EMIT_STATES
| PREP_EMIT_VARRAYS_SWTCL
|
816 (indexed
? PREP_INDEXED
: 0),
819 for (i
= 0; i
< r300
->vbuf_mgr
->nr_vertex_buffers
; i
++) {
820 if (r300
->vbuf_mgr
->vertex_buffer
[i
].buffer
) {
821 void *buf
= pipe_buffer_map(pipe
,
822 r300
->vbuf_mgr
->vertex_buffer
[i
].buffer
,
824 PIPE_TRANSFER_UNSYNCHRONIZED
,
826 draw_set_mapped_vertex_buffer(r300
->draw
, i
, buf
);
831 indices
= pipe_buffer_map(pipe
, r300
->index_buffer
.buffer
,
833 PIPE_TRANSFER_UNSYNCHRONIZED
, &ib_transfer
);
836 draw_set_mapped_index_buffer(r300
->draw
, indices
);
838 r300
->draw_vbo_locked
= TRUE
;
839 r300
->draw_first_emitted
= FALSE
;
840 draw_vbo(r300
->draw
, info
);
841 draw_flush(r300
->draw
);
842 r300
->draw_vbo_locked
= FALSE
;
844 for (i
= 0; i
< r300
->vbuf_mgr
->nr_vertex_buffers
; i
++) {
845 if (r300
->vbuf_mgr
->vertex_buffer
[i
].buffer
) {
846 pipe_buffer_unmap(pipe
, vb_transfer
[i
]);
847 draw_set_mapped_vertex_buffer(r300
->draw
, i
, NULL
);
852 pipe_buffer_unmap(pipe
, ib_transfer
);
853 draw_set_mapped_index_buffer(r300
->draw
, NULL
);
857 /* Object for rendering using Draw. */
860 struct vbuf_render base
;
863 struct r300_context
* r300
;
865 /* Vertex information */
874 struct pipe_transfer
*vbo_transfer
;
877 static INLINE
struct r300_render
*
878 r300_render(struct vbuf_render
* render
)
880 return (struct r300_render
*)render
;
883 static const struct vertex_info
*
884 r300_render_get_vertex_info(struct vbuf_render
* render
)
886 struct r300_render
* r300render
= r300_render(render
);
887 struct r300_context
* r300
= r300render
->r300
;
889 return &r300
->vertex_info
;
892 static boolean
r300_render_allocate_vertices(struct vbuf_render
* render
,
896 struct r300_render
* r300render
= r300_render(render
);
897 struct r300_context
* r300
= r300render
->r300
;
898 struct pipe_screen
* screen
= r300
->context
.screen
;
899 size_t size
= (size_t)vertex_size
* (size_t)count
;
901 DBG(r300
, DBG_DRAW
, "r300: render_allocate_vertices (size: %d)\n", size
);
903 if (size
+ r300
->draw_vbo_offset
> r300
->draw_vbo_size
)
905 pipe_resource_reference(&r300
->vbo
, NULL
);
906 r300
->vbo
= pipe_buffer_create(screen
,
907 PIPE_BIND_VERTEX_BUFFER
,
909 R300_MAX_DRAW_VBO_SIZE
);
910 r300
->draw_vbo_offset
= 0;
911 r300
->draw_vbo_size
= R300_MAX_DRAW_VBO_SIZE
;
914 r300render
->vertex_size
= vertex_size
;
916 return (r300
->vbo
) ? TRUE
: FALSE
;
919 static void* r300_render_map_vertices(struct vbuf_render
* render
)
921 struct r300_render
* r300render
= r300_render(render
);
922 struct r300_context
* r300
= r300render
->r300
;
924 assert(!r300render
->vbo_transfer
);
926 DBG(r300
, DBG_DRAW
, "r300: render_map_vertices\n");
928 r300render
->vbo_ptr
= pipe_buffer_map(&r300render
->r300
->context
,
930 PIPE_TRANSFER_WRITE
|
931 PIPE_TRANSFER_UNSYNCHRONIZED
,
932 &r300render
->vbo_transfer
);
934 assert(r300render
->vbo_ptr
);
936 return ((uint8_t*)r300render
->vbo_ptr
+ r300
->draw_vbo_offset
);
939 static void r300_render_unmap_vertices(struct vbuf_render
* render
,
943 struct r300_render
* r300render
= r300_render(render
);
944 struct pipe_context
* context
= &r300render
->r300
->context
;
945 struct r300_context
* r300
= r300render
->r300
;
947 assert(r300render
->vbo_transfer
);
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));
953 pipe_buffer_unmap(context
, r300render
->vbo_transfer
);
955 r300render
->vbo_transfer
= NULL
;
958 static void r300_render_release_vertices(struct vbuf_render
* render
)
960 struct r300_render
* r300render
= r300_render(render
);
961 struct r300_context
* r300
= r300render
->r300
;
963 DBG(r300
, DBG_DRAW
, "r300: render_release_vertices\n");
965 r300
->draw_vbo_offset
+= r300render
->vbo_max_used
;
966 r300render
->vbo_max_used
= 0;
969 static boolean
r300_render_set_primitive(struct vbuf_render
* render
,
972 struct r300_render
* r300render
= r300_render(render
);
974 r300render
->prim
= prim
;
975 r300render
->hwprim
= r300_translate_primitive(prim
);
980 static void r300_render_draw_arrays(struct vbuf_render
* render
,
984 struct r300_render
* r300render
= r300_render(render
);
985 struct r300_context
* r300
= r300render
->r300
;
991 (void) i
; (void) ptr
;
993 DBG(r300
, DBG_DRAW
, "r300: render_draw_arrays (count: %d)\n", count
);
995 if (r300
->draw_first_emitted
) {
996 if (!r300_prepare_for_rendering(r300
,
997 PREP_EMIT_STATES
| PREP_EMIT_VARRAYS_SWTCL
,
998 NULL
, dwords
, 0, 0, -1))
1001 if (!r300_emit_states(r300
,
1002 PREP_EMIT_STATES
| PREP_EMIT_VARRAYS_SWTCL
,
1008 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
1009 r300_provoking_vertex_fixes(r300
, r300render
->prim
));
1010 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX
, count
- 1);
1011 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2
, 0);
1012 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST
| (count
<< 16) |
1013 r300render
->hwprim
);
1016 r300
->draw_first_emitted
= TRUE
;
1019 static void r300_render_draw_elements(struct vbuf_render
* render
,
1020 const ushort
* indices
,
1023 struct r300_render
* r300render
= r300_render(render
);
1024 struct r300_context
* r300
= r300render
->r300
;
1026 unsigned end_cs_dwords
;
1027 unsigned max_index
= (r300
->draw_vbo_size
- r300
->draw_vbo_offset
) /
1028 (r300render
->r300
->vertex_info
.size
* 4) - 1;
1029 unsigned short_count
;
1030 unsigned free_dwords
;
1033 DBG(r300
, DBG_DRAW
, "r300: render_draw_elements (count: %d)\n", count
);
1035 if (r300
->draw_first_emitted
) {
1036 if (!r300_prepare_for_rendering(r300
,
1037 PREP_EMIT_STATES
| PREP_EMIT_VARRAYS_SWTCL
| PREP_INDEXED
,
1038 NULL
, 256, 0, 0, -1))
1041 if (!r300_emit_states(r300
,
1042 PREP_EMIT_STATES
| PREP_EMIT_VARRAYS_SWTCL
| PREP_INDEXED
,
1047 /* Below we manage the CS space manually because there may be more
1048 * indices than it can fit in CS. */
1050 end_cs_dwords
= r300_get_num_cs_end_dwords(r300
);
1053 free_dwords
= RADEON_MAX_CMDBUF_DWORDS
- r300
->cs
->cdw
;
1055 short_count
= MIN2(count
, (free_dwords
- end_cs_dwords
- 6) * 2);
1057 BEGIN_CS(6 + (short_count
+1)/2);
1058 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
1059 r300_provoking_vertex_fixes(r300
, r300render
->prim
));
1060 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX
, max_index
);
1061 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, (short_count
+1)/2);
1062 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (short_count
<< 16) |
1063 r300render
->hwprim
);
1064 for (i
= 0; i
< short_count
-1; i
+= 2) {
1065 OUT_CS(indices
[i
+1] << 16 | indices
[i
]);
1067 if (short_count
% 2) {
1068 OUT_CS(indices
[short_count
-1]);
1072 /* OK now subtract the emitted indices and see if we need to emit
1073 * another draw packet. */
1074 indices
+= short_count
;
1075 count
-= short_count
;
1078 if (!r300_prepare_for_rendering(r300
,
1079 PREP_EMIT_VARRAYS_SWTCL
| PREP_INDEXED
,
1080 NULL
, 256, 0, 0, -1))
1083 end_cs_dwords
= r300_get_num_cs_end_dwords(r300
);
1087 r300
->draw_first_emitted
= TRUE
;
1090 static void r300_render_destroy(struct vbuf_render
* render
)
1095 static struct vbuf_render
* r300_render_create(struct r300_context
* r300
)
1097 struct r300_render
* r300render
= CALLOC_STRUCT(r300_render
);
1099 r300render
->r300
= r300
;
1101 r300render
->base
.max_vertex_buffer_bytes
= 1024 * 1024;
1102 r300render
->base
.max_indices
= 16 * 1024;
1104 r300render
->base
.get_vertex_info
= r300_render_get_vertex_info
;
1105 r300render
->base
.allocate_vertices
= r300_render_allocate_vertices
;
1106 r300render
->base
.map_vertices
= r300_render_map_vertices
;
1107 r300render
->base
.unmap_vertices
= r300_render_unmap_vertices
;
1108 r300render
->base
.set_primitive
= r300_render_set_primitive
;
1109 r300render
->base
.draw_elements
= r300_render_draw_elements
;
1110 r300render
->base
.draw_arrays
= r300_render_draw_arrays
;
1111 r300render
->base
.release_vertices
= r300_render_release_vertices
;
1112 r300render
->base
.destroy
= r300_render_destroy
;
1114 return &r300render
->base
;
1117 struct draw_stage
* r300_draw_stage(struct r300_context
* r300
)
1119 struct vbuf_render
* render
;
1120 struct draw_stage
* stage
;
1122 render
= r300_render_create(r300
);
1128 stage
= draw_vbuf_stage(r300
->draw
, render
);
1131 render
->destroy(render
);
1135 draw_set_render(r300
->draw
, render
);
1140 void r300_draw_flush_vbuf(struct r300_context
*r300
)
1142 pipe_resource_reference(&r300
->vbo
, NULL
);
1143 r300
->draw_vbo_size
= 0;
1146 /****************************************************************************
1147 * End of SW TCL functions *
1148 ***************************************************************************/
1150 /* This functions is used to draw a rectangle for the blitter module.
1152 * If we rendered a quad, the pixels on the main diagonal
1153 * would be computed and stored twice, which makes the clear/copy codepaths
1154 * somewhat inefficient. Instead we use a rectangular point sprite. */
1155 static void r300_blitter_draw_rectangle(struct blitter_context
*blitter
,
1156 unsigned x1
, unsigned y1
,
1157 unsigned x2
, unsigned y2
,
1159 enum blitter_attrib_type type
,
1160 const union pipe_color_union
*attrib
)
1162 struct r300_context
*r300
= r300_context(util_blitter_get_pipe(blitter
));
1163 unsigned last_sprite_coord_enable
= r300
->sprite_coord_enable
;
1164 unsigned width
= x2
- x1
;
1165 unsigned height
= y2
- y1
;
1166 unsigned vertex_size
=
1167 type
== UTIL_BLITTER_ATTRIB_COLOR
|| !r300
->draw
? 8 : 4;
1168 unsigned dwords
= 13 + vertex_size
+
1169 (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
? 7 : 0);
1170 static const union pipe_color_union zeros
;
1173 if (r300
->skip_rendering
)
1176 r300
->context
.set_vertex_buffers(&r300
->context
, 0, NULL
);
1178 if (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
)
1179 r300
->sprite_coord_enable
= 1;
1181 r300_update_derived_state(r300
);
1183 /* Mark some states we don't care about as non-dirty. */
1184 r300
->clip_state
.dirty
= FALSE
;
1185 r300
->viewport_state
.dirty
= FALSE
;
1187 if (!r300_prepare_for_rendering(r300
, PREP_EMIT_STATES
, NULL
, dwords
, 0, 0, -1))
1190 DBG(r300
, DBG_DRAW
, "r300: draw_rectangle\n");
1194 OUT_CS_REG(R300_GA_POINT_SIZE
, (height
* 6) | ((width
* 6) << 16));
1196 if (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
) {
1197 /* Set up the GA to generate texcoords. */
1198 OUT_CS_REG(R300_GB_ENABLE
, R300_GB_POINT_STUFF_ENABLE
|
1199 (R300_GB_TEX_STR
<< R300_GB_TEX0_SOURCE_SHIFT
));
1200 OUT_CS_REG_SEQ(R300_GA_POINT_S0
, 4);
1201 OUT_CS_32F(attrib
->f
[0]);
1202 OUT_CS_32F(attrib
->f
[3]);
1203 OUT_CS_32F(attrib
->f
[2]);
1204 OUT_CS_32F(attrib
->f
[1]);
1207 /* Set up VAP controls. */
1208 OUT_CS_REG(R300_VAP_CLIP_CNTL
, R300_CLIP_DISABLE
);
1209 OUT_CS_REG(R300_VAP_VTE_CNTL
, R300_VTX_XY_FMT
| R300_VTX_Z_FMT
);
1210 OUT_CS_REG(R300_VAP_VTX_SIZE
, vertex_size
);
1211 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
1216 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2
, vertex_size
);
1217 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED
| (1 << 16) |
1218 R300_VAP_VF_CNTL__PRIM_POINTS
);
1220 OUT_CS_32F(x1
+ width
* 0.5f
);
1221 OUT_CS_32F(y1
+ height
* 0.5f
);
1225 if (vertex_size
== 8) {
1228 OUT_CS_TABLE(attrib
->f
, 4);
1233 /* Restore the state. */
1234 r300_mark_atom_dirty(r300
, &r300
->clip_state
);
1235 r300_mark_atom_dirty(r300
, &r300
->rs_state
);
1236 r300_mark_atom_dirty(r300
, &r300
->viewport_state
);
1238 r300
->sprite_coord_enable
= last_sprite_coord_enable
;
1241 static void r300_resource_resolve(struct pipe_context
*pipe
,
1242 const struct pipe_resolve_info
*info
)
1244 struct r300_context
*r300
= r300_context(pipe
);
1245 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
1246 struct r300_aa_state
*aa
= (struct r300_aa_state
*)r300
->aa_state
.state
;
1247 static const union pipe_color_union color
;
1249 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1250 surf_tmpl
.format
= info
->src
.res
->format
;
1251 surf_tmpl
.u
.tex
.first_layer
=
1252 surf_tmpl
.u
.tex
.last_layer
= info
->src
.layer
;
1253 srcsurf
= pipe
->create_surface(pipe
, info
->src
.res
, &surf_tmpl
);
1254 /* XXX Offset both surfaces by x0,y1. */
1256 surf_tmpl
.format
= info
->dst
.res
->format
;
1257 surf_tmpl
.u
.tex
.level
= info
->dst
.level
;
1258 surf_tmpl
.u
.tex
.first_layer
=
1259 surf_tmpl
.u
.tex
.last_layer
= info
->dst
.layer
;
1260 dstsurf
= pipe
->create_surface(pipe
, info
->dst
.res
, &surf_tmpl
);
1262 DBG(r300
, DBG_DRAW
, "r300: Resolving resource...\n");
1264 /* Enable AA resolve. */
1265 aa
->dest
= r300_surface(dstsurf
);
1267 R300_RB3D_AARESOLVE_CTL_AARESOLVE_MODE_RESOLVE
|
1268 R300_RB3D_AARESOLVE_CTL_AARESOLVE_ALPHA_AVERAGE
;
1269 r300
->aa_state
.size
= 10;
1270 r300_mark_atom_dirty(r300
, &r300
->aa_state
);
1272 /* Resolve the surface. */
1273 /* XXX: y1 < 0 ==> Y flip */
1274 r300
->context
.clear_render_target(pipe
,
1275 srcsurf
, &color
, 0, 0,
1276 info
->dst
.x1
- info
->dst
.x0
,
1277 info
->dst
.y1
- info
->dst
.y0
);
1279 /* Disable AA resolve. */
1280 aa
->aaresolve_ctl
= 0;
1281 r300
->aa_state
.size
= 4;
1282 r300_mark_atom_dirty(r300
, &r300
->aa_state
);
1284 pipe_surface_reference(&srcsurf
, NULL
);
1285 pipe_surface_reference(&dstsurf
, NULL
);
1288 void r300_init_render_functions(struct r300_context
*r300
)
1290 /* Set draw functions based on presence of HW TCL. */
1291 if (r300
->screen
->caps
.has_tcl
) {
1292 r300
->context
.draw_vbo
= r300_draw_vbo
;
1294 r300
->context
.draw_vbo
= r300_swtcl_draw_vbo
;
1297 r300
->context
.resource_resolve
= r300_resource_resolve
;
1298 r300
->blitter
->draw_rectangle
= r300_blitter_draw_rectangle
;
1300 /* Plug in the two-sided stencil reference value fallback if needed. */
1301 if (!r300
->screen
->caps
.is_r500
)
1302 r300_plug_in_stencil_ref_fallback(r300
);