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 /* XXX The DRM rejects VAP_ALT_NUM_VERTICES.. */
45 //#define ENABLE_ALT_NUM_VERTS
47 uint32_t r300_translate_primitive(unsigned prim)
50 case PIPE_PRIM_POINTS:
51 return R300_VAP_VF_CNTL__PRIM_POINTS;
53 return R300_VAP_VF_CNTL__PRIM_LINES;
54 case PIPE_PRIM_LINE_LOOP:
55 return R300_VAP_VF_CNTL__PRIM_LINE_LOOP;
56 case PIPE_PRIM_LINE_STRIP:
57 return R300_VAP_VF_CNTL__PRIM_LINE_STRIP;
58 case PIPE_PRIM_TRIANGLES:
59 return R300_VAP_VF_CNTL__PRIM_TRIANGLES;
60 case PIPE_PRIM_TRIANGLE_STRIP:
61 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP;
62 case PIPE_PRIM_TRIANGLE_FAN:
63 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN;
65 return R300_VAP_VF_CNTL__PRIM_QUADS;
66 case PIPE_PRIM_QUAD_STRIP:
67 return R300_VAP_VF_CNTL__PRIM_QUAD_STRIP;
68 case PIPE_PRIM_POLYGON:
69 return R300_VAP_VF_CNTL__PRIM_POLYGON;
75 static uint32_t r300_provoking_vertex_fixes(struct r300_context *r300,
78 struct r300_rs_state* rs = (struct r300_rs_state*)r300->rs_state.state;
79 uint32_t color_control = rs->color_control;
81 /* By default (see r300_state.c:r300_create_rs_state) color_control is
82 * initialized to provoking the first vertex.
84 * Triangle fans must be reduced to the second vertex, not the first, in
85 * Gallium flatshade-first mode, as per the GL spec.
86 * (http://www.opengl.org/registry/specs/ARB/provoking_vertex.txt)
88 * Quads never provoke correctly in flatshade-first mode. The first
89 * vertex is never considered as provoking, so only the second, third,
90 * and fourth vertices can be selected, and both "third" and "last" modes
91 * select the fourth vertex. This is probably due to D3D lacking quads.
93 * Similarly, polygons reduce to the first, not the last, vertex, when in
94 * "last" mode, and all other modes start from the second vertex.
99 if (rs->rs.flatshade_first) {
101 case PIPE_PRIM_TRIANGLE_FAN:
102 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND;
104 case PIPE_PRIM_QUADS:
105 case PIPE_PRIM_QUAD_STRIP:
106 case PIPE_PRIM_POLYGON:
107 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
110 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST;
114 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
117 return color_control;
120 /* Check if the requested number of dwords is available in the CS and
121 * if not, flush. Return TRUE if the flush occured. */
122 static boolean r300_reserve_cs_space(struct r300_context *r300,
125 if (!r300->rws->check_cs(r300->rws, dwords)) {
126 r300->context.flush(&r300->context, 0, NULL);
132 static boolean immd_is_good_idea(struct r300_context *r300,
135 struct pipe_vertex_element* velem;
136 struct pipe_vertex_buffer* vbuf;
137 boolean checked[PIPE_MAX_ATTRIBS] = {0};
138 unsigned vertex_element_count = r300->velems->count;
145 /* We shouldn't map buffers referenced by CS, busy buffers,
146 * and ones placed in VRAM. */
147 /* XXX Check for VRAM buffers. */
148 for (i = 0; i < vertex_element_count; i++) {
149 velem = &r300->velems->velem[i];
150 vbi = velem->vertex_buffer_index;
153 vbuf = &r300->vertex_buffer[vbi];
155 if (r300_buffer_is_referenced(r300, vbuf->buffer)) {
156 /* It's a very bad idea to map it... */
165 /*****************************************************************************
166 * The emission of draw packets for r500. Older GPUs may use these functions *
167 * after resolving fallback issues (e.g. stencil ref two-sided). *
168 ****************************************************************************/
170 void r500_emit_draw_arrays_immediate(struct r300_context *r300,
175 struct pipe_vertex_element* velem;
176 struct pipe_vertex_buffer* vbuf;
177 unsigned vertex_element_count = r300->velems->count;
178 unsigned i, v, vbi, dw, elem_offset, dwords;
180 /* Size of the vertex, in dwords. */
181 unsigned vertex_size = 0;
183 /* Offsets of the attribute, in dwords, from the start of the vertex. */
184 unsigned offset[PIPE_MAX_ATTRIBS];
186 /* Size of the vertex element, in dwords. */
187 unsigned size[PIPE_MAX_ATTRIBS];
189 /* Stride to the same attrib in the next vertex in the vertex buffer,
191 unsigned stride[PIPE_MAX_ATTRIBS] = {0};
193 /* Mapped vertex buffers. */
194 uint32_t* map[PIPE_MAX_ATTRIBS] = {0};
198 /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
199 for (i = 0; i < vertex_element_count; i++) {
200 velem = &r300->velems->velem[i];
201 offset[i] = velem->src_offset / 4;
202 size[i] = util_format_get_blocksize(velem->src_format) / 4;
203 vertex_size += size[i];
204 vbi = velem->vertex_buffer_index;
206 /* Map the buffer. */
208 vbuf = &r300->vertex_buffer[vbi];
209 map[vbi] = (uint32_t*)pipe_buffer_map(r300->context.screen,
211 PIPE_BUFFER_USAGE_CPU_READ);
212 map[vbi] += vbuf->buffer_offset / 4;
213 stride[vbi] = vbuf->stride / 4;
217 dwords = 9 + count * vertex_size;
219 r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + dwords);
220 r300_emit_buffer_validate(r300, FALSE, NULL);
221 r300_emit_dirty_state(r300);
224 OUT_CS_REG(R300_GA_COLOR_CONTROL,
225 r300_provoking_vertex_fixes(r300, mode));
226 OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
227 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
230 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, count * vertex_size);
231 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (count << 16) |
232 r300_translate_primitive(mode));
235 for (v = 0; v < count; v++) {
236 for (i = 0; i < vertex_element_count; i++) {
237 velem = &r300->velems->velem[i];
238 vbi = velem->vertex_buffer_index;
239 elem_offset = offset[i] + stride[vbi] * (v + start);
241 for (dw = 0; dw < size[i]; dw++) {
242 OUT_CS(map[vbi][elem_offset + dw]);
249 for (i = 0; i < vertex_element_count; i++) {
250 vbi = r300->velems->velem[i].vertex_buffer_index;
253 vbuf = &r300->vertex_buffer[vbi];
254 pipe_buffer_unmap(r300->context.screen, vbuf->buffer);
260 void r500_emit_draw_arrays(struct r300_context *r300,
264 #if defined(ENABLE_ALT_NUM_VERTS)
265 boolean alt_num_verts = count > 65535;
267 boolean alt_num_verts = FALSE;
272 assert(count < (1 << 24));
274 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
278 OUT_CS_REG(R300_GA_COLOR_CONTROL,
279 r300_provoking_vertex_fixes(r300, mode));
280 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
283 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
284 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
285 r300_translate_primitive(mode) |
286 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
290 void r500_emit_draw_elements(struct r300_context *r300,
291 struct pipe_buffer* indexBuffer,
299 uint32_t count_dwords;
300 uint32_t offset_dwords = indexSize * start / sizeof(uint32_t);
301 #if defined(ENABLE_ALT_NUM_VERTS)
302 boolean alt_num_verts = count > 65535;
304 boolean alt_num_verts = FALSE;
308 assert(count < (1 << 24));
310 maxIndex = MIN2(maxIndex, r300->vertex_buffer_max_index);
312 DBG(r300, DBG_DRAW, "r300: Indexbuf of %u indices, min %u max %u\n",
313 count, minIndex, maxIndex);
317 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
321 OUT_CS_REG(R300_GA_COLOR_CONTROL,
322 r300_provoking_vertex_fixes(r300, mode));
323 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
326 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);
327 if (indexSize == 4) {
328 count_dwords = count;
329 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
330 R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
331 r300_translate_primitive(mode) |
332 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
334 count_dwords = (count + 1) / 2;
335 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
336 r300_translate_primitive(mode) |
337 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
340 /* INDX_BUFFER is a truly special packet3.
341 * Unlike most other packet3, where the offset is after the count,
342 * the order is reversed, so the relocation ends up carrying the
343 * size of the indexbuf instead of the offset.
345 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2);
346 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) |
347 (0 << R300_INDX_BUFFER_SKIP_SHIFT));
348 OUT_CS(offset_dwords << 2);
349 OUT_CS_BUF_RELOC(indexBuffer, count_dwords,
350 RADEON_GEM_DOMAIN_GTT, 0, 0);
355 /*****************************************************************************
356 * The emission of draw packets for r300 which take care of the two-sided *
357 * stencil ref fallback and call r500's functions. *
358 ****************************************************************************/
360 /* Set drawing for front faces. */
361 static void r300_begin_stencil_ref_fallback(struct r300_context *r300)
363 struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;
367 OUT_CS_REG(R300_SU_CULL_MODE, rs->cull_mode | R300_CULL_BACK);
371 /* Set drawing for back faces. */
372 static void r300_switch_stencil_ref_side(struct r300_context *r300)
374 struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;
375 struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
379 OUT_CS_REG(R300_SU_CULL_MODE, rs->cull_mode | R300_CULL_FRONT);
380 OUT_CS_REG(R300_ZB_STENCILREFMASK,
381 dsa->stencil_ref_bf | r300->stencil_ref.ref_value[1]);
385 /* Restore the original state. */
386 static void r300_end_stencil_ref_fallback(struct r300_context *r300)
388 struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;
389 struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
393 OUT_CS_REG(R300_SU_CULL_MODE, rs->cull_mode);
394 OUT_CS_REG(R300_ZB_STENCILREFMASK,
395 dsa->stencil_ref_mask | r300->stencil_ref.ref_value[0]);
399 void r300_emit_draw_arrays_immediate(struct r300_context *r300,
404 if (!r300->stencil_ref_bf_fallback) {
405 r500_emit_draw_arrays_immediate(r300, mode, start, count);
407 r300_begin_stencil_ref_fallback(r300);
408 r500_emit_draw_arrays_immediate(r300, mode, start, count);
409 r300_switch_stencil_ref_side(r300);
410 r500_emit_draw_arrays_immediate(r300, mode, start, count);
411 r300_end_stencil_ref_fallback(r300);
415 void r300_emit_draw_arrays(struct r300_context *r300,
419 if (!r300->stencil_ref_bf_fallback) {
420 r500_emit_draw_arrays(r300, mode, count);
422 r300_begin_stencil_ref_fallback(r300);
423 r500_emit_draw_arrays(r300, mode, count);
424 r300_switch_stencil_ref_side(r300);
425 r500_emit_draw_arrays(r300, mode, count);
426 r300_end_stencil_ref_fallback(r300);
430 void r300_emit_draw_elements(struct r300_context *r300,
431 struct pipe_buffer* indexBuffer,
439 if (!r300->stencil_ref_bf_fallback) {
440 r500_emit_draw_elements(r300, indexBuffer, indexSize, minIndex,
441 maxIndex, mode, start, count);
443 r300_begin_stencil_ref_fallback(r300);
444 r500_emit_draw_elements(r300, indexBuffer, indexSize, minIndex,
445 maxIndex, mode, start, count);
446 r300_switch_stencil_ref_side(r300);
447 r500_emit_draw_elements(r300, indexBuffer, indexSize, minIndex,
448 maxIndex, mode, start, count);
449 r300_end_stencil_ref_fallback(r300);
453 static void r300_shorten_ubyte_elts(struct r300_context* r300,
454 struct pipe_buffer** elts,
458 struct pipe_screen* screen = r300->context.screen;
459 struct pipe_buffer* new_elts;
460 unsigned char *in_map;
461 unsigned short *out_map;
464 new_elts = screen->buffer_create(screen, 32,
465 PIPE_BUFFER_USAGE_INDEX |
466 PIPE_BUFFER_USAGE_CPU_WRITE |
467 PIPE_BUFFER_USAGE_GPU_READ,
470 in_map = pipe_buffer_map(screen, *elts, PIPE_BUFFER_USAGE_CPU_READ);
471 out_map = pipe_buffer_map(screen, new_elts, PIPE_BUFFER_USAGE_CPU_WRITE);
475 for (i = 0; i < count; i++) {
476 *out_map = (unsigned short)*in_map;
481 pipe_buffer_unmap(screen, *elts);
482 pipe_buffer_unmap(screen, new_elts);
487 static void r300_align_ushort_elts(struct r300_context *r300,
488 struct pipe_buffer **elts,
489 unsigned start, unsigned count)
491 struct pipe_screen* screen = r300->context.screen;
492 struct pipe_buffer* new_elts;
493 unsigned short *in_map;
494 unsigned short *out_map;
496 new_elts = screen->buffer_create(screen, 32,
497 PIPE_BUFFER_USAGE_INDEX |
498 PIPE_BUFFER_USAGE_CPU_WRITE |
499 PIPE_BUFFER_USAGE_GPU_READ,
502 in_map = pipe_buffer_map(screen, *elts, PIPE_BUFFER_USAGE_CPU_READ);
503 out_map = pipe_buffer_map(screen, new_elts, PIPE_BUFFER_USAGE_CPU_WRITE);
505 memcpy(out_map, in_map+start, 2 * count);
507 pipe_buffer_unmap(screen, *elts);
508 pipe_buffer_unmap(screen, new_elts);
513 /* This is the fast-path drawing & emission for HW TCL. */
514 void r300_draw_range_elements(struct pipe_context* pipe,
515 struct pipe_buffer* indexBuffer,
523 struct r300_context* r300 = r300_context(pipe);
524 struct pipe_buffer* orgIndexBuffer = indexBuffer;
525 #if defined(ENABLE_ALT_NUM_VERTS)
526 boolean alt_num_verts = r300->screen->caps.is_r500 &&
529 boolean alt_num_verts = FALSE;
531 unsigned short_count;
533 if (r300->skip_rendering) {
537 if (!u_trim_pipe_prim(mode, &count)) {
541 if (indexSize == 1) {
542 r300_shorten_ubyte_elts(r300, &indexBuffer, start, count);
545 } else if (indexSize == 2 && start % 2 != 0) {
546 r300_align_ushort_elts(r300, &indexBuffer, start, count);
550 r300_update_derived_state(r300);
552 r300_upload_index_buffer(r300, &indexBuffer, indexSize, start, count);
554 /* 128 dwords for emit_aos and emit_draw_elements */
555 r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + 128);
556 r300_emit_buffer_validate(r300, TRUE, indexBuffer);
557 r300_emit_dirty_state(r300);
558 r300_emit_aos(r300, 0);
560 u_upload_flush(r300->upload_vb);
561 u_upload_flush(r300->upload_ib);
562 if (alt_num_verts || count <= 65535) {
563 r300->emit_draw_elements(r300, indexBuffer, indexSize, minIndex,
564 maxIndex, mode, start, count);
567 short_count = MIN2(count, 65534);
568 r300->emit_draw_elements(r300, indexBuffer, indexSize, minIndex,
569 maxIndex, mode, start, short_count);
571 start += short_count;
572 count -= short_count;
574 /* 16 spare dwords are enough for emit_draw_elements. */
575 if (count && r300_reserve_cs_space(r300, 16)) {
576 r300_emit_buffer_validate(r300, TRUE, indexBuffer);
577 r300_emit_dirty_state(r300);
578 r300_emit_aos(r300, 0);
583 if (indexBuffer != orgIndexBuffer) {
584 pipe_buffer_reference( &indexBuffer, NULL );
588 /* Simple helpers for context setup. Should probably be moved to util. */
589 void r300_draw_elements(struct pipe_context* pipe,
590 struct pipe_buffer* indexBuffer,
591 unsigned indexSize, unsigned mode,
592 unsigned start, unsigned count)
594 struct r300_context *r300 = r300_context(pipe);
596 pipe->draw_range_elements(pipe, indexBuffer, indexSize, 0,
597 r300->vertex_buffer_max_index,
601 void r300_draw_arrays(struct pipe_context* pipe, unsigned mode,
602 unsigned start, unsigned count)
604 struct r300_context* r300 = r300_context(pipe);
605 #if defined(ENABLE_ALT_NUM_VERTS)
606 boolean alt_num_verts = r300->screen->caps.is_r500 &&
609 boolean alt_num_verts = FALSE;
611 unsigned short_count;
613 if (r300->skip_rendering) {
617 if (!u_trim_pipe_prim(mode, &count)) {
621 r300_update_derived_state(r300);
623 if (immd_is_good_idea(r300, count)) {
624 r300->emit_draw_arrays_immediate(r300, mode, start, count);
626 /* Make sure there are at least 128 spare dwords in the command buffer.
627 * (most of it being consumed by emit_aos) */
628 r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + 128);
629 r300_emit_buffer_validate(r300, TRUE, NULL);
630 r300_emit_dirty_state(r300);
632 if (alt_num_verts || count <= 65535) {
633 r300_emit_aos(r300, start);
634 r300->emit_draw_arrays(r300, mode, count);
637 short_count = MIN2(count, 65535);
638 r300_emit_aos(r300, start);
639 r300->emit_draw_arrays(r300, mode, short_count);
641 start += short_count;
642 count -= short_count;
644 /* Again, we emit both AOS and draw_arrays so there should be
645 * at least 128 spare dwords. */
646 if (count && r300_reserve_cs_space(r300, 128)) {
647 r300_emit_buffer_validate(r300, TRUE, NULL);
648 r300_emit_dirty_state(r300);
652 u_upload_flush(r300->upload_vb);
656 /****************************************************************************
657 * The rest of this file is for SW TCL rendering only. Please be polite and *
658 * keep these functions separated so that they are easier to locate. ~C. *
659 ***************************************************************************/
661 /* SW TCL arrays, using Draw. */
662 void r300_swtcl_draw_arrays(struct pipe_context* pipe,
667 struct r300_context* r300 = r300_context(pipe);
670 if (r300->skip_rendering) {
674 if (!u_trim_pipe_prim(mode, &count)) {
678 for (i = 0; i < r300->vertex_buffer_count; i++) {
679 void* buf = pipe_buffer_map(pipe->screen,
680 r300->vertex_buffer[i].buffer,
681 PIPE_BUFFER_USAGE_CPU_READ);
682 draw_set_mapped_vertex_buffer(r300->draw, i, buf);
685 draw_set_mapped_element_buffer(r300->draw, 0, NULL);
687 draw_arrays(r300->draw, mode, start, count);
689 for (i = 0; i < r300->vertex_buffer_count; i++) {
690 pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer);
691 draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
695 /* SW TCL elements, using Draw. */
696 void r300_swtcl_draw_range_elements(struct pipe_context* pipe,
697 struct pipe_buffer* indexBuffer,
705 struct r300_context* r300 = r300_context(pipe);
709 if (r300->skip_rendering) {
713 if (!u_trim_pipe_prim(mode, &count)) {
717 for (i = 0; i < r300->vertex_buffer_count; i++) {
718 void* buf = pipe_buffer_map(pipe->screen,
719 r300->vertex_buffer[i].buffer,
720 PIPE_BUFFER_USAGE_CPU_READ);
721 draw_set_mapped_vertex_buffer(r300->draw, i, buf);
724 indices = pipe_buffer_map(pipe->screen, indexBuffer,
725 PIPE_BUFFER_USAGE_CPU_READ);
726 draw_set_mapped_element_buffer_range(r300->draw, indexSize,
727 minIndex, maxIndex, indices);
729 draw_arrays(r300->draw, mode, start, count);
731 for (i = 0; i < r300->vertex_buffer_count; i++) {
732 pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer);
733 draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
736 pipe_buffer_unmap(pipe->screen, indexBuffer);
737 draw_set_mapped_element_buffer_range(r300->draw, 0, start,
738 start + count - 1, NULL);
741 /* Object for rendering using Draw. */
744 struct vbuf_render base;
747 struct r300_context* r300;
749 /* Vertex information */
755 struct pipe_buffer* vbo;
762 static INLINE struct r300_render*
763 r300_render(struct vbuf_render* render)
765 return (struct r300_render*)render;
768 static const struct vertex_info*
769 r300_render_get_vertex_info(struct vbuf_render* render)
771 struct r300_render* r300render = r300_render(render);
772 struct r300_context* r300 = r300render->r300;
774 r300_update_derived_state(r300);
776 return &r300->vertex_info;
779 static boolean r300_render_allocate_vertices(struct vbuf_render* render,
783 struct r300_render* r300render = r300_render(render);
784 struct r300_context* r300 = r300render->r300;
785 struct pipe_screen* screen = r300->context.screen;
786 size_t size = (size_t)vertex_size * (size_t)count;
788 if (size + r300render->vbo_offset > r300render->vbo_size)
790 pipe_buffer_reference(&r300->vbo, NULL);
791 r300render->vbo = pipe_buffer_create(screen,
793 PIPE_BUFFER_USAGE_VERTEX,
794 R300_MAX_DRAW_VBO_SIZE);
795 r300render->vbo_offset = 0;
796 r300render->vbo_size = R300_MAX_DRAW_VBO_SIZE;
799 r300render->vertex_size = vertex_size;
800 r300->vbo = r300render->vbo;
801 r300->vbo_offset = r300render->vbo_offset;
803 return (r300render->vbo) ? TRUE : FALSE;
806 static void* r300_render_map_vertices(struct vbuf_render* render)
808 struct r300_render* r300render = r300_render(render);
809 struct pipe_screen* screen = r300render->r300->context.screen;
811 r300render->vbo_ptr = pipe_buffer_map(screen, r300render->vbo,
812 PIPE_BUFFER_USAGE_CPU_WRITE);
814 return ((uint8_t*)r300render->vbo_ptr + r300render->vbo_offset);
817 static void r300_render_unmap_vertices(struct vbuf_render* render,
821 struct r300_render* r300render = r300_render(render);
822 struct pipe_screen* screen = r300render->r300->context.screen;
823 CS_LOCALS(r300render->r300);
825 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max);
828 r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
829 r300render->vertex_size * (max + 1));
830 pipe_buffer_unmap(screen, r300render->vbo);
833 static void r300_render_release_vertices(struct vbuf_render* render)
835 struct r300_render* r300render = r300_render(render);
837 r300render->vbo_offset += r300render->vbo_max_used;
838 r300render->vbo_max_used = 0;
841 static boolean r300_render_set_primitive(struct vbuf_render* render,
844 struct r300_render* r300render = r300_render(render);
846 r300render->prim = prim;
847 r300render->hwprim = r300_translate_primitive(prim);
852 static void r500_render_draw_arrays(struct vbuf_render* render,
856 struct r300_render* r300render = r300_render(render);
857 struct r300_context* r300 = r300render->r300;
861 r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + 2);
862 r300_emit_buffer_validate(r300, FALSE, NULL);
863 r300_emit_dirty_state(r300);
865 DBG(r300, DBG_DRAW, "r300: Doing vbuf render, count %d\n", count);
868 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
869 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
874 static void r500_render_draw(struct vbuf_render* render,
875 const ushort* indices,
878 struct r300_render* r300render = r300_render(render);
879 struct r300_context* r300 = r300render->r300;
881 unsigned dwords = 2 + (count+1)/2;
885 r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + dwords);
886 r300_emit_buffer_validate(r300, FALSE, NULL);
887 r300_emit_dirty_state(r300);
890 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (count+1)/2);
891 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
893 for (i = 0; i < count-1; i += 2) {
894 OUT_CS(indices[i+1] << 16 | indices[i]);
897 OUT_CS(indices[count-1]);
902 static void r300_render_draw_arrays(struct vbuf_render* render,
906 struct r300_context* r300 = r300_render(render)->r300;
908 if (!r300->stencil_ref_bf_fallback) {
909 r500_render_draw_arrays(render, start, count);
911 r300_begin_stencil_ref_fallback(r300);
912 r500_render_draw_arrays(render, start, count);
913 r300_switch_stencil_ref_side(r300);
914 r500_render_draw_arrays(render, start, count);
915 r300_end_stencil_ref_fallback(r300);
919 static void r300_render_draw(struct vbuf_render* render,
920 const ushort* indices,
923 struct r300_context* r300 = r300_render(render)->r300;
925 if (!r300->stencil_ref_bf_fallback) {
926 r500_render_draw(render, indices, count);
928 r300_begin_stencil_ref_fallback(r300);
929 r500_render_draw(render, indices, count);
930 r300_switch_stencil_ref_side(r300);
931 r500_render_draw(render, indices, count);
932 r300_end_stencil_ref_fallback(r300);
936 static void r300_render_destroy(struct vbuf_render* render)
941 static struct vbuf_render* r300_render_create(struct r300_context* r300)
943 struct r300_render* r300render = CALLOC_STRUCT(r300_render);
945 r300render->r300 = r300;
947 /* XXX find real numbers plz */
948 r300render->base.max_vertex_buffer_bytes = 128 * 1024;
949 r300render->base.max_indices = 16 * 1024;
951 r300render->base.get_vertex_info = r300_render_get_vertex_info;
952 r300render->base.allocate_vertices = r300_render_allocate_vertices;
953 r300render->base.map_vertices = r300_render_map_vertices;
954 r300render->base.unmap_vertices = r300_render_unmap_vertices;
955 r300render->base.set_primitive = r300_render_set_primitive;
956 if (r300->screen->caps.is_r500) {
957 r300render->base.draw = r500_render_draw;
958 r300render->base.draw_arrays = r500_render_draw_arrays;
960 r300render->base.draw = r300_render_draw;
961 r300render->base.draw_arrays = r300_render_draw_arrays;
963 r300render->base.release_vertices = r300_render_release_vertices;
964 r300render->base.destroy = r300_render_destroy;
966 r300render->vbo = NULL;
967 r300render->vbo_size = 0;
968 r300render->vbo_offset = 0;
970 return &r300render->base;
973 struct draw_stage* r300_draw_stage(struct r300_context* r300)
975 struct vbuf_render* render;
976 struct draw_stage* stage;
978 render = r300_render_create(r300);
984 stage = draw_vbuf_stage(r300->draw, render);
987 render->destroy(render);
991 draw_set_render(r300->draw, render);