mesa: add extra null checks in vbo_rebase_prims()
[mesa.git] / src / mesa / vbo / vbo_rebase.c
1
2 /*
3 * Mesa 3-D graphics library
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
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 shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Keith Whitwell <keithw@vmware.com>
27 */
28
29 /* Helper for drivers which find themselves rendering a range of
30 * indices starting somewhere above zero. Typically the application
31 * is issuing multiple DrawArrays() or DrawElements() to draw
32 * successive primitives layed out linearly in the vertex arrays.
33 * Unless the vertex arrays are all in a VBO, the OpenGL semantics
34 * imply that we need to re-upload the vertex data on each draw call.
35 * In that case, we want to avoid starting the upload at zero, as it
36 * will mean every draw call uploads an increasing amount of not-used
37 * vertex data. Worse - in the software tnl module, all those
38 * vertices will be transformed and lit.
39 *
40 * If we just upload the new data, however, the indices will be
41 * incorrect as we tend to upload each set of vertex data to a new
42 * region.
43 *
44 * This file provides a helper to adjust the arrays, primitives and
45 * indices of a draw call so that it can be re-issued with a min_index
46 * of zero.
47 */
48
49 #include "main/glheader.h"
50 #include "main/imports.h"
51 #include "main/mtypes.h"
52
53 #include "vbo.h"
54
55
56 #define REBASE(TYPE) \
57 static void *rebase_##TYPE( const void *ptr, \
58 GLuint count, \
59 TYPE min_index ) \
60 { \
61 GLuint i; \
62 const TYPE *in = (TYPE *)ptr; \
63 TYPE *tmp_indices = malloc(count * sizeof(TYPE)); \
64 \
65 if (tmp_indices == NULL) { \
66 _mesa_error_no_memory(__func__); \
67 return NULL; \
68 } \
69 \
70 for (i = 0; i < count; i++) \
71 tmp_indices[i] = in[i] - min_index; \
72 \
73 return (void *)tmp_indices; \
74 }
75
76
77 REBASE(GLuint)
78 REBASE(GLushort)
79 REBASE(GLubyte)
80
81 GLboolean vbo_all_varyings_in_vbos( const struct gl_client_array *arrays[] )
82 {
83 GLuint i;
84
85 for (i = 0; i < VERT_ATTRIB_MAX; i++)
86 if (arrays[i]->StrideB &&
87 arrays[i]->BufferObj->Name == 0)
88 return GL_FALSE;
89
90 return GL_TRUE;
91 }
92
93 GLboolean vbo_any_varyings_in_vbos( const struct gl_client_array *arrays[] )
94 {
95 GLuint i;
96
97 for (i = 0; i < VERT_ATTRIB_MAX; i++)
98 if (arrays[i]->BufferObj->Name != 0)
99 return GL_TRUE;
100
101 return GL_FALSE;
102 }
103
104 /* Adjust primitives, indices and vertex definitions so that min_index
105 * becomes zero. There are lots of reasons for wanting to do this, eg:
106 *
107 * Software tnl:
108 * - any time min_index != 0, otherwise unused vertices lower than
109 * min_index will be transformed.
110 *
111 * Hardware tnl:
112 * - if ib != NULL and min_index != 0, otherwise vertices lower than
113 * min_index will be uploaded. Requires adjusting index values.
114 *
115 * - if ib == NULL and min_index != 0, just for convenience so this doesn't
116 * have to be handled within the driver.
117 *
118 * Hardware tnl with VBO support:
119 * - as above, but only when vertices are not (all?) in VBO's.
120 * - can't save time by trying to upload half a vbo - typically it is
121 * all or nothing.
122 */
123 void vbo_rebase_prims( struct gl_context *ctx,
124 const struct gl_client_array *arrays[],
125 const struct _mesa_prim *prim,
126 GLuint nr_prims,
127 const struct _mesa_index_buffer *ib,
128 GLuint min_index,
129 GLuint max_index,
130 vbo_draw_func draw )
131 {
132 struct gl_client_array tmp_arrays[VERT_ATTRIB_MAX];
133 const struct gl_client_array *tmp_array_pointers[VERT_ATTRIB_MAX];
134
135 struct _mesa_index_buffer tmp_ib;
136 struct _mesa_prim *tmp_prims = NULL;
137 const struct gl_client_array **saved_arrays = ctx->Array._DrawArrays;
138 void *tmp_indices = NULL;
139 GLuint i;
140
141 assert(min_index != 0);
142
143 if (0)
144 printf("%s %d..%d\n", __FUNCTION__, min_index, max_index);
145
146
147 /* XXX this path is disabled for now.
148 * There's rendering corruption in some apps when it's enabled.
149 */
150 if (0 && ib && ctx->Extensions.ARB_draw_elements_base_vertex) {
151 /* If we can just tell the hardware or the TNL to interpret our
152 * indices with a different base, do so.
153 */
154 tmp_prims = malloc(sizeof(*prim) * nr_prims);
155
156 if (tmp_prims == NULL) {
157 _mesa_error_no_memory(__func__);
158 return;
159 }
160
161 for (i = 0; i < nr_prims; i++) {
162 tmp_prims[i] = prim[i];
163 tmp_prims[i].basevertex -= min_index;
164 }
165
166 prim = tmp_prims;
167 } else if (ib) {
168 /* Unfortunately need to adjust each index individually.
169 */
170 GLboolean map_ib = ib->obj->Name &&
171 !ib->obj->Mappings[MAP_INTERNAL].Pointer;
172 void *ptr;
173
174 if (map_ib)
175 ctx->Driver.MapBufferRange(ctx, 0, ib->obj->Size, GL_MAP_READ_BIT,
176 ib->obj, MAP_INTERNAL);
177
178
179 ptr = ADD_POINTERS(ib->obj->Mappings[MAP_INTERNAL].Pointer, ib->ptr);
180
181 /* Some users might prefer it if we translated elements to
182 * GLuints here. Others wouldn't...
183 */
184 switch (ib->type) {
185 case GL_UNSIGNED_INT:
186 tmp_indices = rebase_GLuint( ptr, ib->count, min_index );
187 break;
188 case GL_UNSIGNED_SHORT:
189 tmp_indices = rebase_GLushort( ptr, ib->count, min_index );
190 break;
191 case GL_UNSIGNED_BYTE:
192 tmp_indices = rebase_GLubyte( ptr, ib->count, min_index );
193 break;
194 }
195
196 if (map_ib)
197 ctx->Driver.UnmapBuffer(ctx, ib->obj, MAP_INTERNAL);
198
199 if (tmp_indices == NULL) {
200 return;
201 }
202
203 tmp_ib.obj = ctx->Shared->NullBufferObj;
204 tmp_ib.ptr = tmp_indices;
205 tmp_ib.count = ib->count;
206 tmp_ib.type = ib->type;
207
208 ib = &tmp_ib;
209 }
210 else {
211 /* Otherwise the primitives need adjustment.
212 */
213 tmp_prims = malloc(sizeof(*prim) * nr_prims);
214
215 if (tmp_prims == NULL) {
216 _mesa_error_no_memory(__func__);
217 return;
218 }
219
220 for (i = 0; i < nr_prims; i++) {
221 /* If this fails, it could indicate an application error:
222 */
223 assert(prim[i].start >= min_index);
224
225 tmp_prims[i] = prim[i];
226 tmp_prims[i].start -= min_index;
227 }
228
229 prim = tmp_prims;
230 }
231
232 /* Just need to adjust the pointer values on each incoming array.
233 * This works for VBO and non-vbo rendering and shouldn't pesimize
234 * VBO-based upload schemes. However this may still not be a fast
235 * path for hardware tnl for VBO based rendering as most machines
236 * will be happier if you just specify a starting vertex value in
237 * each primitive.
238 *
239 * For drivers with hardware tnl, you only want to do this if you
240 * are forced to, eg non-VBO indexed rendering with start != 0.
241 */
242 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
243 tmp_arrays[i] = *arrays[i];
244 tmp_arrays[i].Ptr += min_index * tmp_arrays[i].StrideB;
245 tmp_array_pointers[i] = &tmp_arrays[i];
246 }
247
248 /* Re-issue the draw call.
249 */
250 ctx->Array._DrawArrays = tmp_array_pointers;
251 ctx->NewDriverState |= ctx->DriverFlags.NewArray;
252
253 draw( ctx,
254 prim,
255 nr_prims,
256 ib,
257 GL_TRUE,
258 0,
259 max_index - min_index,
260 NULL, NULL );
261
262 ctx->Array._DrawArrays = saved_arrays;
263 ctx->NewDriverState |= ctx->DriverFlags.NewArray;
264
265 free(tmp_indices);
266
267 free(tmp_prims);
268 }
269
270
271