OSDN Git Service

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