Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[mesa.git] / src / mesa / vbo / vbo_rebase.c
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 6.5
5 *
6 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Keith Whitwell <keith@tungstengraphics.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 const TYPE *in = (TYPE *)ptr; \
62 TYPE *tmp_indices = malloc(count * sizeof(TYPE)); \
63 GLuint i; \
64 \
65 for (i = 0; i < count; i++) \
66 tmp_indices[i] = in[i] - min_index; \
67 \
68 return (void *)tmp_indices; \
69 }
70
71
72 REBASE(GLuint)
73 REBASE(GLushort)
74 REBASE(GLubyte)
75
76 GLboolean vbo_all_varyings_in_vbos( const struct gl_client_array *arrays[] )
77 {
78 GLuint i;
79
80 for (i = 0; i < VERT_ATTRIB_MAX; i++)
81 if (arrays[i]->StrideB &&
82 arrays[i]->BufferObj->Name == 0)
83 return GL_FALSE;
84
85 return GL_TRUE;
86 }
87
88 /* Adjust primitives, indices and vertex definitions so that min_index
89 * becomes zero. There are lots of reasons for wanting to do this, eg:
90 *
91 * Software tnl:
92 * - any time min_index != 0, otherwise unused vertices lower than
93 * min_index will be transformed.
94 *
95 * Hardware tnl:
96 * - if ib != NULL and min_index != 0, otherwise vertices lower than
97 * min_index will be uploaded. Requires adjusting index values.
98 *
99 * - if ib == NULL and min_index != 0, just for convenience so this doesn't
100 * have to be handled within the driver.
101 *
102 * Hardware tnl with VBO support:
103 * - as above, but only when vertices are not (all?) in VBO's.
104 * - can't save time by trying to upload half a vbo - typically it is
105 * all or nothing.
106 */
107 void vbo_rebase_prims( GLcontext *ctx,
108 const struct gl_client_array *arrays[],
109 const struct _mesa_prim *prim,
110 GLuint nr_prims,
111 const struct _mesa_index_buffer *ib,
112 GLuint min_index,
113 GLuint max_index,
114 vbo_draw_func draw )
115 {
116 struct gl_client_array tmp_arrays[VERT_ATTRIB_MAX];
117 const struct gl_client_array *tmp_array_pointers[VERT_ATTRIB_MAX];
118
119 struct _mesa_index_buffer tmp_ib;
120 struct _mesa_prim *tmp_prims = NULL;
121 void *tmp_indices = NULL;
122 GLuint i;
123
124 assert(min_index != 0);
125
126 if (0)
127 _mesa_printf("%s %d..%d\n", __FUNCTION__, min_index, max_index);
128
129
130 /* XXX this path is disabled for now.
131 * There's rendering corruption in some apps when it's enabled.
132 */
133 if (0 && ib && ctx->Extensions.ARB_draw_elements_base_vertex) {
134 /* If we can just tell the hardware or the TNL to interpret our
135 * indices with a different base, do so.
136 */
137 tmp_prims = (struct _mesa_prim *)_mesa_malloc(sizeof(*prim) * nr_prims);
138
139 for (i = 0; i < nr_prims; i++) {
140 tmp_prims[i] = prim[i];
141 tmp_prims[i].basevertex -= min_index;
142 }
143
144 prim = tmp_prims;
145 } else if (ib) {
146 /* Unfortunately need to adjust each index individually.
147 */
148 GLboolean map_ib = ib->obj->Name && !ib->obj->Pointer;
149 void *ptr;
150
151 if (map_ib)
152 ctx->Driver.MapBuffer(ctx,
153 GL_ELEMENT_ARRAY_BUFFER,
154 GL_READ_ONLY_ARB,
155 ib->obj);
156
157
158 ptr = ADD_POINTERS(ib->obj->Pointer, ib->ptr);
159
160 /* Some users might prefer it if we translated elements to
161 * GLuints here. Others wouldn't...
162 */
163 switch (ib->type) {
164 case GL_UNSIGNED_INT:
165 tmp_indices = rebase_GLuint( ptr, ib->count, min_index );
166 break;
167 case GL_UNSIGNED_SHORT:
168 tmp_indices = rebase_GLushort( ptr, ib->count, min_index );
169 break;
170 case GL_UNSIGNED_BYTE:
171 tmp_indices = rebase_GLubyte( ptr, ib->count, min_index );
172 break;
173 }
174
175 if (map_ib)
176 ctx->Driver.UnmapBuffer(ctx,
177 GL_ELEMENT_ARRAY_BUFFER,
178 ib->obj);
179
180 tmp_ib.obj = ctx->Shared->NullBufferObj;
181 tmp_ib.ptr = tmp_indices;
182 tmp_ib.count = ib->count;
183 tmp_ib.type = ib->type;
184
185 ib = &tmp_ib;
186 }
187 else {
188 /* Otherwise the primitives need adjustment.
189 */
190 tmp_prims = (struct _mesa_prim *)_mesa_malloc(sizeof(*prim) * nr_prims);
191
192 for (i = 0; i < nr_prims; i++) {
193 /* If this fails, it could indicate an application error:
194 */
195 assert(prim[i].start >= min_index);
196
197 tmp_prims[i] = prim[i];
198 tmp_prims[i].start -= min_index;
199 }
200
201 prim = tmp_prims;
202 }
203
204 /* Just need to adjust the pointer values on each incoming array.
205 * This works for VBO and non-vbo rendering and shouldn't pesimize
206 * VBO-based upload schemes. However this may still not be a fast
207 * path for hardware tnl for VBO based rendering as most machines
208 * will be happier if you just specify a starting vertex value in
209 * each primitive.
210 *
211 * For drivers with hardware tnl, you only want to do this if you
212 * are forced to, eg non-VBO indexed rendering with start != 0.
213 */
214 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
215 tmp_arrays[i] = *arrays[i];
216 tmp_arrays[i].Ptr += min_index * tmp_arrays[i].StrideB;
217 tmp_array_pointers[i] = &tmp_arrays[i];
218 }
219
220 /* Re-issue the draw call.
221 */
222 draw( ctx,
223 tmp_array_pointers,
224 prim,
225 nr_prims,
226 ib,
227 GL_TRUE,
228 0,
229 max_index - min_index );
230
231 if (tmp_indices)
232 _mesa_free(tmp_indices);
233
234 if (tmp_prims)
235 _mesa_free(tmp_prims);
236 }
237
238
239