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