OSDN Git Service

i965: Remove software geometry query code.
[android-x86/external-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_defines.h"
33 #include "brw_draw.h"
34
35 #include "intel_batchbuffer.h"
36
37 /**
38  * Check if the hardware's cut index support can handle the primitive
39  * restart index value (pre-Haswell only).
40  */
41 static bool
42 can_cut_index_handle_restart_index(struct gl_context *ctx,
43                                    const struct _mesa_index_buffer *ib)
44 {
45    bool cut_index_will_work;
46
47    switch (ib->type) {
48    case GL_UNSIGNED_BYTE:
49       cut_index_will_work = (ctx->Array._RestartIndex & 0xff) == 0xff;
50       break;
51    case GL_UNSIGNED_SHORT:
52       cut_index_will_work = (ctx->Array._RestartIndex & 0xffff) == 0xffff;
53       break;
54    case GL_UNSIGNED_INT:
55       cut_index_will_work = ctx->Array._RestartIndex == 0xffffffff;
56       break;
57    default:
58       cut_index_will_work = false;
59       assert(0);
60    }
61
62    return cut_index_will_work;
63 }
64
65 /**
66  * Check if the hardware's cut index support can handle the primitive
67  * restart case.
68  */
69 static bool
70 can_cut_index_handle_prims(struct gl_context *ctx,
71                            const struct _mesa_prim *prim,
72                            GLuint nr_prims,
73                            const struct _mesa_index_buffer *ib)
74 {
75    struct intel_context *intel = intel_context(ctx);
76
77    /* Otherwise Haswell can do it all. */
78    if (intel->is_haswell)
79       return true;
80
81    if (!can_cut_index_handle_restart_index(ctx, ib)) {
82       /* The primitive restart index can't be handled, so take
83        * the software path
84        */
85       return false;
86    }
87
88    for ( ; nr_prims > 0; nr_prims--) {
89       switch(prim->mode) {
90       case GL_POINTS:
91       case GL_LINES:
92       case GL_LINE_STRIP:
93       case GL_TRIANGLES:
94       case GL_TRIANGLE_STRIP:
95          /* Cut index supports these primitive types */
96          break;
97       default:
98          /* Cut index does not support these primitive types */
99       //case GL_LINE_LOOP:
100       //case GL_TRIANGLE_FAN:
101       //case GL_QUADS:
102       //case GL_QUAD_STRIP:
103       //case GL_POLYGON:
104          return false;
105       }
106    }
107
108    return true;
109 }
110
111 /**
112  * Check if primitive restart is enabled, and if so, handle it properly.
113  *
114  * In some cases the support will be handled in software. When available
115  * hardware will handle primitive restart.
116  */
117 GLboolean
118 brw_handle_primitive_restart(struct gl_context *ctx,
119                              const struct _mesa_prim *prim,
120                              GLuint nr_prims,
121                              const struct _mesa_index_buffer *ib)
122 {
123    struct brw_context *brw = brw_context(ctx);
124
125    /* We only need to handle cases where there is an index buffer. */
126    if (ib == NULL) {
127       return GL_FALSE;
128    }
129
130    /* If the driver has requested software handling of primitive restarts,
131     * then the VBO module has already taken care of things, and we can
132     * just draw as normal.
133     */
134    if (ctx->Const.PrimitiveRestartInSoftware) {
135       return GL_FALSE;
136    }
137
138    /* If we have set the in_progress flag, then we are in the middle
139     * of handling the primitive restart draw.
140     */
141    if (brw->prim_restart.in_progress) {
142       return GL_FALSE;
143    }
144
145    /* If PrimitiveRestart is not enabled, then we aren't concerned about
146     * handling this draw.
147     */
148    if (!(ctx->Array._PrimitiveRestart)) {
149       return GL_FALSE;
150    }
151
152    /* Signal that we are in the process of handling the
153     * primitive restart draw
154     */
155    brw->prim_restart.in_progress = true;
156
157    if (can_cut_index_handle_prims(ctx, prim, nr_prims, ib)) {
158       /* Cut index should work for primitive restart, so use it
159        */
160       brw->prim_restart.enable_cut_index = true;
161       brw_draw_prims(ctx, prim, nr_prims, ib, GL_FALSE, -1, -1, NULL);
162       brw->prim_restart.enable_cut_index = false;
163    } else {
164       /* Not all the primitive draw modes are supported by the cut index,
165        * so take the software path
166        */
167       vbo_sw_primitive_restart(ctx, prim, nr_prims, ib);
168    }
169
170    brw->prim_restart.in_progress = false;
171
172    /* The primitive restart draw was completed, so return true. */
173    return GL_TRUE;
174 }
175
176 static void
177 haswell_upload_cut_index(struct brw_context *brw)
178 {
179    struct intel_context *intel = &brw->intel;
180    struct gl_context *ctx = &intel->ctx;
181
182    /* Don't trigger on Ivybridge */
183    if (!intel->is_haswell)
184       return;
185
186    const unsigned cut_index_setting =
187       ctx->Array._PrimitiveRestart ? HSW_CUT_INDEX_ENABLE : 0;
188
189    BEGIN_BATCH(2);
190    OUT_BATCH(_3DSTATE_VF << 16 | cut_index_setting | (2 - 2));
191    OUT_BATCH(ctx->Array._RestartIndex);
192    ADVANCE_BATCH();
193 }
194
195 const struct brw_tracked_state haswell_cut_index = {
196    .dirty = {
197       .mesa  = _NEW_TRANSFORM,
198       .brw   = 0,
199       .cache = 0,
200    },
201    .emit = haswell_upload_cut_index,
202 };