2 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23 /* r300_render: Vertex and index buffer primitive emission. Contains both
24 * HW TCL fastpath rendering, and SW TCL Draw-assisted rendering. */
26 #include "draw/draw_context.h"
27 #include "draw/draw_vbuf.h"
29 #include "util/u_inlines.h"
31 #include "util/u_format.h"
32 #include "util/u_memory.h"
33 #include "util/u_upload_mgr.h"
34 #include "util/u_prim.h"
37 #include "r300_context.h"
38 #include "r300_screen_buffer.h"
39 #include "r300_emit.h"
41 #include "r300_render.h"
42 #include "r300_state_derived.h"
44 static uint32_t r300_translate_primitive(unsigned prim
)
47 case PIPE_PRIM_POINTS
:
48 return R300_VAP_VF_CNTL__PRIM_POINTS
;
50 return R300_VAP_VF_CNTL__PRIM_LINES
;
51 case PIPE_PRIM_LINE_LOOP
:
52 return R300_VAP_VF_CNTL__PRIM_LINE_LOOP
;
53 case PIPE_PRIM_LINE_STRIP
:
54 return R300_VAP_VF_CNTL__PRIM_LINE_STRIP
;
55 case PIPE_PRIM_TRIANGLES
:
56 return R300_VAP_VF_CNTL__PRIM_TRIANGLES
;
57 case PIPE_PRIM_TRIANGLE_STRIP
:
58 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP
;
59 case PIPE_PRIM_TRIANGLE_FAN
:
60 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN
;
62 return R300_VAP_VF_CNTL__PRIM_QUADS
;
63 case PIPE_PRIM_QUAD_STRIP
:
64 return R300_VAP_VF_CNTL__PRIM_QUAD_STRIP
;
65 case PIPE_PRIM_POLYGON
:
66 return R300_VAP_VF_CNTL__PRIM_POLYGON
;
72 static uint32_t r300_provoking_vertex_fixes(struct r300_context
*r300
,
75 struct r300_rs_state
* rs
= (struct r300_rs_state
*)r300
->rs_state
.state
;
76 uint32_t color_control
= rs
->color_control
;
78 /* By default (see r300_state.c:r300_create_rs_state) color_control is
79 * initialized to provoking the first vertex.
81 * Triangle fans must be reduced to the second vertex, not the first, in
82 * Gallium flatshade-first mode, as per the GL spec.
83 * (http://www.opengl.org/registry/specs/ARB/provoking_vertex.txt)
85 * Quads never provoke correctly in flatshade-first mode. The first
86 * vertex is never considered as provoking, so only the second, third,
87 * and fourth vertices can be selected, and both "third" and "last" modes
88 * select the fourth vertex. This is probably due to D3D lacking quads.
90 * Similarly, polygons reduce to the first, not the last, vertex, when in
91 * "last" mode, and all other modes start from the second vertex.
96 if (rs
->rs
.flatshade_first
) {
98 case PIPE_PRIM_TRIANGLE_FAN
:
99 color_control
|= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND
;
101 case PIPE_PRIM_QUADS
:
102 case PIPE_PRIM_QUAD_STRIP
:
103 case PIPE_PRIM_POLYGON
:
104 color_control
|= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST
;
107 color_control
|= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST
;
111 color_control
|= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST
;
114 return color_control
;
117 static void r500_emit_index_offset(struct r300_context
*r300
, int index_bias
)
121 if (r300
->screen
->caps
.is_r500
&&
122 r300
->rws
->get_value(r300
->rws
, R300_VID_DRM_2_3_0
)) {
124 OUT_CS_REG(R500_VAP_INDEX_OFFSET
,
125 (index_bias
& 0xFFFFFF) | (index_bias
< 0 ? 1<<24 : 0));
129 fprintf(stderr
, "r300: Non-zero index bias is unsupported "
130 "on this hardware.\n");
136 enum r300_prepare_flags
{
137 PREP_FIRST_DRAW
= (1 << 0),
138 PREP_VALIDATE_VBOS
= (1 << 1),
139 PREP_EMIT_AOS
= (1 << 2),
140 PREP_INDEXED
= (1 << 3)
143 /* Check if the requested number of dwords is available in the CS and
144 * if not, flush. Then validate buffers and emit dirty state.
145 * Return TRUE if flush occured. */
146 static void r300_prepare_for_rendering(struct r300_context
*r300
,
147 enum r300_prepare_flags flags
,
148 struct pipe_resource
*index_buffer
,
153 boolean flushed
= FALSE
;
154 boolean first_draw
= flags
& PREP_FIRST_DRAW
;
155 boolean emit_aos
= flags
& PREP_EMIT_AOS
;
157 /* Stencil ref fallback. */
158 if (r300
->stencil_ref_bf_fallback
) {
159 cs_dwords
= cs_dwords
* 2 + 10;
162 /* Add dirty state, index offset, and AOS. */
164 cs_dwords
+= r300_get_num_dirty_dwords(r300
);
166 if (r300
->screen
->caps
.is_r500
)
167 cs_dwords
+= 2; /* emit_index_offset */
170 cs_dwords
+= 55; /* emit_aos */
173 /* Emitted in flush. */
174 cs_dwords
+= 26; /* emit_query_end */
176 /* Reserve requested CS space. */
177 if (!r300
->rws
->check_cs(r300
->rws
, cs_dwords
)) {
178 r300
->context
.flush(&r300
->context
, 0, NULL
);
182 /* Validate buffers and emit dirty state if needed. */
183 if (first_draw
|| flushed
) {
184 r300_emit_buffer_validate(r300
, flags
& PREP_VALIDATE_VBOS
, index_buffer
);
185 r300_emit_dirty_state(r300
);
186 r500_emit_index_offset(r300
, index_bias
);
188 r300_emit_aos(r300
, aos_offset
, flags
& PREP_INDEXED
);
192 static boolean
immd_is_good_idea(struct r300_context
*r300
,
195 struct pipe_vertex_element
* velem
;
196 struct pipe_vertex_buffer
* vbuf
;
197 boolean checked
[PIPE_MAX_ATTRIBS
] = {0};
198 unsigned vertex_element_count
= r300
->velems
->count
;
201 if (DBG_ON(r300
, DBG_NO_IMMD
)) {
213 /* We shouldn't map buffers referenced by CS, busy buffers,
214 * and ones placed in VRAM. */
215 /* XXX Check for VRAM buffers. */
216 for (i
= 0; i
< vertex_element_count
; i
++) {
217 velem
= &r300
->velems
->velem
[i
];
218 vbi
= velem
->vertex_buffer_index
;
221 vbuf
= &r300
->vertex_buffer
[vbi
];
223 if (r300_buffer_is_referenced(&r300
->context
,
225 R300_REF_CS
| R300_REF_HW
)) {
226 /* It's a very bad idea to map it... */
235 /*****************************************************************************
236 * The emission of draw packets for r500. Older GPUs may use these functions *
237 * after resolving fallback issues (e.g. stencil ref two-sided). *
238 ****************************************************************************/
240 void r500_emit_draw_arrays_immediate(struct r300_context
*r300
,
245 struct pipe_vertex_element
* velem
;
246 struct pipe_vertex_buffer
* vbuf
;
247 unsigned vertex_element_count
= r300
->velems
->count
;
248 unsigned i
, v
, vbi
, dw
, elem_offset
, dwords
;
250 /* Size of the vertex, in dwords. */
251 unsigned vertex_size
= 0;
253 /* Offsets of the attribute, in dwords, from the start of the vertex. */
254 unsigned offset
[PIPE_MAX_ATTRIBS
];
256 /* Size of the vertex element, in dwords. */
257 unsigned size
[PIPE_MAX_ATTRIBS
];
259 /* Stride to the same attrib in the next vertex in the vertex buffer,
261 unsigned stride
[PIPE_MAX_ATTRIBS
] = {0};
263 /* Mapped vertex buffers. */
264 uint32_t* map
[PIPE_MAX_ATTRIBS
] = {0};
265 struct pipe_transfer
* transfer
[PIPE_MAX_ATTRIBS
] = {NULL
};
269 /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
270 for (i
= 0; i
< vertex_element_count
; i
++) {
271 velem
= &r300
->velems
->velem
[i
];
272 offset
[i
] = velem
->src_offset
/ 4;
273 size
[i
] = util_format_get_blocksize(velem
->src_format
) / 4;
274 vertex_size
+= size
[i
];
275 vbi
= velem
->vertex_buffer_index
;
277 /* Map the buffer. */
279 vbuf
= &r300
->vertex_buffer
[vbi
];
280 map
[vbi
] = (uint32_t*)pipe_buffer_map(&r300
->context
,
284 map
[vbi
] += vbuf
->buffer_offset
/ 4;
285 stride
[vbi
] = vbuf
->stride
/ 4;
289 dwords
= 9 + count
* vertex_size
;
291 r300_prepare_for_rendering(r300
, PREP_FIRST_DRAW
, NULL
, dwords
, 0, 0);
294 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
295 r300_provoking_vertex_fixes(r300
, mode
));
296 OUT_CS_REG(R300_VAP_VTX_SIZE
, vertex_size
);
297 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
300 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2
, count
* vertex_size
);
301 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED
| (count
<< 16) |
302 r300_translate_primitive(mode
));
305 for (v
= 0; v
< count
; v
++) {
306 for (i
= 0; i
< vertex_element_count
; i
++) {
307 velem
= &r300
->velems
->velem
[i
];
308 vbi
= velem
->vertex_buffer_index
;
309 elem_offset
= offset
[i
] + stride
[vbi
] * (v
+ start
);
311 for (dw
= 0; dw
< size
[i
]; dw
++) {
312 OUT_CS(map
[vbi
][elem_offset
+ dw
]);
319 for (i
= 0; i
< vertex_element_count
; i
++) {
320 vbi
= r300
->velems
->velem
[i
].vertex_buffer_index
;
323 vbuf
= &r300
->vertex_buffer
[vbi
];
324 pipe_buffer_unmap(&r300
->context
, vbuf
->buffer
, transfer
[vbi
]);
330 void r500_emit_draw_arrays(struct r300_context
*r300
,
334 boolean alt_num_verts
= count
> 65535;
337 if (count
>= (1 << 24)) {
338 fprintf(stderr
, "r300: Got a huge number of vertices: %i, "
339 "refusing to render.\n", count
);
343 BEGIN_CS(7 + (alt_num_verts
? 2 : 0));
345 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES
, count
);
347 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
348 r300_provoking_vertex_fixes(r300
, mode
));
349 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
352 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2
, 0);
353 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST
| (count
<< 16) |
354 r300_translate_primitive(mode
) |
355 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
359 void r500_emit_draw_elements(struct r300_context
*r300
,
360 struct pipe_resource
* indexBuffer
,
368 uint32_t count_dwords
;
369 uint32_t offset_dwords
= indexSize
* start
/ sizeof(uint32_t);
370 boolean alt_num_verts
= count
> 65535;
373 if (count
>= (1 << 24)) {
374 fprintf(stderr
, "r300: Got a huge number of vertices: %i, "
375 "refusing to render.\n", count
);
379 maxIndex
= MIN2(maxIndex
, r300
->vertex_buffer_max_index
);
381 DBG(r300
, DBG_DRAW
, "r300: Indexbuf of %u indices, min %u max %u\n",
382 count
, minIndex
, maxIndex
);
384 BEGIN_CS(13 + (alt_num_verts
? 2 : 0));
386 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES
, count
);
388 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
389 r300_provoking_vertex_fixes(r300
, mode
));
390 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX
, 2);
393 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, 0);
394 if (indexSize
== 4) {
395 count_dwords
= count
;
396 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
397 R300_VAP_VF_CNTL__INDEX_SIZE_32bit
|
398 r300_translate_primitive(mode
) |
399 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
401 count_dwords
= (count
+ 1) / 2;
402 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
403 r300_translate_primitive(mode
) |
404 (alt_num_verts
? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS
: 0));
407 /* INDX_BUFFER is a truly special packet3.
408 * Unlike most other packet3, where the offset is after the count,
409 * the order is reversed, so the relocation ends up carrying the
410 * size of the indexbuf instead of the offset.
412 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER
, 2);
413 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR
| (R300_VAP_PORT_IDX0
>> 2) |
414 (0 << R300_INDX_BUFFER_SKIP_SHIFT
));
415 OUT_CS(offset_dwords
<< 2);
416 OUT_CS_BUF_RELOC(indexBuffer
, count_dwords
,
417 RADEON_GEM_DOMAIN_GTT
, 0, 0);
422 /*****************************************************************************
423 * The emission of draw packets for r300 which take care of the two-sided *
424 * stencil ref fallback and call r500's functions. *
425 ****************************************************************************/
427 /* Set drawing for front faces. */
428 static void r300_begin_stencil_ref_fallback(struct r300_context
*r300
)
430 struct r300_rs_state
*rs
= (struct r300_rs_state
*)r300
->rs_state
.state
;
434 OUT_CS_REG(R300_SU_CULL_MODE
, rs
->cull_mode
| R300_CULL_BACK
);
438 /* Set drawing for back faces. */
439 static void r300_switch_stencil_ref_side(struct r300_context
*r300
)
441 struct r300_rs_state
*rs
= (struct r300_rs_state
*)r300
->rs_state
.state
;
442 struct r300_dsa_state
*dsa
= (struct r300_dsa_state
*)r300
->dsa_state
.state
;
446 OUT_CS_REG(R300_SU_CULL_MODE
, rs
->cull_mode
| R300_CULL_FRONT
);
447 OUT_CS_REG(R300_ZB_STENCILREFMASK
,
448 dsa
->stencil_ref_bf
| r300
->stencil_ref
.ref_value
[1]);
452 /* Restore the original state. */
453 static void r300_end_stencil_ref_fallback(struct r300_context
*r300
)
455 struct r300_rs_state
*rs
= (struct r300_rs_state
*)r300
->rs_state
.state
;
456 struct r300_dsa_state
*dsa
= (struct r300_dsa_state
*)r300
->dsa_state
.state
;
460 OUT_CS_REG(R300_SU_CULL_MODE
, rs
->cull_mode
);
461 OUT_CS_REG(R300_ZB_STENCILREFMASK
,
462 dsa
->stencil_ref_mask
| r300
->stencil_ref
.ref_value
[0]);
466 void r300_emit_draw_arrays_immediate(struct r300_context
*r300
,
471 if (!r300
->stencil_ref_bf_fallback
) {
472 r500_emit_draw_arrays_immediate(r300
, mode
, start
, count
);
474 r300_begin_stencil_ref_fallback(r300
);
475 r500_emit_draw_arrays_immediate(r300
, mode
, start
, count
);
476 r300_switch_stencil_ref_side(r300
);
477 r500_emit_draw_arrays_immediate(r300
, mode
, start
, count
);
478 r300_end_stencil_ref_fallback(r300
);
482 void r300_emit_draw_arrays(struct r300_context
*r300
,
486 if (!r300
->stencil_ref_bf_fallback
) {
487 r500_emit_draw_arrays(r300
, mode
, count
);
489 r300_begin_stencil_ref_fallback(r300
);
490 r500_emit_draw_arrays(r300
, mode
, count
);
491 r300_switch_stencil_ref_side(r300
);
492 r500_emit_draw_arrays(r300
, mode
, count
);
493 r300_end_stencil_ref_fallback(r300
);
497 void r300_emit_draw_elements(struct r300_context
*r300
,
498 struct pipe_resource
* indexBuffer
,
506 if (!r300
->stencil_ref_bf_fallback
) {
507 r500_emit_draw_elements(r300
, indexBuffer
, indexSize
,
508 minIndex
, maxIndex
, mode
, start
, count
);
510 r300_begin_stencil_ref_fallback(r300
);
511 r500_emit_draw_elements(r300
, indexBuffer
, indexSize
,
512 minIndex
, maxIndex
, mode
, start
, count
);
513 r300_switch_stencil_ref_side(r300
);
514 r500_emit_draw_elements(r300
, indexBuffer
, indexSize
,
515 minIndex
, maxIndex
, mode
, start
, count
);
516 r300_end_stencil_ref_fallback(r300
);
520 static void r300_shorten_ubyte_elts(struct r300_context
* r300
,
521 struct pipe_resource
** elts
,
525 struct pipe_context
* context
= &r300
->context
;
526 struct pipe_screen
* screen
= r300
->context
.screen
;
527 struct pipe_resource
* new_elts
;
528 unsigned char *in_map
;
529 unsigned short *out_map
;
530 struct pipe_transfer
*src_transfer
, *dst_transfer
;
533 new_elts
= pipe_buffer_create(screen
,
534 PIPE_BIND_INDEX_BUFFER
,
537 in_map
= pipe_buffer_map(context
, *elts
, PIPE_TRANSFER_READ
, &src_transfer
);
538 out_map
= pipe_buffer_map(context
, new_elts
, PIPE_TRANSFER_WRITE
, &dst_transfer
);
542 for (i
= 0; i
< count
; i
++) {
543 *out_map
= (unsigned short)*in_map
;
548 pipe_buffer_unmap(context
, *elts
, src_transfer
);
549 pipe_buffer_unmap(context
, new_elts
, dst_transfer
);
554 static void r300_align_ushort_elts(struct r300_context
*r300
,
555 struct pipe_resource
**elts
,
556 unsigned start
, unsigned count
)
558 struct pipe_context
* context
= &r300
->context
;
559 struct pipe_transfer
*in_transfer
= NULL
;
560 struct pipe_transfer
*out_transfer
= NULL
;
561 struct pipe_resource
* new_elts
;
562 unsigned short *in_map
;
563 unsigned short *out_map
;
565 new_elts
= pipe_buffer_create(context
->screen
,
566 PIPE_BIND_INDEX_BUFFER
,
569 in_map
= pipe_buffer_map(context
, *elts
,
570 PIPE_TRANSFER_READ
, &in_transfer
);
571 out_map
= pipe_buffer_map(context
, new_elts
,
572 PIPE_TRANSFER_WRITE
, &out_transfer
);
574 memcpy(out_map
, in_map
+start
, 2 * count
);
576 pipe_buffer_unmap(context
, *elts
, in_transfer
);
577 pipe_buffer_unmap(context
, new_elts
, out_transfer
);
582 /* This is the fast-path drawing & emission for HW TCL. */
583 void r300_draw_range_elements(struct pipe_context
* pipe
,
584 struct pipe_resource
* indexBuffer
,
593 struct r300_context
* r300
= r300_context(pipe
);
594 struct pipe_resource
* orgIndexBuffer
= indexBuffer
;
595 boolean alt_num_verts
= r300
->screen
->caps
.is_r500
&&
597 r300
->rws
->get_value(r300
->rws
, R300_VID_DRM_2_3_0
);
598 unsigned short_count
;
600 if (r300
->skip_rendering
) {
604 if (!u_trim_pipe_prim(mode
, &count
)) {
608 if (indexSize
== 1) {
609 r300_shorten_ubyte_elts(r300
, &indexBuffer
, start
, count
);
612 } else if (indexSize
== 2 && start
% 2 != 0) {
613 r300_align_ushort_elts(r300
, &indexBuffer
, start
, count
);
617 r300_update_derived_state(r300
);
618 r300_upload_index_buffer(r300
, &indexBuffer
, indexSize
, start
, count
);
620 /* 15 dwords for emit_draw_elements */
621 r300_prepare_for_rendering(r300
,
622 PREP_FIRST_DRAW
| PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
| PREP_INDEXED
,
623 indexBuffer
, 15, 0, indexBias
);
625 u_upload_flush(r300
->upload_vb
);
626 u_upload_flush(r300
->upload_ib
);
627 if (alt_num_verts
|| count
<= 65535) {
628 r300
->emit_draw_elements(r300
, indexBuffer
, indexSize
,
629 minIndex
, maxIndex
, mode
, start
, count
);
632 short_count
= MIN2(count
, 65534);
633 r300
->emit_draw_elements(r300
, indexBuffer
, indexSize
,
635 mode
, start
, short_count
);
637 start
+= short_count
;
638 count
-= short_count
;
640 /* 15 dwords for emit_draw_elements */
642 r300_prepare_for_rendering(r300
,
643 PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
| PREP_INDEXED
,
644 indexBuffer
, 15, 0, indexBias
);
649 if (indexBuffer
!= orgIndexBuffer
) {
650 pipe_resource_reference( &indexBuffer
, NULL
);
654 /* Simple helpers for context setup. Should probably be moved to util. */
655 void r300_draw_elements(struct pipe_context
* pipe
,
656 struct pipe_resource
* indexBuffer
,
657 unsigned indexSize
, int indexBias
, unsigned mode
,
658 unsigned start
, unsigned count
)
660 struct r300_context
*r300
= r300_context(pipe
);
662 pipe
->draw_range_elements(pipe
, indexBuffer
, indexSize
, indexBias
,
663 0, r300
->vertex_buffer_max_index
,
667 void r300_draw_arrays(struct pipe_context
* pipe
, unsigned mode
,
668 unsigned start
, unsigned count
)
670 struct r300_context
* r300
= r300_context(pipe
);
671 boolean alt_num_verts
= r300
->screen
->caps
.is_r500
&&
673 r300
->rws
->get_value(r300
->rws
, R300_VID_DRM_2_3_0
);
674 unsigned short_count
;
676 if (r300
->skip_rendering
) {
680 if (!u_trim_pipe_prim(mode
, &count
)) {
684 r300_update_derived_state(r300
);
686 if (immd_is_good_idea(r300
, count
)) {
687 r300
->emit_draw_arrays_immediate(r300
, mode
, start
, count
);
689 /* 9 spare dwords for emit_draw_arrays. */
690 r300_prepare_for_rendering(r300
, PREP_FIRST_DRAW
| PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
,
693 if (alt_num_verts
|| count
<= 65535) {
694 r300
->emit_draw_arrays(r300
, mode
, count
);
697 short_count
= MIN2(count
, 65535);
698 r300
->emit_draw_arrays(r300
, mode
, short_count
);
700 start
+= short_count
;
701 count
-= short_count
;
703 /* 9 spare dwords for emit_draw_arrays. */
705 r300_prepare_for_rendering(r300
,
706 PREP_VALIDATE_VBOS
| PREP_EMIT_AOS
, NULL
, 9,
711 u_upload_flush(r300
->upload_vb
);
715 /****************************************************************************
716 * The rest of this file is for SW TCL rendering only. Please be polite and *
717 * keep these functions separated so that they are easier to locate. ~C. *
718 ***************************************************************************/
720 /* SW TCL arrays, using Draw. */
721 void r300_swtcl_draw_arrays(struct pipe_context
* pipe
,
726 struct r300_context
* r300
= r300_context(pipe
);
727 struct pipe_transfer
*vb_transfer
[PIPE_MAX_ATTRIBS
];
730 if (r300
->skip_rendering
) {
734 if (!u_trim_pipe_prim(mode
, &count
)) {
738 r300_update_derived_state(r300
);
740 for (i
= 0; i
< r300
->vertex_buffer_count
; i
++) {
741 void* buf
= pipe_buffer_map(pipe
,
742 r300
->vertex_buffer
[i
].buffer
,
745 draw_set_mapped_vertex_buffer(r300
->draw
, i
, buf
);
748 draw_set_mapped_element_buffer(r300
->draw
, 0, 0, NULL
);
750 draw_arrays(r300
->draw
, mode
, start
, count
);
752 /* XXX Not sure whether this is the best fix.
753 * It prevents CS from being rejected and weird assertion failures. */
754 draw_flush(r300
->draw
);
756 for (i
= 0; i
< r300
->vertex_buffer_count
; i
++) {
757 pipe_buffer_unmap(pipe
, r300
->vertex_buffer
[i
].buffer
,
759 draw_set_mapped_vertex_buffer(r300
->draw
, i
, NULL
);
763 /* SW TCL elements, using Draw. */
764 void r300_swtcl_draw_range_elements(struct pipe_context
* pipe
,
765 struct pipe_resource
* indexBuffer
,
774 struct r300_context
* r300
= r300_context(pipe
);
775 struct pipe_transfer
*vb_transfer
[PIPE_MAX_ATTRIBS
];
776 struct pipe_transfer
*ib_transfer
;
780 if (r300
->skip_rendering
) {
784 if (!u_trim_pipe_prim(mode
, &count
)) {
788 r300_update_derived_state(r300
);
790 for (i
= 0; i
< r300
->vertex_buffer_count
; i
++) {
791 void* buf
= pipe_buffer_map(pipe
,
792 r300
->vertex_buffer
[i
].buffer
,
795 draw_set_mapped_vertex_buffer(r300
->draw
, i
, buf
);
798 indices
= pipe_buffer_map(pipe
, indexBuffer
,
799 PIPE_TRANSFER_READ
, &ib_transfer
);
800 draw_set_mapped_element_buffer_range(r300
->draw
, indexSize
, indexBias
,
801 minIndex
, maxIndex
, indices
);
803 draw_arrays(r300
->draw
, mode
, start
, count
);
805 /* XXX Not sure whether this is the best fix.
806 * It prevents CS from being rejected and weird assertion failures. */
807 draw_flush(r300
->draw
);
809 for (i
= 0; i
< r300
->vertex_buffer_count
; i
++) {
810 pipe_buffer_unmap(pipe
, r300
->vertex_buffer
[i
].buffer
,
812 draw_set_mapped_vertex_buffer(r300
->draw
, i
, NULL
);
815 pipe_buffer_unmap(pipe
, indexBuffer
,
817 draw_set_mapped_element_buffer_range(r300
->draw
, 0, 0,
818 start
, start
+ count
- 1,
822 /* Object for rendering using Draw. */
825 struct vbuf_render base
;
828 struct r300_context
* r300
;
830 /* Vertex information */
836 struct pipe_resource
* vbo
;
842 struct pipe_transfer
*vbo_transfer
;
845 static INLINE
struct r300_render
*
846 r300_render(struct vbuf_render
* render
)
848 return (struct r300_render
*)render
;
851 static const struct vertex_info
*
852 r300_render_get_vertex_info(struct vbuf_render
* render
)
854 struct r300_render
* r300render
= r300_render(render
);
855 struct r300_context
* r300
= r300render
->r300
;
857 return &r300
->vertex_info
;
860 static boolean
r300_render_allocate_vertices(struct vbuf_render
* render
,
864 struct r300_render
* r300render
= r300_render(render
);
865 struct r300_context
* r300
= r300render
->r300
;
866 struct pipe_screen
* screen
= r300
->context
.screen
;
867 size_t size
= (size_t)vertex_size
* (size_t)count
;
869 if (size
+ r300render
->vbo_offset
> r300render
->vbo_size
)
871 pipe_resource_reference(&r300
->vbo
, NULL
);
872 r300render
->vbo
= pipe_buffer_create(screen
,
873 PIPE_BIND_VERTEX_BUFFER
,
874 R300_MAX_DRAW_VBO_SIZE
);
875 r300render
->vbo_offset
= 0;
876 r300render
->vbo_size
= R300_MAX_DRAW_VBO_SIZE
;
879 r300render
->vertex_size
= vertex_size
;
880 r300
->vbo
= r300render
->vbo
;
881 r300
->vbo_offset
= r300render
->vbo_offset
;
883 return (r300render
->vbo
) ? TRUE
: FALSE
;
886 static void* r300_render_map_vertices(struct vbuf_render
* render
)
888 struct r300_render
* r300render
= r300_render(render
);
890 r300render
->vbo_ptr
= pipe_buffer_map(&r300render
->r300
->context
,
893 &r300render
->vbo_transfer
);
895 return ((uint8_t*)r300render
->vbo_ptr
+ r300render
->vbo_offset
);
898 static void r300_render_unmap_vertices(struct vbuf_render
* render
,
902 struct r300_render
* r300render
= r300_render(render
);
903 struct pipe_context
* context
= &r300render
->r300
->context
;
905 r300render
->vbo_max_used
= MAX2(r300render
->vbo_max_used
,
906 r300render
->vertex_size
* (max
+ 1));
907 pipe_buffer_unmap(context
, r300render
->vbo
, r300render
->vbo_transfer
);
910 static void r300_render_release_vertices(struct vbuf_render
* render
)
912 struct r300_render
* r300render
= r300_render(render
);
914 r300render
->vbo_offset
+= r300render
->vbo_max_used
;
915 r300render
->vbo_max_used
= 0;
918 static boolean
r300_render_set_primitive(struct vbuf_render
* render
,
921 struct r300_render
* r300render
= r300_render(render
);
923 r300render
->prim
= prim
;
924 r300render
->hwprim
= r300_translate_primitive(prim
);
929 static void r500_render_draw_arrays(struct vbuf_render
* render
,
933 struct r300_render
* r300render
= r300_render(render
);
934 struct r300_context
* r300
= r300render
->r300
;
941 (void) i
; (void) ptr
;
943 r300_prepare_for_rendering(r300
, PREP_FIRST_DRAW
, NULL
, dwords
, 0, 0);
945 DBG(r300
, DBG_DRAW
, "r300: Doing vbuf render, count %d\n", count
);
947 /* Uncomment to dump all VBOs rendered through this interface.
949 ptr = pipe_buffer_map(&r300render->r300->context,
950 r300render->vbo, PIPE_TRANSFER_READ,
951 &r300render->vbo_transfer);
953 for (i = 0; i < count; i++) {
954 printf("r300: Vertex %d\n", i);
955 draw_dump_emitted_vertex(&r300->vertex_info, ptr);
956 ptr += r300->vertex_info.size * 4;
960 pipe_buffer_unmap(&r300render->r300->context, r300render->vbo,
961 r300render->vbo_transfer);
965 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
966 r300_provoking_vertex_fixes(r300
, r300render
->prim
));
967 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX
, count
- 1);
968 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2
, 0);
969 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST
| (count
<< 16) |
974 static void r500_render_draw_elements(struct vbuf_render
* render
,
975 const ushort
* indices
,
978 struct r300_render
* r300render
= r300_render(render
);
979 struct r300_context
* r300
= r300render
->r300
;
981 unsigned dwords
= 6 + (count
+1)/2;
982 unsigned max_index
= (r300render
->vbo_size
- r300render
->vbo_offset
) /
983 (r300render
->r300
->vertex_info
.size
* 4) - 1;
987 r300_prepare_for_rendering(r300
, PREP_FIRST_DRAW
, NULL
, dwords
, 0, 0);
990 OUT_CS_REG(R300_GA_COLOR_CONTROL
,
991 r300_provoking_vertex_fixes(r300
, r300render
->prim
));
992 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX
, max_index
);
993 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, (count
+1)/2);
994 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
996 for (i
= 0; i
< count
-1; i
+= 2) {
997 OUT_CS(indices
[i
+1] << 16 | indices
[i
]);
1000 OUT_CS(indices
[count
-1]);
1005 static void r300_render_draw_arrays(struct vbuf_render
* render
,
1009 struct r300_context
* r300
= r300_render(render
)->r300
;
1011 if (!r300
->stencil_ref_bf_fallback
) {
1012 r500_render_draw_arrays(render
, start
, count
);
1014 r300_begin_stencil_ref_fallback(r300
);
1015 r500_render_draw_arrays(render
, start
, count
);
1016 r300_switch_stencil_ref_side(r300
);
1017 r500_render_draw_arrays(render
, start
, count
);
1018 r300_end_stencil_ref_fallback(r300
);
1022 static void r300_render_draw_elements(struct vbuf_render
* render
,
1023 const ushort
* indices
,
1026 struct r300_context
* r300
= r300_render(render
)->r300
;
1028 if (!r300
->stencil_ref_bf_fallback
) {
1029 r500_render_draw_elements(render
, indices
, count
);
1031 r300_begin_stencil_ref_fallback(r300
);
1032 r500_render_draw_elements(render
, indices
, count
);
1033 r300_switch_stencil_ref_side(r300
);
1034 r500_render_draw_elements(render
, indices
, count
);
1035 r300_end_stencil_ref_fallback(r300
);
1039 static void r300_render_destroy(struct vbuf_render
* render
)
1044 static struct vbuf_render
* r300_render_create(struct r300_context
* r300
)
1046 struct r300_render
* r300render
= CALLOC_STRUCT(r300_render
);
1048 r300render
->r300
= r300
;
1050 /* XXX find real numbers plz */
1051 r300render
->base
.max_vertex_buffer_bytes
= 128 * 1024;
1052 r300render
->base
.max_indices
= 16 * 1024;
1054 r300render
->base
.get_vertex_info
= r300_render_get_vertex_info
;
1055 r300render
->base
.allocate_vertices
= r300_render_allocate_vertices
;
1056 r300render
->base
.map_vertices
= r300_render_map_vertices
;
1057 r300render
->base
.unmap_vertices
= r300_render_unmap_vertices
;
1058 r300render
->base
.set_primitive
= r300_render_set_primitive
;
1059 if (r300
->screen
->caps
.is_r500
) {
1060 r300render
->base
.draw_elements
= r500_render_draw_elements
;
1061 r300render
->base
.draw_arrays
= r500_render_draw_arrays
;
1063 r300render
->base
.draw_elements
= r300_render_draw_elements
;
1064 r300render
->base
.draw_arrays
= r300_render_draw_arrays
;
1066 r300render
->base
.release_vertices
= r300_render_release_vertices
;
1067 r300render
->base
.destroy
= r300_render_destroy
;
1069 r300render
->vbo
= NULL
;
1070 r300render
->vbo_size
= 0;
1071 r300render
->vbo_offset
= 0;
1073 return &r300render
->base
;
1076 struct draw_stage
* r300_draw_stage(struct r300_context
* r300
)
1078 struct vbuf_render
* render
;
1079 struct draw_stage
* stage
;
1081 render
= r300_render_create(r300
);
1087 stage
= draw_vbuf_stage(r300
->draw
, render
);
1090 render
->destroy(render
);
1094 draw_set_render(r300
->draw
, render
);