Merge branch 'mesa_7_5_branch' into mesa_7_6_branch
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_vbuf.c
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 */
52 struct 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 */
88 static INLINE struct vbuf_stage *
89 vbuf_stage( struct draw_stage *stage )
90 {
91 assert(stage);
92 return (struct vbuf_stage *)stage;
93 }
94
95
96 static void vbuf_flush_vertices( struct vbuf_stage *vbuf );
97 static void vbuf_alloc_vertices( struct vbuf_stage *vbuf );
98
99
100 static INLINE boolean
101 overflow( 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
108 static INLINE void
109 check_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 */
129 static INLINE ushort
130 emit_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);
141 vbuf->translate->run(vbuf->translate, 0, 1, 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
153 static void
154 vbuf_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 if (vbuf->stage.draw->rasterizer->flatshade_first) {
163 /* Put provoking vertex in position expected by the driver.
164 * Emit last provoking vertex in first pos.
165 * Swap verts 0 & 1 to preserve polygon winding.
166 */
167 vbuf->indices[vbuf->nr_indices++] = emit_vertex( vbuf, prim->v[2] );
168 vbuf->indices[vbuf->nr_indices++] = emit_vertex( vbuf, prim->v[0] );
169 vbuf->indices[vbuf->nr_indices++] = emit_vertex( vbuf, prim->v[1] );
170 }
171 else {
172 for (i = 0; i < 3; i++) {
173 vbuf->indices[vbuf->nr_indices++] = emit_vertex( vbuf, prim->v[i] );
174 }
175 }
176 }
177
178
179 static void
180 vbuf_line( struct draw_stage *stage,
181 struct prim_header *prim )
182 {
183 struct vbuf_stage *vbuf = vbuf_stage( stage );
184 unsigned i;
185
186 check_space( vbuf, 2 );
187
188 for (i = 0; i < 2; i++) {
189 vbuf->indices[vbuf->nr_indices++] = emit_vertex( vbuf, prim->v[i] );
190 }
191 }
192
193
194 static void
195 vbuf_point( struct draw_stage *stage,
196 struct prim_header *prim )
197 {
198 struct vbuf_stage *vbuf = vbuf_stage( stage );
199
200 check_space( vbuf, 1 );
201
202 vbuf->indices[vbuf->nr_indices++] = emit_vertex( vbuf, prim->v[0] );
203 }
204
205
206
207
208 /**
209 * Set the prim type for subsequent vertices.
210 * This may result in a new vertex size. The existing vbuffer (if any)
211 * will be flushed if needed and a new one allocated.
212 */
213 static void
214 vbuf_start_prim( struct vbuf_stage *vbuf, uint prim )
215 {
216 struct translate_key hw_key;
217 unsigned dst_offset;
218 unsigned i;
219
220 vbuf->render->set_primitive(vbuf->render, prim);
221
222 /* Must do this after set_primitive() above:
223 *
224 * XXX: need some state managment to track when this needs to be
225 * recalculated. The driver should tell us whether there was a
226 * state change.
227 */
228 vbuf->vinfo = vbuf->render->get_vertex_info(vbuf->render);
229 vbuf->vertex_size = vbuf->vinfo->size * sizeof(float);
230
231 /* Translate from pipeline vertices to hw vertices.
232 */
233 dst_offset = 0;
234
235 for (i = 0; i < vbuf->vinfo->num_attribs; i++) {
236 unsigned emit_sz = 0;
237 unsigned src_buffer = 0;
238 unsigned output_format;
239 unsigned src_offset = (vbuf->vinfo->attrib[i].src_index * 4 * sizeof(float) );
240
241 switch (vbuf->vinfo->attrib[i].emit) {
242 case EMIT_4F:
243 output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
244 emit_sz = 4 * sizeof(float);
245 break;
246 case EMIT_3F:
247 output_format = PIPE_FORMAT_R32G32B32_FLOAT;
248 emit_sz = 3 * sizeof(float);
249 break;
250 case EMIT_2F:
251 output_format = PIPE_FORMAT_R32G32_FLOAT;
252 emit_sz = 2 * sizeof(float);
253 break;
254 case EMIT_1F:
255 output_format = PIPE_FORMAT_R32_FLOAT;
256 emit_sz = 1 * sizeof(float);
257 break;
258 case EMIT_1F_PSIZE:
259 output_format = PIPE_FORMAT_R32_FLOAT;
260 emit_sz = 1 * sizeof(float);
261 src_buffer = 1;
262 src_offset = 0;
263 break;
264 case EMIT_4UB:
265 output_format = PIPE_FORMAT_B8G8R8A8_UNORM;
266 emit_sz = 4 * sizeof(ubyte);
267 break;
268 default:
269 assert(0);
270 output_format = PIPE_FORMAT_NONE;
271 emit_sz = 0;
272 break;
273 }
274
275 hw_key.element[i].input_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
276 hw_key.element[i].input_buffer = src_buffer;
277 hw_key.element[i].input_offset = src_offset;
278 hw_key.element[i].output_format = output_format;
279 hw_key.element[i].output_offset = dst_offset;
280
281 dst_offset += emit_sz;
282 }
283
284 hw_key.nr_elements = vbuf->vinfo->num_attribs;
285 hw_key.output_stride = vbuf->vinfo->size * 4;
286
287 /* Don't bother with caching at this stage:
288 */
289 if (!vbuf->translate ||
290 translate_key_compare(&vbuf->translate->key, &hw_key) != 0)
291 {
292 translate_key_sanitize(&hw_key);
293 vbuf->translate = translate_cache_find(vbuf->cache, &hw_key);
294
295 vbuf->translate->set_buffer(vbuf->translate, 1, &vbuf->point_size, 0);
296 }
297
298 vbuf->point_size = vbuf->stage.draw->rasterizer->point_size;
299
300 /* Allocate new buffer?
301 */
302 assert(vbuf->vertices == NULL);
303 vbuf_alloc_vertices(vbuf);
304 }
305
306
307 static void
308 vbuf_first_tri( struct draw_stage *stage,
309 struct prim_header *prim )
310 {
311 struct vbuf_stage *vbuf = vbuf_stage( stage );
312
313 vbuf_flush_vertices( vbuf );
314 vbuf_start_prim(vbuf, PIPE_PRIM_TRIANGLES);
315 stage->tri = vbuf_tri;
316 stage->tri( stage, prim );
317 }
318
319
320 static void
321 vbuf_first_line( struct draw_stage *stage,
322 struct prim_header *prim )
323 {
324 struct vbuf_stage *vbuf = vbuf_stage( stage );
325
326 vbuf_flush_vertices( vbuf );
327 vbuf_start_prim(vbuf, PIPE_PRIM_LINES);
328 stage->line = vbuf_line;
329 stage->line( stage, prim );
330 }
331
332
333 static void
334 vbuf_first_point( struct draw_stage *stage,
335 struct prim_header *prim )
336 {
337 struct vbuf_stage *vbuf = vbuf_stage( stage );
338
339 vbuf_flush_vertices(vbuf);
340 vbuf_start_prim(vbuf, PIPE_PRIM_POINTS);
341 stage->point = vbuf_point;
342 stage->point( stage, prim );
343 }
344
345
346
347 /**
348 * Flush existing vertex buffer and allocate a new one.
349 */
350 static void
351 vbuf_flush_vertices( struct vbuf_stage *vbuf )
352 {
353 if(vbuf->vertices) {
354
355 vbuf->render->unmap_vertices( vbuf->render, 0, vbuf->nr_vertices - 1 );
356
357 if (vbuf->nr_indices)
358 {
359 vbuf->render->draw(vbuf->render,
360 vbuf->indices,
361 vbuf->nr_indices );
362
363 vbuf->nr_indices = 0;
364 }
365
366 /* Reset temporary vertices ids */
367 if(vbuf->nr_vertices)
368 draw_reset_vertex_ids( vbuf->stage.draw );
369
370 /* Free the vertex buffer */
371 vbuf->render->release_vertices( vbuf->render );
372
373 vbuf->max_vertices = vbuf->nr_vertices = 0;
374 vbuf->vertex_ptr = vbuf->vertices = NULL;
375 }
376 }
377
378
379 static void
380 vbuf_alloc_vertices( struct vbuf_stage *vbuf )
381 {
382 assert(!vbuf->nr_indices);
383 assert(!vbuf->vertices);
384
385 /* Allocate a new vertex buffer */
386 vbuf->max_vertices = vbuf->render->max_vertex_buffer_bytes / vbuf->vertex_size;
387
388 /* even number */
389 vbuf->max_vertices = vbuf->max_vertices & ~1;
390
391 if(vbuf->max_vertices >= UNDEFINED_VERTEX_ID)
392 vbuf->max_vertices = UNDEFINED_VERTEX_ID - 1;
393
394 /* Must always succeed -- driver gives us a
395 * 'max_vertex_buffer_bytes' which it guarantees it can allocate,
396 * and it will flush itself if necessary to do so. If this does
397 * fail, we are basically without usable hardware.
398 */
399 vbuf->render->allocate_vertices(vbuf->render,
400 (ushort) vbuf->vertex_size,
401 (ushort) vbuf->max_vertices);
402
403 vbuf->vertices = (uint *) vbuf->render->map_vertices( vbuf->render );
404
405 vbuf->vertex_ptr = vbuf->vertices;
406 }
407
408
409
410 static void
411 vbuf_flush( struct draw_stage *stage, unsigned flags )
412 {
413 struct vbuf_stage *vbuf = vbuf_stage( stage );
414
415 vbuf_flush_vertices( vbuf );
416
417 stage->point = vbuf_first_point;
418 stage->line = vbuf_first_line;
419 stage->tri = vbuf_first_tri;
420 }
421
422
423 static void
424 vbuf_reset_stipple_counter( struct draw_stage *stage )
425 {
426 /* XXX: Need to do something here for hardware with linestipple.
427 */
428 (void) stage;
429 }
430
431
432 static void vbuf_destroy( struct draw_stage *stage )
433 {
434 struct vbuf_stage *vbuf = vbuf_stage( stage );
435
436 if(vbuf->indices)
437 align_free( vbuf->indices );
438
439 if (vbuf->render)
440 vbuf->render->destroy( vbuf->render );
441
442 if (vbuf->cache)
443 translate_cache_destroy(vbuf->cache);
444
445 FREE( stage );
446 }
447
448
449 /**
450 * Create a new primitive vbuf/render stage.
451 */
452 struct draw_stage *draw_vbuf_stage( struct draw_context *draw,
453 struct vbuf_render *render )
454 {
455 struct vbuf_stage *vbuf = CALLOC_STRUCT(vbuf_stage);
456 if (vbuf == NULL)
457 goto fail;
458
459 vbuf->stage.draw = draw;
460 vbuf->stage.name = "vbuf";
461 vbuf->stage.point = vbuf_first_point;
462 vbuf->stage.line = vbuf_first_line;
463 vbuf->stage.tri = vbuf_first_tri;
464 vbuf->stage.flush = vbuf_flush;
465 vbuf->stage.reset_stipple_counter = vbuf_reset_stipple_counter;
466 vbuf->stage.destroy = vbuf_destroy;
467
468 vbuf->render = render;
469 vbuf->max_indices = MAX2(render->max_indices, UNDEFINED_VERTEX_ID-1);
470
471 vbuf->indices = (ushort *) align_malloc( vbuf->max_indices *
472 sizeof(vbuf->indices[0]),
473 16 );
474 if (!vbuf->indices)
475 goto fail;
476
477 vbuf->cache = translate_cache_create();
478 if (!vbuf->cache)
479 goto fail;
480
481
482 vbuf->vertices = NULL;
483 vbuf->vertex_ptr = vbuf->vertices;
484
485 return &vbuf->stage;
486
487 fail:
488 if (vbuf)
489 vbuf_destroy(&vbuf->stage);
490
491 return NULL;
492 }