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