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);
76 struct brw_context *brw = brw_context(ctx);
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.
88 /* Otherwise Haswell can do it all. */
89 if (intel->is_haswell)
92 if (!can_cut_index_handle_restart_index(ctx, ib)) {
93 /* The primitive restart index can't be handled, so take
99 for ( ; nr_prims > 0; nr_prims--) {
105 case GL_TRIANGLE_STRIP:
106 /* Cut index supports these primitive types */
109 /* Cut index does not support these primitive types */
111 //case GL_TRIANGLE_FAN:
113 //case GL_QUAD_STRIP:
123 * Check if primitive restart is enabled, and if so, handle it properly.
125 * In some cases the support will be handled in software. When available
126 * hardware will handle primitive restart.
129 brw_handle_primitive_restart(struct gl_context *ctx,
130 const struct _mesa_prim *prim,
132 const struct _mesa_index_buffer *ib)
134 struct brw_context *brw = brw_context(ctx);
136 /* We only need to handle cases where there is an index buffer. */
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.
145 if (ctx->Const.PrimitiveRestartInSoftware) {
149 /* If we have set the in_progress flag, then we are in the middle
150 * of handling the primitive restart draw.
152 if (brw->prim_restart.in_progress) {
156 /* If PrimitiveRestart is not enabled, then we aren't concerned about
157 * handling this draw.
159 if (!(ctx->Array._PrimitiveRestart)) {
163 /* Signal that we are in the process of handling the
164 * primitive restart draw
166 brw->prim_restart.in_progress = true;
168 if (can_cut_index_handle_prims(ctx, prim, nr_prims, ib)) {
169 /* Cut index should work for primitive restart, so use it
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;
175 /* Not all the primitive draw modes are supported by the cut index,
176 * so take the software path
178 vbo_sw_primitive_restart(ctx, prim, nr_prims, ib);
181 brw->prim_restart.in_progress = false;
183 /* The primitive restart draw was completed, so return true. */
188 haswell_upload_cut_index(struct brw_context *brw)
190 struct intel_context *intel = &brw->intel;
191 struct gl_context *ctx = &intel->ctx;
193 /* Don't trigger on Ivybridge */
194 if (!intel->is_haswell)
197 const unsigned cut_index_setting =
198 ctx->Array._PrimitiveRestart ? HSW_CUT_INDEX_ENABLE : 0;
201 OUT_BATCH(_3DSTATE_VF << 16 | cut_index_setting | (2 - 2));
202 OUT_BATCH(ctx->Array._RestartIndex);
206 const struct brw_tracked_state haswell_cut_index = {
208 .mesa = _NEW_TRANSFORM,
212 .emit = haswell_upload_cut_index,