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