vbo: remove vbo_context.h and change includes to use vbo.h instead
[mesa.git] / src / mesa / vbo / vbo_primitive_restart.c
1 /*
2 * Copyright 2007 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Copyright © 2012 Intel Corporation
6 *
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:
13 *
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
16 * Software.
17 *
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
24 * IN THE SOFTWARE.
25 *
26 * Authors:
27 * Jordan Justen <jordan.l.justen@intel.com>
28 *
29 */
30
31 #include "main/imports.h"
32 #include "main/bufferobj.h"
33 #include "main/macros.h"
34 #include "main/varray.h"
35
36 #include "vbo.h"
37 #include "vbo_private.h"
38
39
40 #define UPDATE_MIN2(a, b) (a) = MIN2((a), (b))
41 #define UPDATE_MAX2(a, b) (a) = MAX2((a), (b))
42
43 /*
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).
47 *
48 * We map the index buffer, find the restart indexes, unmap
49 * the index buffer then draw the sub-primitives delineated by the restarts.
50 *
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
58 * for this.
59 *
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.
63 */
64
65
66 struct sub_primitive
67 {
68 GLuint start;
69 GLuint count;
70 GLuint min_index;
71 GLuint max_index;
72 };
73
74
75 /**
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.
79 */
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)
84 {
85 const unsigned max_prims = end - start;
86 struct sub_primitive *sub_prims;
87 unsigned i, cur_start, cur_count;
88 GLuint scan_index;
89 unsigned scan_num;
90
91 sub_prims =
92 malloc(max_prims * sizeof(struct sub_primitive));
93
94 if (!sub_prims) {
95 *num_sub_prims = 0;
96 return NULL;
97 }
98
99 cur_start = start;
100 cur_count = 0;
101 scan_num = 0;
102
103 #define IB_INDEX_READ(TYPE, INDEX) (((const GL##TYPE *) elements)[INDEX])
104
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; \
115 scan_num++; \
116 sub_prims[scan_num].min_index = (GL##TYPE) 0xffffffff; \
117 sub_prims[scan_num].max_index = 0; \
118 } \
119 cur_start = i + 1; \
120 cur_count = 0; \
121 } \
122 else { \
123 UPDATE_MIN2(sub_prims[scan_num].min_index, scan_index); \
124 UPDATE_MAX2(sub_prims[scan_num].max_index, scan_index); \
125 cur_count++; \
126 } \
127 } \
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; \
132 scan_num++; \
133 }
134
135 switch (element_size) {
136 case 1:
137 SCAN_ELEMENTS(ubyte);
138 break;
139 case 2:
140 SCAN_ELEMENTS(ushort);
141 break;
142 case 4:
143 SCAN_ELEMENTS(uint);
144 break;
145 default:
146 assert(0 && "bad index_size in find_sub_primitives()");
147 }
148
149 #undef SCAN_ELEMENTS
150
151 *num_sub_prims = scan_num;
152
153 return sub_prims;
154 }
155
156
157 /**
158 * Handle primitive restart in software.
159 *
160 * This function breaks up calls into the driver so primitive restart
161 * support is not required in the driver.
162 */
163 void
164 vbo_sw_primitive_restart(struct gl_context *ctx,
165 const struct _mesa_prim *prims,
166 GLuint nr_prims,
167 const struct _mesa_index_buffer *ib,
168 struct gl_buffer_object *indirect)
169 {
170 GLuint prim_num;
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;
176 GLuint sub_prim_num;
177 GLuint end_index;
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;
184 void *ptr;
185
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)) {
191
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");
195 return;
196 }
197
198 assert(nr_prims == 1);
199 new_prim = prims[0];
200 indirect_params = (const uint32_t *)
201 ADD_POINTERS(indirect->Mappings[MAP_INTERNAL].Pointer,
202 new_prim.indirect_offset);
203
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];
210
211 new_ib = *ib;
212 new_ib.count = new_prim.count;
213
214 prims = &new_prim;
215 ib = &new_ib;
216
217 ctx->Driver.UnmapBuffer(ctx, indirect, MAP_INTERNAL);
218 }
219
220 /* Find the sub-primitives. These are regions in the index buffer which
221 * are split based on the primitive restart index value.
222 */
223 if (map_ib) {
224 ctx->Driver.MapBufferRange(ctx, 0, ib->obj->Size, GL_MAP_READ_BIT,
225 ib->obj, MAP_INTERNAL);
226 }
227
228 ptr = ADD_POINTERS(ib->obj->Mappings[MAP_INTERNAL].Pointer, ib->ptr);
229
230 sub_prims = find_sub_primitives(ptr, ib->index_size,
231 0, ib->count, restart_index,
232 &num_sub_prims);
233
234 if (map_ib) {
235 ctx->Driver.UnmapBuffer(ctx, ib->obj, MAP_INTERNAL);
236 }
237
238 /* Loop over the primitives, and use the located sub-primitives to draw
239 * each primitive with a break to implement each primitive restart.
240 */
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,
255 NULL, 0, NULL);
256 } else {
257 draw_prims_func(ctx, &temp_prim, 1, ib,
258 GL_FALSE, -1, -1,
259 NULL, 0, NULL);
260 }
261 }
262 if (sub_end_index >= end_index) {
263 break;
264 }
265 }
266 }
267
268 free(sub_prims);
269 }
270