3d5a1a1c4c86b7c70dc6185359f6b71a409e763a
[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 "util/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 unsigned count, \
63 TYPE min_index) \
64 { \
65 const TYPE *in = (TYPE *)ptr; \
66 TYPE *tmp_indices = malloc(count * sizeof(TYPE)); \
67 \
68 if (tmp_indices == NULL) { \
69 _mesa_error_no_memory(__func__); \
70 return NULL; \
71 } \
72 \
73 for (unsigned i = 0; i < count; i++) \
74 tmp_indices[i] = in[i] - min_index; \
75 \
76 return (void *)tmp_indices; \
77 }
78
79 REBASE(GLuint)
80 REBASE(GLushort)
81 REBASE(GLubyte)
82
83
84 /* Adjust primitives, indices and vertex definitions so that min_index
85 * becomes zero. There are lots of reasons for wanting to do this, eg:
86 *
87 * Software tnl:
88 * - any time min_index != 0, otherwise unused vertices lower than
89 * min_index will be transformed.
90 *
91 * Hardware tnl:
92 * - if ib != NULL and min_index != 0, otherwise vertices lower than
93 * min_index will be uploaded. Requires adjusting index values.
94 *
95 * - if ib == NULL and min_index != 0, just for convenience so this doesn't
96 * have to be handled within the driver.
97 *
98 * Hardware tnl with VBO support:
99 * - as above, but only when vertices are not (all?) in VBO's.
100 * - can't save time by trying to upload half a vbo - typically it is
101 * all or nothing.
102 */
103 void t_rebase_prims(struct gl_context *ctx,
104 const struct tnl_vertex_array *arrays,
105 const struct _mesa_prim *prim,
106 GLuint nr_prims,
107 const struct _mesa_index_buffer *ib,
108 GLuint min_index,
109 GLuint max_index,
110 GLuint num_instances,
111 GLuint base_instance,
112 tnl_draw_func draw)
113 {
114 struct gl_array_attributes tmp_attribs[VERT_ATTRIB_MAX];
115 struct tnl_vertex_array tmp_arrays[VERT_ATTRIB_MAX];
116
117 struct _mesa_index_buffer tmp_ib;
118 struct _mesa_prim *tmp_prims = NULL;
119 void *tmp_indices = NULL;
120 GLuint i;
121
122 assert(min_index != 0);
123
124 if (0)
125 printf("%s %d..%d\n", __func__, min_index, max_index);
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 indices
132 * 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 bool map_ib = false;
151 const void *ptr;
152
153 if (ib->obj) {
154 if (!ib->obj->Mappings[MAP_INTERNAL].Pointer) {
155 ctx->Driver.MapBufferRange(ctx, 0, ib->obj->Size, GL_MAP_READ_BIT,
156 ib->obj, MAP_INTERNAL);
157 map_ib = true;
158 }
159
160 ptr = ADD_POINTERS(ib->obj->Mappings[MAP_INTERNAL].Pointer, ib->ptr);
161 } else
162 ptr = ib->ptr;
163
164 /* Some users might prefer it if we translated elements to GLuints here.
165 * Others wouldn't...
166 */
167 switch (ib->index_size_shift) {
168 case 2:
169 tmp_indices = rebase_GLuint( ptr, ib->count, min_index );
170 break;
171 case 1:
172 tmp_indices = rebase_GLushort( ptr, ib->count, min_index );
173 break;
174 case 0:
175 tmp_indices = rebase_GLubyte( ptr, ib->count, min_index );
176 break;
177 }
178
179 if (map_ib)
180 ctx->Driver.UnmapBuffer(ctx, ib->obj, MAP_INTERNAL);
181
182 if (tmp_indices == NULL)
183 return;
184
185 tmp_ib.obj = NULL;
186 tmp_ib.ptr = tmp_indices;
187 tmp_ib.count = ib->count;
188 tmp_ib.index_size_shift = ib->index_size_shift;
189
190 ib = &tmp_ib;
191 }
192 else {
193 /* Otherwise the primitives need adjustment. */
194 tmp_prims = malloc(sizeof(*prim) * nr_prims);
195
196 if (tmp_prims == NULL) {
197 _mesa_error_no_memory(__func__);
198 return;
199 }
200
201 for (i = 0; i < nr_prims; i++) {
202 /* If this fails, it could indicate an application error: */
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 (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 draw(ctx,
235 tmp_arrays,
236 prim,
237 nr_prims,
238 ib,
239 GL_TRUE,
240 0,
241 max_index - min_index,
242 num_instances, base_instance);
243
244 free(tmp_indices);
245 free(tmp_prims);
246 }
247
248
249