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"
42 #include "r300_state_derived.h"
46 #define IMMD_DWORDS 32
48 static uint32_t r300_translate_primitive(unsigned prim
)
51 case PIPE_PRIM_POINTS
:
52 return R300_VAP_VF_CNTL__PRIM_POINTS
;
54 return R300_VAP_VF_CNTL__PRIM_LINES
;
55 case PIPE_PRIM_LINE_LOOP
:
56 return R300_VAP_VF_CNTL__PRIM_LINE_LOOP
;
57 case PIPE_PRIM_LINE_STRIP
:
58 return R300_VAP_VF_CNTL__PRIM_LINE_STRIP
;
59 case PIPE_PRIM_TRIANGLES
:
60 return R300_VAP_VF_CNTL__PRIM_TRIANGLES
;
61 case PIPE_PRIM_TRIANGLE_STRIP
:
62 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP
;
63 case PIPE_PRIM_TRIANGLE_FAN
:
64 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN
;
66 return R300_VAP_VF_CNTL__PRIM_QUADS
;
67 case PIPE_PRIM_QUAD_STRIP
:
68 return R300_VAP_VF_CNTL__PRIM_QUAD_STRIP
;
69 case PIPE_PRIM_POLYGON
:
70 return R300_VAP_VF_CNTL__PRIM_POLYGON
;
76 static uint32_t r300_provoking_vertex_fixes(struct r300_context
*r300
,
79 struct r300_rs_state
* rs
= (struct r300_rs_state
*)r300
->rs_state
.state
;
80 uint32_t color_control
= rs
->color_control
;
82 /* By default (see r300_state.c:r300_create_rs_state) color_control is
83 * initialized to provoking the first vertex.
85 * Triangle fans must be reduced to the second vertex, not the first, in
86 * Gallium flatshade-first mode, as per the GL spec.
87 * (http://www.opengl.org/registry/specs/ARB/provoking_vertex.txt)
89 * Quads never provoke correctly in flatshade-first mode. The first
90 * vertex is never considered as provoking, so only the second, third,
91 * and fourth vertices can be selected, and both "third" and "last" modes
92 * select the fourth vertex. This is probably due to D3D lacking quads.
94 * Similarly, polygons reduce to the first, not the last, vertex, when in
95 * "last" mode, and all other modes start from the second vertex.
100 if (rs
->rs
.flatshade_first
) {
102 case PIPE_PRIM_TRIANGLE_FAN
:
103 color_control
|= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND
;
105 case PIPE_PRIM_QUADS
:
106 case PIPE_PRIM_QUAD_STRIP
:
107 case PIPE_PRIM_POLYGON
:
108 color_control
|= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST
;
111 color_control
|= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST
;
115 color_control
|= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST
;
118 return color_control
;
121 void r500_emit_index_bias(struct r300_context
*r300
, int index_bias
)
126 OUT_CS_REG(R500_VAP_INDEX_OFFSET
,
127 (index_bias
& 0xFFFFFF) | (index_bias
< 0 ? 1<<24 : 0));
131 /* This function splits the index bias value into two parts:
132 * - buffer_offset: the value that can be safely added to buffer offsets
133 * in r300_emit_aos (it must yield a positive offset when added to
134 * a vertex buffer offset)
135 * - index_offset: the value that must be manually subtracted from indices
136 * in an index buffer to achieve negative offsets. */
137 static void r300_split_index_bias(struct r300_context
*r300
, int index_bias
,
138 int *buffer_offset
, int *index_offset
)
140 struct pipe_vertex_buffer
*vb
, *vbufs
= r300
->vertex_buffer
;
141 struct pipe_vertex_element
*velem
= r300
->velems
->velem
;
145 if (index_bias
< 0) {
146 /* See how large index bias we may subtract. We must be careful
147 * here because negative buffer offsets are not allowed
149 max_neg_bias
= INT_MAX
;
150 for (i
= 0; i
< r300
->velems
->count
; i
++) {
151 vb
= &vbufs
[velem
[i
].vertex_buffer_index
];
152 size
= (vb
->buffer_offset
+ velem
[i
].src_offset
) / vb
->stride
;
153 max_neg_bias
= MIN2(max_neg_bias
, size
);
156 /* Now set the minimum allowed value. */
157 *buffer_offset
= MAX2(-max_neg_bias
, index_bias
);
159 /* A positive index bias is OK. */
160 *buffer_offset
= index_bias
;
163 *index_offset
= index_bias
- *buffer_offset
;
166 enum r300_prepare_flags
{
167 PREP_FIRST_DRAW
= (1 << 0), /* call emit_dirty_state and friends? */
168 PREP_VALIDATE_VBOS
= (1 << 1), /* validate VBOs? */
169 PREP_EMIT_AOS
= (1 << 2), /* call emit_aos? */
170 PREP_EMIT_AOS_SWTCL
= (1 << 3), /* call emit_aos_swtcl? */
171 PREP_INDEXED
= (1 << 4) /* is this draw_elements? */
175 * Check if the requested number of dwords is available in the CS and
177 * \param r300 The context.
178 * \param flags See r300_prepare_flags.
179 * \param cs_dwords The number of dwords to reserve in CS.
180 * \return TRUE if the CS was flushed
182 static boolean
r300_reserve_cs_dwords(struct r300_context
*r300
,
183 enum r300_prepare_flags flags
,
186 boolean flushed
= FALSE
;
187 boolean first_draw
= flags
& PREP_FIRST_DRAW
;
188 boolean emit_aos
= flags
& PREP_EMIT_AOS
;
189 boolean emit_aos_swtcl
= flags
& PREP_EMIT_AOS_SWTCL
;
191 /* Add dirty state, index offset, and AOS. */
193 cs_dwords
+= r300_get_num_dirty_dwords(r300
);
195 if (r300
->screen
->caps
.index_bias_supported
)
196 cs_dwords
+= 2; /* emit_index_offset */
199 cs_dwords
+= 55; /* emit_aos */
202 cs_dwords
+= 7; /* emit_aos_swtcl */
205 cs_dwords
+= r300_get_num_cs_end_dwords(r300
);
207 /* Reserve requested CS space. */
208 if (cs_dwords
> (R300_MAX_CMDBUF_DWORDS
- r300
->cs
->cdw
)) {
209 r300
->context
.flush(&r300
->context
, 0, NULL
);
217 * Validate buffers and emit dirty state.
218 * \param r300 The context.
219 * \param flags See r300_prepare_flags.
220 * \param index_buffer The index buffer to validate. The parameter may be NULL.
221 * \param aos_offset The offset passed to emit_aos.
222 * \param index_bias The index bias to emit.
223 * \return TRUE if rendering should be skipped
225 static boolean
r300_emit_states(struct r300_context
*r300
,
226 enum r300_prepare_flags flags
,
227 struct pipe_resource
*index_buffer
,
231 boolean first_draw
= flags
& PREP_FIRST_DRAW
;
232 boolean emit_aos
= flags
& PREP_EMIT_AOS
;
233 boolean emit_aos_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 /* upload buffers first */
240 if (r300
->screen
->caps
.has_tcl
&& r300
->any_user_vbs
) {
241 r300_upload_user_buffers(r300
);
242 r300
->any_user_vbs
= false;
245 if (r300
->validate_buffers
) {
246 if (!r300_emit_buffer_validate(r300
, validate_vbos
,
248 fprintf(stderr
, "r300: CS space validation failed. "
249 "(not enough memory?) Skipping rendering.\n");
253 /* Consider the validation done only if everything was validated. */
255 r300
->validate_buffers
= FALSE
;
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);
268 r300_emit_aos(r300
, aos_offset
, indexed
);
271 r300_emit_aos_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 aos_offset The offset passed to emit_aos.
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
,
295 if (r300_reserve_cs_dwords(r300
, flags
, cs_dwords
))
296 flags
|= PREP_FIRST_DRAW
;
298 return r300_emit_states(r300
, flags
, index_buffer
, aos_offset
, index_bias
);
301 static boolean
immd_is_good_idea(struct r300_context
*r300
,
304 struct pipe_vertex_element
* velem
;
305 struct pipe_vertex_buffer
* vbuf
;
306 boolean checked
[PIPE_MAX_ATTRIBS
] = {0};
307 unsigned vertex_element_count
= r300
->velems
->count
;
310 if (DBG_ON(r300
, DBG_NO_IMMD
)) {
318 if (count
* r300
->velems
->vertex_size_dwords
> IMMD_DWORDS
) {
322 /* We shouldn't map buffers referenced by CS, busy buffers,
323 * and ones placed in VRAM. */
324 for (i
= 0; i
< vertex_element_count
; i
++) {
325 velem
= &r300
->velems
->velem
[i
];
326 vbi
= velem
->vertex_buffer_index
;
329 vbuf
= &r300
->vertex_buffer
[vbi
];
331 if (!(r300_buffer(vbuf
->buffer
)->domain
& R300_DOMAIN_GTT
)) {
335 if (r300_buffer_is_referenced(&r300
->context
,
337 R300_REF_CS
| R300_REF_HW
)) {
338 /* It's a very bad idea to map it... */
347 /*****************************************************************************
348 * The HWTCL draw functions. *
349 ****************************************************************************/
351 static void r300_emit_draw_arrays_immediate(struct r300_context
*r300
,
356 struct pipe_vertex_element
* velem
;
357 struct pipe_vertex_buffer
* vbuf
;
358 unsigned vertex_element_count
= r300
->velems
->count
;
361 /* Size of the vertex, in dwords. */
362 unsigned vertex_size
= r300
->velems
->vertex_size_dwords
;
364 /* The number of dwords for this draw operation. */
365 unsigned dwords
= 9 + count
* vertex_size
;
367 /* Size of the vertex element, in dwords. */
368 unsigned size
[PIPE_MAX_ATTRIBS
];
370 /* Stride to the same attrib in the next vertex in the vertex buffer,
372 unsigned stride
[PIPE_MAX_ATTRIBS
];
374 /* Mapped vertex buffers. */
375 uint32_t* map
[PIPE_MAX_ATTRIBS
];
376 uint32_t* mapelem
[PIPE_MAX_ATTRIBS
];
377 struct pipe_transfer
* transfer
[PIPE_MAX_ATTRIBS
] = {0};
381 if (!r300_prepare_for_rendering(r300
, PREP_FIRST_DRAW
, NULL
, dwords
, 0, 0))
384 /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
385 for (i
= 0; i
< vertex_element_count
; i
++) {
386 velem
= &r300
->velems
->velem
[i
];
387 size
[i
] = r300
->velems
->hw_format_size
[i
] / 4;
388 vbi
= velem
->vertex_buffer_index
;
389 vbuf
= &r300
->vertex_buffer
[vbi
];
390 stride
[i
] = vbuf
->stride
/ 4;
392 /* Map the buffer. */
393 if (!transfer
[vbi
]) {
394 map
[vbi
] = (uint32_t*)pipe_buffer_map(&r300
->context
,
398 map
[vbi
] += (vbuf
->buffer_offset
/ 4) + stride
[i
] * start
;
400 mapelem
[i
] = map
[vbi
] + (velem
->src_offset
/ 4);
404 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
405 r300_provoking_vertex_fixes(r300
, mode
));
406 OUT_CS_REG(R300_VAP_VTX_SIZE
, vertex_size
);
407 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
410 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2
, count
* vertex_size
);
411 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED
| (count
<< 16) |
412 r300_translate_primitive(mode
));
415 for (v
= 0; v
< count
; v
++) {
416 for (i
= 0; i
< vertex_element_count
; i
++) {
417 OUT_CS_TABLE(&mapelem
[i
][stride
[i
] * v
], size
[i
]);
423 for (i
= 0; i
< vertex_element_count
; i
++) {
424 vbi
= r300
->velems
->velem
[i
].vertex_buffer_index
;
427 vbuf
= &r300
->vertex_buffer
[vbi
];
428 pipe_buffer_unmap(&r300
->context
, transfer
[vbi
]);
429 transfer
[vbi
] = NULL
;
434 static void r300_emit_draw_arrays(struct r300_context
*r300
,
438 boolean alt_num_verts
= count
> 65535;
441 if (count
>= (1 << 24)) {
442 fprintf(stderr
, "r300: Got a huge number of vertices: %i, "
443 "refusing to render.\n", count
);
447 BEGIN_CS(7 + (alt_num_verts
? 2 : 0));
449 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES
, count
);
451 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
452 r300_provoking_vertex_fixes(r300
, mode
));
453 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
456 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2
, 0);
457 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST
| (count
<< 16) |
458 r300_translate_primitive(mode
) |
459 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
463 static void r300_emit_draw_elements(struct r300_context
*r300
,
464 struct pipe_resource
* indexBuffer
,
471 uint16_t *imm_indices3
)
473 uint32_t count_dwords
, offset_dwords
;
474 boolean alt_num_verts
= count
> 65535;
477 if (count
>= (1 << 24)) {
478 fprintf(stderr
, "r300: Got a huge number of vertices: %i, "
479 "refusing to render.\n", count
);
483 maxIndex
= MIN2(maxIndex
, r300
->vertex_buffer_max_index
);
485 DBG(r300
, DBG_DRAW
, "r300: Indexbuf of %u indices, min %u max %u\n",
486 count
, minIndex
, maxIndex
);
489 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
490 r300_provoking_vertex_fixes(r300
, mode
));
491 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
496 /* If start is odd, render the first triangle with indices embedded
497 * in the command stream. This will increase start by 3 and make it
498 * even. We can then proceed without a fallback. */
499 if (indexSize
== 2 && (start
& 1) &&
500 mode
== PIPE_PRIM_TRIANGLES
) {
502 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, 2);
503 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (3 << 16) |
504 R300_VAP_VF_CNTL__PRIM_TRIANGLES
);
505 OUT_CS(imm_indices3
[1] << 16 | imm_indices3
[0]);
506 OUT_CS(imm_indices3
[2]);
515 offset_dwords
= indexSize
* start
/ sizeof(uint32_t);
517 BEGIN_CS(8 + (alt_num_verts
? 2 : 0));
519 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES
, count
);
521 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, 0);
522 if (indexSize
== 4) {
523 count_dwords
= count
;
524 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
525 R300_VAP_VF_CNTL__INDEX_SIZE_32bit
|
526 r300_translate_primitive(mode
) |
527 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
529 count_dwords
= (count
+ 1) / 2;
530 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
531 r300_translate_primitive(mode
) |
532 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
535 /* INDX_BUFFER is a truly special packet3.
536 * Unlike most other packet3, where the offset is after the count,
537 * the order is reversed, so the relocation ends up carrying the
538 * size of the indexbuf instead of the offset.
540 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER
, 2);
541 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR
| (R300_VAP_PORT_IDX0
>> 2) |
542 (0 << R300_INDX_BUFFER_SKIP_SHIFT
));
543 OUT_CS(offset_dwords
<< 2);
544 OUT_CS_BUF_RELOC(indexBuffer
, count_dwords
,
545 r300_buffer(indexBuffer
)->domain
, 0);
550 /* This is the fast-path drawing & emission for HW TCL. */
551 static void r300_draw_range_elements(struct pipe_context
* pipe
,
552 struct pipe_resource
* indexBuffer
,
561 struct r300_context
* r300
= r300_context(pipe
);
562 struct pipe_resource
* orgIndexBuffer
= indexBuffer
;
563 boolean alt_num_verts
= r300
->screen
->caps
.is_r500
&&
565 r300
->rws
->get_value(r300
->rws
, R300_VID_DRM_2_3_0
);
566 unsigned short_count
;
567 int buffer_offset
= 0, index_offset
= 0; /* for index bias emulation */
568 uint16_t indices3
[3];
570 if (indexBias
&& !r300
->screen
->caps
.index_bias_supported
) {
571 r300_split_index_bias(r300
, indexBias
, &buffer_offset
, &index_offset
);
574 r300_translate_index_buffer(r300
, &indexBuffer
, &indexSize
, index_offset
,
577 r300_update_derived_state(r300
);
579 /* Fallback for misaligned ushort indices. */
580 if (indexSize
== 2 && (start
& 1) &&
581 !r300_buffer_is_user_buffer(indexBuffer
)) {
582 struct pipe_transfer
*transfer
;
583 struct pipe_resource
*userbuf
;
585 uint16_t *ptr
= pipe_buffer_map(pipe
, indexBuffer
,
586 PIPE_TRANSFER_READ
, &transfer
);
588 if (mode
== PIPE_PRIM_TRIANGLES
) {
589 memcpy(indices3
, ptr
+ start
, 6);
591 /* Copy the mapped index buffer directly to the upload buffer.
592 * The start index will be aligned simply from the fact that
593 * every sub-buffer in u_upload_mgr is aligned. */
594 userbuf
= pipe
->screen
->user_buffer_create(pipe
->screen
,
595 ptr
+ start
, count
* 2,
596 PIPE_BIND_INDEX_BUFFER
);
597 indexBuffer
= userbuf
;
598 r300_upload_index_buffer(r300
, &indexBuffer
, indexSize
, 0, count
, &start
);
599 pipe_resource_reference(&userbuf
, NULL
);
601 pipe_buffer_unmap(pipe
, transfer
);
603 r300_upload_index_buffer(r300
, &indexBuffer
, indexSize
, start
, count
, &start
);
606 /* 19 dwords for emit_draw_elements. Give up if the function fails. */
607 if (!r300_prepare_for_rendering(r300
,
608 PREP_FIRST_DRAW
| PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
|
609 PREP_INDEXED
, indexBuffer
, 19, buffer_offset
, indexBias
))
612 if (alt_num_verts
|| count
<= 65535) {
613 r300_emit_draw_elements(r300
, indexBuffer
, indexSize
,
614 minIndex
, maxIndex
, mode
, start
, count
, indices3
);
617 short_count
= MIN2(count
, 65534);
618 r300_emit_draw_elements(r300
, indexBuffer
, indexSize
,
620 mode
, start
, short_count
, indices3
);
622 start
+= short_count
;
623 count
-= short_count
;
625 /* 15 dwords for emit_draw_elements */
627 if (!r300_prepare_for_rendering(r300
,
628 PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
| PREP_INDEXED
,
629 indexBuffer
, 19, buffer_offset
, indexBias
))
636 if (indexBuffer
!= orgIndexBuffer
) {
637 pipe_resource_reference( &indexBuffer
, NULL
);
641 static void r300_draw_arrays(struct pipe_context
* pipe
, unsigned mode
,
642 unsigned start
, unsigned count
)
644 struct r300_context
* r300
= r300_context(pipe
);
645 boolean alt_num_verts
= r300
->screen
->caps
.is_r500
&&
647 r300
->rws
->get_value(r300
->rws
, R300_VID_DRM_2_3_0
);
648 unsigned short_count
;
650 r300_update_derived_state(r300
);
652 if (immd_is_good_idea(r300
, count
)) {
653 r300_emit_draw_arrays_immediate(r300
, mode
, start
, count
);
655 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
656 if (!r300_prepare_for_rendering(r300
,
657 PREP_FIRST_DRAW
| PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
,
661 if (alt_num_verts
|| count
<= 65535) {
662 r300_emit_draw_arrays(r300
, mode
, count
);
665 short_count
= MIN2(count
, 65535);
666 r300_emit_draw_arrays(r300
, mode
, short_count
);
668 start
+= short_count
;
669 count
-= short_count
;
671 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
673 if (!r300_prepare_for_rendering(r300
,
674 PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
, NULL
, 9,
683 static void r300_draw_vbo(struct pipe_context
* pipe
,
684 const struct pipe_draw_info
*info
)
686 struct r300_context
* r300
= r300_context(pipe
);
687 unsigned count
= info
->count
;
688 boolean translate
= FALSE
;
689 boolean indexed
= info
->indexed
&& r300
->index_buffer
.buffer
;
690 unsigned start_indexed
= 0;
692 if (r300
->skip_rendering
) {
696 if (!u_trim_pipe_prim(info
->mode
, &count
)) {
700 /* Index buffer range checking. */
702 assert(r300
->index_buffer
.offset
% r300
->index_buffer
.index_size
== 0);
704 /* Compute start for draw_elements, taking the offset into account. */
707 (r300
->index_buffer
.offset
/ r300
->index_buffer
.index_size
);
709 if ((start_indexed
+ count
) * r300
->index_buffer
.index_size
>
710 r300
->index_buffer
.buffer
->width0
) {
711 fprintf(stderr
, "r300: Invalid index buffer range. Skipping rendering.\n");
716 /* Set up fallback for incompatible vertex layout if needed. */
717 if (r300
->incompatible_vb_layout
|| r300
->velems
->incompatible_layout
) {
718 r300_begin_vertex_translate(r300
);
723 r300_draw_range_elements(pipe
,
724 r300
->index_buffer
.buffer
,
725 r300
->index_buffer
.index_size
,
733 r300_draw_arrays(pipe
,
740 r300_end_vertex_translate(r300
);
744 /****************************************************************************
745 * The rest of this file is for SW TCL rendering only. Please be polite and *
746 * keep these functions separated so that they are easier to locate. ~C. *
747 ***************************************************************************/
749 /* SW TCL elements, using Draw. */
750 static void r300_swtcl_draw_vbo(struct pipe_context
* pipe
,
751 const struct pipe_draw_info
*info
)
753 struct r300_context
* r300
= r300_context(pipe
);
754 struct pipe_transfer
*vb_transfer
[PIPE_MAX_ATTRIBS
];
755 struct pipe_transfer
*ib_transfer
= NULL
;
756 unsigned count
= info
->count
;
758 void *indices
= NULL
;
759 boolean indexed
= info
->indexed
&& r300
->index_buffer
.buffer
;
761 if (r300
->skip_rendering
) {
765 if (!u_trim_pipe_prim(info
->mode
, &count
)) {
769 r300_update_derived_state(r300
);
771 r300_reserve_cs_dwords(r300
,
772 PREP_FIRST_DRAW
| PREP_EMIT_AOS_SWTCL
|
773 (indexed
? PREP_INDEXED
: 0),
776 for (i
= 0; i
< r300
->vertex_buffer_count
; i
++) {
777 if (r300
->vertex_buffer
[i
].buffer
) {
778 void *buf
= pipe_buffer_map(pipe
,
779 r300
->vertex_buffer
[i
].buffer
,
782 draw_set_mapped_vertex_buffer(r300
->draw
, i
, buf
);
787 indices
= pipe_buffer_map(pipe
, r300
->index_buffer
.buffer
,
788 PIPE_TRANSFER_READ
, &ib_transfer
);
791 draw_set_mapped_index_buffer(r300
->draw
, indices
);
793 r300
->draw_vbo_locked
= TRUE
;
794 r300
->draw_first_emitted
= FALSE
;
795 draw_vbo(r300
->draw
, info
);
796 draw_flush(r300
->draw
);
797 r300
->draw_vbo_locked
= FALSE
;
799 for (i
= 0; i
< r300
->vertex_buffer_count
; i
++) {
800 if (r300
->vertex_buffer
[i
].buffer
) {
801 pipe_buffer_unmap(pipe
, vb_transfer
[i
]);
802 draw_set_mapped_vertex_buffer(r300
->draw
, i
, NULL
);
807 pipe_buffer_unmap(pipe
, ib_transfer
);
808 draw_set_mapped_index_buffer(r300
->draw
, NULL
);
812 /* Object for rendering using Draw. */
815 struct vbuf_render base
;
818 struct r300_context
* r300
;
820 /* Vertex information */
829 struct pipe_transfer
*vbo_transfer
;
832 static INLINE
struct r300_render
*
833 r300_render(struct vbuf_render
* render
)
835 return (struct r300_render
*)render
;
838 static const struct vertex_info
*
839 r300_render_get_vertex_info(struct vbuf_render
* render
)
841 struct r300_render
* r300render
= r300_render(render
);
842 struct r300_context
* r300
= r300render
->r300
;
844 return &r300
->vertex_info
;
847 static boolean
r300_render_allocate_vertices(struct vbuf_render
* render
,
851 struct r300_render
* r300render
= r300_render(render
);
852 struct r300_context
* r300
= r300render
->r300
;
853 struct pipe_screen
* screen
= r300
->context
.screen
;
854 size_t size
= (size_t)vertex_size
* (size_t)count
;
856 DBG(r300
, DBG_DRAW
, "r300: render_allocate_vertices (size: %d)\n", size
);
858 if (size
+ r300
->draw_vbo_offset
> r300
->draw_vbo_size
)
860 pipe_resource_reference(&r300
->vbo
, NULL
);
861 r300
->vbo
= pipe_buffer_create(screen
,
862 PIPE_BIND_VERTEX_BUFFER
,
863 R300_MAX_DRAW_VBO_SIZE
);
864 r300
->draw_vbo_offset
= 0;
865 r300
->draw_vbo_size
= R300_MAX_DRAW_VBO_SIZE
;
866 r300
->validate_buffers
= TRUE
;
869 r300render
->vertex_size
= vertex_size
;
871 return (r300
->vbo
) ? TRUE
: FALSE
;
874 static void* r300_render_map_vertices(struct vbuf_render
* render
)
876 struct r300_render
* r300render
= r300_render(render
);
877 struct r300_context
* r300
= r300render
->r300
;
879 assert(!r300render
->vbo_transfer
);
881 DBG(r300
, DBG_DRAW
, "r300: render_map_vertices\n");
883 r300render
->vbo_ptr
= pipe_buffer_map(&r300render
->r300
->context
,
886 &r300render
->vbo_transfer
);
888 assert(r300render
->vbo_ptr
);
890 return ((uint8_t*)r300render
->vbo_ptr
+ r300
->draw_vbo_offset
);
893 static void r300_render_unmap_vertices(struct vbuf_render
* render
,
897 struct r300_render
* r300render
= r300_render(render
);
898 struct pipe_context
* context
= &r300render
->r300
->context
;
899 struct r300_context
* r300
= r300render
->r300
;
901 assert(r300render
->vbo_transfer
);
903 DBG(r300
, DBG_DRAW
, "r300: render_unmap_vertices\n");
905 r300render
->vbo_max_used
= MAX2(r300render
->vbo_max_used
,
906 r300render
->vertex_size
* (max
+ 1));
907 pipe_buffer_unmap(context
, r300render
->vbo_transfer
);
909 r300render
->vbo_transfer
= NULL
;
912 static void r300_render_release_vertices(struct vbuf_render
* render
)
914 struct r300_render
* r300render
= r300_render(render
);
915 struct r300_context
* r300
= r300render
->r300
;
917 DBG(r300
, DBG_DRAW
, "r300: render_release_vertices\n");
919 r300
->draw_vbo_offset
+= r300render
->vbo_max_used
;
920 r300render
->vbo_max_used
= 0;
923 static boolean
r300_render_set_primitive(struct vbuf_render
* render
,
926 struct r300_render
* r300render
= r300_render(render
);
928 r300render
->prim
= prim
;
929 r300render
->hwprim
= r300_translate_primitive(prim
);
934 static void r300_render_draw_arrays(struct vbuf_render
* render
,
938 struct r300_render
* r300render
= r300_render(render
);
939 struct r300_context
* r300
= r300render
->r300
;
945 (void) i
; (void) ptr
;
947 DBG(r300
, DBG_DRAW
, "r300: render_draw_arrays (count: %d)\n", count
);
949 if (r300
->draw_first_emitted
) {
950 if (!r300_prepare_for_rendering(r300
,
951 PREP_FIRST_DRAW
| PREP_EMIT_AOS_SWTCL
,
955 if (!r300_emit_states(r300
,
956 PREP_FIRST_DRAW
| PREP_EMIT_AOS_SWTCL
,
961 /* Uncomment to dump all VBOs rendered through this interface.
963 ptr = pipe_buffer_map(&r300render->r300->context,
964 r300render->vbo, PIPE_TRANSFER_READ,
965 &r300render->vbo_transfer);
967 for (i = 0; i < count; i++) {
968 printf("r300: Vertex %d\n", i);
969 draw_dump_emitted_vertex(&r300->vertex_info, ptr);
970 ptr += r300->vertex_info.size * 4;
974 pipe_buffer_unmap(&r300render->r300->context, r300render->vbo,
975 r300render->vbo_transfer);
979 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
980 r300_provoking_vertex_fixes(r300
, r300render
->prim
));
981 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX
, count
- 1);
982 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2
, 0);
983 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST
| (count
<< 16) |
987 r300
->draw_first_emitted
= TRUE
;
990 static void r300_render_draw_elements(struct vbuf_render
* render
,
991 const ushort
* indices
,
994 struct r300_render
* r300render
= r300_render(render
);
995 struct r300_context
* r300
= r300render
->r300
;
997 unsigned end_cs_dwords
;
998 unsigned max_index
= (r300
->draw_vbo_size
- r300
->draw_vbo_offset
) /
999 (r300render
->r300
->vertex_info
.size
* 4) - 1;
1000 unsigned short_count
;
1001 unsigned free_dwords
;
1004 DBG(r300
, DBG_DRAW
, "r300: render_draw_elements (count: %d)\n", count
);
1006 if (r300
->draw_first_emitted
) {
1007 if (!r300_prepare_for_rendering(r300
,
1008 PREP_FIRST_DRAW
| PREP_EMIT_AOS_SWTCL
| PREP_INDEXED
,
1012 if (!r300_emit_states(r300
,
1013 PREP_FIRST_DRAW
| PREP_EMIT_AOS_SWTCL
| PREP_INDEXED
,
1018 /* Below we manage the CS space manually because there may be more
1019 * indices than it can fit in CS. */
1021 end_cs_dwords
= r300_get_num_cs_end_dwords(r300
);
1024 free_dwords
= R300_MAX_CMDBUF_DWORDS
- r300
->cs
->cdw
;
1026 short_count
= MIN2(count
, (free_dwords
- end_cs_dwords
- 6) * 2);
1028 BEGIN_CS(6 + (short_count
+1)/2);
1029 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
1030 r300_provoking_vertex_fixes(r300
, r300render
->prim
));
1031 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX
, max_index
);
1032 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, (short_count
+1)/2);
1033 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (short_count
<< 16) |
1034 r300render
->hwprim
);
1035 for (i
= 0; i
< short_count
-1; i
+= 2) {
1036 OUT_CS(indices
[i
+1] << 16 | indices
[i
]);
1038 if (short_count
% 2) {
1039 OUT_CS(indices
[short_count
-1]);
1043 /* OK now subtract the emitted indices and see if we need to emit
1044 * another draw packet. */
1045 indices
+= short_count
;
1046 count
-= short_count
;
1049 if (!r300_prepare_for_rendering(r300
,
1050 PREP_EMIT_AOS_SWTCL
| PREP_INDEXED
,
1054 end_cs_dwords
= r300_get_num_cs_end_dwords(r300
);
1058 r300
->draw_first_emitted
= TRUE
;
1061 static void r300_render_destroy(struct vbuf_render
* render
)
1066 static struct vbuf_render
* r300_render_create(struct r300_context
* r300
)
1068 struct r300_render
* r300render
= CALLOC_STRUCT(r300_render
);
1070 r300render
->r300
= r300
;
1072 r300render
->base
.max_vertex_buffer_bytes
= 1024 * 1024;
1073 r300render
->base
.max_indices
= 16 * 1024;
1075 r300render
->base
.get_vertex_info
= r300_render_get_vertex_info
;
1076 r300render
->base
.allocate_vertices
= r300_render_allocate_vertices
;
1077 r300render
->base
.map_vertices
= r300_render_map_vertices
;
1078 r300render
->base
.unmap_vertices
= r300_render_unmap_vertices
;
1079 r300render
->base
.set_primitive
= r300_render_set_primitive
;
1080 r300render
->base
.draw_elements
= r300_render_draw_elements
;
1081 r300render
->base
.draw_arrays
= r300_render_draw_arrays
;
1082 r300render
->base
.release_vertices
= r300_render_release_vertices
;
1083 r300render
->base
.destroy
= r300_render_destroy
;
1085 return &r300render
->base
;
1088 struct draw_stage
* r300_draw_stage(struct r300_context
* r300
)
1090 struct vbuf_render
* render
;
1091 struct draw_stage
* stage
;
1093 render
= r300_render_create(r300
);
1099 stage
= draw_vbuf_stage(r300
->draw
, render
);
1102 render
->destroy(render
);
1106 draw_set_render(r300
->draw
, render
);
1111 void r300_draw_flush_vbuf(struct r300_context
*r300
)
1113 pipe_resource_reference(&r300
->vbo
, NULL
);
1114 r300
->draw_vbo_size
= 0;
1117 /****************************************************************************
1118 * End of SW TCL functions *
1119 ***************************************************************************/
1121 /* This functions is used to draw a rectangle for the blitter module.
1123 * If we rendered a quad, the pixels on the main diagonal
1124 * would be computed and stored twice, which makes the clear/copy codepaths
1125 * somewhat inefficient. Instead we use a rectangular point sprite. */
1126 static void r300_blitter_draw_rectangle(struct blitter_context
*blitter
,
1127 unsigned x1
, unsigned y1
,
1128 unsigned x2
, unsigned y2
,
1130 enum blitter_attrib_type type
,
1131 const float attrib
[4])
1133 struct r300_context
*r300
= r300_context(util_blitter_get_pipe(blitter
));
1134 unsigned last_sprite_coord_enable
= r300
->sprite_coord_enable
;
1135 unsigned width
= x2
- x1
;
1136 unsigned height
= y2
- y1
;
1137 unsigned vertex_size
=
1138 type
== UTIL_BLITTER_ATTRIB_COLOR
|| !r300
->draw
? 8 : 4;
1139 unsigned dwords
= 13 + vertex_size
+
1140 (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
? 7 : 0);
1141 const float zeros
[4] = {0, 0, 0, 0};
1144 r300
->context
.set_vertex_buffers(&r300
->context
, 0, NULL
);
1146 if (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
)
1147 r300
->sprite_coord_enable
= 1;
1149 r300_update_derived_state(r300
);
1151 /* Mark some states we don't care about as non-dirty. */
1152 r300
->clip_state
.dirty
= FALSE
;
1153 r300
->viewport_state
.dirty
= FALSE
;
1155 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
);