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