r300_render.c revision 1f7f9bab8139681e1dcbc6c10fb42965059d1395
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#include "draw/draw_context.h" 24#include "draw/draw_vbuf.h" 25 26#include "pipe/p_inlines.h" 27 28#include "util/u_memory.h" 29 30#include "r300_cs.h" 31#include "r300_context.h" 32#include "r300_emit.h" 33#include "r300_reg.h" 34#include "r300_render.h" 35#include "r300_state_derived.h" 36 37/* r300_render: Vertex and index buffer primitive emission. */ 38#define R300_MAX_VBO_SIZE (1024 * 1024) 39 40static uint32_t r300_translate_primitive(unsigned prim) 41{ 42 switch (prim) { 43 case PIPE_PRIM_POINTS: 44 return R300_VAP_VF_CNTL__PRIM_POINTS; 45 case PIPE_PRIM_LINES: 46 return R300_VAP_VF_CNTL__PRIM_LINES; 47 case PIPE_PRIM_LINE_LOOP: 48 return R300_VAP_VF_CNTL__PRIM_LINE_LOOP; 49 case PIPE_PRIM_LINE_STRIP: 50 return R300_VAP_VF_CNTL__PRIM_LINE_STRIP; 51 case PIPE_PRIM_TRIANGLES: 52 return R300_VAP_VF_CNTL__PRIM_TRIANGLES; 53 case PIPE_PRIM_TRIANGLE_STRIP: 54 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP; 55 case PIPE_PRIM_TRIANGLE_FAN: 56 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN; 57 case PIPE_PRIM_QUADS: 58 return R300_VAP_VF_CNTL__PRIM_QUADS; 59 case PIPE_PRIM_QUAD_STRIP: 60 return R300_VAP_VF_CNTL__PRIM_QUAD_STRIP; 61 case PIPE_PRIM_POLYGON: 62 return R300_VAP_VF_CNTL__PRIM_POLYGON; 63 default: 64 return 0; 65 } 66} 67 68/* This is the fast-path drawing & emission for HW TCL. */ 69boolean r300_draw_range_elements(struct pipe_context* pipe, 70 struct pipe_buffer* indexBuffer, 71 unsigned indexSize, 72 unsigned minIndex, 73 unsigned maxIndex, 74 unsigned mode, 75 unsigned start, 76 unsigned count) 77{ 78 struct r300_context* r300 = r300_context(pipe); 79 CS_LOCALS(r300); 80 uint32_t prim = r300_translate_primitive(mode); 81 struct pipe_vertex_buffer* aos = r300->vertex_buffers; 82 unsigned aos_count = r300->vertex_buffer_count; 83 short* indices; 84 unsigned packet_size; 85 unsigned i; 86 bool invalid = FALSE; 87 88validate: 89 for (i = 0; i < aos_count; i++) { 90 if (!r300->winsys->add_buffer(r300->winsys, aos[i].buffer, 91 RADEON_GEM_DOMAIN_GTT, 0)) { 92 pipe->flush(pipe, 0, NULL); 93 goto validate; 94 } 95 } 96 if (!r300->winsys->validate(r300->winsys)) { 97 pipe->flush(pipe, 0, NULL); 98 if (invalid) { 99 /* Well, hell. */ 100 debug_printf("r300: Stuck in validation loop, gonna quit now."); 101 exit(1); 102 } 103 invalid = TRUE; 104 goto validate; 105 } 106 107 r300_emit_dirty_state(r300); 108 109 packet_size = (aos_count >> 1) * 3 + (aos_count & 1) * 2; 110 111 BEGIN_CS(3 + packet_size + (aos_count * 2)); 112 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR, packet_size); 113 OUT_CS(aos_count); 114 for (i = 0; i < aos_count - 1; i += 2) { 115 OUT_CS(aos[i].stride | 116 (aos[i].stride << 8) | 117 (aos[i + 1].stride << 16) | 118 (aos[i + 1].stride << 24)); 119 OUT_CS(aos[i].buffer_offset + start * 4 * aos[i].stride); 120 OUT_CS(aos[i + 1].buffer_offset + start * 4 * aos[i + 1].stride); 121 } 122 if (aos_count & 1) { 123 OUT_CS(aos[i].stride | (aos[i].stride << 8)); 124 OUT_CS(aos[i].buffer_offset + start * 4 * aos[i].stride); 125 } 126 for (i = 0; i < aos_count; i++) { 127 OUT_CS_RELOC(aos[i].buffer, 0, RADEON_GEM_DOMAIN_GTT, 0, 0); 128 } 129 END_CS; 130 131 if (indexBuffer) { 132 indices = (short*)pipe_buffer_map(pipe->screen, indexBuffer, 133 PIPE_BUFFER_USAGE_CPU_READ); 134 135 /* Set the starting point. */ 136 indices += start; 137 138 BEGIN_CS(2 + (count+1)/2); 139 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (count + 1)/2); 140 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) | prim); 141 for (i = 0; i < count - 1; i += 2) { 142 OUT_CS(indices[i + 1] << 16 | indices[i]); 143 } 144 if (count % 2) { 145 OUT_CS(indices[count - 1]); 146 } 147 END_CS; 148 } else { 149 BEGIN_CS(2); 150 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0); 151 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) | 152 prim); 153 END_CS; 154 } 155 156 return TRUE; 157} 158 159/* Simple helpers for context setup. Should probably be moved to util. */ 160boolean r300_draw_elements(struct pipe_context* pipe, 161 struct pipe_buffer* indexBuffer, 162 unsigned indexSize, unsigned mode, 163 unsigned start, unsigned count) 164{ 165 return pipe->draw_range_elements(pipe, indexBuffer, indexSize, 0, ~0, 166 mode, start, count); 167} 168 169boolean r300_draw_arrays(struct pipe_context* pipe, unsigned mode, 170 unsigned start, unsigned count) 171{ 172 return pipe->draw_elements(pipe, NULL, 0, mode, start, count); 173} 174 175/**************************************************************************** 176 * The rest of this file is for SW TCL rendering only. Please be polite and * 177 * keep these functions separated so that they are easier to locate. ~C. * 178 ***************************************************************************/ 179 180/* Draw-based drawing for SW TCL chipsets. */ 181boolean r300_swtcl_draw_range_elements(struct pipe_context* pipe, 182 struct pipe_buffer* indexBuffer, 183 unsigned indexSize, 184 unsigned minIndex, 185 unsigned maxIndex, 186 unsigned mode, 187 unsigned start, 188 unsigned count) 189{ 190 struct r300_context* r300 = r300_context(pipe); 191 int i; 192 193 for (i = 0; i < r300->vertex_buffer_count; i++) { 194 void* buf = pipe_buffer_map(pipe->screen, 195 r300->vertex_buffers[i].buffer, 196 PIPE_BUFFER_USAGE_CPU_READ); 197 draw_set_mapped_vertex_buffer(r300->draw, i, buf); 198 } 199 200 if (indexBuffer) { 201 void* indices = pipe_buffer_map(pipe->screen, indexBuffer, 202 PIPE_BUFFER_USAGE_CPU_READ); 203 draw_set_mapped_element_buffer_range(r300->draw, indexSize, 204 minIndex, maxIndex, indices); 205 } else { 206 draw_set_mapped_element_buffer(r300->draw, 0, NULL); 207 } 208 209 draw_set_mapped_constant_buffer(r300->draw, 210 r300->shader_constants[PIPE_SHADER_VERTEX].constants, 211 r300->shader_constants[PIPE_SHADER_VERTEX].count * 212 (sizeof(float) * 4)); 213 214 draw_arrays(r300->draw, mode, start, count); 215 216 for (i = 0; i < r300->vertex_buffer_count; i++) { 217 pipe_buffer_unmap(pipe->screen, r300->vertex_buffers[i].buffer); 218 draw_set_mapped_vertex_buffer(r300->draw, i, NULL); 219 } 220 221 if (indexBuffer) { 222 pipe_buffer_unmap(pipe->screen, indexBuffer); 223 draw_set_mapped_element_buffer_range(r300->draw, 0, start, 224 start + count - 1, NULL); 225 } 226 227 return TRUE; 228} 229 230/* Object for rendering using Draw. */ 231struct r300_render { 232 /* Parent class */ 233 struct vbuf_render base; 234 235 /* Pipe context */ 236 struct r300_context* r300; 237 238 /* Vertex information */ 239 size_t vertex_size; 240 unsigned prim; 241 unsigned hwprim; 242 243 /* VBO */ 244 struct pipe_buffer* vbo; 245 size_t vbo_size; 246 size_t vbo_offset; 247 size_t vbo_max_used; 248 void * vbo_ptr; 249}; 250 251static INLINE struct r300_render* 252r300_render(struct vbuf_render* render) 253{ 254 return (struct r300_render*)render; 255} 256 257static const struct vertex_info* 258r300_render_get_vertex_info(struct vbuf_render* render) 259{ 260 struct r300_render* r300render = r300_render(render); 261 struct r300_context* r300 = r300render->r300; 262 263 r300_update_derived_state(r300); 264 265 return &r300->vertex_info->vinfo; 266} 267 268static boolean r300_render_allocate_vertices(struct vbuf_render* render, 269 ushort vertex_size, 270 ushort count) 271{ 272 struct r300_render* r300render = r300_render(render); 273 struct r300_context* r300 = r300render->r300; 274 struct pipe_screen* screen = r300->context.screen; 275 size_t size = (size_t)vertex_size * (size_t)count; 276 277 if (size + r300render->vbo_offset > r300render->vbo_size) 278 { 279 pipe_buffer_reference(&r300->vbo, NULL); 280 r300render->vbo = pipe_buffer_create(screen, 281 64, 282 PIPE_BUFFER_USAGE_VERTEX, 283 R300_MAX_VBO_SIZE); 284 r300render->vbo_offset = 0; 285 r300render->vbo_size = R300_MAX_VBO_SIZE; 286 } 287 288 r300render->vertex_size = vertex_size; 289 r300->vbo = r300render->vbo; 290 r300->vbo_offset = r300render->vbo_offset; 291 292 return (r300render->vbo) ? TRUE : FALSE; 293} 294 295static void* r300_render_map_vertices(struct vbuf_render* render) 296{ 297 struct r300_render* r300render = r300_render(render); 298 struct pipe_screen* screen = r300render->r300->context.screen; 299 300 r300render->vbo_ptr = pipe_buffer_map(screen, r300render->vbo, 301 PIPE_BUFFER_USAGE_CPU_WRITE); 302 303 return (r300render->vbo_ptr + r300render->vbo_offset); 304} 305 306static void r300_render_unmap_vertices(struct vbuf_render* render, 307 ushort min, 308 ushort max) 309{ 310 struct r300_render* r300render = r300_render(render); 311 struct pipe_screen* screen = r300render->r300->context.screen; 312 CS_LOCALS(r300render->r300); 313 BEGIN_CS(2); 314 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max); 315 END_CS; 316 317 r300render->vbo_max_used = MAX2(r300render->vbo_max_used, 318 r300render->vertex_size * (max + 1)); 319 pipe_buffer_unmap(screen, r300render->vbo); 320} 321 322static void r300_render_release_vertices(struct vbuf_render* render) 323{ 324 struct r300_render* r300render = r300_render(render); 325 326 r300render->vbo_offset += r300render->vbo_max_used; 327 r300render->vbo_max_used = 0; 328} 329 330static boolean r300_render_set_primitive(struct vbuf_render* render, 331 unsigned prim) 332{ 333 struct r300_render* r300render = r300_render(render); 334 r300render->prim = prim; 335 336 switch (prim) { 337 case PIPE_PRIM_POINTS: 338 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_POINTS; 339 break; 340 case PIPE_PRIM_LINES: 341 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_LINES; 342 break; 343 case PIPE_PRIM_LINE_LOOP: 344 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_LINE_LOOP; 345 break; 346 case PIPE_PRIM_LINE_STRIP: 347 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_LINE_STRIP; 348 break; 349 case PIPE_PRIM_TRIANGLES: 350 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_TRIANGLES; 351 break; 352 case PIPE_PRIM_TRIANGLE_STRIP: 353 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP; 354 break; 355 case PIPE_PRIM_TRIANGLE_FAN: 356 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN; 357 break; 358 case PIPE_PRIM_QUADS: 359 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_QUADS; 360 break; 361 case PIPE_PRIM_QUAD_STRIP: 362 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_QUAD_STRIP; 363 break; 364 case PIPE_PRIM_POLYGON: 365 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_POLYGON; 366 break; 367 default: 368 return FALSE; 369 break; 370 } 371 372 return TRUE; 373} 374 375static void r300_prepare_render(struct r300_render* render, unsigned count) 376{ 377 struct r300_context* r300 = render->r300; 378 379 r300_emit_dirty_state(r300); 380} 381 382static void r300_render_draw_arrays(struct vbuf_render* render, 383 unsigned start, 384 unsigned count) 385{ 386 struct r300_render* r300render = r300_render(render); 387 struct r300_context* r300 = r300render->r300; 388 389 CS_LOCALS(r300); 390 391 r300_prepare_render(r300render, count); 392 393 DBG(r300, DBG_DRAW, "r300: Doing vbuf render, count %d\n", count); 394 395 BEGIN_CS(2); 396 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0); 397 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) | 398 r300render->hwprim); 399 END_CS; 400} 401 402static void r300_render_draw(struct vbuf_render* render, 403 const ushort* indices, 404 uint count) 405{ 406 struct r300_render* r300render = r300_render(render); 407 struct r300_context* r300 = r300render->r300; 408 int i; 409 410 CS_LOCALS(r300); 411 412 r300_prepare_render(r300render, count); 413 414 BEGIN_CS(2 + (count+1)/2); 415 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (count+1)/2); 416 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) | 417 r300render->hwprim); 418 for (i = 0; i < count-1; i += 2) { 419 OUT_CS(indices[i+1] << 16 | indices[i]); 420 } 421 if (count % 2) { 422 OUT_CS(indices[count-1]); 423 } 424 END_CS; 425} 426 427static void r300_render_destroy(struct vbuf_render* render) 428{ 429 FREE(render); 430} 431 432static struct vbuf_render* r300_render_create(struct r300_context* r300) 433{ 434 struct r300_render* r300render = CALLOC_STRUCT(r300_render); 435 436 r300render->r300 = r300; 437 438 /* XXX find real numbers plz */ 439 r300render->base.max_vertex_buffer_bytes = 128 * 1024; 440 r300render->base.max_indices = 16 * 1024; 441 442 r300render->base.get_vertex_info = r300_render_get_vertex_info; 443 r300render->base.allocate_vertices = r300_render_allocate_vertices; 444 r300render->base.map_vertices = r300_render_map_vertices; 445 r300render->base.unmap_vertices = r300_render_unmap_vertices; 446 r300render->base.set_primitive = r300_render_set_primitive; 447 r300render->base.draw = r300_render_draw; 448 r300render->base.draw_arrays = r300_render_draw_arrays; 449 r300render->base.release_vertices = r300_render_release_vertices; 450 r300render->base.destroy = r300_render_destroy; 451 452 r300render->vbo = NULL; 453 r300render->vbo_size = 0; 454 r300render->vbo_offset = 0; 455 456 return &r300render->base; 457} 458 459struct draw_stage* r300_draw_stage(struct r300_context* r300) 460{ 461 struct vbuf_render* render; 462 struct draw_stage* stage; 463 464 render = r300_render_create(r300); 465 466 if (!render) { 467 return NULL; 468 } 469 470 stage = draw_vbuf_stage(r300->draw, render); 471 472 if (!stage) { 473 render->destroy(render); 474 return NULL; 475 } 476 477 draw_set_render(r300->draw, render); 478 479 return stage; 480} 481