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 boolean
r500_index_bias_supported(struct r300_context
*r300
)
123 return r300
->screen
->caps
.is_r500
&&
124 r300
->rws
->get_value(r300
->rws
, R300_VID_DRM_2_3_0
);
127 void r500_emit_index_bias(struct r300_context
*r300
, int index_bias
)
132 OUT_CS_REG(R500_VAP_INDEX_OFFSET
,
133 (index_bias
& 0xFFFFFF) | (index_bias
< 0 ? 1<<24 : 0));
137 /* This function splits the index bias value into two parts:
138 * - buffer_offset: the value that can be safely added to buffer offsets
139 * in r300_emit_aos (it must yield a positive offset when added to
140 * a vertex buffer offset)
141 * - index_offset: the value that must be manually subtracted from indices
142 * in an index buffer to achieve negative offsets. */
143 static void r300_split_index_bias(struct r300_context
*r300
, int index_bias
,
144 int *buffer_offset
, int *index_offset
)
146 struct pipe_vertex_buffer
*vb
, *vbufs
= r300
->vertex_buffer
;
147 struct pipe_vertex_element
*velem
= r300
->velems
->velem
;
151 if (index_bias
< 0) {
152 /* See how large index bias we may subtract. We must be careful
153 * here because negative buffer offsets are not allowed
155 max_neg_bias
= INT_MAX
;
156 for (i
= 0; i
< r300
->velems
->count
; i
++) {
157 vb
= &vbufs
[velem
[i
].vertex_buffer_index
];
158 size
= (vb
->buffer_offset
+ velem
[i
].src_offset
) / vb
->stride
;
159 max_neg_bias
= MIN2(max_neg_bias
, size
);
162 /* Now set the minimum allowed value. */
163 *buffer_offset
= MAX2(-max_neg_bias
, index_bias
);
165 /* A positive index bias is OK. */
166 *buffer_offset
= index_bias
;
169 *index_offset
= index_bias
- *buffer_offset
;
172 enum r300_prepare_flags
{
173 PREP_FIRST_DRAW
= (1 << 0), /* call emit_dirty_state and friends? */
174 PREP_VALIDATE_VBOS
= (1 << 1), /* validate VBOs? */
175 PREP_EMIT_AOS
= (1 << 2), /* call emit_aos? */
176 PREP_EMIT_AOS_SWTCL
= (1 << 3), /* call emit_aos_swtcl? */
177 PREP_INDEXED
= (1 << 4) /* is this draw_elements? */
181 * Check if the requested number of dwords is available in the CS and
182 * if not, flush. Then validate buffers and emit dirty state.
183 * \param r300 The context.
184 * \param flags See r300_prepare_flags.
185 * \param index_buffer The index buffer to validate. The parameter may be NULL.
186 * \param cs_dwords The number of dwords to reserve in CS.
187 * \param aos_offset The offset passed to emit_aos.
188 * \param index_bias The index bias to emit.
190 static void r300_prepare_for_rendering(struct r300_context
*r300
,
191 enum r300_prepare_flags flags
,
192 struct pipe_resource
*index_buffer
,
197 boolean flushed
= FALSE
;
198 boolean first_draw
= flags
& PREP_FIRST_DRAW
;
199 boolean emit_aos
= flags
& PREP_EMIT_AOS
;
200 boolean emit_aos_swtcl
= flags
& PREP_EMIT_AOS_SWTCL
;
201 boolean indexed
= flags
& PREP_INDEXED
;
202 boolean hw_index_bias
= r500_index_bias_supported(r300
);
204 /* Add dirty state, index offset, and AOS. */
206 cs_dwords
+= r300_get_num_dirty_dwords(r300
);
209 cs_dwords
+= 2; /* emit_index_offset */
212 cs_dwords
+= 55; /* emit_aos */
215 cs_dwords
+= 7; /* emit_aos_swtcl */
218 cs_dwords
+= r300_get_num_cs_end_dwords(r300
);
220 /* Reserve requested CS space. */
221 if (cs_dwords
> (r300
->cs
->ndw
- r300
->cs
->cdw
)) {
222 r300
->context
.flush(&r300
->context
, 0, NULL
);
226 /* Validate buffers and emit dirty state if needed. */
227 if (first_draw
|| flushed
) {
228 r300_emit_buffer_validate(r300
, flags
& PREP_VALIDATE_VBOS
, index_buffer
);
229 r300_emit_dirty_state(r300
);
231 if (r300
->screen
->caps
.has_tcl
)
232 r500_emit_index_bias(r300
, index_bias
);
234 r500_emit_index_bias(r300
, 0);
238 r300_emit_aos(r300
, aos_offset
, indexed
);
241 r300_emit_aos_swtcl(r300
, indexed
);
245 static boolean
immd_is_good_idea(struct r300_context
*r300
,
248 struct pipe_vertex_element
* velem
;
249 struct pipe_vertex_buffer
* vbuf
;
250 boolean checked
[PIPE_MAX_ATTRIBS
] = {0};
251 unsigned vertex_element_count
= r300
->velems
->count
;
254 if (DBG_ON(r300
, DBG_NO_IMMD
)) {
262 if (count
* r300
->velems
->vertex_size_dwords
> IMMD_DWORDS
) {
266 /* We shouldn't map buffers referenced by CS, busy buffers,
267 * and ones placed in VRAM. */
268 for (i
= 0; i
< vertex_element_count
; i
++) {
269 velem
= &r300
->velems
->velem
[i
];
270 vbi
= velem
->vertex_buffer_index
;
273 vbuf
= &r300
->vertex_buffer
[vbi
];
275 if (!(r300_buffer(vbuf
->buffer
)->domain
& R300_DOMAIN_GTT
)) {
279 if (r300_buffer_is_referenced(&r300
->context
,
281 R300_REF_CS
| R300_REF_HW
)) {
282 /* It's a very bad idea to map it... */
291 /*****************************************************************************
292 * The HWTCL draw functions. *
293 ****************************************************************************/
295 static void r300_emit_draw_arrays_immediate(struct r300_context
*r300
,
300 struct pipe_vertex_element
* velem
;
301 struct pipe_vertex_buffer
* vbuf
;
302 unsigned vertex_element_count
= r300
->velems
->count
;
303 unsigned i
, v
, vbi
, dwords
;
305 /* Size of the vertex, in dwords. */
306 unsigned vertex_size
= r300
->velems
->vertex_size_dwords
;
308 /* Size of the vertex element, in dwords. */
309 unsigned size
[PIPE_MAX_ATTRIBS
];
311 /* Stride to the same attrib in the next vertex in the vertex buffer,
313 unsigned stride
[PIPE_MAX_ATTRIBS
];
315 /* Mapped vertex buffers. */
316 uint32_t* map
[PIPE_MAX_ATTRIBS
];
317 uint32_t* mapelem
[PIPE_MAX_ATTRIBS
];
318 struct pipe_transfer
* transfer
[PIPE_MAX_ATTRIBS
] = {0};
322 /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
323 for (i
= 0; i
< vertex_element_count
; i
++) {
324 velem
= &r300
->velems
->velem
[i
];
325 size
[i
] = r300
->velems
->hw_format_size
[i
] / 4;
326 vbi
= velem
->vertex_buffer_index
;
327 vbuf
= &r300
->vertex_buffer
[vbi
];
328 stride
[i
] = vbuf
->stride
/ 4;
330 /* Map the buffer. */
331 if (!transfer
[vbi
]) {
332 map
[vbi
] = (uint32_t*)pipe_buffer_map(&r300
->context
,
336 map
[vbi
] += (vbuf
->buffer_offset
/ 4) + stride
[i
] * start
;
338 mapelem
[i
] = map
[vbi
] + (velem
->src_offset
/ 4);
341 dwords
= 9 + count
* vertex_size
;
343 r300_prepare_for_rendering(r300
, PREP_FIRST_DRAW
, NULL
, dwords
, 0, 0);
346 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
347 r300_provoking_vertex_fixes(r300
, mode
));
348 OUT_CS_REG(R300_VAP_VTX_SIZE
, vertex_size
);
349 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
352 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2
, count
* vertex_size
);
353 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED
| (count
<< 16) |
354 r300_translate_primitive(mode
));
357 for (v
= 0; v
< count
; v
++) {
358 for (i
= 0; i
< vertex_element_count
; i
++) {
359 OUT_CS_TABLE(&mapelem
[i
][stride
[i
] * v
], size
[i
]);
365 for (i
= 0; i
< vertex_element_count
; i
++) {
366 vbi
= r300
->velems
->velem
[i
].vertex_buffer_index
;
369 vbuf
= &r300
->vertex_buffer
[vbi
];
370 pipe_buffer_unmap(&r300
->context
, vbuf
->buffer
, transfer
[vbi
]);
371 transfer
[vbi
] = NULL
;
376 static void r300_emit_draw_arrays(struct r300_context
*r300
,
380 boolean alt_num_verts
= count
> 65535;
383 if (count
>= (1 << 24)) {
384 fprintf(stderr
, "r300: Got a huge number of vertices: %i, "
385 "refusing to render.\n", count
);
389 BEGIN_CS(7 + (alt_num_verts
? 2 : 0));
391 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES
, count
);
393 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
394 r300_provoking_vertex_fixes(r300
, mode
));
395 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
398 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2
, 0);
399 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST
| (count
<< 16) |
400 r300_translate_primitive(mode
) |
401 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
405 static void r300_emit_draw_elements(struct r300_context
*r300
,
406 struct pipe_resource
* indexBuffer
,
414 uint32_t count_dwords
;
415 uint32_t offset_dwords
= indexSize
* start
/ sizeof(uint32_t);
416 boolean alt_num_verts
= count
> 65535;
419 if (count
>= (1 << 24)) {
420 fprintf(stderr
, "r300: Got a huge number of vertices: %i, "
421 "refusing to render.\n", count
);
425 maxIndex
= MIN2(maxIndex
, r300
->vertex_buffer_max_index
);
427 DBG(r300
, DBG_DRAW
, "r300: Indexbuf of %u indices, min %u max %u\n",
428 count
, minIndex
, maxIndex
);
430 BEGIN_CS(13 + (alt_num_verts
? 2 : 0));
432 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES
, count
);
434 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
435 r300_provoking_vertex_fixes(r300
, mode
));
436 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
439 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, 0);
440 if (indexSize
== 4) {
441 count_dwords
= count
;
442 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
443 R300_VAP_VF_CNTL__INDEX_SIZE_32bit
|
444 r300_translate_primitive(mode
) |
445 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
447 count_dwords
= (count
+ 1) / 2;
448 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
449 r300_translate_primitive(mode
) |
450 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
453 /* INDX_BUFFER is a truly special packet3.
454 * Unlike most other packet3, where the offset is after the count,
455 * the order is reversed, so the relocation ends up carrying the
456 * size of the indexbuf instead of the offset.
458 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER
, 2);
459 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR
| (R300_VAP_PORT_IDX0
>> 2) |
460 (0 << R300_INDX_BUFFER_SKIP_SHIFT
));
461 OUT_CS(offset_dwords
<< 2);
462 OUT_CS_BUF_RELOC(indexBuffer
, count_dwords
,
463 r300_buffer(indexBuffer
)->domain
, 0);
468 /* This is the fast-path drawing & emission for HW TCL. */
469 static void r300_draw_range_elements(struct pipe_context
* pipe
,
470 struct pipe_resource
* indexBuffer
,
479 struct r300_context
* r300
= r300_context(pipe
);
480 struct pipe_resource
* orgIndexBuffer
= indexBuffer
;
481 boolean alt_num_verts
= r300
->screen
->caps
.is_r500
&&
483 r300
->rws
->get_value(r300
->rws
, R300_VID_DRM_2_3_0
);
484 unsigned short_count
;
485 int buffer_offset
= 0, index_offset
= 0; /* for index bias emulation */
486 boolean translate
= FALSE
;
489 if (r300
->skip_rendering
) {
493 if (!u_trim_pipe_prim(mode
, &count
)) {
497 /* Index buffer range checking. */
498 if ((start
+ count
) * indexSize
> indexBuffer
->width0
) {
499 fprintf(stderr
, "r300: Invalid index buffer range. Skipping rendering.\n");
503 /* Set up fallback for incompatible vertex layout if needed. */
504 if (r300
->incompatible_vb_layout
|| r300
->velems
->incompatible_layout
) {
505 r300_begin_vertex_translate(r300
);
509 if (indexBias
&& !r500_index_bias_supported(r300
)) {
510 r300_split_index_bias(r300
, indexBias
, &buffer_offset
, &index_offset
);
513 r300_translate_index_buffer(r300
, &indexBuffer
, &indexSize
, index_offset
,
516 r300_update_derived_state(r300
);
517 r300_upload_index_buffer(r300
, &indexBuffer
, indexSize
, start
, count
, &new_offset
);
520 /* 15 dwords for emit_draw_elements */
521 r300_prepare_for_rendering(r300
,
522 PREP_FIRST_DRAW
| PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
| PREP_INDEXED
,
523 indexBuffer
, 15, buffer_offset
, indexBias
);
525 if (alt_num_verts
|| count
<= 65535) {
526 r300_emit_draw_elements(r300
, indexBuffer
, indexSize
,
527 minIndex
, maxIndex
, mode
, start
, count
);
530 short_count
= MIN2(count
, 65534);
531 r300_emit_draw_elements(r300
, indexBuffer
, indexSize
,
533 mode
, start
, short_count
);
535 start
+= short_count
;
536 count
-= short_count
;
538 /* 15 dwords for emit_draw_elements */
540 r300_prepare_for_rendering(r300
,
541 PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
| PREP_INDEXED
,
542 indexBuffer
, 15, buffer_offset
, indexBias
);
547 if (indexBuffer
!= orgIndexBuffer
) {
548 pipe_resource_reference( &indexBuffer
, NULL
);
552 r300_end_vertex_translate(r300
);
556 static void r300_draw_arrays(struct pipe_context
* pipe
, unsigned mode
,
557 unsigned start
, unsigned count
)
559 struct r300_context
* r300
= r300_context(pipe
);
560 boolean alt_num_verts
= r300
->screen
->caps
.is_r500
&&
562 r300
->rws
->get_value(r300
->rws
, R300_VID_DRM_2_3_0
);
563 unsigned short_count
;
564 boolean translate
= FALSE
;
566 if (r300
->skip_rendering
) {
570 if (!u_trim_pipe_prim(mode
, &count
)) {
574 /* Set up fallback for incompatible vertex layout if needed. */
575 if (r300
->incompatible_vb_layout
|| r300
->velems
->incompatible_layout
) {
576 r300_begin_vertex_translate(r300
);
580 r300_update_derived_state(r300
);
582 if (immd_is_good_idea(r300
, count
)) {
583 r300_emit_draw_arrays_immediate(r300
, mode
, start
, count
);
585 /* 9 spare dwords for emit_draw_arrays. */
586 r300_prepare_for_rendering(r300
, PREP_FIRST_DRAW
| PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
,
589 if (alt_num_verts
|| count
<= 65535) {
590 r300_emit_draw_arrays(r300
, mode
, count
);
593 short_count
= MIN2(count
, 65535);
594 r300_emit_draw_arrays(r300
, mode
, short_count
);
596 start
+= short_count
;
597 count
-= short_count
;
599 /* 9 spare dwords for emit_draw_arrays. */
601 r300_prepare_for_rendering(r300
,
602 PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
, NULL
, 9,
607 u_upload_flush(r300
->upload_vb
);
611 r300_end_vertex_translate(r300
);
615 static void r300_draw_vbo(struct pipe_context
* pipe
,
616 const struct pipe_draw_info
*info
)
618 struct r300_context
* r300
= r300_context(pipe
);
620 if (!r300
->velems
->count
|| !r300
->vertex_buffer_count
)
623 if (info
->indexed
&& r300
->index_buffer
.buffer
) {
626 assert(r300
->index_buffer
.offset
% r300
->index_buffer
.index_size
== 0);
627 offset
= r300
->index_buffer
.offset
/ r300
->index_buffer
.index_size
;
629 r300_draw_range_elements(pipe
,
630 r300
->index_buffer
.buffer
,
631 r300
->index_buffer
.index_size
,
636 info
->start
+ offset
,
640 r300_draw_arrays(pipe
,
647 /****************************************************************************
648 * The rest of this file is for SW TCL rendering only. Please be polite and *
649 * keep these functions separated so that they are easier to locate. ~C. *
650 ***************************************************************************/
652 /* SW TCL elements, using Draw. */
653 static void r300_swtcl_draw_vbo(struct pipe_context
* pipe
,
654 const struct pipe_draw_info
*info
)
656 struct r300_context
* r300
= r300_context(pipe
);
657 struct pipe_transfer
*vb_transfer
[PIPE_MAX_ATTRIBS
];
658 struct pipe_transfer
*ib_transfer
= NULL
;
659 unsigned count
= info
->count
;
661 void* indices
= NULL
;
663 if (r300
->skip_rendering
) {
667 if (!u_trim_pipe_prim(info
->mode
, &count
)) {
671 r300_update_derived_state(r300
);
673 for (i
= 0; i
< r300
->vertex_buffer_count
; i
++) {
674 if (r300
->vertex_buffer
[i
].buffer
) {
675 void *buf
= pipe_buffer_map(pipe
,
676 r300
->vertex_buffer
[i
].buffer
,
679 draw_set_mapped_vertex_buffer(r300
->draw
, i
, buf
);
683 if (info
->indexed
&& r300
->index_buffer
.buffer
) {
684 indices
= pipe_buffer_map(pipe
, r300
->index_buffer
.buffer
,
685 PIPE_TRANSFER_READ
, &ib_transfer
);
688 draw_set_mapped_index_buffer(r300
->draw
, indices
);
690 draw_vbo(r300
->draw
, info
);
692 /* XXX Not sure whether this is the best fix.
693 * It prevents CS from being rejected and weird assertion failures. */
694 draw_flush(r300
->draw
);
696 for (i
= 0; i
< r300
->vertex_buffer_count
; i
++) {
697 if (r300
->vertex_buffer
[i
].buffer
) {
698 pipe_buffer_unmap(pipe
, r300
->vertex_buffer
[i
].buffer
,
700 draw_set_mapped_vertex_buffer(r300
->draw
, i
, NULL
);
705 pipe_buffer_unmap(pipe
, r300
->index_buffer
.buffer
, ib_transfer
);
706 draw_set_mapped_index_buffer(r300
->draw
, NULL
);
710 /* Object for rendering using Draw. */
713 struct vbuf_render base
;
716 struct r300_context
* r300
;
718 /* Vertex information */
728 struct pipe_transfer
*vbo_transfer
;
731 static INLINE
struct r300_render
*
732 r300_render(struct vbuf_render
* render
)
734 return (struct r300_render
*)render
;
737 static const struct vertex_info
*
738 r300_render_get_vertex_info(struct vbuf_render
* render
)
740 struct r300_render
* r300render
= r300_render(render
);
741 struct r300_context
* r300
= r300render
->r300
;
743 return &r300
->vertex_info
;
746 static boolean
r300_render_allocate_vertices(struct vbuf_render
* render
,
750 struct r300_render
* r300render
= r300_render(render
);
751 struct r300_context
* r300
= r300render
->r300
;
752 struct pipe_screen
* screen
= r300
->context
.screen
;
753 size_t size
= (size_t)vertex_size
* (size_t)count
;
755 if (size
+ r300render
->vbo_offset
> r300
->draw_vbo_size
)
757 pipe_resource_reference(&r300
->vbo
, NULL
);
758 r300
->vbo
= pipe_buffer_create(screen
,
759 PIPE_BIND_VERTEX_BUFFER
,
760 R300_MAX_DRAW_VBO_SIZE
);
761 r300render
->vbo_offset
= 0;
762 r300
->draw_vbo_size
= R300_MAX_DRAW_VBO_SIZE
;
765 r300render
->vertex_size
= vertex_size
;
766 r300
->vbo_offset
= r300render
->vbo_offset
;
768 return (r300
->vbo
) ? TRUE
: FALSE
;
771 static void* r300_render_map_vertices(struct vbuf_render
* render
)
773 struct r300_render
* r300render
= r300_render(render
);
774 struct r300_context
* r300
= r300render
->r300
;
776 assert(!r300render
->vbo_transfer
);
778 r300render
->vbo_ptr
= pipe_buffer_map(&r300render
->r300
->context
,
781 &r300render
->vbo_transfer
);
783 assert(r300render
->vbo_ptr
);
785 return ((uint8_t*)r300render
->vbo_ptr
+ r300render
->vbo_offset
);
788 static void r300_render_unmap_vertices(struct vbuf_render
* render
,
792 struct r300_render
* r300render
= r300_render(render
);
793 struct pipe_context
* context
= &r300render
->r300
->context
;
794 struct r300_context
* r300
= r300render
->r300
;
796 assert(r300render
->vbo_transfer
);
798 r300render
->vbo_max_used
= MAX2(r300render
->vbo_max_used
,
799 r300render
->vertex_size
* (max
+ 1));
800 pipe_buffer_unmap(context
, r300
->vbo
, r300render
->vbo_transfer
);
802 r300render
->vbo_transfer
= NULL
;
805 static void r300_render_release_vertices(struct vbuf_render
* render
)
807 struct r300_render
* r300render
= r300_render(render
);
809 r300render
->vbo_offset
+= r300render
->vbo_max_used
;
810 r300render
->vbo_max_used
= 0;
813 static boolean
r300_render_set_primitive(struct vbuf_render
* render
,
816 struct r300_render
* r300render
= r300_render(render
);
818 r300render
->prim
= prim
;
819 r300render
->hwprim
= r300_translate_primitive(prim
);
824 static void r300_render_draw_arrays(struct vbuf_render
* render
,
828 struct r300_render
* r300render
= r300_render(render
);
829 struct r300_context
* r300
= r300render
->r300
;
835 (void) i
; (void) ptr
;
837 r300_prepare_for_rendering(r300
, PREP_FIRST_DRAW
| PREP_EMIT_AOS_SWTCL
,
840 DBG(r300
, DBG_DRAW
, "r300: render_draw_arrays (count: %d)\n", count
);
842 /* Uncomment to dump all VBOs rendered through this interface.
844 ptr = pipe_buffer_map(&r300render->r300->context,
845 r300render->vbo, PIPE_TRANSFER_READ,
846 &r300render->vbo_transfer);
848 for (i = 0; i < count; i++) {
849 printf("r300: Vertex %d\n", i);
850 draw_dump_emitted_vertex(&r300->vertex_info, ptr);
851 ptr += r300->vertex_info.size * 4;
855 pipe_buffer_unmap(&r300render->r300->context, r300render->vbo,
856 r300render->vbo_transfer);
860 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
861 r300_provoking_vertex_fixes(r300
, r300render
->prim
));
862 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX
, count
- 1);
863 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2
, 0);
864 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST
| (count
<< 16) |
869 static void r300_render_draw_elements(struct vbuf_render
* render
,
870 const ushort
* indices
,
873 struct r300_render
* r300render
= r300_render(render
);
874 struct r300_context
* r300
= r300render
->r300
;
876 unsigned end_cs_dwords
;
877 unsigned max_index
= (r300
->draw_vbo_size
- r300render
->vbo_offset
) /
878 (r300render
->r300
->vertex_info
.size
* 4) - 1;
879 unsigned short_count
;
880 unsigned free_dwords
;
883 DBG(r300
, DBG_DRAW
, "r300: render_draw_elements (count: %d)\n", count
);
885 /* Reserve at least 256 dwords.
887 * Below we manage the CS space manually because there may be more
888 * indices than it can fit in CS. */
889 r300_prepare_for_rendering(r300
,
890 PREP_FIRST_DRAW
| PREP_EMIT_AOS_SWTCL
| PREP_INDEXED
,
892 end_cs_dwords
= r300_get_num_cs_end_dwords(r300
);
895 free_dwords
= r300
->cs
->ndw
- r300
->cs
->cdw
;
897 short_count
= MIN2(count
, (free_dwords
- end_cs_dwords
- 6) * 2);
899 BEGIN_CS(6 + (short_count
+1)/2);
900 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
901 r300_provoking_vertex_fixes(r300
, r300render
->prim
));
902 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX
, max_index
);
903 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, (short_count
+1)/2);
904 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (short_count
<< 16) |
906 for (i
= 0; i
< short_count
-1; i
+= 2) {
907 OUT_CS(indices
[i
+1] << 16 | indices
[i
]);
909 if (short_count
% 2) {
910 OUT_CS(indices
[short_count
-1]);
914 /* OK now subtract the emitted indices and see if we need to emit
915 * another draw packet. */
916 indices
+= short_count
;
917 count
-= short_count
;
920 r300_prepare_for_rendering(r300
,
921 PREP_EMIT_AOS_SWTCL
| PREP_INDEXED
,
923 end_cs_dwords
= r300_get_num_cs_end_dwords(r300
);
928 static void r300_render_destroy(struct vbuf_render
* render
)
933 static struct vbuf_render
* r300_render_create(struct r300_context
* r300
)
935 struct r300_render
* r300render
= CALLOC_STRUCT(r300_render
);
937 r300render
->r300
= r300
;
939 /* XXX find real numbers plz */
940 r300render
->base
.max_vertex_buffer_bytes
= 128 * 1024;
941 r300render
->base
.max_indices
= 16 * 1024;
943 r300render
->base
.get_vertex_info
= r300_render_get_vertex_info
;
944 r300render
->base
.allocate_vertices
= r300_render_allocate_vertices
;
945 r300render
->base
.map_vertices
= r300_render_map_vertices
;
946 r300render
->base
.unmap_vertices
= r300_render_unmap_vertices
;
947 r300render
->base
.set_primitive
= r300_render_set_primitive
;
948 r300render
->base
.draw_elements
= r300_render_draw_elements
;
949 r300render
->base
.draw_arrays
= r300_render_draw_arrays
;
950 r300render
->base
.release_vertices
= r300_render_release_vertices
;
951 r300render
->base
.destroy
= r300_render_destroy
;
953 r300render
->vbo_offset
= 0;
955 return &r300render
->base
;
958 struct draw_stage
* r300_draw_stage(struct r300_context
* r300
)
960 struct vbuf_render
* render
;
961 struct draw_stage
* stage
;
963 render
= r300_render_create(r300
);
969 stage
= draw_vbuf_stage(r300
->draw
, render
);
972 render
->destroy(render
);
976 draw_set_render(r300
->draw
, render
);
981 void r300_draw_flush_vbuf(struct r300_context
*r300
)
983 pipe_resource_reference(&r300
->vbo
, NULL
);
984 r300
->draw_vbo_size
= 0;
987 /****************************************************************************
988 * End of SW TCL functions *
989 ***************************************************************************/
991 /* If we used a quad to draw a rectangle, the pixels on the main diagonal
992 * would be computed and stored twice, which makes the clear/copy codepaths
993 * somewhat inefficient. Instead we use a rectangular point sprite. */
994 static void r300_blitter_draw_rectangle(struct blitter_context
*blitter
,
995 unsigned x1
, unsigned y1
,
996 unsigned x2
, unsigned y2
,
998 enum blitter_attrib_type type
,
999 const float attrib
[4])
1001 struct r300_context
*r300
= r300_context(util_blitter_get_pipe(blitter
));
1002 unsigned last_sprite_coord_enable
= r300
->sprite_coord_enable
;
1003 unsigned width
= x2
- x1
;
1004 unsigned height
= y2
- y1
;
1005 unsigned vertex_size
=
1006 type
== UTIL_BLITTER_ATTRIB_COLOR
|| !r300
->draw
? 8 : 4;
1007 unsigned dwords
= 13 + vertex_size
+
1008 (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
? 7 : 0);
1009 const float zeros
[4] = {0, 0, 0, 0};
1012 if (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
)
1013 r300
->sprite_coord_enable
= 1;
1015 r300_update_derived_state(r300
);
1017 /* Mark some states we don't care about as non-dirty. */
1018 r300
->clip_state
.dirty
= FALSE
;
1019 r300
->viewport_state
.dirty
= FALSE
;
1021 r300_prepare_for_rendering(r300
, PREP_FIRST_DRAW
, NULL
, dwords
, 0, 0);
1023 DBG(r300
, DBG_DRAW
, "r300: draw_rectangle\n");
1027 OUT_CS_REG(R300_GA_POINT_SIZE
, (height
* 6) | ((width
* 6) << 16));
1029 if (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
) {
1030 /* Set up the GA to generate texcoords. */
1031 OUT_CS_REG(R300_GB_ENABLE
, R300_GB_POINT_STUFF_ENABLE
|
1032 (R300_GB_TEX_STR
<< R300_GB_TEX0_SOURCE_SHIFT
));
1033 OUT_CS_REG_SEQ(R300_GA_POINT_S0
, 4);
1034 OUT_CS_32F(attrib
[0]);
1035 OUT_CS_32F(attrib
[3]);
1036 OUT_CS_32F(attrib
[2]);
1037 OUT_CS_32F(attrib
[1]);
1040 /* Set up VAP controls. */
1041 OUT_CS_REG(R300_VAP_CLIP_CNTL
, R300_CLIP_DISABLE
);
1042 OUT_CS_REG(R300_VAP_VTE_CNTL
, R300_VTX_XY_FMT
| R300_VTX_Z_FMT
);
1043 OUT_CS_REG(R300_VAP_VTX_SIZE
, vertex_size
);
1044 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
1049 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2
, vertex_size
);
1050 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED
| (1 << 16) |
1051 R300_VAP_VF_CNTL__PRIM_POINTS
);
1053 OUT_CS_32F(x1
+ width
* 0.5f
);
1054 OUT_CS_32F(y1
+ height
* 0.5f
);
1058 if (vertex_size
== 8) {
1061 OUT_CS_TABLE(attrib
, 4);
1065 /* Restore the state. */
1066 r300
->clip_state
.dirty
= TRUE
;
1067 r300
->rs_state
.dirty
= TRUE
;
1068 r300
->viewport_state
.dirty
= TRUE
;
1070 r300
->sprite_coord_enable
= last_sprite_coord_enable
;
1073 static void r300_resource_resolve(struct pipe_context
* pipe
,
1074 struct pipe_resource
* dest
,
1075 struct pipe_subresource subdest
,
1076 struct pipe_resource
* src
,
1077 struct pipe_subresource subsrc
)
1079 struct r300_context
* r300
= r300_context(pipe
);
1080 struct r300_aa_state
*aa
= (struct r300_aa_state
*)r300
->aa_state
.state
;
1081 struct pipe_surface
* srcsurf
= src
->screen
->get_tex_surface(src
->screen
,
1082 src
, subsrc
.face
, subsrc
.level
, 0, 0);
1083 float color
[] = {0, 0, 0, 0};
1085 DBG(r300
, DBG_DRAW
, "r300: Resolving resource...\n");
1087 /* Enable AA resolve. */
1088 aa
->dest
= r300_surface(
1089 dest
->screen
->get_tex_surface(dest
->screen
, dest
, subdest
.face
,
1090 subdest
.level
, 0, 0));
1093 R300_RB3D_AARESOLVE_CTL_AARESOLVE_MODE_RESOLVE
|
1094 R300_RB3D_AARESOLVE_CTL_AARESOLVE_ALPHA_AVERAGE
;
1095 r300
->aa_state
.size
= 12;
1096 r300
->aa_state
.dirty
= TRUE
;
1098 /* Resolve the surface. */
1099 r300
->context
.clear_render_target(pipe
,
1100 srcsurf
, color
, 0, 0, src
->width0
, src
->height0
);
1102 /* Disable AA resolve. */
1103 aa
->aaresolve_ctl
= 0;
1104 r300
->aa_state
.size
= 4;
1105 r300
->aa_state
.dirty
= TRUE
;
1107 pipe_surface_reference((struct pipe_surface
**)&srcsurf
, NULL
);
1108 pipe_surface_reference((struct pipe_surface
**)&aa
->dest
, NULL
);
1111 void r300_init_render_functions(struct r300_context
*r300
)
1113 /* Set draw functions based on presence of HW TCL. */
1114 if (r300
->screen
->caps
.has_tcl
) {
1115 r300
->context
.draw_vbo
= r300_draw_vbo
;
1117 r300
->context
.draw_vbo
= r300_swtcl_draw_vbo
;
1120 r300
->context
.resource_resolve
= r300_resource_resolve
;
1121 r300
->blitter
->draw_rectangle
= r300_blitter_draw_rectangle
;
1123 /* Plug in the two-sided stencil reference value fallback if needed. */
1124 if (!r300
->screen
->caps
.is_r500
)
1125 r300_plug_in_stencil_ref_fallback(r300
);