draw_pipe_vbuf.c revision e3f5370d637f367dbfe7d21f726e84185ad1e07d
1/************************************************************************** 2 * 3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28/** 29 * \file 30 * Vertex buffer drawing stage. 31 * 32 * \author Jos� Fonseca <jrfonsec@tungstengraphics.com> 33 * \author Keith Whitwell <keith@tungstengraphics.com> 34 */ 35 36 37#include "pipe/p_debug.h" 38#include "util/u_math.h" 39#include "util/u_memory.h" 40 41#include "draw_vbuf.h" 42#include "draw_private.h" 43#include "draw_vertex.h" 44#include "draw_pipe.h" 45#include "translate/translate.h" 46#include "translate/translate_cache.h" 47 48 49/** 50 * Vertex buffer emit stage. 51 */ 52struct vbuf_stage { 53 struct draw_stage stage; /**< This must be first (base class) */ 54 55 struct vbuf_render *render; 56 57 const struct vertex_info *vinfo; 58 59 /** Vertex size in bytes */ 60 unsigned vertex_size; 61 62 struct translate *translate; 63 64 /* FIXME: we have no guarantee that 'unsigned' is 32bit */ 65 66 /** Vertices in hardware format */ 67 unsigned *vertices; 68 unsigned *vertex_ptr; 69 unsigned max_vertices; 70 unsigned nr_vertices; 71 72 /** Indices */ 73 ushort *indices; 74 unsigned max_indices; 75 unsigned nr_indices; 76 77 /* Cache point size somewhere it's address won't change: 78 */ 79 float point_size; 80 81 struct translate_cache *cache; 82}; 83 84 85/** 86 * Basically a cast wrapper. 87 */ 88static INLINE struct vbuf_stage * 89vbuf_stage( struct draw_stage *stage ) 90{ 91 assert(stage); 92 return (struct vbuf_stage *)stage; 93} 94 95 96static void vbuf_flush_indices( struct vbuf_stage *vbuf ); 97static void vbuf_flush_vertices( struct vbuf_stage *vbuf ); 98static void vbuf_alloc_vertices( struct vbuf_stage *vbuf ); 99 100 101static INLINE boolean 102overflow( void *map, void *ptr, unsigned bytes, unsigned bufsz ) 103{ 104 unsigned long used = (unsigned long) ((char *)ptr - (char *)map); 105 return (used + bytes) > bufsz; 106} 107 108 109static INLINE void 110check_space( struct vbuf_stage *vbuf, unsigned nr ) 111{ 112 if (vbuf->nr_vertices + nr > vbuf->max_vertices ) { 113 vbuf_flush_vertices(vbuf); 114 vbuf_alloc_vertices(vbuf); 115 } 116 117 if (vbuf->nr_indices + nr > vbuf->max_indices ) 118 vbuf_flush_indices(vbuf); 119} 120 121 122 123 124/** 125 * Extract the needed fields from post-transformed vertex and emit 126 * a hardware(driver) vertex. 127 * Recall that the vertices are constructed by the 'draw' module and 128 * have a couple of slots at the beginning (1-dword header, 4-dword 129 * clip pos) that we ignore here. We only use the vertex->data[] fields. 130 */ 131static INLINE ushort 132emit_vertex( struct vbuf_stage *vbuf, 133 struct vertex_header *vertex ) 134{ 135 if(vertex->vertex_id == UNDEFINED_VERTEX_ID) { 136 /* Hmm - vertices are emitted one at a time - better make sure 137 * set_buffer is efficient. Consider a special one-shot mode for 138 * translate. 139 */ 140 /* Note: we really do want data[0] here, not data[pos]: 141 */ 142 vbuf->translate->set_buffer(vbuf->translate, 0, vertex->data[0], 0); 143 vbuf->translate->run(vbuf->translate, 0, 1, vbuf->vertex_ptr); 144 145 if (0) draw_dump_emitted_vertex(vbuf->vinfo, (uint8_t *)vbuf->vertex_ptr); 146 147 vbuf->vertex_ptr += vbuf->vertex_size/4; 148 vertex->vertex_id = vbuf->nr_vertices++; 149 } 150 151 return (ushort)vertex->vertex_id; 152} 153 154 155static void 156vbuf_tri( struct draw_stage *stage, 157 struct prim_header *prim ) 158{ 159 struct vbuf_stage *vbuf = vbuf_stage( stage ); 160 unsigned i; 161 162 check_space( vbuf, 3 ); 163 164 for (i = 0; i < 3; i++) { 165 vbuf->indices[vbuf->nr_indices++] = emit_vertex( vbuf, prim->v[i] ); 166 } 167} 168 169 170static void 171vbuf_line( struct draw_stage *stage, 172 struct prim_header *prim ) 173{ 174 struct vbuf_stage *vbuf = vbuf_stage( stage ); 175 unsigned i; 176 177 check_space( vbuf, 2 ); 178 179 for (i = 0; i < 2; i++) { 180 vbuf->indices[vbuf->nr_indices++] = emit_vertex( vbuf, prim->v[i] ); 181 } 182} 183 184 185static void 186vbuf_point( struct draw_stage *stage, 187 struct prim_header *prim ) 188{ 189 struct vbuf_stage *vbuf = vbuf_stage( stage ); 190 191 check_space( vbuf, 1 ); 192 193 vbuf->indices[vbuf->nr_indices++] = emit_vertex( vbuf, prim->v[0] ); 194} 195 196 197 198 199/** 200 * Set the prim type for subsequent vertices. 201 * This may result in a new vertex size. The existing vbuffer (if any) 202 * will be flushed if needed and a new one allocated. 203 */ 204static void 205vbuf_set_prim( struct vbuf_stage *vbuf, uint prim ) 206{ 207 struct translate_key hw_key; 208 unsigned dst_offset; 209 unsigned i; 210 211 vbuf->render->set_primitive(vbuf->render, prim); 212 213 /* Must do this after set_primitive() above: 214 * 215 * XXX: need some state managment to track when this needs to be 216 * recalculated. The driver should tell us whether there was a 217 * state change. 218 */ 219 vbuf->vinfo = vbuf->render->get_vertex_info(vbuf->render); 220 221 if (vbuf->vertex_size != vbuf->vinfo->size * sizeof(float)) { 222 vbuf_flush_vertices(vbuf); 223 vbuf->vertex_size = vbuf->vinfo->size * sizeof(float); 224 } 225 226 /* Translate from pipeline vertices to hw vertices. 227 */ 228 dst_offset = 0; 229 230 for (i = 0; i < vbuf->vinfo->num_attribs; i++) { 231 unsigned emit_sz = 0; 232 unsigned src_buffer = 0; 233 unsigned output_format; 234 unsigned src_offset = (vbuf->vinfo->attrib[i].src_index * 4 * sizeof(float) ); 235 236 switch (vbuf->vinfo->attrib[i].emit) { 237 case EMIT_4F: 238 output_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 239 emit_sz = 4 * sizeof(float); 240 break; 241 case EMIT_3F: 242 output_format = PIPE_FORMAT_R32G32B32_FLOAT; 243 emit_sz = 3 * sizeof(float); 244 break; 245 case EMIT_2F: 246 output_format = PIPE_FORMAT_R32G32_FLOAT; 247 emit_sz = 2 * sizeof(float); 248 break; 249 case EMIT_1F: 250 output_format = PIPE_FORMAT_R32_FLOAT; 251 emit_sz = 1 * sizeof(float); 252 break; 253 case EMIT_1F_PSIZE: 254 output_format = PIPE_FORMAT_R32_FLOAT; 255 emit_sz = 1 * sizeof(float); 256 src_buffer = 1; 257 src_offset = 0; 258 break; 259 case EMIT_4UB: 260 output_format = PIPE_FORMAT_B8G8R8A8_UNORM; 261 emit_sz = 4 * sizeof(ubyte); 262 break; 263 default: 264 assert(0); 265 output_format = PIPE_FORMAT_NONE; 266 emit_sz = 0; 267 break; 268 } 269 270 hw_key.element[i].input_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 271 hw_key.element[i].input_buffer = src_buffer; 272 hw_key.element[i].input_offset = src_offset; 273 hw_key.element[i].output_format = output_format; 274 hw_key.element[i].output_offset = dst_offset; 275 276 dst_offset += emit_sz; 277 } 278 279 hw_key.nr_elements = vbuf->vinfo->num_attribs; 280 hw_key.output_stride = vbuf->vinfo->size * 4; 281 282 /* Don't bother with caching at this stage: 283 */ 284 if (!vbuf->translate || 285 translate_key_compare(&vbuf->translate->key, &hw_key) != 0) 286 { 287 translate_key_sanitize(&hw_key); 288 vbuf->translate = translate_cache_find(vbuf->cache, &hw_key); 289 290 vbuf->translate->set_buffer(vbuf->translate, 1, &vbuf->point_size, 0); 291 } 292 293 vbuf->point_size = vbuf->stage.draw->rasterizer->point_size; 294 295 /* Allocate new buffer? 296 */ 297 if (!vbuf->vertices) 298 vbuf_alloc_vertices(vbuf); 299} 300 301 302static void 303vbuf_first_tri( struct draw_stage *stage, 304 struct prim_header *prim ) 305{ 306 struct vbuf_stage *vbuf = vbuf_stage( stage ); 307 308 vbuf_flush_indices( vbuf ); 309 stage->tri = vbuf_tri; 310 vbuf_set_prim(vbuf, PIPE_PRIM_TRIANGLES); 311 stage->tri( stage, prim ); 312} 313 314 315static void 316vbuf_first_line( struct draw_stage *stage, 317 struct prim_header *prim ) 318{ 319 struct vbuf_stage *vbuf = vbuf_stage( stage ); 320 321 vbuf_flush_indices( vbuf ); 322 stage->line = vbuf_line; 323 vbuf_set_prim(vbuf, PIPE_PRIM_LINES); 324 stage->line( stage, prim ); 325} 326 327 328static void 329vbuf_first_point( struct draw_stage *stage, 330 struct prim_header *prim ) 331{ 332 struct vbuf_stage *vbuf = vbuf_stage( stage ); 333 334 vbuf_flush_indices( vbuf ); 335 stage->point = vbuf_point; 336 vbuf_set_prim(vbuf, PIPE_PRIM_POINTS); 337 stage->point( stage, prim ); 338} 339 340 341static void 342vbuf_flush_indices( struct vbuf_stage *vbuf ) 343{ 344 if(!vbuf->nr_indices) 345 return; 346 347 assert((uint) (vbuf->vertex_ptr - vbuf->vertices) == 348 vbuf->nr_vertices * vbuf->vertex_size / sizeof(unsigned)); 349 350 vbuf->render->draw(vbuf->render, vbuf->indices, vbuf->nr_indices); 351 352 vbuf->nr_indices = 0; 353} 354 355 356/** 357 * Flush existing vertex buffer and allocate a new one. 358 * 359 * XXX: We separate flush-on-index-full and flush-on-vb-full, but may 360 * raise issues uploading vertices if the hardware wants to flush when 361 * we flush. 362 */ 363static void 364vbuf_flush_vertices( struct vbuf_stage *vbuf ) 365{ 366 if(vbuf->vertices) { 367 vbuf_flush_indices(vbuf); 368 369 /* Reset temporary vertices ids */ 370 if(vbuf->nr_vertices) 371 draw_reset_vertex_ids( vbuf->stage.draw ); 372 373 /* Free the vertex buffer */ 374 vbuf->render->release_vertices(vbuf->render, 375 vbuf->vertices, 376 vbuf->vertex_size, 377 vbuf->nr_vertices); 378 vbuf->max_vertices = vbuf->nr_vertices = 0; 379 vbuf->vertex_ptr = vbuf->vertices = NULL; 380 381 } 382} 383 384 385static void 386vbuf_alloc_vertices( struct vbuf_stage *vbuf ) 387{ 388 assert(!vbuf->nr_indices); 389 assert(!vbuf->vertices); 390 391 /* Allocate a new vertex buffer */ 392 vbuf->max_vertices = vbuf->render->max_vertex_buffer_bytes / vbuf->vertex_size; 393 394 /* even number */ 395 vbuf->max_vertices = vbuf->max_vertices & ~1; 396 397 /* Must always succeed -- driver gives us a 398 * 'max_vertex_buffer_bytes' which it guarantees it can allocate, 399 * and it will flush itself if necessary to do so. If this does 400 * fail, we are basically without usable hardware. 401 */ 402 assert(vbuf->max_vertices < 65536); 403 404 vbuf->vertices = (uint *) vbuf->render->allocate_vertices(vbuf->render, 405 (ushort) vbuf->vertex_size, 406 (ushort) vbuf->max_vertices); 407 vbuf->vertex_ptr = vbuf->vertices; 408} 409 410 411 412static void 413vbuf_flush( struct draw_stage *stage, unsigned flags ) 414{ 415 struct vbuf_stage *vbuf = vbuf_stage( stage ); 416 417 vbuf_flush_indices( vbuf ); 418 419 stage->point = vbuf_first_point; 420 stage->line = vbuf_first_line; 421 stage->tri = vbuf_first_tri; 422 423 if (flags & DRAW_FLUSH_BACKEND) 424 vbuf_flush_vertices( vbuf ); 425} 426 427 428static void 429vbuf_reset_stipple_counter( struct draw_stage *stage ) 430{ 431 /* XXX: Need to do something here for hardware with linestipple. 432 */ 433 (void) stage; 434} 435 436 437static void vbuf_destroy( struct draw_stage *stage ) 438{ 439 struct vbuf_stage *vbuf = vbuf_stage( stage ); 440 441 if(vbuf->indices) 442 align_free( vbuf->indices ); 443 444 if (vbuf->render) 445 vbuf->render->destroy( vbuf->render ); 446 447 if (vbuf->cache) 448 translate_cache_destroy(vbuf->cache); 449 450 FREE( stage ); 451} 452 453 454/** 455 * Create a new primitive vbuf/render stage. 456 */ 457struct draw_stage *draw_vbuf_stage( struct draw_context *draw, 458 struct vbuf_render *render ) 459{ 460 struct vbuf_stage *vbuf = CALLOC_STRUCT(vbuf_stage); 461 if (vbuf == NULL) 462 goto fail; 463 464 vbuf->stage.draw = draw; 465 vbuf->stage.point = vbuf_first_point; 466 vbuf->stage.line = vbuf_first_line; 467 vbuf->stage.tri = vbuf_first_tri; 468 vbuf->stage.flush = vbuf_flush; 469 vbuf->stage.reset_stipple_counter = vbuf_reset_stipple_counter; 470 vbuf->stage.destroy = vbuf_destroy; 471 472 vbuf->render = render; 473 vbuf->max_indices = MAX2(render->max_indices, UNDEFINED_VERTEX_ID-1); 474 475 vbuf->indices = (ushort *) align_malloc( vbuf->max_indices * 476 sizeof(vbuf->indices[0]), 477 16 ); 478 if (!vbuf->indices) 479 goto fail; 480 481 vbuf->cache = translate_cache_create(); 482 if (!vbuf->cache) 483 goto fail; 484 485 486 vbuf->vertices = NULL; 487 vbuf->vertex_ptr = vbuf->vertices; 488 489 return &vbuf->stage; 490 491 fail: 492 if (vbuf) 493 vbuf_destroy(&vbuf->stage); 494 495 return NULL; 496} 497