OSDN Git Service

i965: Split brw_set_prim into brw/gen6 variants
[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/condrender.h"
32 #include "main/samplerobj.h"
33 #include "main/state.h"
34 #include "main/enums.h"
35 #include "tnl/tnl.h"
36 #include "vbo/vbo_context.h"
37 #include "swrast/swrast.h"
38 #include "swrast_setup/swrast_setup.h"
39
40 #include "brw_draw.h"
41 #include "brw_defines.h"
42 #include "brw_context.h"
43 #include "brw_state.h"
44
45 #include "intel_batchbuffer.h"
46
47 #define FILE_DEBUG_FLAG DEBUG_PRIMS
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,
83                            const struct _mesa_prim *prim)
84 {
85    struct gl_context *ctx = &brw->intel.ctx;
86    GLenum mode = prim->mode;
87
88    DBG("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim->mode));
89
90    /* Slight optimization to avoid the GS program when not needed:
91     */
92    if (mode == GL_QUAD_STRIP &&
93        ctx->Light.ShadeModel != GL_FLAT &&
94        ctx->Polygon.FrontMode == GL_FILL &&
95        ctx->Polygon.BackMode == GL_FILL)
96       mode = GL_TRIANGLE_STRIP;
97
98    if (prim->mode == GL_QUADS && prim->count == 4 &&
99        ctx->Light.ShadeModel != GL_FLAT &&
100        ctx->Polygon.FrontMode == GL_FILL &&
101        ctx->Polygon.BackMode == GL_FILL) {
102       mode = GL_TRIANGLE_FAN;
103    }
104
105    if (mode != brw->primitive) {
106       brw->primitive = mode;
107       brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;
108
109       if (reduced_prim[mode] != brw->intel.reduced_primitive) {
110          brw->intel.reduced_primitive = reduced_prim[mode];
111          brw->state.dirty.brw |= BRW_NEW_REDUCED_PRIMITIVE;
112       }
113    }
114
115    return prim_to_hw_prim[mode];
116 }
117
118 static GLuint gen6_set_prim(struct brw_context *brw,
119                             const struct _mesa_prim *prim)
120 {
121    DBG("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim->mode));
122
123    if (prim->mode != brw->primitive) {
124       brw->primitive = prim->mode;
125       brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;
126    }
127
128    return prim_to_hw_prim[mode];
129 }
130
131
132 static GLuint trim(GLenum prim, GLuint length)
133 {
134    if (prim == GL_QUAD_STRIP)
135       return length > 3 ? (length - length % 2) : 0;
136    else if (prim == GL_QUADS)
137       return length - length % 4;
138    else 
139       return length;
140 }
141
142
143 static void brw_emit_prim(struct brw_context *brw,
144                           const struct _mesa_prim *prim,
145                           uint32_t hw_prim)
146 {
147    struct intel_context *intel = &brw->intel;
148    int verts_per_instance;
149    int vertex_access_type;
150    int start_vertex_location;
151    int base_vertex_location;
152
153    DBG("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim->mode),
154        prim->start, prim->count);
155
156    start_vertex_location = prim->start;
157    base_vertex_location = prim->basevertex;
158    if (prim->indexed) {
159       vertex_access_type = GEN4_3DPRIM_VERTEXBUFFER_ACCESS_RANDOM;
160       start_vertex_location += brw->ib.start_vertex_offset;
161       base_vertex_location += brw->vb.start_vertex_bias;
162    } else {
163       vertex_access_type = GEN4_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL;
164       start_vertex_location += brw->vb.start_vertex_bias;
165    }
166
167    verts_per_instance = trim(prim->mode, prim->count);
168
169    /* If nothing to emit, just return. */
170    if (verts_per_instance == 0)
171       return;
172
173    /* If we're set to always flush, do it before and after the primitive emit.
174     * We want to catch both missed flushes that hurt instruction/state cache
175     * and missed flushes of the render cache as it heads to other parts of
176     * the besides the draw code.
177     */
178    if (intel->always_flush_cache) {
179       intel_batchbuffer_emit_mi_flush(intel);
180    }
181
182    BEGIN_BATCH(6);
183    OUT_BATCH(CMD_3D_PRIM << 16 | (6 - 2) |
184              hw_prim << GEN4_3DPRIM_TOPOLOGY_TYPE_SHIFT |
185              vertex_access_type);
186    OUT_BATCH(verts_per_instance);
187    OUT_BATCH(start_vertex_location);
188    OUT_BATCH(1); // instance count
189    OUT_BATCH(0); // start instance location
190    OUT_BATCH(base_vertex_location);
191    ADVANCE_BATCH();
192
193    intel->batch.need_workaround_flush = true;
194
195    if (intel->always_flush_cache) {
196       intel_batchbuffer_emit_mi_flush(intel);
197    }
198 }
199
200 static void gen7_emit_prim(struct brw_context *brw,
201                            const struct _mesa_prim *prim,
202                            uint32_t hw_prim)
203 {
204    struct intel_context *intel = &brw->intel;
205    int verts_per_instance;
206    int vertex_access_type;
207    int start_vertex_location;
208    int base_vertex_location;
209
210    DBG("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim->mode),
211        prim->start, prim->count);
212
213    start_vertex_location = prim->start;
214    base_vertex_location = prim->basevertex;
215    if (prim->indexed) {
216       vertex_access_type = GEN7_3DPRIM_VERTEXBUFFER_ACCESS_RANDOM;
217       start_vertex_location += brw->ib.start_vertex_offset;
218       base_vertex_location += brw->vb.start_vertex_bias;
219    } else {
220       vertex_access_type = GEN7_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL;
221       start_vertex_location += brw->vb.start_vertex_bias;
222    }
223
224    verts_per_instance = trim(prim->mode, prim->count);
225
226    /* If nothing to emit, just return. */
227    if (verts_per_instance == 0)
228       return;
229
230    /* If we're set to always flush, do it before and after the primitive emit.
231     * We want to catch both missed flushes that hurt instruction/state cache
232     * and missed flushes of the render cache as it heads to other parts of
233     * the besides the draw code.
234     */
235    if (intel->always_flush_cache) {
236       intel_batchbuffer_emit_mi_flush(intel);
237    }
238
239    BEGIN_BATCH(7);
240    OUT_BATCH(CMD_3D_PRIM << 16 | (7 - 2));
241    OUT_BATCH(hw_prim | vertex_access_type);
242    OUT_BATCH(verts_per_instance);
243    OUT_BATCH(start_vertex_location);
244    OUT_BATCH(1); // instance count
245    OUT_BATCH(0); // start instance location
246    OUT_BATCH(base_vertex_location);
247    ADVANCE_BATCH();
248
249    if (intel->always_flush_cache) {
250       intel_batchbuffer_emit_mi_flush(intel);
251    }
252 }
253
254
255 static void brw_merge_inputs( struct brw_context *brw,
256                        const struct gl_client_array *arrays[])
257 {
258    struct brw_vertex_info old = brw->vb.info;
259    GLuint i;
260
261    for (i = 0; i < brw->vb.nr_buffers; i++) {
262       drm_intel_bo_unreference(brw->vb.buffers[i].bo);
263       brw->vb.buffers[i].bo = NULL;
264    }
265    brw->vb.nr_buffers = 0;
266
267    memset(&brw->vb.info, 0, sizeof(brw->vb.info));
268
269    for (i = 0; i < VERT_ATTRIB_MAX; i++) {
270       brw->vb.inputs[i].buffer = -1;
271       brw->vb.inputs[i].glarray = arrays[i];
272       brw->vb.inputs[i].attrib = (gl_vert_attrib) i;
273
274       if (arrays[i]->StrideB != 0)
275          brw->vb.info.sizes[i/16] |= (brw->vb.inputs[i].glarray->Size - 1) <<
276             ((i%16) * 2);
277    }
278
279    /* Raise statechanges if input sizes have changed. */
280    if (memcmp(brw->vb.info.sizes, old.sizes, sizeof(old.sizes)) != 0)
281       brw->state.dirty.brw |= BRW_NEW_INPUT_DIMENSIONS;
282 }
283
284 /* May fail if out of video memory for texture or vbo upload, or on
285  * fallback conditions.
286  */
287 static GLboolean brw_try_draw_prims( struct gl_context *ctx,
288                                      const struct gl_client_array *arrays[],
289                                      const struct _mesa_prim *prim,
290                                      GLuint nr_prims,
291                                      const struct _mesa_index_buffer *ib,
292                                      GLuint min_index,
293                                      GLuint max_index )
294 {
295    struct intel_context *intel = intel_context(ctx);
296    struct brw_context *brw = brw_context(ctx);
297    GLboolean retval = GL_FALSE;
298    GLboolean warn = GL_FALSE;
299    GLuint i;
300
301    if (ctx->NewState)
302       _mesa_update_state( ctx );
303
304    /* We have to validate the textures *before* checking for fallbacks;
305     * otherwise, the software fallback won't be able to rely on the
306     * texture state, the firstLevel and lastLevel fields won't be
307     * set in the intel texture object (they'll both be 0), and the 
308     * software fallback will segfault if it attempts to access any
309     * texture level other than level 0.
310     */
311    brw_validate_textures( brw );
312
313    /* Bind all inputs, derive varying and size information:
314     */
315    brw_merge_inputs( brw, arrays );
316
317    brw->ib.ib = ib;
318    brw->state.dirty.brw |= BRW_NEW_INDICES;
319
320    brw->vb.min_index = min_index;
321    brw->vb.max_index = max_index;
322    brw->state.dirty.brw |= BRW_NEW_VERTICES;
323
324    /* Have to validate state quite late.  Will rebuild tnl_program,
325     * which depends on varying information.  
326     * 
327     * Note this is where brw->vs->prog_data.inputs_read is calculated,
328     * so can't access it earlier.
329     */
330
331    intel_prepare_render(intel);
332
333    for (i = 0; i < nr_prims; i++) {
334       uint32_t hw_prim;
335       int estimated_max_prim_size;
336
337       estimated_max_prim_size = 512; /* batchbuffer commands */
338       estimated_max_prim_size += (BRW_MAX_TEX_UNIT *
339                                   (sizeof(struct brw_sampler_state) +
340                                    sizeof(struct gen5_sampler_default_color)));
341       estimated_max_prim_size += 1024; /* gen6 VS push constants */
342       estimated_max_prim_size += 1024; /* gen6 WM push constants */
343       estimated_max_prim_size += 512; /* misc. pad */
344
345       /* Flush the batch if it's approaching full, so that we don't wrap while
346        * we've got validated state that needs to be in the same batch as the
347        * primitives.
348        */
349       intel_batchbuffer_require_space(intel, estimated_max_prim_size, false);
350
351       if (intel->gen < 6)
352          hw_prim = brw_set_prim(brw, &prim[i]);
353       else
354          hw_prim = gen6_set_prim(brw, &prim[i]);
355
356       if (brw->state.dirty.brw) {
357          brw_validate_state(brw);
358
359          /* Various fallback checks:  */
360          if (brw->intel.Fallback)
361             goto out;
362
363          /* Check that we can fit our state in with our existing batchbuffer, or
364           * flush otherwise.
365           */
366          if (dri_bufmgr_check_aperture_space(brw->state.validated_bos,
367                                              brw->state.validated_bo_count)) {
368             static GLboolean warned;
369             intel_batchbuffer_flush(intel);
370
371             /* Validate the state after we flushed the batch (which would have
372              * changed the set of dirty state).  If we still fail to
373              * check_aperture, warn of what's happening, but attempt to continue
374              * on since it may succeed anyway, and the user would probably rather
375              * see a failure and a warning than a fallback.
376              */
377             brw_validate_state(brw);
378             if (!warned &&
379                 dri_bufmgr_check_aperture_space(brw->state.validated_bos,
380                                                 brw->state.validated_bo_count)) {
381                warn = GL_TRUE;
382                warned = GL_TRUE;
383             }
384          }
385
386          intel->no_batch_wrap = GL_TRUE;
387          brw_upload_state(brw);
388       }
389
390       if (intel->gen >= 7)
391          gen7_emit_prim(brw, &prim[i], hw_prim);
392       else
393          brw_emit_prim(brw, &prim[i], hw_prim);
394
395       intel->no_batch_wrap = GL_FALSE;
396
397       retval = GL_TRUE;
398    }
399
400    if (intel->always_flush_batch)
401       intel_batchbuffer_flush(intel);
402  out:
403
404    brw_state_cache_check_size(brw);
405
406    if (warn)
407       fprintf(stderr, "i965: Single primitive emit potentially exceeded "
408               "available aperture space\n");
409
410    if (!retval)
411       DBG("%s failed\n", __FUNCTION__);
412
413    return retval;
414 }
415
416 void brw_draw_prims( struct gl_context *ctx,
417                      const struct gl_client_array *arrays[],
418                      const struct _mesa_prim *prim,
419                      GLuint nr_prims,
420                      const struct _mesa_index_buffer *ib,
421                      GLboolean index_bounds_valid,
422                      GLuint min_index,
423                      GLuint max_index )
424 {
425    GLboolean retval;
426
427    if (!_mesa_check_conditional_render(ctx))
428       return;
429
430    if (!vbo_all_varyings_in_vbos(arrays)) {
431       if (!index_bounds_valid)
432          vbo_get_minmax_index(ctx, prim, ib, &min_index, &max_index);
433
434       /* Decide if we want to rebase.  If so we end up recursing once
435        * only into this function.
436        */
437       if (min_index != 0 && !vbo_any_varyings_in_vbos(arrays)) {
438          vbo_rebase_prims(ctx, arrays,
439                           prim, nr_prims,
440                           ib, min_index, max_index,
441                           brw_draw_prims );
442          return;
443       }
444    }
445
446    /* Make a first attempt at drawing:
447     */
448    retval = brw_try_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
449
450    /* Otherwise, we really are out of memory.  Pass the drawing
451     * command to the software tnl module and which will in turn call
452     * swrast to do the drawing.
453     */
454    if (!retval) {
455        _swsetup_Wakeup(ctx);
456        _tnl_wakeup(ctx);
457       _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
458    }
459
460 }
461
462 void brw_draw_init( struct brw_context *brw )
463 {
464    struct gl_context *ctx = &brw->intel.ctx;
465    struct vbo_context *vbo = vbo_context(ctx);
466    int i;
467
468    /* Register our drawing function: 
469     */
470    vbo->draw_prims = brw_draw_prims;
471
472    for (i = 0; i < VERT_ATTRIB_MAX; i++)
473       brw->vb.inputs[i].buffer = -1;
474    brw->vb.nr_buffers = 0;
475    brw->vb.nr_enabled = 0;
476 }
477
478 void brw_draw_destroy( struct brw_context *brw )
479 {
480    int i;
481
482    for (i = 0; i < brw->vb.nr_buffers; i++) {
483       drm_intel_bo_unreference(brw->vb.buffers[i].bo);
484       brw->vb.buffers[i].bo = NULL;
485    }
486    brw->vb.nr_buffers = 0;
487
488    for (i = 0; i < brw->vb.nr_enabled; i++) {
489       brw->vb.enabled[i]->buffer = -1;
490    }
491    brw->vb.nr_enabled = 0;
492
493    drm_intel_bo_unreference(brw->ib.bo);
494    brw->ib.bo = NULL;
495 }