i965: fix transform feedback with primitive restart
[profile/ivi/mesa.git] / src / mesa / drivers / dri / i965 / brw_primitive_restart.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Jordan Justen <jordan.l.justen@intel.com>
25  *
26  */
27
28 #include "main/imports.h"
29 #include "main/bufferobj.h"
30
31 #include "brw_context.h"
32 #include "brw_draw.h"
33
34 /**
35  * Check if the hardware's cut index support can handle the primitive
36  * restart index value.
37  */
38 static bool
39 can_cut_index_handle_restart_index(struct gl_context *ctx,
40                                    const struct _mesa_index_buffer *ib)
41 {
42    bool cut_index_will_work;
43
44    switch (ib->type) {
45    case GL_UNSIGNED_BYTE:
46       cut_index_will_work = (ctx->Array.RestartIndex & 0xff) == 0xff;
47       break;
48    case GL_UNSIGNED_SHORT:
49       cut_index_will_work = (ctx->Array.RestartIndex & 0xffff) == 0xffff;
50       break;
51    case GL_UNSIGNED_INT:
52       cut_index_will_work = ctx->Array.RestartIndex == 0xffffffff;
53       break;
54    default:
55       cut_index_will_work = false;
56       assert(0);
57    }
58
59    return cut_index_will_work;
60 }
61
62 /**
63  * Check if the hardware's cut index support can handle the primitive
64  * restart case.
65  */
66 static bool
67 can_cut_index_handle_prims(struct gl_context *ctx,
68                            const struct _mesa_prim *prim,
69                            GLuint nr_prims,
70                            const struct _mesa_index_buffer *ib)
71 {
72    struct brw_context *brw = brw_context(ctx);
73
74    if (brw->sol.counting_primitives_generated ||
75        brw->sol.counting_primitives_written) {
76       /* Counting primitives generated in hardware is not currently
77        * supported, so take the software path. We need to investigate
78        * the *_PRIMITIVES_COUNT registers to allow this to be handled
79        * entirely in hardware.
80        */
81       return false;
82    }
83
84    if (!can_cut_index_handle_restart_index(ctx, ib)) {
85       /* The primitive restart index can't be handled, so take
86        * the software path
87        */
88       return false;
89    }
90
91    for ( ; nr_prims > 0; nr_prims--) {
92       switch(prim->mode) {
93       case GL_POINTS:
94       case GL_LINES:
95       case GL_LINE_STRIP:
96       case GL_TRIANGLES:
97       case GL_TRIANGLE_STRIP:
98          /* Cut index supports these primitive types */
99          break;
100       default:
101          /* Cut index does not support these primitive types */
102       //case GL_LINE_LOOP:
103       //case GL_TRIANGLE_FAN:
104       //case GL_QUADS:
105       //case GL_QUAD_STRIP:
106       //case GL_POLYGON:
107          return false;
108       }
109    }
110
111    return true;
112 }
113
114 /**
115  * Check if primitive restart is enabled, and if so, handle it properly.
116  *
117  * In some cases the support will be handled in software. When available
118  * hardware will handle primitive restart.
119  */
120 GLboolean
121 brw_handle_primitive_restart(struct gl_context *ctx,
122                              const struct _mesa_prim *prim,
123                              GLuint nr_prims,
124                              const struct _mesa_index_buffer *ib)
125 {
126    struct brw_context *brw = brw_context(ctx);
127
128    /* We only need to handle cases where there is an index buffer. */
129    if (ib == NULL) {
130       return GL_FALSE;
131    }
132
133    /* If the driver has requested software handling of primitive restarts,
134     * then the VBO module has already taken care of things, and we can
135     * just draw as normal.
136     */
137    if (ctx->Const.PrimitiveRestartInSoftware) {
138       return GL_FALSE;
139    }
140
141    /* If we have set the in_progress flag, then we are in the middle
142     * of handling the primitive restart draw.
143     */
144    if (brw->prim_restart.in_progress) {
145       return GL_FALSE;
146    }
147
148    /* If PrimitiveRestart is not enabled, then we aren't concerned about
149     * handling this draw.
150     */
151    if (!(ctx->Array.PrimitiveRestart)) {
152       return GL_FALSE;
153    }
154
155    /* Signal that we are in the process of handling the
156     * primitive restart draw
157     */
158    brw->prim_restart.in_progress = true;
159
160    if (can_cut_index_handle_prims(ctx, prim, nr_prims, ib)) {
161       /* Cut index should work for primitive restart, so use it
162        */
163       brw->prim_restart.enable_cut_index = true;
164       brw_draw_prims(ctx, prim, nr_prims, ib, GL_FALSE, -1, -1, NULL);
165       brw->prim_restart.enable_cut_index = false;
166    } else {
167       /* Not all the primitive draw modes are supported by the cut index,
168        * so take the software path
169        */
170       vbo_sw_primitive_restart(ctx, prim, nr_prims, ib);
171    }
172
173    brw->prim_restart.in_progress = false;
174
175    /* The primitive restart draw was completed, so return true. */
176    return GL_TRUE;
177 }
178