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