OSDN Git Service

10581b3b36c77f2d2f9d8ea61d354c530d906e23
[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    struct brw_context *brw = brw_context(ctx);
77
78    if (brw->sol.counting_primitives_generated ||
79        brw->sol.counting_primitives_written) {
80       /* Counting primitives generated in hardware is not currently
81        * supported, so take the software path. We need to investigate
82        * the *_PRIMITIVES_COUNT registers to allow this to be handled
83        * entirely in hardware.
84        */
85       return false;
86    }
87
88    /* Otherwise Haswell can do it all. */
89    if (intel->is_haswell)
90       return true;
91
92    if (!can_cut_index_handle_restart_index(ctx, ib)) {
93       /* The primitive restart index can't be handled, so take
94        * the software path
95        */
96       return false;
97    }
98
99    for ( ; nr_prims > 0; nr_prims--) {
100       switch(prim->mode) {
101       case GL_POINTS:
102       case GL_LINES:
103       case GL_LINE_STRIP:
104       case GL_TRIANGLES:
105       case GL_TRIANGLE_STRIP:
106          /* Cut index supports these primitive types */
107          break;
108       default:
109          /* Cut index does not support these primitive types */
110       //case GL_LINE_LOOP:
111       //case GL_TRIANGLE_FAN:
112       //case GL_QUADS:
113       //case GL_QUAD_STRIP:
114       //case GL_POLYGON:
115          return false;
116       }
117    }
118
119    return true;
120 }
121
122 /**
123  * Check if primitive restart is enabled, and if so, handle it properly.
124  *
125  * In some cases the support will be handled in software. When available
126  * hardware will handle primitive restart.
127  */
128 GLboolean
129 brw_handle_primitive_restart(struct gl_context *ctx,
130                              const struct _mesa_prim *prim,
131                              GLuint nr_prims,
132                              const struct _mesa_index_buffer *ib)
133 {
134    struct brw_context *brw = brw_context(ctx);
135
136    /* We only need to handle cases where there is an index buffer. */
137    if (ib == NULL) {
138       return GL_FALSE;
139    }
140
141    /* If the driver has requested software handling of primitive restarts,
142     * then the VBO module has already taken care of things, and we can
143     * just draw as normal.
144     */
145    if (ctx->Const.PrimitiveRestartInSoftware) {
146       return GL_FALSE;
147    }
148
149    /* If we have set the in_progress flag, then we are in the middle
150     * of handling the primitive restart draw.
151     */
152    if (brw->prim_restart.in_progress) {
153       return GL_FALSE;
154    }
155
156    /* If PrimitiveRestart is not enabled, then we aren't concerned about
157     * handling this draw.
158     */
159    if (!(ctx->Array._PrimitiveRestart)) {
160       return GL_FALSE;
161    }
162
163    /* Signal that we are in the process of handling the
164     * primitive restart draw
165     */
166    brw->prim_restart.in_progress = true;
167
168    if (can_cut_index_handle_prims(ctx, prim, nr_prims, ib)) {
169       /* Cut index should work for primitive restart, so use it
170        */
171       brw->prim_restart.enable_cut_index = true;
172       brw_draw_prims(ctx, prim, nr_prims, ib, GL_FALSE, -1, -1, NULL);
173       brw->prim_restart.enable_cut_index = false;
174    } else {
175       /* Not all the primitive draw modes are supported by the cut index,
176        * so take the software path
177        */
178       vbo_sw_primitive_restart(ctx, prim, nr_prims, ib);
179    }
180
181    brw->prim_restart.in_progress = false;
182
183    /* The primitive restart draw was completed, so return true. */
184    return GL_TRUE;
185 }
186
187 static void
188 haswell_upload_cut_index(struct brw_context *brw)
189 {
190    struct intel_context *intel = &brw->intel;
191    struct gl_context *ctx = &intel->ctx;
192
193    /* Don't trigger on Ivybridge */
194    if (!intel->is_haswell)
195       return;
196
197    const unsigned cut_index_setting =
198       ctx->Array._PrimitiveRestart ? HSW_CUT_INDEX_ENABLE : 0;
199
200    BEGIN_BATCH(2);
201    OUT_BATCH(_3DSTATE_VF << 16 | cut_index_setting | (2 - 2));
202    OUT_BATCH(ctx->Array._RestartIndex);
203    ADVANCE_BATCH();
204 }
205
206 const struct brw_tracked_state haswell_cut_index = {
207    .dirty = {
208       .mesa  = _NEW_TRANSFORM,
209       .brw   = 0,
210       .cache = 0,
211    },
212    .emit = haswell_upload_cut_index,
213 };