1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Vertex buffer drawing stage.
32 * \author José Fonseca <jrfonsec@tungstengraphics.com>
33 * \author Keith Whitwell <keith@tungstengraphics.com>
37 #include "pipe/p_debug.h"
38 #include "pipe/p_util.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"
49 * Vertex buffer emit stage.
52 struct draw_stage stage
; /**< This must be first (base class) */
54 struct vbuf_render
*render
;
56 const struct vertex_info
*vinfo
;
58 /** Vertex size in bytes */
61 struct translate
*translate
;
63 /* FIXME: we have no guarantee that 'unsigned' is 32bit */
65 /** Vertices in hardware format */
68 unsigned max_vertices
;
76 /* Cache point size somewhere it's address won't change:
80 struct translate_cache
*cache
;
85 * Basically a cast wrapper.
87 static INLINE
struct vbuf_stage
*
88 vbuf_stage( struct draw_stage
*stage
)
91 return (struct vbuf_stage
*)stage
;
95 static void vbuf_flush_indices( struct vbuf_stage
*vbuf
);
96 static void vbuf_flush_vertices( struct vbuf_stage
*vbuf
);
97 static void vbuf_alloc_vertices( struct vbuf_stage
*vbuf
);
100 static INLINE boolean
101 overflow( void *map
, void *ptr
, unsigned bytes
, unsigned bufsz
)
103 unsigned long used
= (unsigned long) ((char *)ptr
- (char *)map
);
104 return (used
+ bytes
) > bufsz
;
109 check_space( struct vbuf_stage
*vbuf
, unsigned nr
)
111 if (vbuf
->nr_vertices
+ nr
> vbuf
->max_vertices
) {
112 vbuf_flush_vertices(vbuf
);
113 vbuf_alloc_vertices(vbuf
);
116 if (vbuf
->nr_indices
+ nr
> vbuf
->max_indices
)
117 vbuf_flush_indices(vbuf
);
124 * Extract the needed fields from post-transformed vertex and emit
125 * a hardware(driver) vertex.
126 * Recall that the vertices are constructed by the 'draw' module and
127 * have a couple of slots at the beginning (1-dword header, 4-dword
128 * clip pos) that we ignore here. We only use the vertex->data[] fields.
131 emit_vertex( struct vbuf_stage
*vbuf
,
132 struct vertex_header
*vertex
)
134 if(vertex
->vertex_id
== UNDEFINED_VERTEX_ID
) {
135 /* Hmm - vertices are emitted one at a time - better make sure
136 * set_buffer is efficient. Consider a special one-shot mode for
139 vbuf
->translate
->set_buffer(vbuf
->translate
, 0, vertex
->data
[0], 0);
140 vbuf
->translate
->run(vbuf
->translate
, 0, 1, vbuf
->vertex_ptr
);
142 if (0) draw_dump_emitted_vertex(vbuf
->vinfo
, (uint8_t *)vbuf
->vertex_ptr
);
144 vbuf
->vertex_ptr
+= vbuf
->vertex_size
/4;
145 vertex
->vertex_id
= vbuf
->nr_vertices
++;
148 return vertex
->vertex_id
;
153 vbuf_tri( struct draw_stage
*stage
,
154 struct prim_header
*prim
)
156 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
159 check_space( vbuf
, 3 );
161 for (i
= 0; i
< 3; i
++) {
162 vbuf
->indices
[vbuf
->nr_indices
++] = emit_vertex( vbuf
, prim
->v
[i
] );
168 vbuf_line( struct draw_stage
*stage
,
169 struct prim_header
*prim
)
171 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
174 check_space( vbuf
, 2 );
176 for (i
= 0; i
< 2; i
++) {
177 vbuf
->indices
[vbuf
->nr_indices
++] = emit_vertex( vbuf
, prim
->v
[i
] );
183 vbuf_point( struct draw_stage
*stage
,
184 struct prim_header
*prim
)
186 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
188 check_space( vbuf
, 1 );
190 vbuf
->indices
[vbuf
->nr_indices
++] = emit_vertex( vbuf
, prim
->v
[0] );
197 * Set the prim type for subsequent vertices.
198 * This may result in a new vertex size. The existing vbuffer (if any)
199 * will be flushed if needed and a new one allocated.
202 vbuf_set_prim( struct vbuf_stage
*vbuf
, uint prim
)
204 struct translate_key hw_key
;
208 vbuf
->render
->set_primitive(vbuf
->render
, prim
);
210 /* Must do this after set_primitive() above:
212 * XXX: need some state managment to track when this needs to be
213 * recalculated. The driver should tell us whether there was a
216 vbuf
->vinfo
= vbuf
->render
->get_vertex_info(vbuf
->render
);
218 if (vbuf
->vertex_size
!= vbuf
->vinfo
->size
* sizeof(float)) {
219 vbuf_flush_vertices(vbuf
);
220 vbuf
->vertex_size
= vbuf
->vinfo
->size
* sizeof(float);
223 /* Translate from pipeline vertices to hw vertices.
227 for (i
= 0; i
< vbuf
->vinfo
->num_attribs
; i
++) {
228 unsigned emit_sz
= 0;
229 unsigned src_buffer
= 0;
230 unsigned output_format
;
231 unsigned src_offset
= (vbuf
->vinfo
->src_index
[i
] * 4 * sizeof(float) );
233 switch (vbuf
->vinfo
->emit
[i
]) {
235 output_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
236 emit_sz
= 4 * sizeof(float);
239 output_format
= PIPE_FORMAT_R32G32B32_FLOAT
;
240 emit_sz
= 3 * sizeof(float);
243 output_format
= PIPE_FORMAT_R32G32_FLOAT
;
244 emit_sz
= 2 * sizeof(float);
247 output_format
= PIPE_FORMAT_R32_FLOAT
;
248 emit_sz
= 1 * sizeof(float);
251 output_format
= PIPE_FORMAT_R32_FLOAT
;
252 emit_sz
= 1 * sizeof(float);
257 output_format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
258 emit_sz
= 4 * sizeof(ubyte
);
262 output_format
= PIPE_FORMAT_NONE
;
267 hw_key
.element
[i
].input_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
268 hw_key
.element
[i
].input_buffer
= src_buffer
;
269 hw_key
.element
[i
].input_offset
= src_offset
;
270 hw_key
.element
[i
].output_format
= output_format
;
271 hw_key
.element
[i
].output_offset
= dst_offset
;
273 dst_offset
+= emit_sz
;
276 hw_key
.nr_elements
= vbuf
->vinfo
->num_attribs
;
277 hw_key
.output_stride
= vbuf
->vinfo
->size
* 4;
279 /* Don't bother with caching at this stage:
281 if (!vbuf
->translate
||
282 translate_key_compare(&vbuf
->translate
->key
, &hw_key
) != 0)
284 translate_key_sanitize(&hw_key
);
285 vbuf
->translate
= translate_cache_find(vbuf
->cache
, &hw_key
);
287 vbuf
->translate
->set_buffer(vbuf
->translate
, 1, &vbuf
->point_size
, 0);
290 vbuf
->point_size
= vbuf
->stage
.draw
->rasterizer
->point_size
;
292 /* Allocate new buffer?
295 vbuf_alloc_vertices(vbuf
);
300 vbuf_first_tri( struct draw_stage
*stage
,
301 struct prim_header
*prim
)
303 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
305 vbuf_flush_indices( vbuf
);
306 stage
->tri
= vbuf_tri
;
307 vbuf_set_prim(vbuf
, PIPE_PRIM_TRIANGLES
);
308 stage
->tri( stage
, prim
);
313 vbuf_first_line( struct draw_stage
*stage
,
314 struct prim_header
*prim
)
316 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
318 vbuf_flush_indices( vbuf
);
319 stage
->line
= vbuf_line
;
320 vbuf_set_prim(vbuf
, PIPE_PRIM_LINES
);
321 stage
->line( stage
, prim
);
326 vbuf_first_point( struct draw_stage
*stage
,
327 struct prim_header
*prim
)
329 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
331 vbuf_flush_indices( vbuf
);
332 stage
->point
= vbuf_point
;
333 vbuf_set_prim(vbuf
, PIPE_PRIM_POINTS
);
334 stage
->point( stage
, prim
);
339 vbuf_flush_indices( struct vbuf_stage
*vbuf
)
341 if(!vbuf
->nr_indices
)
344 assert((uint
) (vbuf
->vertex_ptr
- vbuf
->vertices
) ==
345 vbuf
->nr_vertices
* vbuf
->vertex_size
/ sizeof(unsigned));
347 vbuf
->render
->draw(vbuf
->render
, vbuf
->indices
, vbuf
->nr_indices
);
349 vbuf
->nr_indices
= 0;
354 * Flush existing vertex buffer and allocate a new one.
356 * XXX: We separate flush-on-index-full and flush-on-vb-full, but may
357 * raise issues uploading vertices if the hardware wants to flush when
361 vbuf_flush_vertices( struct vbuf_stage
*vbuf
)
364 vbuf_flush_indices(vbuf
);
366 /* Reset temporary vertices ids */
367 if(vbuf
->nr_vertices
)
368 draw_reset_vertex_ids( vbuf
->stage
.draw
);
370 /* Free the vertex buffer */
371 vbuf
->render
->release_vertices(vbuf
->render
,
375 vbuf
->max_vertices
= vbuf
->nr_vertices
= 0;
376 vbuf
->vertex_ptr
= vbuf
->vertices
= NULL
;
383 vbuf_alloc_vertices( struct vbuf_stage
*vbuf
)
385 assert(!vbuf
->nr_indices
);
386 assert(!vbuf
->vertices
);
388 /* Allocate a new vertex buffer */
389 vbuf
->max_vertices
= vbuf
->render
->max_vertex_buffer_bytes
/ vbuf
->vertex_size
;
390 vbuf
->vertices
= (uint
*) vbuf
->render
->allocate_vertices(vbuf
->render
,
391 (ushort
) vbuf
->vertex_size
,
392 (ushort
) vbuf
->max_vertices
);
393 vbuf
->vertex_ptr
= vbuf
->vertices
;
399 vbuf_flush( struct draw_stage
*stage
, unsigned flags
)
401 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
403 vbuf_flush_indices( vbuf
);
405 stage
->point
= vbuf_first_point
;
406 stage
->line
= vbuf_first_line
;
407 stage
->tri
= vbuf_first_tri
;
409 if (flags
& DRAW_FLUSH_BACKEND
)
410 vbuf_flush_vertices( vbuf
);
415 vbuf_reset_stipple_counter( struct draw_stage
*stage
)
417 /* XXX: Need to do something here for hardware with linestipple.
423 static void vbuf_destroy( struct draw_stage
*stage
)
425 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
428 align_free( vbuf
->indices
);
431 vbuf
->render
->destroy( vbuf
->render
);
434 translate_cache_destroy(vbuf
->cache
);
441 * Create a new primitive vbuf/render stage.
443 struct draw_stage
*draw_vbuf_stage( struct draw_context
*draw
,
444 struct vbuf_render
*render
)
446 struct vbuf_stage
*vbuf
= CALLOC_STRUCT(vbuf_stage
);
450 vbuf
->stage
.draw
= draw
;
451 vbuf
->stage
.point
= vbuf_first_point
;
452 vbuf
->stage
.line
= vbuf_first_line
;
453 vbuf
->stage
.tri
= vbuf_first_tri
;
454 vbuf
->stage
.flush
= vbuf_flush
;
455 vbuf
->stage
.reset_stipple_counter
= vbuf_reset_stipple_counter
;
456 vbuf
->stage
.destroy
= vbuf_destroy
;
458 vbuf
->render
= render
;
459 vbuf
->max_indices
= MAX2(render
->max_indices
, UNDEFINED_VERTEX_ID
-1);
461 vbuf
->indices
= (ushort
*) align_malloc( vbuf
->max_indices
*
462 sizeof(vbuf
->indices
[0]),
467 vbuf
->cache
= translate_cache_create();
472 vbuf
->vertices
= NULL
;
473 vbuf
->vertex_ptr
= vbuf
->vertices
;
479 vbuf_destroy(&vbuf
->stage
);