include swrast_setup/swrast_setup.h to silence warning
[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 "glheader.h"
31 #include "context.h"
32 #include "state.h"
33 #include "api_validate.h"
34 #include "enums.h"
35
36 #include "brw_draw.h"
37 #include "brw_defines.h"
38 #include "brw_context.h"
39 #include "brw_aub.h"
40 #include "brw_state.h"
41 #include "brw_fallback.h"
42
43 #include "intel_ioctl.h"
44 #include "intel_batchbuffer.h"
45 #include "intel_buffer_objects.h"
46
47 #include "tnl/tnl.h"
48 #include "vbo/vbo_context.h"
49 #include "swrast/swrast.h"
50 #include "swrast_setup/swrast_setup.h"
51
52
53
54 static GLuint hw_prim[GL_POLYGON+1] = {
55 _3DPRIM_POINTLIST,
56 _3DPRIM_LINELIST,
57 _3DPRIM_LINELOOP,
58 _3DPRIM_LINESTRIP,
59 _3DPRIM_TRILIST,
60 _3DPRIM_TRISTRIP,
61 _3DPRIM_TRIFAN,
62 _3DPRIM_QUADLIST,
63 _3DPRIM_QUADSTRIP,
64 _3DPRIM_POLYGON
65 };
66
67
68 static const GLenum reduced_prim[GL_POLYGON+1] = {
69 GL_POINTS,
70 GL_LINES,
71 GL_LINES,
72 GL_LINES,
73 GL_TRIANGLES,
74 GL_TRIANGLES,
75 GL_TRIANGLES,
76 GL_TRIANGLES,
77 GL_TRIANGLES,
78 GL_TRIANGLES
79 };
80
81
82 /* When the primitive changes, set a state bit and re-validate. Not
83 * the nicest and would rather deal with this by having all the
84 * programs be immune to the active primitive (ie. cope with all
85 * possibilities). That may not be realistic however.
86 */
87 static GLuint brw_set_prim(struct brw_context *brw, GLenum prim)
88 {
89 if (INTEL_DEBUG & DEBUG_PRIMS)
90 _mesa_printf("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim));
91
92 /* Slight optimization to avoid the GS program when not needed:
93 */
94 if (prim == GL_QUAD_STRIP &&
95 brw->attribs.Light->ShadeModel != GL_FLAT &&
96 brw->attribs.Polygon->FrontMode == GL_FILL &&
97 brw->attribs.Polygon->BackMode == GL_FILL)
98 prim = GL_TRIANGLE_STRIP;
99
100 if (prim != brw->primitive) {
101 brw->primitive = prim;
102 brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;
103
104 if (reduced_prim[prim] != brw->intel.reduced_primitive) {
105 brw->intel.reduced_primitive = reduced_prim[prim];
106 brw->state.dirty.brw |= BRW_NEW_REDUCED_PRIMITIVE;
107 }
108
109 brw_validate_state(brw);
110 }
111
112 return hw_prim[prim];
113 }
114
115
116 static GLuint trim(GLenum prim, GLuint length)
117 {
118 if (prim == GL_QUAD_STRIP)
119 return length > 3 ? (length - length % 2) : 0;
120 else if (prim == GL_QUADS)
121 return length - length % 4;
122 else
123 return length;
124 }
125
126
127 static void brw_emit_cliprect( struct brw_context *brw,
128 const drm_clip_rect_t *rect )
129 {
130 struct brw_drawrect bdr;
131
132 bdr.header.opcode = CMD_DRAW_RECT;
133 bdr.header.length = sizeof(bdr)/4 - 2;
134 bdr.xmin = rect->x1;
135 bdr.xmax = rect->x2 - 1;
136 bdr.ymin = rect->y1;
137 bdr.ymax = rect->y2 - 1;
138 bdr.xorg = brw->intel.drawX;
139 bdr.yorg = brw->intel.drawY;
140
141 intel_batchbuffer_data( brw->intel.batch, &bdr, sizeof(bdr),
142 INTEL_BATCH_NO_CLIPRECTS);
143 }
144
145
146 static void brw_emit_prim( struct brw_context *brw,
147 const struct _mesa_prim *prim )
148
149 {
150 struct brw_3d_primitive prim_packet;
151
152 if (INTEL_DEBUG & DEBUG_PRIMS)
153 _mesa_printf("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim->mode),
154 prim->start, prim->count);
155
156 prim_packet.header.opcode = CMD_3D_PRIM;
157 prim_packet.header.length = sizeof(prim_packet)/4 - 2;
158 prim_packet.header.pad = 0;
159 prim_packet.header.topology = brw_set_prim(brw, prim->mode);
160 prim_packet.header.indexed = prim->indexed;
161
162 prim_packet.verts_per_instance = trim(prim->mode, prim->count);
163 prim_packet.start_vert_location = prim->start;
164 prim_packet.instance_count = 1;
165 prim_packet.start_instance_location = 0;
166 prim_packet.base_vert_location = 0;
167
168 if (prim_packet.verts_per_instance) {
169 intel_batchbuffer_data( brw->intel.batch, &prim_packet, sizeof(prim_packet),
170 INTEL_BATCH_NO_CLIPRECTS);
171 }
172 }
173
174 static void brw_merge_inputs( struct brw_context *brw,
175 const struct gl_client_array *arrays[])
176 {
177 struct brw_vertex_element *inputs = brw->vb.inputs;
178 struct brw_vertex_info old = brw->vb.info;
179 GLuint i;
180
181 memset(inputs, 0, sizeof(*inputs));
182 memset(&brw->vb.info, 0, sizeof(brw->vb.info));
183
184 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
185 brw->vb.inputs[i].glarray = arrays[i];
186
187 /* XXX: metaops passes null arrays */
188 if (arrays[i]) {
189 if (arrays[i]->StrideB != 0)
190 brw->vb.info.varying |= 1 << i;
191
192 brw->vb.info.sizes[i/16] |= (inputs[i].glarray->Size - 1) << ((i%16) * 2);
193 }
194 }
195
196 /* Raise statechanges if input sizes and varying have changed:
197 */
198 if (memcmp(brw->vb.info.sizes, old.sizes, sizeof(old.sizes)) != 0)
199 brw->state.dirty.brw |= BRW_NEW_INPUT_DIMENSIONS;
200
201 if (brw->vb.info.varying != old.varying)
202 brw->state.dirty.brw |= BRW_NEW_INPUT_VARYING;
203 }
204
205 /* XXX: could split the primitive list to fallback only on the
206 * non-conformant primitives.
207 */
208 static GLboolean check_fallbacks( struct brw_context *brw,
209 const struct _mesa_prim *prim,
210 GLuint nr_prims )
211 {
212 GLuint i;
213
214 if (!brw->intel.strict_conformance)
215 return GL_FALSE;
216
217 if (brw->attribs.Polygon->SmoothFlag) {
218 for (i = 0; i < nr_prims; i++)
219 if (reduced_prim[prim[i].mode] == GL_TRIANGLES)
220 return GL_TRUE;
221 }
222
223 /* BRW hardware will do AA lines, but they are non-conformant it
224 * seems. TBD whether we keep this fallback:
225 */
226 if (brw->attribs.Line->SmoothFlag) {
227 for (i = 0; i < nr_prims; i++)
228 if (reduced_prim[prim[i].mode] == GL_LINES)
229 return GL_TRUE;
230 }
231
232 /* Stipple -- these fallbacks could be resolved with a little
233 * bit of work?
234 */
235 if (brw->attribs.Line->StippleFlag) {
236 for (i = 0; i < nr_prims; i++) {
237 /* GS doesn't get enough information to know when to reset
238 * the stipple counter?!?
239 */
240 if (prim[i].mode == GL_LINE_LOOP)
241 return GL_TRUE;
242
243 if (prim[i].mode == GL_POLYGON &&
244 (brw->attribs.Polygon->FrontMode == GL_LINE ||
245 brw->attribs.Polygon->BackMode == GL_LINE))
246 return GL_TRUE;
247 }
248 }
249
250
251 if (brw->attribs.Point->SmoothFlag) {
252 for (i = 0; i < nr_prims; i++)
253 if (prim[i].mode == GL_POINTS)
254 return GL_TRUE;
255 }
256
257 return GL_FALSE;
258 }
259
260 /* May fail if out of video memory for texture or vbo upload, or on
261 * fallback conditions.
262 */
263 static GLboolean brw_try_draw_prims( GLcontext *ctx,
264 const struct gl_client_array *arrays[],
265 const struct _mesa_prim *prim,
266 GLuint nr_prims,
267 const struct _mesa_index_buffer *ib,
268 GLuint min_index,
269 GLuint max_index )
270 {
271 struct intel_context *intel = intel_context(ctx);
272 struct brw_context *brw = brw_context(ctx);
273 GLboolean retval = GL_FALSE;
274 GLuint i, j;
275
276 if (ctx->NewState)
277 _mesa_update_state( ctx );
278
279 /* Bind all inputs, derive varying and size information:
280 */
281 brw_merge_inputs( brw, arrays );
282
283 /* Have to validate state quite late. Will rebuild tnl_program,
284 * which depends on varying information.
285 *
286 * Note this is where brw->vs->prog_data.inputs_read is calculated,
287 * so can't access it earlier.
288 */
289
290 LOCK_HARDWARE(intel);
291
292 if (brw->intel.numClipRects == 0) {
293 assert(intel->batch->ptr == intel->batch->map + intel->batch->offset);
294 UNLOCK_HARDWARE(intel);
295 return GL_TRUE;
296 }
297
298 {
299 /* Set the first primitive early, ahead of validate_state:
300 */
301 brw_set_prim(brw, prim[0].mode);
302
303 /* XXX: Need to separate validate and upload of state.
304 */
305 brw_validate_state( brw );
306
307 /* Various fallback checks:
308 */
309 if (brw->intel.Fallback)
310 goto out;
311
312 if (check_fallbacks( brw, prim, nr_prims ))
313 goto out;
314
315 /* Upload index, vertex data:
316 */
317 if (ib)
318 brw_upload_indices( brw, ib );
319
320 if (!brw_upload_vertices( brw, min_index, max_index)) {
321 goto out;
322 }
323
324 /* For single cliprect, state is already emitted:
325 */
326 if (brw->intel.numClipRects == 1) {
327 for (i = 0; i < nr_prims; i++) {
328 brw_emit_prim(brw, &prim[i]);
329 }
330 }
331 else {
332 /* Otherwise, explicitly do the cliprects at this point:
333 */
334 for (j = 0; j < brw->intel.numClipRects; j++) {
335 brw_emit_cliprect(brw, &brw->intel.pClipRects[j]);
336
337 /* Emit prims to batchbuffer:
338 */
339 for (i = 0; i < nr_prims; i++) {
340 brw_emit_prim(brw, &prim[i]);
341 }
342 }
343 }
344
345 intel->need_flush = GL_TRUE;
346 retval = GL_TRUE;
347 }
348
349 out:
350
351 /* Currently have to do this to synchronize with the map/unmap of
352 * the vertex buffer in brw_exec_api.c. Not sure if there is any
353 * way around this, as not every flush is due to a buffer filling
354 * up.
355 */
356 if (!intel_batchbuffer_flush( brw->intel.batch )) {
357 DBG("%s intel_batchbuffer_flush failed\n", __FUNCTION__);
358 retval = GL_FALSE;
359 }
360
361 if (retval && intel->thrashing) {
362 bmSetFence(intel);
363 }
364
365 /* Free any old data so it doesn't clog up texture memory - we
366 * won't be referencing it again.
367 */
368 while (brw->vb.upload.wrap != brw->vb.upload.buf) {
369 ctx->Driver.BufferData(ctx,
370 GL_ARRAY_BUFFER_ARB,
371 BRW_UPLOAD_INIT_SIZE,
372 NULL,
373 GL_DYNAMIC_DRAW_ARB,
374 brw->vb.upload.vbo[brw->vb.upload.wrap]);
375 brw->vb.upload.wrap++;
376 brw->vb.upload.wrap %= BRW_NR_UPLOAD_BUFS;
377 }
378
379 UNLOCK_HARDWARE(intel);
380
381 if (!retval)
382 DBG("%s failed\n", __FUNCTION__);
383
384 return retval;
385 }
386
387 static GLboolean brw_need_rebase( GLcontext *ctx,
388 const struct gl_client_array *arrays[],
389 const struct _mesa_index_buffer *ib,
390 GLuint min_index )
391 {
392 if (min_index == 0)
393 return GL_FALSE;
394
395 if (ib) {
396 if (!vbo_all_varyings_in_vbos(arrays))
397 return GL_TRUE;
398 else
399 return GL_FALSE;
400 }
401 else {
402 /* Hmm. This isn't quite what I wanted. BRW can actually
403 * handle the mixed case well enough that we shouldn't need to
404 * rebase. However, it's probably not very common, nor hugely
405 * expensive to do it this way:
406 */
407 if (!vbo_all_varyings_in_vbos(arrays))
408 return GL_TRUE;
409 else
410 return GL_FALSE;
411 }
412 }
413
414
415 void brw_draw_prims( GLcontext *ctx,
416 const struct gl_client_array *arrays[],
417 const struct _mesa_prim *prim,
418 GLuint nr_prims,
419 const struct _mesa_index_buffer *ib,
420 GLuint min_index,
421 GLuint max_index )
422 {
423 struct intel_context *intel = intel_context(ctx);
424 GLboolean retval;
425
426 /* Decide if we want to rebase. If so we end up recursing once
427 * only into this function.
428 */
429 if (brw_need_rebase( ctx, arrays, ib, min_index )) {
430 vbo_rebase_prims( ctx, arrays,
431 prim, nr_prims,
432 ib, min_index, max_index,
433 brw_draw_prims );
434
435 return;
436 }
437
438
439 /* Make a first attempt at drawing:
440 */
441 retval = brw_try_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
442
443
444 /* This looks like out-of-memory but potentially we have
445 * situation where there is enough memory but it has become
446 * fragmented. Clear out all heaps and start from scratch by
447 * faking a contended lock event: (done elsewhere)
448 */
449 if (!retval && !intel->Fallback && bmError(intel)) {
450 DBG("retrying\n");
451 /* Then try a second time only to upload textures and draw the
452 * primitives:
453 */
454 retval = brw_try_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
455 }
456
457 /* Otherwise, we really are out of memory. Pass the drawing
458 * command to the software tnl module and which will in turn call
459 * swrast to do the drawing.
460 */
461 if (!retval) {
462 _swsetup_Wakeup(ctx);
463 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
464 }
465
466 if (intel->aub_file && (INTEL_DEBUG & DEBUG_SYNC)) {
467 intelFinish( &intel->ctx );
468 intel->aub_wrap = 1;
469 }
470 }
471
472
473 static void brw_invalidate_vbo_cb( struct intel_context *intel, void *ptr )
474 {
475 /* nothing to do, we don't rely on the contents being preserved */
476 }
477
478
479 void brw_draw_init( struct brw_context *brw )
480 {
481 GLcontext *ctx = &brw->intel.ctx;
482 struct vbo_context *vbo = vbo_context(ctx);
483 GLuint i;
484
485 /* Register our drawing function:
486 */
487 vbo->draw_prims = brw_draw_prims;
488
489 brw->vb.upload.size = BRW_UPLOAD_INIT_SIZE;
490
491 for (i = 0; i < BRW_NR_UPLOAD_BUFS; i++) {
492 brw->vb.upload.vbo[i] = ctx->Driver.NewBufferObject(ctx, 1, GL_ARRAY_BUFFER_ARB);
493
494 /* NOTE: These are set to no-backing-store.
495 */
496 bmBufferSetInvalidateCB(&brw->intel,
497 intel_bufferobj_buffer(intel_buffer_object(brw->vb.upload.vbo[i])),
498 brw_invalidate_vbo_cb,
499 &brw->intel,
500 GL_TRUE);
501 }
502
503 ctx->Driver.BufferData( ctx,
504 GL_ARRAY_BUFFER_ARB,
505 BRW_UPLOAD_INIT_SIZE,
506 NULL,
507 GL_DYNAMIC_DRAW_ARB,
508 brw->vb.upload.vbo[0] );
509 }
510
511 void brw_draw_destroy( struct brw_context *brw )
512 {
513 GLcontext *ctx = &brw->intel.ctx;
514 GLuint i;
515
516 for (i = 0; i < BRW_NR_UPLOAD_BUFS; i++)
517 ctx->Driver.DeleteBuffer(ctx, brw->vb.upload.vbo[i]);
518 }