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"
48 * Vertex buffer emit stage.
51 struct draw_stage stage
; /**< This must be first (base class) */
53 struct vbuf_render
*render
;
55 const struct vertex_info
*vinfo
;
57 /** Vertex size in bytes */
60 struct translate
*translate
;
62 /* FIXME: we have no guarantee that 'unsigned' is 32bit */
64 /** Vertices in hardware format */
67 unsigned max_vertices
;
75 /* Cache point size somewhere it's address won't change:
82 * Basically a cast wrapper.
84 static INLINE
struct vbuf_stage
*
85 vbuf_stage( struct draw_stage
*stage
)
88 return (struct vbuf_stage
*)stage
;
92 static void vbuf_flush_indices( struct vbuf_stage
*vbuf
);
93 static void vbuf_flush_vertices( struct vbuf_stage
*vbuf
);
94 static void vbuf_alloc_vertices( struct vbuf_stage
*vbuf
);
98 overflow( void *map
, void *ptr
, unsigned bytes
, unsigned bufsz
)
100 unsigned long used
= (unsigned long) ((char *)ptr
- (char *)map
);
101 return (used
+ bytes
) > bufsz
;
106 check_space( struct vbuf_stage
*vbuf
, unsigned nr
)
108 if (vbuf
->nr_vertices
+ nr
> vbuf
->max_vertices
) {
109 vbuf_flush_vertices(vbuf
);
110 vbuf_alloc_vertices(vbuf
);
113 if (vbuf
->nr_indices
+ nr
> vbuf
->max_indices
)
114 vbuf_flush_indices(vbuf
);
121 * Extract the needed fields from post-transformed vertex and emit
122 * a hardware(driver) vertex.
123 * Recall that the vertices are constructed by the 'draw' module and
124 * have a couple of slots at the beginning (1-dword header, 4-dword
125 * clip pos) that we ignore here. We only use the vertex->data[] fields.
128 emit_vertex( struct vbuf_stage
*vbuf
,
129 struct vertex_header
*vertex
)
131 if(vertex
->vertex_id
== UNDEFINED_VERTEX_ID
) {
132 /* Hmm - vertices are emitted one at a time - better make sure
133 * set_buffer is efficient. Consider a special one-shot mode for
136 vbuf
->translate
->set_buffer(vbuf
->translate
, 0, vertex
->data
[0], 0);
137 vbuf
->translate
->run(vbuf
->translate
, 0, 1, vbuf
->vertex_ptr
);
139 if (1) draw_dump_emitted_vertex(vbuf
->vinfo
, (uint8_t *)vbuf
->vertex_ptr
);
141 vbuf
->vertex_ptr
+= vbuf
->vertex_size
/4;
142 vertex
->vertex_id
= vbuf
->nr_vertices
++;
145 return vertex
->vertex_id
;
150 vbuf_tri( struct draw_stage
*stage
,
151 struct prim_header
*prim
)
153 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
156 check_space( vbuf
, 3 );
158 for (i
= 0; i
< 3; i
++) {
159 vbuf
->indices
[vbuf
->nr_indices
++] = emit_vertex( vbuf
, prim
->v
[i
] );
165 vbuf_line( struct draw_stage
*stage
,
166 struct prim_header
*prim
)
168 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
171 check_space( vbuf
, 2 );
173 for (i
= 0; i
< 2; i
++) {
174 vbuf
->indices
[vbuf
->nr_indices
++] = emit_vertex( vbuf
, prim
->v
[i
] );
180 vbuf_point( struct draw_stage
*stage
,
181 struct prim_header
*prim
)
183 struct vbuf_stage
*vbuf
= vbuf_stage( stage
);
185 check_space( vbuf
, 1 );
187 vbuf
->indices
[vbuf
->nr_indices
++] = emit_vertex( vbuf
, prim
->v
[0] );
194 * Set the prim type for subsequent vertices.
195 * This may result in a new vertex size. The existing vbuffer (if any)
196 * will be flushed if needed and a new one allocated.
199 vbuf_set_prim( struct vbuf_stage
*vbuf
, uint prim
)
201 struct translate_key hw_key
;
205 vbuf
->render
->set_primitive(vbuf
->render
, prim
);
207 /* Must do this after set_primitive() above:
209 * XXX: need some state managment to track when this needs to be
210 * recalculated. The driver should tell us whether there was a
213 vbuf
->vinfo
= vbuf
->render
->get_vertex_info(vbuf
->render
);
215 if (vbuf
->vertex_size
!= vbuf
->vinfo
->size
* sizeof(float)) {
216 vbuf_flush_vertices(vbuf
);
217 vbuf
->vertex_size
= vbuf
->vinfo
->size
* sizeof(float);
220 /* Translate from pipeline vertices to hw vertices.
223 memset(&hw_key
, 0, sizeof(hw_key
));
225 for (i
= 0; i
< vbuf
->vinfo
->num_attribs
; i
++) {
226 unsigned emit_sz
= 0;
227 unsigned src_buffer
= 0;
228 unsigned output_format
;
229 unsigned src_offset
= (vbuf
->vinfo
->src_index
[i
] * 4 * sizeof(float) );
231 switch (vbuf
->vinfo
->emit
[i
]) {
233 output_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
234 emit_sz
= 4 * sizeof(float);
237 output_format
= PIPE_FORMAT_R32G32B32_FLOAT
;
238 emit_sz
= 3 * sizeof(float);
241 output_format
= PIPE_FORMAT_R32G32_FLOAT
;
242 emit_sz
= 2 * sizeof(float);
245 output_format
= PIPE_FORMAT_R32_FLOAT
;
246 emit_sz
= 1 * sizeof(float);
249 output_format
= PIPE_FORMAT_R32_FLOAT
;
250 emit_sz
= 1 * sizeof(float);
255 output_format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
256 emit_sz
= 4 * sizeof(ubyte
);
260 output_format
= PIPE_FORMAT_NONE
;
265 hw_key
.element
[i
].input_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
266 hw_key
.element
[i
].input_buffer
= src_buffer
;
267 hw_key
.element
[i
].input_offset
= src_offset
;
268 hw_key
.element
[i
].output_format
= output_format
;
269 hw_key
.element
[i
].output_offset
= dst_offset
;
271 dst_offset
+= emit_sz
;
274 hw_key
.nr_elements
= vbuf
->vinfo
->num_attribs
;
275 hw_key
.output_stride
= vbuf
->vinfo
->size
* 4;
277 /* Don't bother with caching at this stage:
279 if (!vbuf
->translate
||
280 memcmp(&vbuf
->translate
->key
, &hw_key
, sizeof(hw_key
)) != 0)
283 vbuf
->translate
->release(vbuf
->translate
);
285 vbuf
->translate
= translate_create( &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
->translate
->release( vbuf
->translate
);
434 vbuf
->render
->destroy( vbuf
->render
);
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
->vertices
= NULL
;
468 vbuf
->vertex_ptr
= vbuf
->vertices
;
474 vbuf_destroy(&vbuf
->stage
);