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"
30 #include "main/varray.h"
32 #include "brw_context.h"
33 #include "brw_defines.h"
36 #include "intel_batchbuffer.h"
39 * Check if the hardware's cut index support can handle the primitive
40 * restart index value (pre-Haswell only).
43 can_cut_index_handle_restart_index(struct gl_context
*ctx
,
44 const struct _mesa_index_buffer
*ib
)
46 /* The FixedIndex variant means 0xFF, 0xFFFF, or 0xFFFFFFFF based on
47 * the index buffer type, which corresponds exactly to the hardware.
49 if (ctx
->Array
.PrimitiveRestartFixedIndex
)
52 bool cut_index_will_work
;
55 case GL_UNSIGNED_BYTE
:
56 cut_index_will_work
= ctx
->Array
.RestartIndex
== 0xff;
58 case GL_UNSIGNED_SHORT
:
59 cut_index_will_work
= ctx
->Array
.RestartIndex
== 0xffff;
62 cut_index_will_work
= ctx
->Array
.RestartIndex
== 0xffffffff;
65 cut_index_will_work
= false;
69 return cut_index_will_work
;
73 * Check if the hardware's cut index support can handle the primitive
77 can_cut_index_handle_prims(struct gl_context
*ctx
,
78 const struct _mesa_prim
*prim
,
80 const struct _mesa_index_buffer
*ib
)
82 struct brw_context
*brw
= brw_context(ctx
);
84 /* Otherwise Haswell can do it all. */
85 if (brw
->gen
>= 8 || brw
->is_haswell
)
88 if (!can_cut_index_handle_restart_index(ctx
, ib
)) {
89 /* The primitive restart index can't be handled, so take
95 for ( ; nr_prims
> 0; nr_prims
--) {
101 case GL_TRIANGLE_STRIP
:
102 /* Cut index supports these primitive types */
105 /* Cut index does not support these primitive types */
107 //case GL_TRIANGLE_FAN:
109 //case GL_QUAD_STRIP:
119 * Check if primitive restart is enabled, and if so, handle it properly.
121 * In some cases the support will be handled in software. When available
122 * hardware will handle primitive restart.
125 brw_handle_primitive_restart(struct gl_context
*ctx
,
126 const struct _mesa_prim
*prim
,
128 const struct _mesa_index_buffer
*ib
)
130 struct brw_context
*brw
= brw_context(ctx
);
132 /* We only need to handle cases where there is an index buffer. */
137 /* If the driver has requested software handling of primitive restarts,
138 * then the VBO module has already taken care of things, and we can
139 * just draw as normal.
141 if (ctx
->Const
.PrimitiveRestartInSoftware
) {
145 /* If we have set the in_progress flag, then we are in the middle
146 * of handling the primitive restart draw.
148 if (brw
->prim_restart
.in_progress
) {
152 /* If PrimitiveRestart is not enabled, then we aren't concerned about
153 * handling this draw.
155 if (!(ctx
->Array
._PrimitiveRestart
)) {
159 /* Signal that we are in the process of handling the
160 * primitive restart draw
162 brw
->prim_restart
.in_progress
= true;
164 if (can_cut_index_handle_prims(ctx
, prim
, nr_prims
, ib
)) {
165 /* Cut index should work for primitive restart, so use it
167 brw
->prim_restart
.enable_cut_index
= true;
168 brw_draw_prims(ctx
, prim
, nr_prims
, ib
, GL_FALSE
, -1, -1, NULL
);
169 brw
->prim_restart
.enable_cut_index
= false;
171 /* Not all the primitive draw modes are supported by the cut index,
172 * so take the software path
174 vbo_sw_primitive_restart(ctx
, prim
, nr_prims
, ib
);
177 brw
->prim_restart
.in_progress
= false;
179 /* The primitive restart draw was completed, so return true. */
184 haswell_upload_cut_index(struct brw_context
*brw
)
186 struct gl_context
*ctx
= &brw
->ctx
;
188 /* Don't trigger on Ivybridge */
189 if (!brw
->is_haswell
)
192 const unsigned cut_index_setting
=
193 ctx
->Array
._PrimitiveRestart
? HSW_CUT_INDEX_ENABLE
: 0;
195 /* BRW_NEW_INDEX_BUFFER */
198 cut_index
= _mesa_primitive_restart_index(ctx
, brw
->ib
.type
);
200 /* There's no index buffer, but primitive restart may still apply
201 * to glDrawArrays and such. FIXED_INDEX mode only applies to drawing
202 * operations that use an index buffer, so we can ignore it and use
203 * the GL restart index directly.
205 cut_index
= ctx
->Array
.RestartIndex
;
209 OUT_BATCH(_3DSTATE_VF
<< 16 | cut_index_setting
| (2 - 2));
210 OUT_BATCH(cut_index
);
214 const struct brw_tracked_state haswell_cut_index
= {
216 .mesa
= _NEW_TRANSFORM
,
217 .brw
= BRW_NEW_INDEX_BUFFER
,
220 .emit
= haswell_upload_cut_index
,