draw_pipe_vbuf.c revision 3c56280d330f50025fc948041f1413af55c50581
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 Jose Fonseca <jrfonsec@tungstengraphics.com> 33 * \author Keith Whitwell <keith@tungstengraphics.com> 34 */ 35 36 37#include "util/u_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_vertices( struct vbuf_stage *vbuf ); 97static void vbuf_alloc_vertices( struct vbuf_stage *vbuf ); 98 99 100static INLINE boolean 101overflow( void *map, void *ptr, unsigned bytes, unsigned bufsz ) 102{ 103 unsigned long used = (unsigned long) ((char *)ptr - (char *)map); 104 return (used + bytes) > bufsz; 105} 106 107 108static INLINE void 109check_space( struct vbuf_stage *vbuf, unsigned nr ) 110{ 111 if (vbuf->nr_vertices + nr > vbuf->max_vertices || 112 vbuf->nr_indices + nr > vbuf->max_indices) 113 { 114 vbuf_flush_vertices( vbuf ); 115 vbuf_alloc_vertices( vbuf ); 116 } 117} 118 119 120 121 122/** 123 * Extract the needed fields from post-transformed vertex and emit 124 * a hardware(driver) vertex. 125 * Recall that the vertices are constructed by the 'draw' module and 126 * have a couple of slots at the beginning (1-dword header, 4-dword 127 * clip pos) that we ignore here. We only use the vertex->data[] fields. 128 */ 129static INLINE ushort 130emit_vertex( struct vbuf_stage *vbuf, 131 struct vertex_header *vertex ) 132{ 133 if(vertex->vertex_id == UNDEFINED_VERTEX_ID) { 134 /* Hmm - vertices are emitted one at a time - better make sure 135 * set_buffer is efficient. Consider a special one-shot mode for 136 * translate. 137 */ 138 /* Note: we really do want data[0] here, not data[pos]: 139 */ 140 vbuf->translate->set_buffer(vbuf->translate, 0, vertex->data[0], 0, ~0); 141 vbuf->translate->run(vbuf->translate, 0, 1, 0, vbuf->vertex_ptr); 142 143 if (0) draw_dump_emitted_vertex(vbuf->vinfo, (uint8_t *)vbuf->vertex_ptr); 144 145 vbuf->vertex_ptr += vbuf->vertex_size/4; 146 vertex->vertex_id = vbuf->nr_vertices++; 147 } 148 149 return (ushort)vertex->vertex_id; 150} 151 152 153static void 154vbuf_tri( struct draw_stage *stage, 155 struct prim_header *prim ) 156{ 157 struct vbuf_stage *vbuf = vbuf_stage( stage ); 158 unsigned i; 159 160 check_space( vbuf, 3 ); 161 162 for (i = 0; i < 3; i++) { 163 vbuf->indices[vbuf->nr_indices++] = emit_vertex( vbuf, prim->v[i] ); 164 } 165} 166 167 168static void 169vbuf_line( struct draw_stage *stage, 170 struct prim_header *prim ) 171{ 172 struct vbuf_stage *vbuf = vbuf_stage( stage ); 173 unsigned i; 174 175 check_space( vbuf, 2 ); 176 177 for (i = 0; i < 2; i++) { 178 vbuf->indices[vbuf->nr_indices++] = emit_vertex( vbuf, prim->v[i] ); 179 } 180} 181 182 183static void 184vbuf_point( struct draw_stage *stage, 185 struct prim_header *prim ) 186{ 187 struct vbuf_stage *vbuf = vbuf_stage( stage ); 188 189 check_space( vbuf, 1 ); 190 191 vbuf->indices[vbuf->nr_indices++] = emit_vertex( vbuf, prim->v[0] ); 192} 193 194 195 196 197/** 198 * Set the prim type for subsequent vertices. 199 * This may result in a new vertex size. The existing vbuffer (if any) 200 * will be flushed if needed and a new one allocated. 201 */ 202static void 203vbuf_start_prim( struct vbuf_stage *vbuf, uint prim ) 204{ 205 struct translate_key hw_key; 206 unsigned dst_offset; 207 unsigned i; 208 209 vbuf->render->set_primitive(vbuf->render, prim); 210 211 /* Must do this after set_primitive() above: 212 * 213 * XXX: need some state managment to track when this needs to be 214 * recalculated. The driver should tell us whether there was a 215 * state change. 216 */ 217 vbuf->vinfo = vbuf->render->get_vertex_info(vbuf->render); 218 vbuf->vertex_size = vbuf->vinfo->size * sizeof(float); 219 220 /* Translate from pipeline vertices to hw vertices. 221 */ 222 dst_offset = 0; 223 224 for (i = 0; i < vbuf->vinfo->num_attribs; i++) { 225 unsigned emit_sz = 0; 226 unsigned src_buffer = 0; 227 enum pipe_format output_format; 228 unsigned src_offset = (vbuf->vinfo->attrib[i].src_index * 4 * sizeof(float) ); 229 230 output_format = draw_translate_vinfo_format(vbuf->vinfo->attrib[i].emit); 231 emit_sz = draw_translate_vinfo_size(vbuf->vinfo->attrib[i].emit); 232 233 /* doesn't handle EMIT_OMIT */ 234 assert(emit_sz != 0); 235 236 if (vbuf->vinfo->attrib[i].emit == EMIT_1F_PSIZE) { 237 src_buffer = 1; 238 src_offset = 0; 239 } 240 241 hw_key.element[i].type = TRANSLATE_ELEMENT_NORMAL; 242 hw_key.element[i].input_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 243 hw_key.element[i].input_buffer = src_buffer; 244 hw_key.element[i].input_offset = src_offset; 245 hw_key.element[i].instance_divisor = 0; 246 hw_key.element[i].output_format = output_format; 247 hw_key.element[i].output_offset = dst_offset; 248 249 dst_offset += emit_sz; 250 } 251 252 hw_key.nr_elements = vbuf->vinfo->num_attribs; 253 hw_key.output_stride = vbuf->vinfo->size * 4; 254 255 /* Don't bother with caching at this stage: 256 */ 257 if (!vbuf->translate || 258 translate_key_compare(&vbuf->translate->key, &hw_key) != 0) 259 { 260 translate_key_sanitize(&hw_key); 261 vbuf->translate = translate_cache_find(vbuf->cache, &hw_key); 262 263 vbuf->translate->set_buffer(vbuf->translate, 1, &vbuf->point_size, 0, ~0); 264 } 265 266 vbuf->point_size = vbuf->stage.draw->rasterizer->point_size; 267 268 /* Allocate new buffer? 269 */ 270 assert(vbuf->vertices == NULL); 271 vbuf_alloc_vertices(vbuf); 272} 273 274 275static void 276vbuf_first_tri( struct draw_stage *stage, 277 struct prim_header *prim ) 278{ 279 struct vbuf_stage *vbuf = vbuf_stage( stage ); 280 281 vbuf_flush_vertices( vbuf ); 282 vbuf_start_prim(vbuf, PIPE_PRIM_TRIANGLES); 283 stage->tri = vbuf_tri; 284 stage->tri( stage, prim ); 285} 286 287 288static void 289vbuf_first_line( struct draw_stage *stage, 290 struct prim_header *prim ) 291{ 292 struct vbuf_stage *vbuf = vbuf_stage( stage ); 293 294 vbuf_flush_vertices( vbuf ); 295 vbuf_start_prim(vbuf, PIPE_PRIM_LINES); 296 stage->line = vbuf_line; 297 stage->line( stage, prim ); 298} 299 300 301static void 302vbuf_first_point( struct draw_stage *stage, 303 struct prim_header *prim ) 304{ 305 struct vbuf_stage *vbuf = vbuf_stage( stage ); 306 307 vbuf_flush_vertices(vbuf); 308 vbuf_start_prim(vbuf, PIPE_PRIM_POINTS); 309 stage->point = vbuf_point; 310 stage->point( stage, prim ); 311} 312 313 314 315/** 316 * Flush existing vertex buffer and allocate a new one. 317 */ 318static void 319vbuf_flush_vertices( struct vbuf_stage *vbuf ) 320{ 321 if(vbuf->vertices) { 322 323 vbuf->render->unmap_vertices( vbuf->render, 0, vbuf->nr_vertices - 1 ); 324 325 if (vbuf->nr_indices) 326 { 327 vbuf->render->draw_elements(vbuf->render, 328 vbuf->indices, 329 vbuf->nr_indices ); 330 331 vbuf->nr_indices = 0; 332 } 333 334 /* Reset temporary vertices ids */ 335 if(vbuf->nr_vertices) 336 draw_reset_vertex_ids( vbuf->stage.draw ); 337 338 /* Free the vertex buffer */ 339 vbuf->render->release_vertices( vbuf->render ); 340 341 vbuf->max_vertices = vbuf->nr_vertices = 0; 342 vbuf->vertex_ptr = vbuf->vertices = NULL; 343 } 344 345 /* Reset point/line/tri function pointers. 346 * If (for example) we transition from points to tris and back to points 347 * again, we need to call the vbuf_first_point() function again to flush 348 * the triangles before drawing more points. This can happen when drawing 349 * with front polygon mode = filled and back polygon mode = line or point. 350 */ 351 vbuf->stage.point = vbuf_first_point; 352 vbuf->stage.line = vbuf_first_line; 353 vbuf->stage.tri = vbuf_first_tri; 354} 355 356 357static void 358vbuf_alloc_vertices( struct vbuf_stage *vbuf ) 359{ 360 assert(!vbuf->nr_indices); 361 assert(!vbuf->vertices); 362 363 /* Allocate a new vertex buffer */ 364 vbuf->max_vertices = vbuf->render->max_vertex_buffer_bytes / vbuf->vertex_size; 365 366 if(vbuf->max_vertices >= UNDEFINED_VERTEX_ID) 367 vbuf->max_vertices = UNDEFINED_VERTEX_ID - 1; 368 369 /* Must always succeed -- driver gives us a 370 * 'max_vertex_buffer_bytes' which it guarantees it can allocate, 371 * and it will flush itself if necessary to do so. If this does 372 * fail, we are basically without usable hardware. 373 */ 374 vbuf->render->allocate_vertices(vbuf->render, 375 (ushort) vbuf->vertex_size, 376 (ushort) vbuf->max_vertices); 377 378 vbuf->vertices = (uint *) vbuf->render->map_vertices( vbuf->render ); 379 380 vbuf->vertex_ptr = vbuf->vertices; 381} 382 383 384 385static void 386vbuf_flush( struct draw_stage *stage, unsigned flags ) 387{ 388 struct vbuf_stage *vbuf = vbuf_stage( stage ); 389 390 vbuf_flush_vertices( vbuf ); 391} 392 393 394static void 395vbuf_reset_stipple_counter( struct draw_stage *stage ) 396{ 397 /* XXX: Need to do something here for hardware with linestipple. 398 */ 399 (void) stage; 400} 401 402 403static void vbuf_destroy( struct draw_stage *stage ) 404{ 405 struct vbuf_stage *vbuf = vbuf_stage( stage ); 406 407 if(vbuf->indices) 408 align_free( vbuf->indices ); 409 410 if (vbuf->render) 411 vbuf->render->destroy( vbuf->render ); 412 413 if (vbuf->cache) 414 translate_cache_destroy(vbuf->cache); 415 416 FREE( stage ); 417} 418 419 420/** 421 * Create a new primitive vbuf/render stage. 422 */ 423struct draw_stage *draw_vbuf_stage( struct draw_context *draw, 424 struct vbuf_render *render ) 425{ 426 struct vbuf_stage *vbuf = CALLOC_STRUCT(vbuf_stage); 427 if (vbuf == NULL) 428 goto fail; 429 430 vbuf->stage.draw = draw; 431 vbuf->stage.name = "vbuf"; 432 vbuf->stage.point = vbuf_first_point; 433 vbuf->stage.line = vbuf_first_line; 434 vbuf->stage.tri = vbuf_first_tri; 435 vbuf->stage.flush = vbuf_flush; 436 vbuf->stage.reset_stipple_counter = vbuf_reset_stipple_counter; 437 vbuf->stage.destroy = vbuf_destroy; 438 439 vbuf->render = render; 440 vbuf->max_indices = MIN2(render->max_indices, UNDEFINED_VERTEX_ID-1); 441 442 vbuf->indices = (ushort *) align_malloc( vbuf->max_indices * 443 sizeof(vbuf->indices[0]), 444 16 ); 445 if (!vbuf->indices) 446 goto fail; 447 448 vbuf->cache = translate_cache_create(); 449 if (!vbuf->cache) 450 goto fail; 451 452 453 vbuf->vertices = NULL; 454 vbuf->vertex_ptr = vbuf->vertices; 455 456 return &vbuf->stage; 457 458 fail: 459 if (vbuf) 460 vbuf_destroy(&vbuf->stage); 461 462 return NULL; 463} 464