i965: Delete old metaops code now that there are no remaining consumers.
[mesa.git] / src / mesa / drivers / dri / 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 #include <stdlib.h>
29
30 #include "main/glheader.h"
31 #include "main/context.h"
32 #include "main/state.h"
33 #include "main/api_validate.h"
34 #include "main/enums.h"
35
36 #include "brw_draw.h"
37 #include "brw_defines.h"
38 #include "brw_context.h"
39 #include "brw_state.h"
40 #include "brw_fallback.h"
41
42 #include "intel_batchbuffer.h"
43 #include "intel_buffer_objects.h"
44
45 #include "tnl/tnl.h"
46 #include "vbo/vbo_context.h"
47 #include "swrast/swrast.h"
48 #include "swrast_setup/swrast_setup.h"
49
50 #define FILE_DEBUG_FLAG DEBUG_BATCH
51
52 static GLuint prim_to_hw_prim[GL_POLYGON+1] = {
53 _3DPRIM_POINTLIST,
54 _3DPRIM_LINELIST,
55 _3DPRIM_LINELOOP,
56 _3DPRIM_LINESTRIP,
57 _3DPRIM_TRILIST,
58 _3DPRIM_TRISTRIP,
59 _3DPRIM_TRIFAN,
60 _3DPRIM_QUADLIST,
61 _3DPRIM_QUADSTRIP,
62 _3DPRIM_POLYGON
63 };
64
65
66 static const GLenum reduced_prim[GL_POLYGON+1] = {
67 GL_POINTS,
68 GL_LINES,
69 GL_LINES,
70 GL_LINES,
71 GL_TRIANGLES,
72 GL_TRIANGLES,
73 GL_TRIANGLES,
74 GL_TRIANGLES,
75 GL_TRIANGLES,
76 GL_TRIANGLES
77 };
78
79
80 /* When the primitive changes, set a state bit and re-validate. Not
81 * the nicest and would rather deal with this by having all the
82 * programs be immune to the active primitive (ie. cope with all
83 * possibilities). That may not be realistic however.
84 */
85 static GLuint brw_set_prim(struct brw_context *brw, GLenum prim)
86 {
87 if (INTEL_DEBUG & DEBUG_PRIMS)
88 _mesa_printf("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim));
89
90 /* Slight optimization to avoid the GS program when not needed:
91 */
92 if (prim == GL_QUAD_STRIP &&
93 brw->attribs.Light->ShadeModel != GL_FLAT &&
94 brw->attribs.Polygon->FrontMode == GL_FILL &&
95 brw->attribs.Polygon->BackMode == GL_FILL)
96 prim = GL_TRIANGLE_STRIP;
97
98 if (prim != brw->primitive) {
99 brw->primitive = prim;
100 brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;
101
102 if (reduced_prim[prim] != brw->intel.reduced_primitive) {
103 brw->intel.reduced_primitive = reduced_prim[prim];
104 brw->state.dirty.brw |= BRW_NEW_REDUCED_PRIMITIVE;
105 }
106 }
107
108 return prim_to_hw_prim[prim];
109 }
110
111
112 static GLuint trim(GLenum prim, GLuint length)
113 {
114 if (prim == GL_QUAD_STRIP)
115 return length > 3 ? (length - length % 2) : 0;
116 else if (prim == GL_QUADS)
117 return length - length % 4;
118 else
119 return length;
120 }
121
122
123 static void brw_emit_prim(struct brw_context *brw,
124 const struct _mesa_prim *prim,
125 uint32_t hw_prim)
126 {
127 struct brw_3d_primitive prim_packet;
128
129 if (INTEL_DEBUG & DEBUG_PRIMS)
130 _mesa_printf("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim->mode),
131 prim->start, prim->count);
132
133 prim_packet.header.opcode = CMD_3D_PRIM;
134 prim_packet.header.length = sizeof(prim_packet)/4 - 2;
135 prim_packet.header.pad = 0;
136 prim_packet.header.topology = hw_prim;
137 prim_packet.header.indexed = prim->indexed;
138
139 prim_packet.verts_per_instance = trim(prim->mode, prim->count);
140 prim_packet.start_vert_location = prim->start;
141 prim_packet.instance_count = 1;
142 prim_packet.start_instance_location = 0;
143 prim_packet.base_vert_location = 0;
144
145 /* Can't wrap here, since we rely on the validated state. */
146 brw->no_batch_wrap = GL_TRUE;
147 if (prim_packet.verts_per_instance) {
148 intel_batchbuffer_data( brw->intel.batch, &prim_packet,
149 sizeof(prim_packet), LOOP_CLIPRECTS);
150 }
151 brw->no_batch_wrap = GL_FALSE;
152 }
153
154 static void brw_merge_inputs( struct brw_context *brw,
155 const struct gl_client_array *arrays[])
156 {
157 struct brw_vertex_info old = brw->vb.info;
158 GLuint i;
159
160 for (i = 0; i < VERT_ATTRIB_MAX; i++)
161 dri_bo_unreference(brw->vb.inputs[i].bo);
162
163 memset(&brw->vb.inputs, 0, sizeof(brw->vb.inputs));
164 memset(&brw->vb.info, 0, sizeof(brw->vb.info));
165
166 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
167 brw->vb.inputs[i].glarray = arrays[i];
168
169 if (arrays[i]->StrideB != 0)
170 brw->vb.info.varying |= 1 << i;
171
172 brw->vb.info.sizes[i/16] |= (brw->vb.inputs[i].glarray->Size - 1) <<
173 ((i%16) * 2);
174 }
175
176 /* Raise statechanges if input sizes and varying have changed:
177 */
178 if (memcmp(brw->vb.info.sizes, old.sizes, sizeof(old.sizes)) != 0)
179 brw->state.dirty.brw |= BRW_NEW_INPUT_DIMENSIONS;
180
181 if (brw->vb.info.varying != old.varying)
182 brw->state.dirty.brw |= BRW_NEW_INPUT_VARYING;
183 }
184
185 /* XXX: could split the primitive list to fallback only on the
186 * non-conformant primitives.
187 */
188 static GLboolean check_fallbacks( struct brw_context *brw,
189 const struct _mesa_prim *prim,
190 GLuint nr_prims )
191 {
192 GLuint i;
193
194 if (!brw->intel.strict_conformance)
195 return GL_FALSE;
196
197 if (brw->attribs.Polygon->SmoothFlag) {
198 for (i = 0; i < nr_prims; i++)
199 if (reduced_prim[prim[i].mode] == GL_TRIANGLES)
200 return GL_TRUE;
201 }
202
203 /* BRW hardware will do AA lines, but they are non-conformant it
204 * seems. TBD whether we keep this fallback:
205 */
206 if (brw->attribs.Line->SmoothFlag) {
207 for (i = 0; i < nr_prims; i++)
208 if (reduced_prim[prim[i].mode] == GL_LINES)
209 return GL_TRUE;
210 }
211
212 /* Stipple -- these fallbacks could be resolved with a little
213 * bit of work?
214 */
215 if (brw->attribs.Line->StippleFlag) {
216 for (i = 0; i < nr_prims; i++) {
217 /* GS doesn't get enough information to know when to reset
218 * the stipple counter?!?
219 */
220 if (prim[i].mode == GL_LINE_LOOP)
221 return GL_TRUE;
222
223 if (prim[i].mode == GL_POLYGON &&
224 (brw->attribs.Polygon->FrontMode == GL_LINE ||
225 brw->attribs.Polygon->BackMode == GL_LINE))
226 return GL_TRUE;
227 }
228 }
229
230
231 if (brw->attribs.Point->SmoothFlag) {
232 for (i = 0; i < nr_prims; i++)
233 if (prim[i].mode == GL_POINTS)
234 return GL_TRUE;
235 }
236
237 return GL_FALSE;
238 }
239
240 /* May fail if out of video memory for texture or vbo upload, or on
241 * fallback conditions.
242 */
243 static GLboolean brw_try_draw_prims( GLcontext *ctx,
244 const struct gl_client_array *arrays[],
245 const struct _mesa_prim *prim,
246 GLuint nr_prims,
247 const struct _mesa_index_buffer *ib,
248 GLuint min_index,
249 GLuint max_index )
250 {
251 struct intel_context *intel = intel_context(ctx);
252 struct brw_context *brw = brw_context(ctx);
253 GLboolean retval = GL_FALSE;
254 GLboolean warn = GL_FALSE;
255 GLboolean first_time = GL_TRUE;
256 GLuint i;
257
258 if (ctx->NewState)
259 _mesa_update_state( ctx );
260
261 if (check_fallbacks(brw, prim, nr_prims))
262 return GL_FALSE;
263
264 brw_validate_textures( brw );
265
266 /* Bind all inputs, derive varying and size information:
267 */
268 brw_merge_inputs( brw, arrays );
269
270 brw->ib.ib = ib;
271 brw->state.dirty.brw |= BRW_NEW_INDICES;
272
273 brw->vb.min_index = min_index;
274 brw->vb.max_index = max_index;
275 brw->state.dirty.brw |= BRW_NEW_VERTICES;
276
277 /* Have to validate state quite late. Will rebuild tnl_program,
278 * which depends on varying information.
279 *
280 * Note this is where brw->vs->prog_data.inputs_read is calculated,
281 * so can't access it earlier.
282 */
283
284 LOCK_HARDWARE(intel);
285
286 if (!intel->constant_cliprect && intel->driDrawable->numClipRects == 0) {
287 UNLOCK_HARDWARE(intel);
288 return GL_TRUE;
289 }
290
291 for (i = 0; i < nr_prims; i++) {
292 uint32_t hw_prim;
293
294 /* Flush the batch if it's approaching full, so that we don't wrap while
295 * we've got validated state that needs to be in the same batch as the
296 * primitives. This fraction is just a guess (minimal full state plus
297 * a primitive is around 512 bytes), and would be better if we had
298 * an upper bound of how much we might emit in a single
299 * brw_try_draw_prims().
300 */
301 intel_batchbuffer_require_space(intel->batch, intel->batch->size / 4,
302 LOOP_CLIPRECTS);
303
304 hw_prim = brw_set_prim(brw, prim[i].mode);
305
306 if (first_time || (brw->state.dirty.brw & BRW_NEW_PRIMITIVE)) {
307 first_time = GL_FALSE;
308
309 brw_validate_state(brw);
310
311 /* Various fallback checks: */
312 if (brw->intel.Fallback)
313 goto out;
314
315 /* Check that we can fit our state in with our existing batchbuffer, or
316 * flush otherwise.
317 */
318 if (dri_bufmgr_check_aperture_space(brw->state.validated_bos,
319 brw->state.validated_bo_count)) {
320 static GLboolean warned;
321 intel_batchbuffer_flush(intel->batch);
322
323 /* Validate the state after we flushed the batch (which would have
324 * changed the set of dirty state). If we still fail to
325 * check_aperture, warn of what's happening, but attempt to continue
326 * on since it may succeed anyway, and the user would probably rather
327 * see a failure and a warning than a fallback.
328 */
329 brw_validate_state(brw);
330 if (!warned &&
331 dri_bufmgr_check_aperture_space(brw->state.validated_bos,
332 brw->state.validated_bo_count)) {
333 warn = GL_TRUE;
334 warned = GL_TRUE;
335 }
336 }
337
338 brw_upload_state(brw);
339 }
340
341 brw_emit_prim(brw, &prim[i], hw_prim);
342
343 retval = GL_TRUE;
344 }
345
346 out:
347 UNLOCK_HARDWARE(intel);
348
349 if (warn)
350 fprintf(stderr, "i965: Single primitive emit potentially exceeded "
351 "available aperture space\n");
352
353 if (!retval)
354 DBG("%s failed\n", __FUNCTION__);
355
356 return retval;
357 }
358
359 static GLboolean brw_need_rebase( GLcontext *ctx,
360 const struct gl_client_array *arrays[],
361 const struct _mesa_index_buffer *ib,
362 GLuint min_index )
363 {
364 if (min_index == 0)
365 return GL_FALSE;
366
367 if (ib) {
368 if (!vbo_all_varyings_in_vbos(arrays))
369 return GL_TRUE;
370 else
371 return GL_FALSE;
372 }
373 else {
374 /* Hmm. This isn't quite what I wanted. BRW can actually
375 * handle the mixed case well enough that we shouldn't need to
376 * rebase. However, it's probably not very common, nor hugely
377 * expensive to do it this way:
378 */
379 if (!vbo_all_varyings_in_vbos(arrays))
380 return GL_TRUE;
381 else
382 return GL_FALSE;
383 }
384 }
385
386
387 void brw_draw_prims( GLcontext *ctx,
388 const struct gl_client_array *arrays[],
389 const struct _mesa_prim *prim,
390 GLuint nr_prims,
391 const struct _mesa_index_buffer *ib,
392 GLuint min_index,
393 GLuint max_index )
394 {
395 GLboolean retval;
396
397 /* Decide if we want to rebase. If so we end up recursing once
398 * only into this function.
399 */
400 if (brw_need_rebase( ctx, arrays, ib, min_index )) {
401 vbo_rebase_prims( ctx, arrays,
402 prim, nr_prims,
403 ib, min_index, max_index,
404 brw_draw_prims );
405
406 return;
407 }
408
409 /* Make a first attempt at drawing:
410 */
411 retval = brw_try_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
412
413 /* Otherwise, we really are out of memory. Pass the drawing
414 * command to the software tnl module and which will in turn call
415 * swrast to do the drawing.
416 */
417 if (!retval) {
418 _swsetup_Wakeup(ctx);
419 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
420 }
421
422 }
423
424 void brw_draw_init( struct brw_context *brw )
425 {
426 GLcontext *ctx = &brw->intel.ctx;
427 struct vbo_context *vbo = vbo_context(ctx);
428
429 /* Register our drawing function:
430 */
431 vbo->draw_prims = brw_draw_prims;
432 }
433
434 void brw_draw_destroy( struct brw_context *brw )
435 {
436 int i;
437
438 if (brw->vb.upload.bo != NULL) {
439 dri_bo_unreference(brw->vb.upload.bo);
440 brw->vb.upload.bo = NULL;
441 }
442
443 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
444 dri_bo_unreference(brw->vb.inputs[i].bo);
445 brw->vb.inputs[i].bo = NULL;
446 }
447
448 dri_bo_unreference(brw->ib.bo);
449 brw->ib.bo = NULL;
450 }