r300_render.c revision e4e5acc833d607bdf5cdd728f8a8c5064ea38838
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 "pipe/p_inlines.h" 30 31#include "util/u_memory.h" 32#include "util/u_prim.h" 33 34#include "r300_cs.h" 35#include "r300_context.h" 36#include "r300_emit.h" 37#include "r300_reg.h" 38#include "r300_render.h" 39#include "r300_state_derived.h" 40 41/* r300_render: Vertex and index buffer primitive emission. */ 42#define R300_MAX_VBO_SIZE (1024 * 1024) 43 44uint32_t r300_translate_primitive(unsigned prim) 45{ 46 switch (prim) { 47 case PIPE_PRIM_POINTS: 48 return R300_VAP_VF_CNTL__PRIM_POINTS; 49 case PIPE_PRIM_LINES: 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; 61 case PIPE_PRIM_QUADS: 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; 67 default: 68 return 0; 69 } 70} 71 72static uint32_t r300_provoking_vertex_fixes(struct r300_context *r300, 73 unsigned mode) 74{ 75 struct r300_rs_state* rs = (struct r300_rs_state*)r300->rs_state.state; 76 uint32_t color_control = rs->color_control; 77 78 /* By default (see r300_state.c:r300_create_rs_state) color_control is 79 * initialized to provoking the first vertex. 80 * 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) 84 * 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. 89 * 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. 92 * 93 * ~ C. 94 */ 95 96 if (rs->rs.flatshade_first) { 97 switch (mode) { 98 case PIPE_PRIM_TRIANGLE_FAN: 99 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND; 100 break; 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; 105 break; 106 default: 107 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST; 108 break; 109 } 110 } else { 111 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST; 112 } 113 114 return color_control; 115} 116 117static void r300_emit_draw_immediate(struct r300_context *r300, 118 unsigned mode, 119 unsigned start, 120 unsigned count) 121{ 122 struct pipe_buffer* vbo = r300->vertex_buffer[0].buffer; 123 unsigned vertex_size = r300->vertex_buffer[0].stride / sizeof(float); 124 unsigned i; 125 uint32_t* map; 126 CS_LOCALS(r300); 127 128 map = (uint32_t*)pipe_buffer_map_range(r300->context.screen, vbo, 129 start * vertex_size, count * vertex_size, 130 PIPE_BUFFER_USAGE_CPU_READ); 131 132 BEGIN_CS(10 + count * vertex_size); 133 OUT_CS_REG(R300_GA_COLOR_CONTROL, 134 r300_provoking_vertex_fixes(r300, mode)); 135 OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size); 136 OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, 0); 137 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1); 138 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, count * vertex_size); 139 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (count << 16) | 140 r300_translate_primitive(mode)); 141 //debug_printf("r300: Immd %d verts, %d attrs\n", count, vertex_size); 142 for (i = 0; i < count * vertex_size; i++) { 143 if (i % vertex_size == 0) { 144 //debug_printf("r300: -- vert --\n"); 145 } 146 //debug_printf("r300: 0x%08x\n", *map); 147 OUT_CS(*map); 148 map++; 149 } 150 END_CS; 151 152 pipe_buffer_unmap(r300->context.screen, vbo); 153} 154 155static void r300_emit_draw_arrays(struct r300_context *r300, 156 unsigned mode, 157 unsigned count) 158{ 159 CS_LOCALS(r300); 160 161 BEGIN_CS(8); 162 OUT_CS_REG(R300_GA_COLOR_CONTROL, 163 r300_provoking_vertex_fixes(r300, mode)); 164 OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, 0); 165 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1); 166 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0); 167 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) | 168 r300_translate_primitive(mode)); 169 END_CS; 170} 171 172static void r300_emit_draw_elements(struct r300_context *r300, 173 struct pipe_buffer* indexBuffer, 174 unsigned indexSize, 175 unsigned minIndex, 176 unsigned maxIndex, 177 unsigned mode, 178 unsigned start, 179 unsigned count) 180{ 181 uint32_t count_dwords; 182 uint32_t offset_dwords = indexSize * start / sizeof(uint32_t); 183 CS_LOCALS(r300); 184 185 /* XXX most of these are stupid */ 186 assert(indexSize == 4 || indexSize == 2); 187 assert((start * indexSize) % 4 == 0); 188 assert(offset_dwords == 0); 189 190 BEGIN_CS(14); 191 OUT_CS_REG(R300_GA_COLOR_CONTROL, 192 r300_provoking_vertex_fixes(r300, mode)); 193 OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, minIndex); 194 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, maxIndex); 195 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0); 196 if (indexSize == 4) { 197 count_dwords = count + start; 198 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) | 199 R300_VAP_VF_CNTL__INDEX_SIZE_32bit | 200 r300_translate_primitive(mode)); 201 } else { 202 count_dwords = (count + start + 1) / 2; 203 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) | 204 r300_translate_primitive(mode)); 205 } 206 207 /* INDX_BUFFER is a truly special packet3. 208 * Unlike most other packet3, where the offset is after the count, 209 * the order is reversed, so the relocation ends up carrying the 210 * size of the indexbuf instead of the offset. 211 * 212 * XXX Fix offset 213 */ 214 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2); 215 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) | 216 (0 << R300_INDX_BUFFER_SKIP_SHIFT)); 217 OUT_CS(offset_dwords); 218 OUT_CS_RELOC(indexBuffer, count_dwords, 219 RADEON_GEM_DOMAIN_GTT, 0, 0); 220 221 END_CS; 222} 223 224 225static boolean r300_setup_vertex_buffers(struct r300_context *r300) 226{ 227 struct pipe_vertex_buffer *vbuf = r300->vertex_buffer; 228 struct pipe_vertex_element *velem = r300->vertex_element; 229 230validate: 231 for (int i = 0; i < r300->vertex_element_count; i++) { 232 if (!r300->winsys->add_buffer(r300->winsys, 233 vbuf[velem[i].vertex_buffer_index].buffer, 234 RADEON_GEM_DOMAIN_GTT, 0)) { 235 r300->context.flush(&r300->context, 0, NULL); 236 goto validate; 237 } 238 } 239 240 if (!r300->winsys->validate(r300->winsys)) { 241 r300->context.flush(&r300->context, 0, NULL); 242 return r300->winsys->validate(r300->winsys); 243 } 244 245 return TRUE; 246} 247 248/* This is the fast-path drawing & emission for HW TCL. */ 249void r300_draw_range_elements(struct pipe_context* pipe, 250 struct pipe_buffer* indexBuffer, 251 unsigned indexSize, 252 unsigned minIndex, 253 unsigned maxIndex, 254 unsigned mode, 255 unsigned start, 256 unsigned count) 257{ 258 struct r300_context* r300 = r300_context(pipe); 259 260 if (!u_trim_pipe_prim(mode, &count)) { 261 return; 262 } 263 264 if (count > 65535) { 265 /* XXX: use aux/indices functions to split this into smaller 266 * primitives. 267 */ 268 return; 269 } 270 271 r300_update_derived_state(r300); 272 273 if (!r300_setup_vertex_buffers(r300)) { 274 return; 275 } 276 277 if (!r300->winsys->add_buffer(r300->winsys, indexBuffer, 278 RADEON_GEM_DOMAIN_GTT, 0)) { 279 return; 280 } 281 282 if (!r300->winsys->validate(r300->winsys)) { 283 return; 284 } 285 286 r300_emit_dirty_state(r300); 287 288 r300_emit_aos(r300, 0); 289 290 r300_emit_draw_elements(r300, indexBuffer, indexSize, minIndex, maxIndex, 291 mode, start, count); 292} 293 294/* Simple helpers for context setup. Should probably be moved to util. */ 295void r300_draw_elements(struct pipe_context* pipe, 296 struct pipe_buffer* indexBuffer, 297 unsigned indexSize, unsigned mode, 298 unsigned start, unsigned count) 299{ 300 pipe->draw_range_elements(pipe, indexBuffer, indexSize, 0, ~0, 301 mode, start, count); 302} 303 304void r300_draw_arrays(struct pipe_context* pipe, unsigned mode, 305 unsigned start, unsigned count) 306{ 307 struct r300_context* r300 = r300_context(pipe); 308 309 if (!u_trim_pipe_prim(mode, &count)) { 310 return; 311 } 312 313 if (count > 65535) { 314 /* XXX: driver needs to handle this -- use the functions in 315 * aux/indices to split this into several smaller primitives. 316 */ 317 return; 318 } 319 320 r300_update_derived_state(r300); 321 322 if (!r300_setup_vertex_buffers(r300)) { 323 return; 324 } 325 326 r300_emit_dirty_state(r300); 327 328 if (count <= 4 && r300->vertex_buffer_count == 1) { 329 r300_emit_draw_immediate(r300, mode, start, count); 330 } else { 331 r300_emit_aos(r300, start); 332 r300_emit_draw_arrays(r300, mode, count); 333 } 334} 335 336/**************************************************************************** 337 * The rest of this file is for SW TCL rendering only. Please be polite and * 338 * keep these functions separated so that they are easier to locate. ~C. * 339 ***************************************************************************/ 340 341/* SW TCL arrays, using Draw. */ 342void r300_swtcl_draw_arrays(struct pipe_context* pipe, 343 unsigned mode, 344 unsigned start, 345 unsigned count) 346{ 347 struct r300_context* r300 = r300_context(pipe); 348 int i; 349 350 if (!u_trim_pipe_prim(mode, &count)) { 351 return; 352 } 353 354 for (i = 0; i < r300->vertex_buffer_count; i++) { 355 void* buf = pipe_buffer_map(pipe->screen, 356 r300->vertex_buffer[i].buffer, 357 PIPE_BUFFER_USAGE_CPU_READ); 358 draw_set_mapped_vertex_buffer(r300->draw, i, buf); 359 } 360 361 draw_set_mapped_element_buffer(r300->draw, 0, NULL); 362 363 draw_set_mapped_constant_buffer(r300->draw, 364 PIPE_SHADER_VERTEX, 365 r300->shader_constants[PIPE_SHADER_VERTEX].constants, 366 r300->shader_constants[PIPE_SHADER_VERTEX].count * 367 (sizeof(float) * 4)); 368 369 draw_arrays(r300->draw, mode, start, count); 370 371 for (i = 0; i < r300->vertex_buffer_count; i++) { 372 pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer); 373 draw_set_mapped_vertex_buffer(r300->draw, i, NULL); 374 } 375} 376 377/* SW TCL elements, using Draw. */ 378void r300_swtcl_draw_range_elements(struct pipe_context* pipe, 379 struct pipe_buffer* indexBuffer, 380 unsigned indexSize, 381 unsigned minIndex, 382 unsigned maxIndex, 383 unsigned mode, 384 unsigned start, 385 unsigned count) 386{ 387 struct r300_context* r300 = r300_context(pipe); 388 int i; 389 void* indices; 390 391 if (!u_trim_pipe_prim(mode, &count)) { 392 return; 393 } 394 395 for (i = 0; i < r300->vertex_buffer_count; i++) { 396 void* buf = pipe_buffer_map(pipe->screen, 397 r300->vertex_buffer[i].buffer, 398 PIPE_BUFFER_USAGE_CPU_READ); 399 draw_set_mapped_vertex_buffer(r300->draw, i, buf); 400 } 401 402 indices = pipe_buffer_map(pipe->screen, indexBuffer, 403 PIPE_BUFFER_USAGE_CPU_READ); 404 draw_set_mapped_element_buffer_range(r300->draw, indexSize, 405 minIndex, maxIndex, indices); 406 407 draw_set_mapped_constant_buffer(r300->draw, 408 PIPE_SHADER_VERTEX, 409 r300->shader_constants[PIPE_SHADER_VERTEX].constants, 410 r300->shader_constants[PIPE_SHADER_VERTEX].count * 411 (sizeof(float) * 4)); 412 413 draw_arrays(r300->draw, mode, start, count); 414 415 for (i = 0; i < r300->vertex_buffer_count; i++) { 416 pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer); 417 draw_set_mapped_vertex_buffer(r300->draw, i, NULL); 418 } 419 420 pipe_buffer_unmap(pipe->screen, indexBuffer); 421 draw_set_mapped_element_buffer_range(r300->draw, 0, start, 422 start + count - 1, NULL); 423} 424 425/* Object for rendering using Draw. */ 426struct r300_render { 427 /* Parent class */ 428 struct vbuf_render base; 429 430 /* Pipe context */ 431 struct r300_context* r300; 432 433 /* Vertex information */ 434 size_t vertex_size; 435 unsigned prim; 436 unsigned hwprim; 437 438 /* VBO */ 439 struct pipe_buffer* vbo; 440 size_t vbo_size; 441 size_t vbo_offset; 442 size_t vbo_max_used; 443 void * vbo_ptr; 444}; 445 446static INLINE struct r300_render* 447r300_render(struct vbuf_render* render) 448{ 449 return (struct r300_render*)render; 450} 451 452static const struct vertex_info* 453r300_render_get_vertex_info(struct vbuf_render* render) 454{ 455 struct r300_render* r300render = r300_render(render); 456 struct r300_context* r300 = r300render->r300; 457 458 r300_update_derived_state(r300); 459 460 return &r300->vertex_info->vinfo; 461} 462 463static boolean r300_render_allocate_vertices(struct vbuf_render* render, 464 ushort vertex_size, 465 ushort count) 466{ 467 struct r300_render* r300render = r300_render(render); 468 struct r300_context* r300 = r300render->r300; 469 struct pipe_screen* screen = r300->context.screen; 470 size_t size = (size_t)vertex_size * (size_t)count; 471 472 if (size + r300render->vbo_offset > r300render->vbo_size) 473 { 474 pipe_buffer_reference(&r300->vbo, NULL); 475 r300render->vbo = pipe_buffer_create(screen, 476 64, 477 PIPE_BUFFER_USAGE_VERTEX, 478 R300_MAX_VBO_SIZE); 479 r300render->vbo_offset = 0; 480 r300render->vbo_size = R300_MAX_VBO_SIZE; 481 } 482 483 r300render->vertex_size = vertex_size; 484 r300->vbo = r300render->vbo; 485 r300->vbo_offset = r300render->vbo_offset; 486 487 return (r300render->vbo) ? TRUE : FALSE; 488} 489 490static void* r300_render_map_vertices(struct vbuf_render* render) 491{ 492 struct r300_render* r300render = r300_render(render); 493 struct pipe_screen* screen = r300render->r300->context.screen; 494 495 r300render->vbo_ptr = pipe_buffer_map(screen, r300render->vbo, 496 PIPE_BUFFER_USAGE_CPU_WRITE); 497 498 return ((uint8_t*)r300render->vbo_ptr + r300render->vbo_offset); 499} 500 501static void r300_render_unmap_vertices(struct vbuf_render* render, 502 ushort min, 503 ushort max) 504{ 505 struct r300_render* r300render = r300_render(render); 506 struct pipe_screen* screen = r300render->r300->context.screen; 507 CS_LOCALS(r300render->r300); 508 BEGIN_CS(2); 509 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max); 510 END_CS; 511 512 r300render->vbo_max_used = MAX2(r300render->vbo_max_used, 513 r300render->vertex_size * (max + 1)); 514 pipe_buffer_unmap(screen, r300render->vbo); 515} 516 517static void r300_render_release_vertices(struct vbuf_render* render) 518{ 519 struct r300_render* r300render = r300_render(render); 520 521 r300render->vbo_offset += r300render->vbo_max_used; 522 r300render->vbo_max_used = 0; 523} 524 525static boolean r300_render_set_primitive(struct vbuf_render* render, 526 unsigned prim) 527{ 528 struct r300_render* r300render = r300_render(render); 529 530 r300render->prim = prim; 531 r300render->hwprim = r300_translate_primitive(prim); 532 533 return TRUE; 534} 535 536static void r300_render_draw_arrays(struct vbuf_render* render, 537 unsigned start, 538 unsigned count) 539{ 540 struct r300_render* r300render = r300_render(render); 541 struct r300_context* r300 = r300render->r300; 542 543 CS_LOCALS(r300); 544 545 r300_emit_dirty_state(r300); 546 547 DBG(r300, DBG_DRAW, "r300: Doing vbuf render, count %d\n", count); 548 549 BEGIN_CS(2); 550 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0); 551 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) | 552 r300render->hwprim); 553 END_CS; 554} 555 556static void r300_render_draw(struct vbuf_render* render, 557 const ushort* indices, 558 uint count) 559{ 560 struct r300_render* r300render = r300_render(render); 561 struct r300_context* r300 = r300render->r300; 562 int i; 563 564 CS_LOCALS(r300); 565 566 r300_emit_dirty_state(r300); 567 568 BEGIN_CS(2 + (count+1)/2); 569 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (count+1)/2); 570 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) | 571 r300render->hwprim); 572 for (i = 0; i < count-1; i += 2) { 573 OUT_CS(indices[i+1] << 16 | indices[i]); 574 } 575 if (count % 2) { 576 OUT_CS(indices[count-1]); 577 } 578 END_CS; 579} 580 581static void r300_render_destroy(struct vbuf_render* render) 582{ 583 FREE(render); 584} 585 586static struct vbuf_render* r300_render_create(struct r300_context* r300) 587{ 588 struct r300_render* r300render = CALLOC_STRUCT(r300_render); 589 590 r300render->r300 = r300; 591 592 /* XXX find real numbers plz */ 593 r300render->base.max_vertex_buffer_bytes = 128 * 1024; 594 r300render->base.max_indices = 16 * 1024; 595 596 r300render->base.get_vertex_info = r300_render_get_vertex_info; 597 r300render->base.allocate_vertices = r300_render_allocate_vertices; 598 r300render->base.map_vertices = r300_render_map_vertices; 599 r300render->base.unmap_vertices = r300_render_unmap_vertices; 600 r300render->base.set_primitive = r300_render_set_primitive; 601 r300render->base.draw = r300_render_draw; 602 r300render->base.draw_arrays = r300_render_draw_arrays; 603 r300render->base.release_vertices = r300_render_release_vertices; 604 r300render->base.destroy = r300_render_destroy; 605 606 r300render->vbo = NULL; 607 r300render->vbo_size = 0; 608 r300render->vbo_offset = 0; 609 610 return &r300render->base; 611} 612 613struct draw_stage* r300_draw_stage(struct r300_context* r300) 614{ 615 struct vbuf_render* render; 616 struct draw_stage* stage; 617 618 render = r300_render_create(r300); 619 620 if (!render) { 621 return NULL; 622 } 623 624 stage = draw_vbuf_stage(r300->draw, render); 625 626 if (!stage) { 627 render->destroy(render); 628 return NULL; 629 } 630 631 draw_set_render(r300->draw, render); 632 633 return stage; 634} 635