2 * Copyright 2007 VMware, Inc.
5 * Copyright © 2012 Intel Corporation
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 * Jordan Justen <jordan.l.justen@intel.com>
31 #include "main/imports.h"
32 #include "main/bufferobj.h"
33 #include "main/macros.h"
34 #include "main/varray.h"
37 #include "vbo_private.h"
40 #define UPDATE_MIN2(a, b) (a) = MIN2((a), (b))
41 #define UPDATE_MAX2(a, b) (a) = MAX2((a), (b))
44 * Notes on primitive restart:
45 * The code below is used when the driver does not fully support primitive
46 * restart (for example, if it only does restart index of ~0).
48 * We map the index buffer, find the restart indexes, unmap
49 * the index buffer then draw the sub-primitives delineated by the restarts.
51 * A couple possible optimizations:
52 * 1. Save the list of sub-primitive (start, count) values in a list attached
53 * to the index buffer for re-use in subsequent draws. The list would be
54 * invalidated when the contents of the buffer changed.
55 * 2. If drawing triangle strips or quad strips, create a new index buffer
56 * that uses duplicated vertices to render the disjoint strips as one
57 * long strip. We'd have to be careful to avoid using too much memory
60 * Finally, some apps might perform better if they don't use primitive restart
61 * at all rather than this fallback path. Set MESA_EXTENSION_OVERRIDE to
62 * "-GL_NV_primitive_restart" to test that.
76 * Scan the elements array to find restart indexes. Return an array
77 * of struct sub_primitive to indicate how to draw the sub-primitives
78 * are delineated by the restart index.
80 static struct sub_primitive
*
81 find_sub_primitives(const void *elements
, unsigned element_size
,
82 unsigned start
, unsigned end
, unsigned restart_index
,
83 unsigned *num_sub_prims
)
85 const unsigned max_prims
= end
- start
;
86 struct sub_primitive
*sub_prims
;
87 unsigned i
, cur_start
, cur_count
;
92 malloc(max_prims
* sizeof(struct sub_primitive
));
103 #define IB_INDEX_READ(TYPE, INDEX) (((const GL##TYPE *) elements)[INDEX])
105 #define SCAN_ELEMENTS(TYPE) \
106 sub_prims[scan_num].min_index = (GL##TYPE) 0xffffffff; \
107 sub_prims[scan_num].max_index = 0; \
108 for (i = start; i < end; i++) { \
109 scan_index = IB_INDEX_READ(TYPE, i); \
110 if (scan_index == restart_index) { \
111 if (cur_count > 0) { \
112 assert(scan_num < max_prims); \
113 sub_prims[scan_num].start = cur_start; \
114 sub_prims[scan_num].count = cur_count; \
116 sub_prims[scan_num].min_index = (GL##TYPE) 0xffffffff; \
117 sub_prims[scan_num].max_index = 0; \
123 UPDATE_MIN2(sub_prims[scan_num].min_index, scan_index); \
124 UPDATE_MAX2(sub_prims[scan_num].max_index, scan_index); \
128 if (cur_count > 0) { \
129 assert(scan_num < max_prims); \
130 sub_prims[scan_num].start = cur_start; \
131 sub_prims[scan_num].count = cur_count; \
135 switch (element_size
) {
137 SCAN_ELEMENTS(ubyte
);
140 SCAN_ELEMENTS(ushort
);
146 assert(0 && "bad index_size in find_sub_primitives()");
151 *num_sub_prims
= scan_num
;
158 * Handle primitive restart in software.
160 * This function breaks up calls into the driver so primitive restart
161 * support is not required in the driver.
164 vbo_sw_primitive_restart(struct gl_context
*ctx
,
165 const struct _mesa_prim
*prims
,
167 const struct _mesa_index_buffer
*ib
,
168 struct gl_buffer_object
*indirect
)
171 struct _mesa_prim new_prim
;
172 struct _mesa_index_buffer new_ib
;
173 struct sub_primitive
*sub_prims
;
174 struct sub_primitive
*sub_prim
;
175 GLuint num_sub_prims
;
178 GLuint sub_end_index
;
179 GLuint restart_index
= _mesa_primitive_restart_index(ctx
, ib
->index_size
);
180 struct _mesa_prim temp_prim
;
181 struct vbo_context
*vbo
= vbo_context(ctx
);
182 vbo_draw_func draw_prims_func
= vbo
->draw_prims
;
183 GLboolean map_ib
= ib
->obj
->Name
&& !ib
->obj
->Mappings
[MAP_INTERNAL
].Pointer
;
186 /* If there is an indirect buffer, map it and extract the draw params */
187 if (indirect
&& prims
[0].is_indirect
) {
188 const uint32_t *indirect_params
;
189 if (!ctx
->Driver
.MapBufferRange(ctx
, 0, indirect
->Size
, GL_MAP_READ_BIT
,
190 indirect
, MAP_INTERNAL
)) {
192 /* something went wrong with mapping, give up */
193 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
194 "failed to map indirect buffer for sw primitive restart");
198 assert(nr_prims
== 1);
200 indirect_params
= (const uint32_t *)
201 ADD_POINTERS(indirect
->Mappings
[MAP_INTERNAL
].Pointer
,
202 new_prim
.indirect_offset
);
204 new_prim
.is_indirect
= 0;
205 new_prim
.count
= indirect_params
[0];
206 new_prim
.num_instances
= indirect_params
[1];
207 new_prim
.start
= indirect_params
[2];
208 new_prim
.basevertex
= indirect_params
[3];
209 new_prim
.base_instance
= indirect_params
[4];
212 new_ib
.count
= new_prim
.count
;
217 ctx
->Driver
.UnmapBuffer(ctx
, indirect
, MAP_INTERNAL
);
220 /* Find the sub-primitives. These are regions in the index buffer which
221 * are split based on the primitive restart index value.
224 ctx
->Driver
.MapBufferRange(ctx
, 0, ib
->obj
->Size
, GL_MAP_READ_BIT
,
225 ib
->obj
, MAP_INTERNAL
);
228 ptr
= ADD_POINTERS(ib
->obj
->Mappings
[MAP_INTERNAL
].Pointer
, ib
->ptr
);
230 sub_prims
= find_sub_primitives(ptr
, ib
->index_size
,
231 0, ib
->count
, restart_index
,
235 ctx
->Driver
.UnmapBuffer(ctx
, ib
->obj
, MAP_INTERNAL
);
238 /* Loop over the primitives, and use the located sub-primitives to draw
239 * each primitive with a break to implement each primitive restart.
241 for (prim_num
= 0; prim_num
< nr_prims
; prim_num
++) {
242 end_index
= prims
[prim_num
].start
+ prims
[prim_num
].count
;
243 memcpy(&temp_prim
, &prims
[prim_num
], sizeof (temp_prim
));
244 /* Loop over the sub-primitives drawing sub-ranges of the primitive. */
245 for (sub_prim_num
= 0; sub_prim_num
< num_sub_prims
; sub_prim_num
++) {
246 sub_prim
= &sub_prims
[sub_prim_num
];
247 sub_end_index
= sub_prim
->start
+ sub_prim
->count
;
248 if (prims
[prim_num
].start
<= sub_prim
->start
) {
249 temp_prim
.start
= MAX2(prims
[prim_num
].start
, sub_prim
->start
);
250 temp_prim
.count
= MIN2(sub_end_index
, end_index
) - temp_prim
.start
;
251 if ((temp_prim
.start
== sub_prim
->start
) &&
252 (temp_prim
.count
== sub_prim
->count
)) {
253 draw_prims_func(ctx
, &temp_prim
, 1, ib
,
254 GL_TRUE
, sub_prim
->min_index
, sub_prim
->max_index
,
257 draw_prims_func(ctx
, &temp_prim
, 1, ib
,
262 if (sub_end_index
>= end_index
) {