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 * Build post-transformation, post-clipping vertex buffers and element
31 * lists by hooking into the end of the primitive pipeline and
32 * manipulating the vertex_id field in the vertex headers.
34 * XXX: work in progress
36 * \author José Fonseca <jrfonseca@tungstengraphics.com>
37 * \author Keith Whitwell <keith@tungstengraphics.com>
41 #include "draw/draw_context.h"
42 #include "draw/draw_vbuf.h"
43 #include "pipe/p_debug.h"
44 #include "pipe/p_util.h"
45 #include "pipe/p_inlines.h"
46 #include "pipe/p_winsys.h"
48 #include "i915_context.h"
50 #include "i915_winsys.h"
51 #include "i915_batch.h"
52 #include "i915_state.h"
56 * Primitive renderer for i915.
58 struct i915_vbuf_render
{
59 struct vbuf_render base
;
61 struct i915_context
*i915
;
63 /** Vertex size in bytes */
66 /** Software primitive */
69 /** Hardware primitive */
72 /** Genereate a vertex list */
75 /* Stuff for the vbo */
76 struct pipe_buffer
*vbo
;
80 size_t vbo_alloc_size
;
85 * Basically a cast wrapper.
87 static INLINE
struct i915_vbuf_render
*
88 i915_vbuf_render( struct vbuf_render
*render
)
91 return (struct i915_vbuf_render
*)render
;
95 static const struct vertex_info
*
96 i915_vbuf_render_get_vertex_info( struct vbuf_render
*render
)
98 struct i915_vbuf_render
*i915_render
= i915_vbuf_render(render
);
99 struct i915_context
*i915
= i915_render
->i915
;
102 /* make sure we have up to date vertex layout */
103 i915_update_derived( i915
);
106 return &i915
->current
.vertex_info
;
111 i915_vbuf_render_allocate_vertices( struct vbuf_render
*render
,
115 struct i915_vbuf_render
*i915_render
= i915_vbuf_render(render
);
116 struct i915_context
*i915
= i915_render
->i915
;
117 struct pipe_winsys
*winsys
= i915
->pipe
.winsys
;
118 size_t size
= (size_t)vertex_size
* (size_t)nr_vertices
;
120 /* FIXME: handle failure */
123 if (i915_render
->vbo_size
> size
+ i915_render
->vbo_offset
&& !i915
->vbo_flushed
) {
125 i915
->vbo_flushed
= 0;
126 pipe_buffer_reference(winsys
, &i915_render
->vbo
, NULL
);
129 if (!i915_render
->vbo
) {
130 i915_render
->vbo_size
= MAX2(size
, i915_render
->vbo_alloc_size
);
131 i915_render
->vbo_offset
= 0;
132 i915_render
->vbo
= winsys
->buffer_create(winsys
,
134 I915_BUFFER_USAGE_LIT_VERTEX
,
135 i915_render
->vbo_size
);
136 i915_render
->vbo_ptr
= winsys
->buffer_map(winsys
,
138 PIPE_BUFFER_USAGE_CPU_WRITE
);
139 winsys
->buffer_unmap(winsys
, i915_render
->vbo
);
142 i915
->vbo
= i915_render
->vbo
;
143 i915
->vbo_offset
= i915_render
->vbo_offset
;
144 i915
->dirty
|= I915_NEW_VBO
;
146 return (unsigned char *)i915_render
->vbo_ptr
+ i915
->vbo_offset
;
151 i915_vbuf_render_set_primitive( struct vbuf_render
*render
,
154 struct i915_vbuf_render
*i915_render
= i915_vbuf_render(render
);
155 i915_render
->prim
= prim
;
158 case PIPE_PRIM_POINTS
:
159 i915_render
->hwprim
= PRIM3D_POINTLIST
;
160 i915_render
->fallback
= 0;
162 case PIPE_PRIM_LINES
:
163 i915_render
->hwprim
= PRIM3D_LINELIST
;
164 i915_render
->fallback
= 0;
166 case PIPE_PRIM_LINE_STRIP
:
167 i915_render
->hwprim
= PRIM3D_LINESTRIP
;
168 i915_render
->fallback
= 0;
170 case PIPE_PRIM_TRIANGLES
:
171 i915_render
->hwprim
= PRIM3D_TRILIST
;
172 i915_render
->fallback
= 0;
174 case PIPE_PRIM_TRIANGLE_STRIP
:
175 i915_render
->hwprim
= PRIM3D_TRISTRIP
;
176 i915_render
->fallback
= 0;
178 case PIPE_PRIM_QUADS
:
179 i915_render
->hwprim
= PRIM3D_TRILIST
;
180 i915_render
->fallback
= PIPE_PRIM_QUADS
;
182 case PIPE_PRIM_QUAD_STRIP
:
183 i915_render
->hwprim
= PRIM3D_TRILIST
;
184 i915_render
->fallback
= PIPE_PRIM_QUAD_STRIP
;
187 /* Actually, can handle a lot more just fine... Fixme.
196 * Used for fallbacks in draw_arrays
199 draw_arrays_generate_indices( struct vbuf_render
*render
,
200 unsigned start
, uint nr
,
203 struct i915_vbuf_render
*i915_render
= i915_vbuf_render(render
);
204 struct i915_context
*i915
= i915_render
->i915
;
206 unsigned end
= start
+ nr
;
209 for (i
= start
; i
+1 < end
; i
+= 2)
210 OUT_BATCH( (i
+0) | (i
+1) << 16 );
214 case PIPE_PRIM_QUADS
:
215 for (i
= start
; i
+ 3 < end
; i
+= 4) {
216 OUT_BATCH( (i
+0) | (i
+1) << 16 );
217 OUT_BATCH( (i
+3) | (i
+1) << 16 );
218 OUT_BATCH( (i
+2) | (i
+3) << 16 );
221 case PIPE_PRIM_QUAD_STRIP
:
222 for (i
= start
; i
+ 3 < end
; i
+= 2) {
223 OUT_BATCH( (i
+0) | (i
+1) << 16 );
224 OUT_BATCH( (i
+3) | (i
+2) << 16 );
225 OUT_BATCH( (i
+0) | (i
+3) << 16 );
234 draw_arrays_calc_nr_indices( uint nr
, unsigned type
)
239 case PIPE_PRIM_QUADS
:
241 case PIPE_PRIM_QUAD_STRIP
:
242 return ((nr
- 2) / 2) * 6;
250 draw_arrays_fallback( struct vbuf_render
*render
,
254 struct i915_vbuf_render
*i915_render
= i915_vbuf_render(render
);
255 struct i915_context
*i915
= i915_render
->i915
;
256 struct pipe_winsys
*winsys
= i915
->pipe
.winsys
;
260 i915_update_derived( i915
);
262 if (i915
->hardware_dirty
)
263 i915_emit_hardware_state( i915
);
265 nr_indices
= draw_arrays_calc_nr_indices( nr
, i915_render
->fallback
);
267 if (!BEGIN_BATCH( 1 + (nr_indices
+ 1)/2, 1 )) {
270 /* Make sure state is re-emitted after a flush:
272 i915_update_derived( i915
);
273 i915_emit_hardware_state( i915
);
274 i915
->vbo_flushed
= 1;
276 if (!BEGIN_BATCH( 1 + (nr_indices
+ 1)/2, 1 )) {
281 OUT_BATCH( _3DPRIMITIVE
|
283 i915_render
->hwprim
|
287 draw_arrays_generate_indices( render
, start
, nr
, i915_render
->fallback
);
294 i915_vbuf_render_draw_arrays( struct vbuf_render
*render
,
298 struct i915_vbuf_render
*i915_render
= i915_vbuf_render(render
);
300 if (i915_render
->fallback
) {
301 draw_arrays_fallback( render
, start
, nr
);
305 /* JB: TODO submit direct cmds */
306 draw_arrays_fallback( render
, start
, nr
);
310 * Used for normal and fallback emitting of indices
311 * If type is zero normal operation assumed.
314 draw_generate_indices( struct vbuf_render
*render
,
315 const ushort
*indices
,
319 struct i915_vbuf_render
*i915_render
= i915_vbuf_render(render
);
320 struct i915_context
*i915
= i915_render
->i915
;
325 for (i
= 0; i
+ 1 < nr_indices
; i
+= 2) {
326 OUT_BATCH( indices
[i
] | indices
[i
+1] << 16 );
328 if (i
< nr_indices
) {
329 OUT_BATCH( indices
[i
] );
332 case PIPE_PRIM_QUADS
:
333 for (i
= 0; i
+ 3 < nr_indices
; i
+= 4) {
334 OUT_BATCH( indices
[i
+0] | indices
[i
+1] << 16 );
335 OUT_BATCH( indices
[i
+3] | indices
[i
+1] << 16 );
336 OUT_BATCH( indices
[i
+2] | indices
[i
+3] << 16 );
339 case PIPE_PRIM_QUAD_STRIP
:
340 for (i
= 0; i
+ 3 < nr_indices
; i
+= 2) {
341 OUT_BATCH( indices
[i
+0] | indices
[i
+1] << 16 );
342 OUT_BATCH( indices
[i
+3] | indices
[i
+2] << 16 );
343 OUT_BATCH( indices
[i
+0] | indices
[i
+3] << 16 );
353 draw_calc_nr_indices( uint nr_indices
, unsigned type
)
358 case PIPE_PRIM_QUADS
:
359 return (nr_indices
/ 4) * 6;
360 case PIPE_PRIM_QUAD_STRIP
:
361 return ((nr_indices
- 2) / 2) * 6;
369 i915_vbuf_render_draw( struct vbuf_render
*render
,
370 const ushort
*indices
,
373 struct i915_vbuf_render
*i915_render
= i915_vbuf_render(render
);
374 struct i915_context
*i915
= i915_render
->i915
;
375 struct pipe_winsys
*winsys
= i915
->pipe
.winsys
;
376 unsigned save_nr_indices
;
378 save_nr_indices
= nr_indices
;
379 nr_indices
= draw_calc_nr_indices( nr_indices
, i915_render
->fallback
);
384 i915_update_derived( i915
);
386 if (i915
->hardware_dirty
)
387 i915_emit_hardware_state( i915
);
389 if (!BEGIN_BATCH( 1 + (nr_indices
+ 1)/2, 1 )) {
392 /* Make sure state is re-emitted after a flush:
394 i915_update_derived( i915
);
395 i915_emit_hardware_state( i915
);
396 i915
->vbo_flushed
= 1;
398 if (!BEGIN_BATCH( 1 + (nr_indices
+ 1)/2, 1 )) {
404 OUT_BATCH( _3DPRIMITIVE
|
406 i915_render
->hwprim
|
409 draw_generate_indices( render
,
412 i915_render
->fallback
);
420 i915_vbuf_render_release_vertices( struct vbuf_render
*render
,
422 unsigned vertex_size
,
423 unsigned vertices_used
)
425 struct i915_vbuf_render
*i915_render
= i915_vbuf_render(render
);
426 struct i915_context
*i915
= i915_render
->i915
;
427 struct pipe_winsys
*winsys
= i915
->pipe
.winsys
;
428 size_t size
= (size_t)vertex_size
* (size_t)vertices_used
;
432 i915_render
->vbo_offset
+= size
;
434 i915
->dirty
|= I915_NEW_VBO
;
439 i915_vbuf_render_destroy( struct vbuf_render
*render
)
441 struct i915_vbuf_render
*i915_render
= i915_vbuf_render(render
);
447 * Create a new primitive render.
449 static struct vbuf_render
*
450 i915_vbuf_render_create( struct i915_context
*i915
)
452 struct i915_vbuf_render
*i915_render
= CALLOC_STRUCT(i915_vbuf_render
);
453 struct pipe_winsys
*winsys
= i915
->pipe
.winsys
;
455 i915_render
->i915
= i915
;
457 i915_render
->base
.max_vertex_buffer_bytes
= 128*1024;
459 /* NOTE: it must be such that state and vertices indices fit in a single
462 i915_render
->base
.max_indices
= 16*1024;
464 i915_render
->base
.get_vertex_info
= i915_vbuf_render_get_vertex_info
;
465 i915_render
->base
.allocate_vertices
= i915_vbuf_render_allocate_vertices
;
466 i915_render
->base
.set_primitive
= i915_vbuf_render_set_primitive
;
467 i915_render
->base
.draw
= i915_vbuf_render_draw
;
468 i915_render
->base
.draw_arrays
= i915_vbuf_render_draw_arrays
;
469 i915_render
->base
.release_vertices
= i915_vbuf_render_release_vertices
;
470 i915_render
->base
.destroy
= i915_vbuf_render_destroy
;
472 i915_render
->vbo_alloc_size
= 128 * 4096;
473 i915_render
->vbo_size
= i915_render
->vbo_alloc_size
;
474 i915_render
->vbo_offset
= 0;
475 i915_render
->vbo
= winsys
->buffer_create(winsys
,
477 I915_BUFFER_USAGE_LIT_VERTEX
,
478 i915_render
->vbo_size
);
479 i915_render
->vbo_ptr
= winsys
->buffer_map(winsys
,
481 PIPE_BUFFER_USAGE_CPU_WRITE
);
482 winsys
->buffer_unmap(winsys
, i915_render
->vbo
);
484 return &i915_render
->base
;
489 * Create a new primitive vbuf/render stage.
491 struct draw_stage
*i915_draw_vbuf_stage( struct i915_context
*i915
)
493 struct vbuf_render
*render
;
494 struct draw_stage
*stage
;
496 render
= i915_vbuf_render_create(i915
);
500 stage
= draw_vbuf_stage( i915
->draw
, render
);
502 render
->destroy(render
);
505 draw_set_render(i915
->draw
, render
);