r300_render.c revision 902ccfcb40f21e1a5fca2f1bec1cbbabb053d8cf
1/* 2 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com> 3 * 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: 10 * 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 13 * Software. 14 * 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. */ 22 23/* r300_render: Vertex and index buffer primitive emission. Contains both 24 * HW TCL fastpath rendering, and SW TCL Draw-assisted rendering. */ 25 26#include "draw/draw_context.h" 27#include "draw/draw_vbuf.h" 28 29#include "util/u_inlines.h" 30 31#include "util/u_format.h" 32#include "util/u_memory.h" 33#include "util/u_prim.h" 34 35#include "r300_cs.h" 36#include "r300_context.h" 37#include "r300_emit.h" 38#include "r300_reg.h" 39#include "r300_render.h" 40#include "r300_state_derived.h" 41 42/* r300_render: Vertex and index buffer primitive emission. */ 43#define R300_MAX_VBO_SIZE (1024 * 1024) 44 45/* XXX The DRM rejects VAP_ALT_NUM_VERTICES.. */ 46//#define ENABLE_ALT_NUM_VERTS 47 48uint32_t r300_translate_primitive(unsigned prim) 49{ 50 switch (prim) { 51 case PIPE_PRIM_POINTS: 52 return R300_VAP_VF_CNTL__PRIM_POINTS; 53 case PIPE_PRIM_LINES: 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; 65 case PIPE_PRIM_QUADS: 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; 71 default: 72 return 0; 73 } 74} 75 76static uint32_t r300_provoking_vertex_fixes(struct r300_context *r300, 77 unsigned mode) 78{ 79 struct r300_rs_state* rs = (struct r300_rs_state*)r300->rs_state.state; 80 uint32_t color_control = rs->color_control; 81 82 /* By default (see r300_state.c:r300_create_rs_state) color_control is 83 * initialized to provoking the first vertex. 84 * 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) 88 * 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. 93 * 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. 96 * 97 * ~ C. 98 */ 99 100 if (rs->rs.flatshade_first) { 101 switch (mode) { 102 case PIPE_PRIM_TRIANGLE_FAN: 103 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND; 104 break; 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; 109 break; 110 default: 111 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST; 112 break; 113 } 114 } else { 115 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST; 116 } 117 118 return color_control; 119} 120 121static boolean immd_is_good_idea(struct r300_context *r300, 122 unsigned count) 123{ 124 return count <= 4; 125} 126 127static void r300_emit_draw_arrays_immediate(struct r300_context *r300, 128 unsigned mode, 129 unsigned start, 130 unsigned count) 131{ 132 struct pipe_vertex_element* velem; 133 struct pipe_vertex_buffer* vbuf; 134 unsigned vertex_element_count = r300->vertex_element_count; 135 unsigned i, v, vbi, dw, elem_offset; 136 137 /* Size of the vertex, in dwords. */ 138 unsigned vertex_size = 0; 139 140 /* Offsets of the attribute, in dwords, from the start of the vertex. */ 141 unsigned offset[PIPE_MAX_ATTRIBS]; 142 143 /* Size of the vertex element, in dwords. */ 144 unsigned size[PIPE_MAX_ATTRIBS]; 145 146 /* Stride to the same attrib in the next vertex in the vertex buffer, 147 * in dwords. */ 148 unsigned stride[PIPE_MAX_ATTRIBS] = {0}; 149 150 /* Mapped vertex buffers. */ 151 uint32_t* map[PIPE_MAX_ATTRIBS] = {0}; 152 153 CS_LOCALS(r300); 154 155 /* Calculate the vertex size, offsets, strides etc. and map the buffers. */ 156 for (i = 0; i < vertex_element_count; i++) { 157 velem = &r300->vertex_element[i]; 158 offset[i] = velem->src_offset / 4; 159 size[i] = util_format_get_blocksize(velem->src_format) / 4; 160 vertex_size += size[i]; 161 vbi = velem->vertex_buffer_index; 162 163 /* Map the buffer. */ 164 if (!map[vbi]) { 165 vbuf = &r300->vertex_buffer[vbi]; 166 map[vbi] = (uint32_t*)pipe_buffer_map(r300->context.screen, 167 vbuf->buffer, 168 PIPE_BUFFER_USAGE_CPU_READ); 169 map[vbi] += vbuf->buffer_offset / 4; 170 stride[vbi] = vbuf->stride / 4; 171 } 172 } 173 174 r300_emit_dirty_state(r300); 175 176 BEGIN_CS(10 + count * vertex_size); 177 OUT_CS_REG(R300_GA_COLOR_CONTROL, 178 r300_provoking_vertex_fixes(r300, mode)); 179 OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size); 180 OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, 0); 181 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1); 182 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, count * vertex_size); 183 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (count << 16) | 184 r300_translate_primitive(mode)); 185 186 /* Emit vertices. */ 187 for (v = 0; v < count; v++) { 188 for (i = 0; i < vertex_element_count; i++) { 189 velem = &r300->vertex_element[i]; 190 vbi = velem->vertex_buffer_index; 191 elem_offset = offset[i] + stride[vbi] * (v + start); 192 193 for (dw = 0; dw < size[i]; dw++) { 194 OUT_CS(map[vbi][elem_offset + dw]); 195 } 196 } 197 } 198 END_CS; 199 200 /* Unmap buffers. */ 201 for (i = 0; i < vertex_element_count; i++) { 202 vbi = r300->vertex_element[i].vertex_buffer_index; 203 204 if (map[vbi]) { 205 vbuf = &r300->vertex_buffer[vbi]; 206 pipe_buffer_unmap(r300->context.screen, vbuf->buffer); 207 map[vbi] = NULL; 208 } 209 } 210} 211 212static void r300_emit_draw_arrays(struct r300_context *r300, 213 unsigned mode, 214 unsigned count) 215{ 216#if defined(ENABLE_ALT_NUM_VERTS) 217 boolean alt_num_verts = count > 65535; 218#else 219 boolean alt_num_verts = FALSE; 220#endif 221 CS_LOCALS(r300); 222 223 if (alt_num_verts) { 224 assert(count < (1 << 24)); 225 BEGIN_CS(10); 226 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count); 227 } else { 228 BEGIN_CS(8); 229 } 230 OUT_CS_REG(R300_GA_COLOR_CONTROL, 231 r300_provoking_vertex_fixes(r300, mode)); 232 OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, 0); 233 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1); 234 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0); 235 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) | 236 r300_translate_primitive(mode) | 237 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0)); 238 END_CS; 239} 240 241static void r300_emit_draw_elements(struct r300_context *r300, 242 struct pipe_buffer* indexBuffer, 243 unsigned indexSize, 244 unsigned minIndex, 245 unsigned maxIndex, 246 unsigned mode, 247 unsigned start, 248 unsigned count) 249{ 250 uint32_t count_dwords; 251 uint32_t offset_dwords = indexSize * start / sizeof(uint32_t); 252#if defined(ENABLE_ALT_NUM_VERTS) 253 boolean alt_num_verts = count > 65535; 254#else 255 boolean alt_num_verts = FALSE; 256#endif 257 CS_LOCALS(r300); 258 259 assert((start * indexSize) % 4 == 0); 260 261 if (alt_num_verts) { 262 assert(count < (1 << 24)); 263 BEGIN_CS(16); 264 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count); 265 } else { 266 BEGIN_CS(14); 267 } 268 OUT_CS_REG(R300_GA_COLOR_CONTROL, 269 r300_provoking_vertex_fixes(r300, mode)); 270 OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, minIndex); 271 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, maxIndex); 272 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0); 273 if (indexSize == 4) { 274 count_dwords = count; 275 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) | 276 R300_VAP_VF_CNTL__INDEX_SIZE_32bit | 277 r300_translate_primitive(mode) | 278 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0)); 279 } else { 280 count_dwords = (count + 1) / 2; 281 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) | 282 r300_translate_primitive(mode) | 283 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0)); 284 } 285 286 /* INDX_BUFFER is a truly special packet3. 287 * Unlike most other packet3, where the offset is after the count, 288 * the order is reversed, so the relocation ends up carrying the 289 * size of the indexbuf instead of the offset. 290 */ 291 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2); 292 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) | 293 (0 << R300_INDX_BUFFER_SKIP_SHIFT)); 294 OUT_CS(offset_dwords << 2); 295 OUT_CS_RELOC(indexBuffer, count_dwords, 296 RADEON_GEM_DOMAIN_GTT, 0, 0); 297 298 END_CS; 299} 300 301static boolean r300_setup_vertex_buffers(struct r300_context *r300) 302{ 303 struct pipe_vertex_buffer *vbuf = r300->vertex_buffer; 304 struct pipe_vertex_element *velem = r300->vertex_element; 305 struct pipe_buffer *pbuf; 306 307validate: 308 for (int i = 0; i < r300->vertex_element_count; i++) { 309 pbuf = vbuf[velem[i].vertex_buffer_index].buffer; 310 311 if (!r300->winsys->add_buffer(r300->winsys, pbuf, 312 RADEON_GEM_DOMAIN_GTT, 0)) { 313 r300->context.flush(&r300->context, 0, NULL); 314 goto validate; 315 } 316 } 317 318 if (!r300->winsys->validate(r300->winsys)) { 319 r300->context.flush(&r300->context, 0, NULL); 320 return r300->winsys->validate(r300->winsys); 321 } 322 323 return TRUE; 324} 325 326static void r300_shorten_ubyte_elts(struct r300_context* r300, 327 struct pipe_buffer** elts, 328 unsigned count) 329{ 330 struct pipe_screen* screen = r300->context.screen; 331 struct pipe_buffer* new_elts; 332 unsigned char *in_map; 333 unsigned short *out_map; 334 unsigned i; 335 336 new_elts = screen->buffer_create(screen, 32, 337 PIPE_BUFFER_USAGE_INDEX | 338 PIPE_BUFFER_USAGE_CPU_WRITE | 339 PIPE_BUFFER_USAGE_GPU_READ, 340 2 * count); 341 342 in_map = pipe_buffer_map(screen, *elts, PIPE_BUFFER_USAGE_CPU_READ); 343 out_map = pipe_buffer_map(screen, new_elts, PIPE_BUFFER_USAGE_CPU_WRITE); 344 345 for (i = 0; i < count; i++) { 346 *out_map = (unsigned short)*in_map; 347 in_map++; 348 out_map++; 349 } 350 351 pipe_buffer_unmap(screen, *elts); 352 pipe_buffer_unmap(screen, new_elts); 353 354 *elts = new_elts; 355} 356 357/* This is the fast-path drawing & emission for HW TCL. */ 358void r300_draw_range_elements(struct pipe_context* pipe, 359 struct pipe_buffer* indexBuffer, 360 unsigned indexSize, 361 unsigned minIndex, 362 unsigned maxIndex, 363 unsigned mode, 364 unsigned start, 365 unsigned count) 366{ 367 struct r300_context* r300 = r300_context(pipe); 368 struct pipe_buffer* orgIndexBuffer = indexBuffer; 369#if defined(ENABLE_ALT_NUM_VERTS) 370 boolean alt_num_verts = r300_screen(pipe->screen)->caps->is_r500 && 371 count > 65536; 372#else 373 boolean alt_num_verts = FALSE; 374#endif 375 unsigned short_count; 376 377 if (!u_trim_pipe_prim(mode, &count)) { 378 return; 379 } 380 381 r300_update_derived_state(r300); 382 383 r300_emit_buffer_validate(r300); 384 385 if (!r300_setup_vertex_buffers(r300)) { 386 return; 387 } 388 389 if (indexSize == 1) { 390 r300_shorten_ubyte_elts(r300, &indexBuffer, count); 391 indexSize = 2; 392 } 393 394 if (!r300->winsys->add_buffer(r300->winsys, indexBuffer, 395 RADEON_GEM_DOMAIN_GTT, 0)) { 396 goto cleanup; 397 } 398 399 if (!r300->winsys->validate(r300->winsys)) { 400 goto cleanup; 401 } 402 403 r300_emit_dirty_state(r300); 404 405 r300_emit_aos(r300, 0); 406 407 if (alt_num_verts || count <= 65535) { 408 r300_emit_draw_elements(r300, indexBuffer, indexSize, minIndex, 409 maxIndex, mode, start, count); 410 } else { 411 do { 412 short_count = MIN2(count, 65534); 413 r300_emit_draw_elements(r300, indexBuffer, indexSize, minIndex, 414 maxIndex, mode, start, short_count); 415 416 start += short_count; 417 count -= short_count; 418 } while (count); 419 } 420 421cleanup: 422 if (indexBuffer != orgIndexBuffer) { 423 pipe->screen->buffer_destroy(indexBuffer); 424 } 425} 426 427/* Simple helpers for context setup. Should probably be moved to util. */ 428void r300_draw_elements(struct pipe_context* pipe, 429 struct pipe_buffer* indexBuffer, 430 unsigned indexSize, unsigned mode, 431 unsigned start, unsigned count) 432{ 433 pipe->draw_range_elements(pipe, indexBuffer, indexSize, 0, ~0, 434 mode, start, count); 435} 436 437void r300_draw_arrays(struct pipe_context* pipe, unsigned mode, 438 unsigned start, unsigned count) 439{ 440 struct r300_context* r300 = r300_context(pipe); 441#if defined(ENABLE_ALT_NUM_VERTS) 442 boolean alt_num_verts = r300_screen(pipe->screen)->caps->is_r500 && 443 count > 65536; 444#else 445 boolean alt_num_verts = FALSE; 446#endif 447 unsigned short_count; 448 449 if (!u_trim_pipe_prim(mode, &count)) { 450 return; 451 } 452 453 r300_update_derived_state(r300); 454 455 r300_emit_buffer_validate(r300); 456 457 if (immd_is_good_idea(r300, count)) { 458 r300_emit_draw_arrays_immediate(r300, mode, start, count); 459 } else { 460 if (!r300_setup_vertex_buffers(r300)) { 461 return; 462 } 463 464 r300_emit_dirty_state(r300); 465 466 if (alt_num_verts || count <= 65535) { 467 r300_emit_aos(r300, start); 468 r300_emit_draw_arrays(r300, mode, count); 469 } else { 470 do { 471 short_count = MIN2(count, 65535); 472 r300_emit_aos(r300, start); 473 r300_emit_draw_arrays(r300, mode, short_count); 474 475 start += short_count; 476 count -= short_count; 477 } while (count); 478 } 479 } 480} 481 482/**************************************************************************** 483 * The rest of this file is for SW TCL rendering only. Please be polite and * 484 * keep these functions separated so that they are easier to locate. ~C. * 485 ***************************************************************************/ 486 487/* SW TCL arrays, using Draw. */ 488void r300_swtcl_draw_arrays(struct pipe_context* pipe, 489 unsigned mode, 490 unsigned start, 491 unsigned count) 492{ 493 struct r300_context* r300 = r300_context(pipe); 494 int i; 495 496 if (!u_trim_pipe_prim(mode, &count)) { 497 return; 498 } 499 500 for (i = 0; i < r300->vertex_buffer_count; i++) { 501 void* buf = pipe_buffer_map(pipe->screen, 502 r300->vertex_buffer[i].buffer, 503 PIPE_BUFFER_USAGE_CPU_READ); 504 draw_set_mapped_vertex_buffer(r300->draw, i, buf); 505 } 506 507 draw_set_mapped_element_buffer(r300->draw, 0, NULL); 508 509 draw_set_mapped_constant_buffer(r300->draw, 510 PIPE_SHADER_VERTEX, 511 0, 512 r300->shader_constants[PIPE_SHADER_VERTEX].constants, 513 r300->shader_constants[PIPE_SHADER_VERTEX].count * 514 (sizeof(float) * 4)); 515 516 draw_arrays(r300->draw, mode, start, count); 517 518 for (i = 0; i < r300->vertex_buffer_count; i++) { 519 pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer); 520 draw_set_mapped_vertex_buffer(r300->draw, i, NULL); 521 } 522} 523 524/* SW TCL elements, using Draw. */ 525void r300_swtcl_draw_range_elements(struct pipe_context* pipe, 526 struct pipe_buffer* indexBuffer, 527 unsigned indexSize, 528 unsigned minIndex, 529 unsigned maxIndex, 530 unsigned mode, 531 unsigned start, 532 unsigned count) 533{ 534 struct r300_context* r300 = r300_context(pipe); 535 int i; 536 void* indices; 537 538 if (!u_trim_pipe_prim(mode, &count)) { 539 return; 540 } 541 542 for (i = 0; i < r300->vertex_buffer_count; i++) { 543 void* buf = pipe_buffer_map(pipe->screen, 544 r300->vertex_buffer[i].buffer, 545 PIPE_BUFFER_USAGE_CPU_READ); 546 draw_set_mapped_vertex_buffer(r300->draw, i, buf); 547 } 548 549 indices = pipe_buffer_map(pipe->screen, indexBuffer, 550 PIPE_BUFFER_USAGE_CPU_READ); 551 draw_set_mapped_element_buffer_range(r300->draw, indexSize, 552 minIndex, maxIndex, indices); 553 554 draw_set_mapped_constant_buffer(r300->draw, 555 PIPE_SHADER_VERTEX, 556 0, 557 r300->shader_constants[PIPE_SHADER_VERTEX].constants, 558 r300->shader_constants[PIPE_SHADER_VERTEX].count * 559 (sizeof(float) * 4)); 560 561 draw_arrays(r300->draw, mode, start, count); 562 563 for (i = 0; i < r300->vertex_buffer_count; i++) { 564 pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer); 565 draw_set_mapped_vertex_buffer(r300->draw, i, NULL); 566 } 567 568 pipe_buffer_unmap(pipe->screen, indexBuffer); 569 draw_set_mapped_element_buffer_range(r300->draw, 0, start, 570 start + count - 1, NULL); 571} 572 573/* Object for rendering using Draw. */ 574struct r300_render { 575 /* Parent class */ 576 struct vbuf_render base; 577 578 /* Pipe context */ 579 struct r300_context* r300; 580 581 /* Vertex information */ 582 size_t vertex_size; 583 unsigned prim; 584 unsigned hwprim; 585 586 /* VBO */ 587 struct pipe_buffer* vbo; 588 size_t vbo_size; 589 size_t vbo_offset; 590 size_t vbo_max_used; 591 void * vbo_ptr; 592}; 593 594static INLINE struct r300_render* 595r300_render(struct vbuf_render* render) 596{ 597 return (struct r300_render*)render; 598} 599 600static const struct vertex_info* 601r300_render_get_vertex_info(struct vbuf_render* render) 602{ 603 struct r300_render* r300render = r300_render(render); 604 struct r300_context* r300 = r300render->r300; 605 606 r300_update_derived_state(r300); 607 608 return (struct vertex_info*)r300->vertex_format_state.state; 609} 610 611static boolean r300_render_allocate_vertices(struct vbuf_render* render, 612 ushort vertex_size, 613 ushort count) 614{ 615 struct r300_render* r300render = r300_render(render); 616 struct r300_context* r300 = r300render->r300; 617 struct pipe_screen* screen = r300->context.screen; 618 size_t size = (size_t)vertex_size * (size_t)count; 619 620 if (size + r300render->vbo_offset > r300render->vbo_size) 621 { 622 pipe_buffer_reference(&r300->vbo, NULL); 623 r300render->vbo = pipe_buffer_create(screen, 624 64, 625 PIPE_BUFFER_USAGE_VERTEX, 626 R300_MAX_VBO_SIZE); 627 r300render->vbo_offset = 0; 628 r300render->vbo_size = R300_MAX_VBO_SIZE; 629 } 630 631 r300render->vertex_size = vertex_size; 632 r300->vbo = r300render->vbo; 633 r300->vbo_offset = r300render->vbo_offset; 634 635 return (r300render->vbo) ? TRUE : FALSE; 636} 637 638static void* r300_render_map_vertices(struct vbuf_render* render) 639{ 640 struct r300_render* r300render = r300_render(render); 641 struct pipe_screen* screen = r300render->r300->context.screen; 642 643 r300render->vbo_ptr = pipe_buffer_map(screen, r300render->vbo, 644 PIPE_BUFFER_USAGE_CPU_WRITE); 645 646 return ((uint8_t*)r300render->vbo_ptr + r300render->vbo_offset); 647} 648 649static void r300_render_unmap_vertices(struct vbuf_render* render, 650 ushort min, 651 ushort max) 652{ 653 struct r300_render* r300render = r300_render(render); 654 struct pipe_screen* screen = r300render->r300->context.screen; 655 CS_LOCALS(r300render->r300); 656 BEGIN_CS(2); 657 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max); 658 END_CS; 659 660 r300render->vbo_max_used = MAX2(r300render->vbo_max_used, 661 r300render->vertex_size * (max + 1)); 662 pipe_buffer_unmap(screen, r300render->vbo); 663} 664 665static void r300_render_release_vertices(struct vbuf_render* render) 666{ 667 struct r300_render* r300render = r300_render(render); 668 669 r300render->vbo_offset += r300render->vbo_max_used; 670 r300render->vbo_max_used = 0; 671} 672 673static boolean r300_render_set_primitive(struct vbuf_render* render, 674 unsigned prim) 675{ 676 struct r300_render* r300render = r300_render(render); 677 678 r300render->prim = prim; 679 r300render->hwprim = r300_translate_primitive(prim); 680 681 return TRUE; 682} 683 684static void r300_render_draw_arrays(struct vbuf_render* render, 685 unsigned start, 686 unsigned count) 687{ 688 struct r300_render* r300render = r300_render(render); 689 struct r300_context* r300 = r300render->r300; 690 691 CS_LOCALS(r300); 692 693 r300_emit_dirty_state(r300); 694 695 DBG(r300, DBG_DRAW, "r300: Doing vbuf render, count %d\n", count); 696 697 BEGIN_CS(2); 698 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0); 699 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) | 700 r300render->hwprim); 701 END_CS; 702} 703 704static void r300_render_draw(struct vbuf_render* render, 705 const ushort* indices, 706 uint count) 707{ 708 struct r300_render* r300render = r300_render(render); 709 struct r300_context* r300 = r300render->r300; 710 int i; 711 712 CS_LOCALS(r300); 713 714 r300_emit_dirty_state(r300); 715 716 BEGIN_CS(2 + (count+1)/2); 717 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (count+1)/2); 718 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) | 719 r300render->hwprim); 720 for (i = 0; i < count-1; i += 2) { 721 OUT_CS(indices[i+1] << 16 | indices[i]); 722 } 723 if (count % 2) { 724 OUT_CS(indices[count-1]); 725 } 726 END_CS; 727} 728 729static void r300_render_destroy(struct vbuf_render* render) 730{ 731 FREE(render); 732} 733 734static struct vbuf_render* r300_render_create(struct r300_context* r300) 735{ 736 struct r300_render* r300render = CALLOC_STRUCT(r300_render); 737 738 r300render->r300 = r300; 739 740 /* XXX find real numbers plz */ 741 r300render->base.max_vertex_buffer_bytes = 128 * 1024; 742 r300render->base.max_indices = 16 * 1024; 743 744 r300render->base.get_vertex_info = r300_render_get_vertex_info; 745 r300render->base.allocate_vertices = r300_render_allocate_vertices; 746 r300render->base.map_vertices = r300_render_map_vertices; 747 r300render->base.unmap_vertices = r300_render_unmap_vertices; 748 r300render->base.set_primitive = r300_render_set_primitive; 749 r300render->base.draw = r300_render_draw; 750 r300render->base.draw_arrays = r300_render_draw_arrays; 751 r300render->base.release_vertices = r300_render_release_vertices; 752 r300render->base.destroy = r300_render_destroy; 753 754 r300render->vbo = NULL; 755 r300render->vbo_size = 0; 756 r300render->vbo_offset = 0; 757 758 return &r300render->base; 759} 760 761struct draw_stage* r300_draw_stage(struct r300_context* r300) 762{ 763 struct vbuf_render* render; 764 struct draw_stage* stage; 765 766 render = r300_render_create(r300); 767 768 if (!render) { 769 return NULL; 770 } 771 772 stage = draw_vbuf_stage(r300->draw, render); 773 774 if (!stage) { 775 render->destroy(render); 776 return NULL; 777 } 778 779 draw_set_render(r300->draw, render); 780 781 return stage; 782} 783