2 * Copyright © 2012 Intel Corporation
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:
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
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
24 * Jordan Justen <jordan.l.justen@intel.com>
28 #include "main/imports.h"
29 #include "main/bufferobj.h"
31 #include "brw_context.h"
32 #include "brw_defines.h"
35 #include "intel_batchbuffer.h"
38 * Check if the hardware's cut index support can handle the primitive
39 * restart index value (pre-Haswell only).
42 can_cut_index_handle_restart_index(struct gl_context *ctx,
43 const struct _mesa_index_buffer *ib)
45 bool cut_index_will_work;
48 case GL_UNSIGNED_BYTE:
49 cut_index_will_work = (ctx->Array._RestartIndex & 0xff) == 0xff;
51 case GL_UNSIGNED_SHORT:
52 cut_index_will_work = (ctx->Array._RestartIndex & 0xffff) == 0xffff;
55 cut_index_will_work = ctx->Array._RestartIndex == 0xffffffff;
58 cut_index_will_work = false;
62 return cut_index_will_work;
66 * Check if the hardware's cut index support can handle the primitive
70 can_cut_index_handle_prims(struct gl_context *ctx,
71 const struct _mesa_prim *prim,
73 const struct _mesa_index_buffer *ib)
75 struct intel_context *intel = intel_context(ctx);
77 /* Otherwise Haswell can do it all. */
78 if (intel->is_haswell)
81 if (!can_cut_index_handle_restart_index(ctx, ib)) {
82 /* The primitive restart index can't be handled, so take
88 for ( ; nr_prims > 0; nr_prims--) {
94 case GL_TRIANGLE_STRIP:
95 /* Cut index supports these primitive types */
98 /* Cut index does not support these primitive types */
100 //case GL_TRIANGLE_FAN:
102 //case GL_QUAD_STRIP:
112 * Check if primitive restart is enabled, and if so, handle it properly.
114 * In some cases the support will be handled in software. When available
115 * hardware will handle primitive restart.
118 brw_handle_primitive_restart(struct gl_context *ctx,
119 const struct _mesa_prim *prim,
121 const struct _mesa_index_buffer *ib)
123 struct brw_context *brw = brw_context(ctx);
125 /* We only need to handle cases where there is an index buffer. */
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.
134 if (ctx->Const.PrimitiveRestartInSoftware) {
138 /* If we have set the in_progress flag, then we are in the middle
139 * of handling the primitive restart draw.
141 if (brw->prim_restart.in_progress) {
145 /* If PrimitiveRestart is not enabled, then we aren't concerned about
146 * handling this draw.
148 if (!(ctx->Array._PrimitiveRestart)) {
152 /* Signal that we are in the process of handling the
153 * primitive restart draw
155 brw->prim_restart.in_progress = true;
157 if (can_cut_index_handle_prims(ctx, prim, nr_prims, ib)) {
158 /* Cut index should work for primitive restart, so use it
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;
164 /* Not all the primitive draw modes are supported by the cut index,
165 * so take the software path
167 vbo_sw_primitive_restart(ctx, prim, nr_prims, ib);
170 brw->prim_restart.in_progress = false;
172 /* The primitive restart draw was completed, so return true. */
177 haswell_upload_cut_index(struct brw_context *brw)
179 struct intel_context *intel = &brw->intel;
180 struct gl_context *ctx = &intel->ctx;
182 /* Don't trigger on Ivybridge */
183 if (!intel->is_haswell)
186 const unsigned cut_index_setting =
187 ctx->Array._PrimitiveRestart ? HSW_CUT_INDEX_ENABLE : 0;
190 OUT_BATCH(_3DSTATE_VF << 16 | cut_index_setting | (2 - 2));
191 OUT_BATCH(ctx->Array._RestartIndex);
195 const struct brw_tracked_state haswell_cut_index = {
197 .mesa = _NEW_TRANSFORM,
201 .emit = haswell_upload_cut_index,