a9d8ff93e93cae9f5b83f29fe60f8a568ba325da
[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 <stdio.h>
50 #include "main/glheader.h"
51 #include "main/imports.h"
52 #include "main/mtypes.h"
53
54 #include "vbo.h"
55
56
57 #define REBASE(TYPE) \
58 static void *rebase_##TYPE( const void *ptr, \
59 GLuint count, \
60 TYPE min_index ) \
61 { \
62 GLuint i; \
63 const TYPE *in = (TYPE *)ptr; \
64 TYPE *tmp_indices = malloc(count * sizeof(TYPE)); \
65 \
66 if (tmp_indices == NULL) { \
67 _mesa_error_no_memory(__func__); \
68 return NULL; \
69 } \
70 \
71 for (i = 0; i < count; i++) \
72 tmp_indices[i] = in[i] - min_index; \
73 \
74 return (void *)tmp_indices; \
75 }
76
77
78 REBASE(GLuint)
79 REBASE(GLushort)
80 REBASE(GLubyte)
81
82 GLboolean vbo_all_varyings_in_vbos( const struct gl_vertex_array *arrays[] )
83 {
84 GLuint i;
85
86 for (i = 0; i < VERT_ATTRIB_MAX; i++)
87 if (arrays[i]->StrideB &&
88 arrays[i]->BufferObj->Name == 0)
89 return GL_FALSE;
90
91 return GL_TRUE;
92 }
93
94
95 /* Adjust primitives, indices and vertex definitions so that min_index
96 * becomes zero. There are lots of reasons for wanting to do this, eg:
97 *
98 * Software tnl:
99 * - any time min_index != 0, otherwise unused vertices lower than
100 * min_index will be transformed.
101 *
102 * Hardware tnl:
103 * - if ib != NULL and min_index != 0, otherwise vertices lower than
104 * min_index will be uploaded. Requires adjusting index values.
105 *
106 * - if ib == NULL and min_index != 0, just for convenience so this doesn't
107 * have to be handled within the driver.
108 *
109 * Hardware tnl with VBO support:
110 * - as above, but only when vertices are not (all?) in VBO's.
111 * - can't save time by trying to upload half a vbo - typically it is
112 * all or nothing.
113 */
114 void vbo_rebase_prims( struct gl_context *ctx,
115 const struct gl_vertex_array *arrays[],
116 const struct _mesa_prim *prim,
117 GLuint nr_prims,
118 const struct _mesa_index_buffer *ib,
119 GLuint min_index,
120 GLuint max_index,
121 vbo_draw_func draw )
122 {
123 struct gl_vertex_array tmp_arrays[VERT_ATTRIB_MAX];
124 const struct gl_vertex_array *tmp_array_pointers[VERT_ATTRIB_MAX];
125
126 struct _mesa_index_buffer tmp_ib;
127 struct _mesa_prim *tmp_prims = NULL;
128 const struct gl_vertex_array **saved_arrays = ctx->Array._DrawArrays;
129 void *tmp_indices = NULL;
130 GLuint i;
131
132 assert(min_index != 0);
133
134 if (0)
135 printf("%s %d..%d\n", __func__, min_index, max_index);
136
137
138 /* XXX this path is disabled for now.
139 * There's rendering corruption in some apps when it's enabled.
140 */
141 if (0 && ib && ctx->Extensions.ARB_draw_elements_base_vertex) {
142 /* If we can just tell the hardware or the TNL to interpret our
143 * indices with a different base, do so.
144 */
145 tmp_prims = malloc(sizeof(*prim) * nr_prims);
146
147 if (tmp_prims == NULL) {
148 _mesa_error_no_memory(__func__);
149 return;
150 }
151
152 for (i = 0; i < nr_prims; i++) {
153 tmp_prims[i] = prim[i];
154 tmp_prims[i].basevertex -= min_index;
155 }
156
157 prim = tmp_prims;
158 } else if (ib) {
159 /* Unfortunately need to adjust each index individually.
160 */
161 GLboolean map_ib = ib->obj->Name &&
162 !ib->obj->Mappings[MAP_INTERNAL].Pointer;
163 void *ptr;
164
165 if (map_ib)
166 ctx->Driver.MapBufferRange(ctx, 0, ib->obj->Size, GL_MAP_READ_BIT,
167 ib->obj, MAP_INTERNAL);
168
169
170 ptr = ADD_POINTERS(ib->obj->Mappings[MAP_INTERNAL].Pointer, ib->ptr);
171
172 /* Some users might prefer it if we translated elements to
173 * GLuints here. Others wouldn't...
174 */
175 switch (ib->index_size) {
176 case 4:
177 tmp_indices = rebase_GLuint( ptr, ib->count, min_index );
178 break;
179 case 2:
180 tmp_indices = rebase_GLushort( ptr, ib->count, min_index );
181 break;
182 case 1:
183 tmp_indices = rebase_GLubyte( ptr, ib->count, min_index );
184 break;
185 }
186
187 if (map_ib)
188 ctx->Driver.UnmapBuffer(ctx, ib->obj, MAP_INTERNAL);
189
190 if (tmp_indices == NULL) {
191 return;
192 }
193
194 tmp_ib.obj = ctx->Shared->NullBufferObj;
195 tmp_ib.ptr = tmp_indices;
196 tmp_ib.count = ib->count;
197 tmp_ib.index_size = ib->index_size;
198
199 ib = &tmp_ib;
200 }
201 else {
202 /* Otherwise the primitives need adjustment.
203 */
204 tmp_prims = malloc(sizeof(*prim) * nr_prims);
205
206 if (tmp_prims == NULL) {
207 _mesa_error_no_memory(__func__);
208 return;
209 }
210
211 for (i = 0; i < nr_prims; i++) {
212 /* If this fails, it could indicate an application error:
213 */
214 assert(prim[i].start >= min_index);
215
216 tmp_prims[i] = prim[i];
217 tmp_prims[i].start -= min_index;
218 }
219
220 prim = tmp_prims;
221 }
222
223 /* Just need to adjust the pointer values on each incoming array.
224 * This works for VBO and non-vbo rendering and shouldn't pesimize
225 * VBO-based upload schemes. However this may still not be a fast
226 * path for hardware tnl for VBO based rendering as most machines
227 * will be happier if you just specify a starting vertex value in
228 * each primitive.
229 *
230 * For drivers with hardware tnl, you only want to do this if you
231 * are forced to, eg non-VBO indexed rendering with start != 0.
232 */
233 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
234 tmp_arrays[i] = *arrays[i];
235 tmp_arrays[i].Ptr += min_index * tmp_arrays[i].StrideB;
236 tmp_array_pointers[i] = &tmp_arrays[i];
237 }
238
239 /* Re-issue the draw call.
240 */
241 ctx->Array._DrawArrays = tmp_array_pointers;
242 ctx->NewDriverState |= ctx->DriverFlags.NewArray;
243
244 draw( ctx,
245 prim,
246 nr_prims,
247 ib,
248 GL_TRUE,
249 0,
250 max_index - min_index,
251 NULL, 0, NULL );
252
253 ctx->Array._DrawArrays = saved_arrays;
254 ctx->NewDriverState |= ctx->DriverFlags.NewArray;
255
256 free(tmp_indices);
257
258 free(tmp_prims);
259 }
260
261
262