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 Jose Fonseca <jrfonsec@tungstengraphics.com>
33 * \author Keith Whitwell <keith@tungstengraphics.com>
37 #include "util/u_debug.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
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"
50 * Vertex buffer emit stage.
53 struct draw_stage stage
; /**< This must be first (base class) */
55 struct vbuf_render
*render
;
57 const struct vertex_info
*vinfo
;
59 /** Vertex size in bytes */
62 struct translate
*translate
;
64 /* FIXME: we have no guarantee that 'unsigned' is 32bit */
66 /** Vertices in hardware format */
69 unsigned max_vertices
;
77 /* Cache point size somewhere it's address won't change:
81 struct translate_cache
*cache
;
86 * Basically a cast wrapper.
88 static INLINE
struct vbuf_stage
*
89 vbuf_stage( struct draw_stage
*stage
)
92 return (struct vbuf_stage
*)stage
;
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
->nr_indices
+ nr
> vbuf
->max_indices
)
114 vbuf_flush_vertices( vbuf
);
115 vbuf_alloc_vertices( vbuf
);
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.
130 emit_vertex( struct vbuf_stage
*vbuf
,
131 struct vertex_header
*vertex
)
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
138 /* Note: we really do want data[0] here, not data[pos]:
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
);
143 if (0) draw_dump_emitted_vertex(vbuf
->vinfo
, (uint8_t *)vbuf
->vertex_ptr
);
145 vbuf
->vertex_ptr
+= vbuf
->vertex_size
/4;
146 vertex
->vertex_id
= vbuf
->nr_vertices
++;
149 return (ushort
)vertex
->vertex_id
;
154 vbuf_tri( struct draw_stage
*stage
,
155 struct prim_header
*prim
)
157 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
160 check_space( vbuf
, 3 );
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.
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] );
172 for (i
= 0; i
< 3; i
++) {
173 vbuf
->indices
[vbuf
->nr_indices
++] = emit_vertex( vbuf
, prim
->v
[i
] );
180 vbuf_line( struct draw_stage
*stage
,
181 struct prim_header
*prim
)
183 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
186 check_space( vbuf
, 2 );
188 for (i
= 0; i
< 2; i
++) {
189 vbuf
->indices
[vbuf
->nr_indices
++] = emit_vertex( vbuf
, prim
->v
[i
] );
195 vbuf_point( struct draw_stage
*stage
,
196 struct prim_header
*prim
)
198 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
200 check_space( vbuf
, 1 );
202 vbuf
->indices
[vbuf
->nr_indices
++] = emit_vertex( vbuf
, prim
->v
[0] );
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.
214 vbuf_start_prim( struct vbuf_stage
*vbuf
, uint prim
)
216 struct translate_key hw_key
;
220 vbuf
->render
->set_primitive(vbuf
->render
, prim
);
222 /* Must do this after set_primitive() above:
224 * XXX: need some state managment to track when this needs to be
225 * recalculated. The driver should tell us whether there was a
228 vbuf
->vinfo
= vbuf
->render
->get_vertex_info(vbuf
->render
);
229 vbuf
->vertex_size
= vbuf
->vinfo
->size
* sizeof(float);
231 /* Translate from pipeline vertices to hw vertices.
235 for (i
= 0; i
< vbuf
->vinfo
->num_attribs
; i
++) {
236 unsigned emit_sz
= 0;
237 unsigned src_buffer
= 0;
238 enum pipe_format output_format
;
239 unsigned src_offset
= (vbuf
->vinfo
->attrib
[i
].src_index
* 4 * sizeof(float) );
241 output_format
= draw_translate_vinfo_format(vbuf
->vinfo
->attrib
[i
].emit
);
242 emit_sz
= draw_translate_vinfo_size(vbuf
->vinfo
->attrib
[i
].emit
);
244 /* doesn't handle EMIT_OMIT */
245 assert(emit_sz
!= 0);
247 if (vbuf
->vinfo
->attrib
[i
].emit
== EMIT_1F_PSIZE
) {
252 hw_key
.element
[i
].type
= TRANSLATE_ELEMENT_NORMAL
;
253 hw_key
.element
[i
].input_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
254 hw_key
.element
[i
].input_buffer
= src_buffer
;
255 hw_key
.element
[i
].input_offset
= src_offset
;
256 hw_key
.element
[i
].instance_divisor
= 0;
257 hw_key
.element
[i
].output_format
= output_format
;
258 hw_key
.element
[i
].output_offset
= dst_offset
;
260 dst_offset
+= emit_sz
;
263 hw_key
.nr_elements
= vbuf
->vinfo
->num_attribs
;
264 hw_key
.output_stride
= vbuf
->vinfo
->size
* 4;
266 /* Don't bother with caching at this stage:
268 if (!vbuf
->translate
||
269 translate_key_compare(&vbuf
->translate
->key
, &hw_key
) != 0)
271 translate_key_sanitize(&hw_key
);
272 vbuf
->translate
= translate_cache_find(vbuf
->cache
, &hw_key
);
274 vbuf
->translate
->set_buffer(vbuf
->translate
, 1, &vbuf
->point_size
, 0, ~0);
277 vbuf
->point_size
= vbuf
->stage
.draw
->rasterizer
->point_size
;
279 /* Allocate new buffer?
281 assert(vbuf
->vertices
== NULL
);
282 vbuf_alloc_vertices(vbuf
);
287 vbuf_first_tri( struct draw_stage
*stage
,
288 struct prim_header
*prim
)
290 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
292 vbuf_flush_vertices( vbuf
);
293 vbuf_start_prim(vbuf
, PIPE_PRIM_TRIANGLES
);
294 stage
->tri
= vbuf_tri
;
295 stage
->tri( stage
, prim
);
300 vbuf_first_line( struct draw_stage
*stage
,
301 struct prim_header
*prim
)
303 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
305 vbuf_flush_vertices( vbuf
);
306 vbuf_start_prim(vbuf
, PIPE_PRIM_LINES
);
307 stage
->line
= vbuf_line
;
308 stage
->line( stage
, prim
);
313 vbuf_first_point( struct draw_stage
*stage
,
314 struct prim_header
*prim
)
316 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
318 vbuf_flush_vertices(vbuf
);
319 vbuf_start_prim(vbuf
, PIPE_PRIM_POINTS
);
320 stage
->point
= vbuf_point
;
321 stage
->point( stage
, prim
);
327 * Flush existing vertex buffer and allocate a new one.
330 vbuf_flush_vertices( struct vbuf_stage
*vbuf
)
334 vbuf
->render
->unmap_vertices( vbuf
->render
, 0, vbuf
->nr_vertices
- 1 );
336 if (vbuf
->nr_indices
)
338 vbuf
->render
->draw(vbuf
->render
,
342 vbuf
->nr_indices
= 0;
345 /* Reset temporary vertices ids */
346 if(vbuf
->nr_vertices
)
347 draw_reset_vertex_ids( vbuf
->stage
.draw
);
349 /* Free the vertex buffer */
350 vbuf
->render
->release_vertices( vbuf
->render
);
352 vbuf
->max_vertices
= vbuf
->nr_vertices
= 0;
353 vbuf
->vertex_ptr
= vbuf
->vertices
= NULL
;
359 vbuf_alloc_vertices( struct vbuf_stage
*vbuf
)
361 assert(!vbuf
->nr_indices
);
362 assert(!vbuf
->vertices
);
364 /* Allocate a new vertex buffer */
365 vbuf
->max_vertices
= vbuf
->render
->max_vertex_buffer_bytes
/ vbuf
->vertex_size
;
368 vbuf
->max_vertices
= vbuf
->max_vertices
& ~1;
370 if(vbuf
->max_vertices
>= UNDEFINED_VERTEX_ID
)
371 vbuf
->max_vertices
= UNDEFINED_VERTEX_ID
- 1;
373 /* Must always succeed -- driver gives us a
374 * 'max_vertex_buffer_bytes' which it guarantees it can allocate,
375 * and it will flush itself if necessary to do so. If this does
376 * fail, we are basically without usable hardware.
378 vbuf
->render
->allocate_vertices(vbuf
->render
,
379 (ushort
) vbuf
->vertex_size
,
380 (ushort
) vbuf
->max_vertices
);
382 vbuf
->vertices
= (uint
*) vbuf
->render
->map_vertices( vbuf
->render
);
384 vbuf
->vertex_ptr
= vbuf
->vertices
;
390 vbuf_flush( struct draw_stage
*stage
, unsigned flags
)
392 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
394 vbuf_flush_vertices( vbuf
);
396 stage
->point
= vbuf_first_point
;
397 stage
->line
= vbuf_first_line
;
398 stage
->tri
= vbuf_first_tri
;
403 vbuf_reset_stipple_counter( struct draw_stage
*stage
)
405 /* XXX: Need to do something here for hardware with linestipple.
411 static void vbuf_destroy( struct draw_stage
*stage
)
413 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
416 align_free( vbuf
->indices
);
419 vbuf
->render
->destroy( vbuf
->render
);
422 translate_cache_destroy(vbuf
->cache
);
429 * Create a new primitive vbuf/render stage.
431 struct draw_stage
*draw_vbuf_stage( struct draw_context
*draw
,
432 struct vbuf_render
*render
)
434 struct vbuf_stage
*vbuf
= CALLOC_STRUCT(vbuf_stage
);
438 vbuf
->stage
.draw
= draw
;
439 vbuf
->stage
.name
= "vbuf";
440 vbuf
->stage
.point
= vbuf_first_point
;
441 vbuf
->stage
.line
= vbuf_first_line
;
442 vbuf
->stage
.tri
= vbuf_first_tri
;
443 vbuf
->stage
.flush
= vbuf_flush
;
444 vbuf
->stage
.reset_stipple_counter
= vbuf_reset_stipple_counter
;
445 vbuf
->stage
.destroy
= vbuf_destroy
;
447 vbuf
->render
= render
;
448 vbuf
->max_indices
= MAX2(render
->max_indices
, UNDEFINED_VERTEX_ID
-1);
450 vbuf
->indices
= (ushort
*) align_malloc( vbuf
->max_indices
*
451 sizeof(vbuf
->indices
[0]),
456 vbuf
->cache
= translate_cache_create();
461 vbuf
->vertices
= NULL
;
462 vbuf
->vertex_ptr
= vbuf
->vertices
;
468 vbuf_destroy(&vbuf
->stage
);