1 /**************************************************************************
3 * Copyright 2003 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 **************************************************************************/
29 #include "util/u_inlines.h"
30 #include "util/u_prim.h"
31 #include "util/u_upload_mgr.h"
32 #include "util/u_draw_quad.h"
35 #include "brw_defines.h"
36 #include "brw_context.h"
37 #include "brw_state.h"
38 #include "brw_debug.h"
40 #include "brw_batchbuffer.h"
43 static uint32_t prim_to_hw_prim
[PIPE_PRIM_POLYGON
+1] = {
58 /* When the primitive changes, set a state bit and re-validate. Not
59 * the nicest and would rather deal with this by having all the
60 * programs be immune to the active primitive (ie. cope with all
61 * possibilities). That may not be realistic however.
63 static int brw_set_prim(struct brw_context
*brw
, unsigned prim
)
66 if (BRW_DEBUG
& DEBUG_PRIMS
)
67 debug_printf("PRIM: %s\n", u_prim_name(prim
));
69 if (prim
!= brw
->primitive
) {
70 unsigned reduced_prim
;
72 brw
->primitive
= prim
;
73 brw
->state
.dirty
.brw
|= BRW_NEW_PRIMITIVE
;
75 reduced_prim
= u_reduced_prim(prim
);
76 if (reduced_prim
!= brw
->reduced_primitive
) {
77 brw
->reduced_primitive
= reduced_prim
;
78 brw
->state
.dirty
.brw
|= BRW_NEW_REDUCED_PRIMITIVE
;
82 return prim_to_hw_prim
[prim
];
87 static int brw_emit_prim(struct brw_context
*brw
,
93 struct brw_3d_primitive prim_packet
;
96 if (BRW_DEBUG
& DEBUG_PRIMS
)
97 debug_printf("%s start %d count %d indexed %d hw_prim %d\n",
98 __FUNCTION__
, start
, count
, indexed
, hw_prim
);
100 prim_packet
.header
.opcode
= CMD_3D_PRIM
;
101 prim_packet
.header
.length
= sizeof(prim_packet
)/4 - 2;
102 prim_packet
.header
.pad
= 0;
103 prim_packet
.header
.topology
= hw_prim
;
104 prim_packet
.header
.indexed
= indexed
;
106 prim_packet
.verts_per_instance
= count
;
107 prim_packet
.start_vert_location
= start
;
109 prim_packet
.start_vert_location
+= brw
->ib
.start_vertex_offset
;
110 prim_packet
.instance_count
= 1;
111 prim_packet
.start_instance_location
= 0;
112 prim_packet
.base_vert_location
= 0; /* prim->basevertex; XXX: add this to gallium */
115 /* If we're set to always flush, do it before and after the primitive emit.
116 * We want to catch both missed flushes that hurt instruction/state cache
117 * and missed flushes of the render cache as it heads to other parts of
118 * the besides the draw code.
121 BEGIN_BATCH(1, IGNORE_CLIPRECTS
);
122 OUT_BATCH((CMD_MI_FLUSH
<< 16) | BRW_FLUSH_STATE_CACHE
);
125 if (prim_packet
.verts_per_instance
) {
126 ret
= brw_batchbuffer_data( brw
->batch
, &prim_packet
,
127 sizeof(prim_packet
), LOOP_CLIPRECTS
);
132 BEGIN_BATCH(1, IGNORE_CLIPRECTS
);
133 OUT_BATCH((CMD_MI_FLUSH
<< 16) | BRW_FLUSH_STATE_CACHE
);
141 /* May fail if out of video memory for texture or vbo upload, or on
142 * fallback conditions.
145 try_draw_range_elements(struct brw_context
*brw
,
148 unsigned start
, unsigned count
)
152 ret
= brw_validate_state(brw
);
156 /* Check that we can fit our state in with our existing batchbuffer, or
159 ret
= brw
->sws
->check_aperture_space(brw
->sws
,
160 brw
->state
.validated_bos
,
161 brw
->state
.validated_bo_count
);
165 ret
= brw_upload_state(brw
);
169 ret
= brw_emit_prim(brw
, start
, count
, indexed
, hw_prim
);
173 if (brw
->flags
.always_flush_batch
)
174 brw_context_flush( brw
);
181 brw_draw_vbo(struct pipe_context
*pipe
, const struct pipe_draw_info
*info
)
183 struct brw_context
*brw
= brw_context(pipe
);
187 hw_prim
= brw_set_prim(brw
, info
->mode
);
189 if (BRW_DEBUG
& DEBUG_PRIMS
)
190 debug_printf("PRIM: %s start %d count %d index_buffer %p\n",
191 u_prim_name(info
->mode
), info
->start
, info
->count
,
192 (void *) brw
->curr
.index_buffer
);
194 assert(info
->index_bias
== 0);
196 /* Potentially trigger upload of new index buffer range.
197 * XXX: do we really care?
199 if (brw
->curr
.min_index
!= info
->min_index
||
200 brw
->curr
.max_index
!= info
->max_index
)
202 brw
->curr
.min_index
= info
->min_index
;
203 brw
->curr
.max_index
= info
->max_index
;
204 brw
->state
.dirty
.mesa
|= PIPE_NEW_INDEX_RANGE
;
208 /* Make a first attempt at drawing:
210 ret
= try_draw_range_elements(brw
, info
->indexed
,
211 hw_prim
, info
->start
, info
->count
);
213 /* Otherwise, flush and retry:
216 brw_context_flush( brw
);
217 ret
= try_draw_range_elements(brw
, info
->indexed
,
218 hw_prim
, info
->start
, info
->count
);
224 brw_draw_range_elements(struct pipe_context
*pipe
,
225 struct pipe_resource
*index_buffer
,
226 unsigned index_size
, int index_bias
,
229 unsigned mode
, unsigned start
, unsigned count
)
231 struct brw_context
*brw
= brw_context(pipe
);
232 struct pipe_draw_info info
;
233 struct pipe_index_buffer saved_ib
, ib
;
235 util_draw_init_info(&info
);
239 info
.index_bias
= index_bias
;
240 info
.min_index
= min_index
;
241 info
.max_index
= max_index
;
245 saved_ib
.buffer
= brw
->curr
.index_buffer
;
246 saved_ib
.offset
= brw
->curr
.index_offset
;
247 saved_ib
.index_size
= brw
->curr
.index_size
;
249 ib
.buffer
= index_buffer
;
251 ib
.index_size
= index_size
;
252 pipe
->set_index_buffer(pipe
, &ib
);
255 brw_draw_vbo(pipe
, &info
);
258 pipe
->set_index_buffer(pipe
, &saved_ib
);
262 brw_draw_elements(struct pipe_context
*pipe
,
263 struct pipe_resource
*index_buffer
,
264 unsigned index_size
, int index_bias
,
266 unsigned start
, unsigned count
)
268 brw_draw_range_elements( pipe
, index_buffer
,
269 index_size
, index_bias
,
276 brw_draw_arrays(struct pipe_context
*pipe
, unsigned mode
,
277 unsigned start
, unsigned count
)
279 brw_draw_elements(pipe
, NULL
, 0, 0, mode
, start
, count
);
284 boolean
brw_draw_init( struct brw_context
*brw
)
286 /* Register our drawing function:
288 brw
->base
.draw_arrays
= brw_draw_arrays
;
289 brw
->base
.draw_elements
= brw_draw_elements
;
290 brw
->base
.draw_range_elements
= brw_draw_range_elements
;
291 brw
->base
.draw_vbo
= brw_draw_vbo
;
293 /* Create helpers for uploading data in user buffers:
295 brw
->vb
.upload_vertex
= u_upload_create( &brw
->base
,
298 PIPE_BIND_VERTEX_BUFFER
);
299 if (brw
->vb
.upload_vertex
== NULL
)
302 brw
->vb
.upload_index
= u_upload_create( &brw
->base
,
305 PIPE_BIND_INDEX_BUFFER
);
306 if (brw
->vb
.upload_index
== NULL
)
312 void brw_draw_cleanup( struct brw_context
*brw
)
314 u_upload_destroy( brw
->vb
.upload_vertex
);
315 u_upload_destroy( brw
->vb
.upload_index
);
317 bo_reference(&brw
->ib
.bo
, NULL
);