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"
34 #include "brw_defines.h"
35 #include "brw_context.h"
36 #include "brw_state.h"
37 #include "brw_debug.h"
39 #include "brw_batchbuffer.h"
42 static uint32_t prim_to_hw_prim[PIPE_PRIM_POLYGON+1] = {
57 /* When the primitive changes, set a state bit and re-validate. Not
58 * the nicest and would rather deal with this by having all the
59 * programs be immune to the active primitive (ie. cope with all
60 * possibilities). That may not be realistic however.
62 static int brw_set_prim(struct brw_context *brw, unsigned prim )
65 if (BRW_DEBUG & DEBUG_PRIMS)
66 debug_printf("PRIM: %s\n", u_prim_name(prim));
68 if (prim != brw->primitive) {
69 unsigned reduced_prim;
71 brw->primitive = prim;
72 brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;
74 reduced_prim = u_reduced_prim(prim);
75 if (reduced_prim != brw->reduced_primitive) {
76 brw->reduced_primitive = reduced_prim;
77 brw->state.dirty.brw |= BRW_NEW_REDUCED_PRIMITIVE;
81 return prim_to_hw_prim[prim];
86 static int brw_emit_prim(struct brw_context *brw,
92 struct brw_3d_primitive prim_packet;
95 if (BRW_DEBUG & DEBUG_PRIMS)
96 debug_printf("%s start %d count %d indexed %d hw_prim %d\n",
97 __FUNCTION__, start, count, indexed, hw_prim);
99 prim_packet.header.opcode = CMD_3D_PRIM;
100 prim_packet.header.length = sizeof(prim_packet)/4 - 2;
101 prim_packet.header.pad = 0;
102 prim_packet.header.topology = hw_prim;
103 prim_packet.header.indexed = indexed;
105 prim_packet.verts_per_instance = count;
106 prim_packet.start_vert_location = start;
108 prim_packet.start_vert_location += brw->ib.start_vertex_offset;
109 prim_packet.instance_count = 1;
110 prim_packet.start_instance_location = 0;
111 prim_packet.base_vert_location = 0; /* prim->basevertex; XXX: add this to gallium */
114 /* If we're set to always flush, do it before and after the primitive emit.
115 * We want to catch both missed flushes that hurt instruction/state cache
116 * and missed flushes of the render cache as it heads to other parts of
117 * the besides the draw code.
120 BEGIN_BATCH(1, IGNORE_CLIPRECTS);
121 OUT_BATCH((CMD_MI_FLUSH << 16) | BRW_FLUSH_STATE_CACHE);
124 if (prim_packet.verts_per_instance) {
125 ret = brw_batchbuffer_data( brw->batch, &prim_packet,
126 sizeof(prim_packet), LOOP_CLIPRECTS);
131 BEGIN_BATCH(1, IGNORE_CLIPRECTS);
132 OUT_BATCH((CMD_MI_FLUSH << 16) | BRW_FLUSH_STATE_CACHE);
140 /* May fail if out of video memory for texture or vbo upload, or on
141 * fallback conditions.
144 try_draw_range_elements(struct brw_context *brw,
147 unsigned start, unsigned count)
151 ret = brw_validate_state(brw);
155 /* Check that we can fit our state in with our existing batchbuffer, or
158 ret = brw->sws->check_aperture_space(brw->sws,
159 brw->state.validated_bos,
160 brw->state.validated_bo_count);
164 ret = brw_upload_state(brw);
168 ret = brw_emit_prim(brw, start, count, indexed, hw_prim);
172 if (brw->flags.always_flush_batch)
173 brw_context_flush( brw );
180 brw_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
182 struct brw_context *brw = brw_context(pipe);
186 hw_prim = brw_set_prim(brw, info->mode);
188 if (BRW_DEBUG & DEBUG_PRIMS)
189 debug_printf("PRIM: %s start %d count %d index_buffer %p\n",
190 u_prim_name(info->mode), info->start, info->count,
191 (void *) brw->curr.index_buffer);
193 assert(info->index_bias == 0);
195 /* Potentially trigger upload of new index buffer range.
196 * XXX: do we really care?
198 if (brw->curr.min_index != info->min_index ||
199 brw->curr.max_index != info->max_index)
201 brw->curr.min_index = info->min_index;
202 brw->curr.max_index = info->max_index;
203 brw->state.dirty.mesa |= PIPE_NEW_INDEX_RANGE;
207 /* Make a first attempt at drawing:
209 ret = try_draw_range_elements(brw, info->indexed,
210 hw_prim, info->start, info->count);
212 /* Otherwise, flush and retry:
215 brw_context_flush( brw );
216 ret = try_draw_range_elements(brw, info->indexed,
217 hw_prim, info->start, info->count);
223 boolean brw_draw_init( struct brw_context *brw )
225 /* Register our drawing function:
227 brw->base.draw_vbo = brw_draw_vbo;
229 /* Create helpers for uploading data in user buffers:
231 brw->vb.upload_vertex = u_upload_create( &brw->base,
234 PIPE_BIND_VERTEX_BUFFER );
235 if (brw->vb.upload_vertex == NULL)
238 brw->vb.upload_index = u_upload_create( &brw->base,
241 PIPE_BIND_INDEX_BUFFER );
242 if (brw->vb.upload_index == NULL)
248 void brw_draw_cleanup( struct brw_context *brw )
250 u_upload_destroy( brw->vb.upload_vertex );
251 u_upload_destroy( brw->vb.upload_index );
253 bo_reference(&brw->ib.bo, NULL);