741537309ad8610fec9c7bb65b1be60469657ccb
[mesa.git] / src / gallium / drivers / i965 / brw_draw.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 #include "brw_draw.h"
30 #include "brw_defines.h"
31 #include "brw_context.h"
32 #include "brw_state.h"
33
34 #include "brw_batchbuffer.h"
35 #include "intel_buffer_objects.h"
36
37 #define FILE_DEBUG_FLAG DEBUG_BATCH
38
39 static uint32_t prim_to_hw_prim[PIPE_PRIM_POLYGON+1] = {
40 _3DPRIM_POINTLIST,
41 _3DPRIM_LINELIST,
42 _3DPRIM_LINELOOP,
43 _3DPRIM_LINESTRIP,
44 _3DPRIM_TRILIST,
45 _3DPRIM_TRISTRIP,
46 _3DPRIM_TRIFAN,
47 _3DPRIM_QUADLIST,
48 _3DPRIM_QUADSTRIP,
49 _3DPRIM_POLYGON
50 };
51
52
53
54 /* When the primitive changes, set a state bit and re-validate. Not
55 * the nicest and would rather deal with this by having all the
56 * programs be immune to the active primitive (ie. cope with all
57 * possibilities). That may not be realistic however.
58 */
59 static GLuint brw_set_prim(struct brw_context *brw, GLenum prim)
60 {
61
62 if (INTEL_DEBUG & DEBUG_PRIMS)
63 _mesa_printf("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim));
64
65 /* Slight optimization to avoid the GS program when not needed:
66 */
67 if (prim == GL_QUAD_STRIP &&
68 ctx->Light.ShadeModel != GL_FLAT &&
69 ctx->Polygon.FrontMode == GL_FILL &&
70 ctx->Polygon.BackMode == GL_FILL)
71 prim = GL_TRIANGLE_STRIP;
72
73 if (prim != brw->primitive) {
74 brw->primitive = prim;
75 brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;
76
77 if (reduced_prim[prim] != brw->intel.reduced_primitive) {
78 brw->intel.reduced_primitive = reduced_prim[prim];
79 brw->state.dirty.brw |= BRW_NEW_REDUCED_PRIMITIVE;
80 }
81 }
82
83 return prim_to_hw_prim[prim];
84 }
85
86
87 static GLuint trim(GLenum prim, GLuint length)
88 {
89 if (prim == GL_QUAD_STRIP)
90 return length > 3 ? (length - length % 2) : 0;
91 else if (prim == GL_QUADS)
92 return length - length % 4;
93 else
94 return length;
95 }
96
97
98 static void brw_emit_prim(struct brw_context *brw,
99 const struct _mesa_prim *prim,
100 uint32_t hw_prim)
101 {
102 struct brw_3d_primitive prim_packet;
103
104 if (INTEL_DEBUG & DEBUG_PRIMS)
105 _mesa_printf("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim->mode),
106 prim->start, prim->count);
107
108 prim_packet.header.opcode = CMD_3D_PRIM;
109 prim_packet.header.length = sizeof(prim_packet)/4 - 2;
110 prim_packet.header.pad = 0;
111 prim_packet.header.topology = hw_prim;
112 prim_packet.header.indexed = prim->indexed;
113
114 prim_packet.verts_per_instance = trim(prim->mode, prim->count);
115 prim_packet.start_vert_location = prim->start;
116 if (prim->indexed)
117 prim_packet.start_vert_location += brw->ib.start_vertex_offset;
118 prim_packet.instance_count = 1;
119 prim_packet.start_instance_location = 0;
120 prim_packet.base_vert_location = prim->basevertex;
121
122 /* Can't wrap here, since we rely on the validated state. */
123 brw->no_batch_wrap = GL_TRUE;
124
125 /* If we're set to always flush, do it before and after the primitive emit.
126 * We want to catch both missed flushes that hurt instruction/state cache
127 * and missed flushes of the render cache as it heads to other parts of
128 * the besides the draw code.
129 */
130 if (intel->always_flush_cache) {
131 BEGIN_BATCH(1, IGNORE_CLIPRECTS);
132 OUT_BATCH(intel->vtbl.flush_cmd());
133 ADVANCE_BATCH();
134 }
135 if (prim_packet.verts_per_instance) {
136 brw_batchbuffer_data( brw->intel.batch, &prim_packet,
137 sizeof(prim_packet), LOOP_CLIPRECTS);
138 }
139 if (intel->always_flush_cache) {
140 BEGIN_BATCH(1, IGNORE_CLIPRECTS);
141 OUT_BATCH(intel->vtbl.flush_cmd());
142 ADVANCE_BATCH();
143 }
144
145 brw->no_batch_wrap = GL_FALSE;
146 }
147
148 static void brw_merge_inputs( struct brw_context *brw,
149 const struct gl_client_array *arrays[])
150 {
151 struct brw_vertex_info old = brw->vb.info;
152 GLuint i;
153
154 for (i = 0; i < VERT_ATTRIB_MAX; i++)
155 brw->sws->bo_unreference(brw->vb.inputs[i].bo);
156
157 memset(&brw->vb.inputs, 0, sizeof(brw->vb.inputs));
158 memset(&brw->vb.info, 0, sizeof(brw->vb.info));
159
160 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
161 brw->vb.inputs[i].glarray = arrays[i];
162 brw->vb.inputs[i].attrib = (gl_vert_attrib) i;
163
164 if (arrays[i]->StrideB != 0)
165 brw->vb.info.sizes[i/16] |= (brw->vb.inputs[i].glarray->Size - 1) <<
166 ((i%16) * 2);
167 }
168
169 /* Raise statechanges if input sizes have changed. */
170 if (memcmp(brw->vb.info.sizes, old.sizes, sizeof(old.sizes)) != 0)
171 brw->state.dirty.brw |= BRW_NEW_INPUT_DIMENSIONS;
172 }
173
174 /* May fail if out of video memory for texture or vbo upload, or on
175 * fallback conditions.
176 */
177 static GLboolean brw_try_draw_prims( struct brw_context *brw,
178 const struct gl_client_array *arrays[],
179 const struct _mesa_prim *prim,
180 GLuint nr_prims,
181 const struct _mesa_index_buffer *ib,
182 GLuint min_index,
183 GLuint max_index )
184 {
185 struct brw_context *brw = brw_context(ctx);
186 GLboolean retval = GL_FALSE;
187 GLboolean warn = GL_FALSE;
188 GLboolean first_time = GL_TRUE;
189 uint32_t hw_prim;
190 GLuint i;
191
192 if (ctx->NewState)
193 _mesa_update_state( ctx );
194
195 /* Bind all inputs, derive varying and size information:
196 */
197 brw_merge_inputs( brw, arrays );
198
199 brw->ib.ib = ib;
200 brw->state.dirty.brw |= BRW_NEW_INDICES;
201
202 brw->vb.min_index = min_index;
203 brw->vb.max_index = max_index;
204 brw->state.dirty.brw |= BRW_NEW_VERTICES;
205
206 hw_prim = brw_set_prim(brw, prim[i].mode);
207
208 brw_validate_state(brw);
209
210 /* Check that we can fit our state in with our existing batchbuffer, or
211 * flush otherwise.
212 */
213 ret = dri_bufmgr_check_aperture_space(brw->state.validated_bos,
214 brw->state.validated_bo_count);
215 if (ret)
216 return ret;
217
218 ret = brw_upload_state(brw);
219 if (ret)
220 return ret;
221
222 ret = brw_emit_prim(brw, &prim[i], hw_prim);
223 if (ret)
224 return ret;
225
226 if (intel->always_flush_batch)
227 brw_batchbuffer_flush(intel->batch);
228
229 return 0;
230 }
231
232 void brw_draw_prims( struct brw_context *brw,
233 const struct gl_client_array *arrays[],
234 const struct _mesa_prim *prim,
235 GLuint nr_prims,
236 const struct _mesa_index_buffer *ib,
237 GLboolean index_bounds_valid,
238 GLuint min_index,
239 GLuint max_index )
240 {
241 enum pipe_error ret;
242
243 if (!vbo_all_varyings_in_vbos(arrays)) {
244 if (!index_bounds_valid)
245 vbo_get_minmax_index(ctx, prim, ib, &min_index, &max_index);
246 }
247
248 /* Make a first attempt at drawing:
249 */
250 ret = brw_try_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
251
252 /* Otherwise, flush and retry:
253 */
254 if (ret != 0) {
255 brw_batchbuffer_flush(intel->batch);
256 ret = brw_try_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
257 assert(ret == 0);
258 }
259 }
260
261 void brw_draw_init( struct brw_context *brw )
262 {
263 struct vbo_context *vbo = vbo_context(ctx);
264
265 /* Register our drawing function:
266 */
267 vbo->draw_prims = brw_draw_prims;
268 }
269
270 void brw_draw_destroy( struct brw_context *brw )
271 {
272 int i;
273
274 if (brw->vb.upload.bo != NULL) {
275 brw->sws->bo_unreference(brw->vb.upload.bo);
276 brw->vb.upload.bo = NULL;
277 }
278
279 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
280 brw->sws->bo_unreference(brw->vb.inputs[i].bo);
281 brw->vb.inputs[i].bo = NULL;
282 }
283
284 brw->sws->bo_unreference(brw->ib.bo);
285 brw->ib.bo = NULL;
286 }