[SDK] Provide .const macro for gas
[reactos.git] / dll / opengl / 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 <precomp.h>
50
51 #define REBASE(TYPE) \
52 static void *rebase_##TYPE( const void *ptr, \
53 GLuint count, \
54 TYPE min_index ) \
55 { \
56 const TYPE *in = (TYPE *)ptr; \
57 TYPE *tmp_indices = malloc(count * sizeof(TYPE)); \
58 GLuint i; \
59 \
60 for (i = 0; i < count; i++) \
61 tmp_indices[i] = in[i] - min_index; \
62 \
63 return (void *)tmp_indices; \
64 }
65
66
67 REBASE(GLuint)
68 REBASE(GLushort)
69 REBASE(GLubyte)
70
71 /* Adjust primitives, indices and vertex definitions so that min_index
72 * becomes zero. There are lots of reasons for wanting to do this, eg:
73 *
74 * Software tnl:
75 * - any time min_index != 0, otherwise unused vertices lower than
76 * min_index will be transformed.
77 *
78 * Hardware tnl:
79 * - if ib != NULL and min_index != 0, otherwise vertices lower than
80 * min_index will be uploaded. Requires adjusting index values.
81 *
82 * - if ib == NULL and min_index != 0, just for convenience so this doesn't
83 * have to be handled within the driver.
84 *
85 * Hardware tnl with VBO support:
86 * - as above, but only when vertices are not (all?) in VBO's.
87 * - can't save time by trying to upload half a vbo - typically it is
88 * all or nothing.
89 */
90 void vbo_rebase_prims( struct gl_context *ctx,
91 const struct gl_client_array *arrays[],
92 const struct _mesa_prim *prim,
93 GLuint nr_prims,
94 const struct _mesa_index_buffer *ib,
95 GLuint min_index,
96 GLuint max_index,
97 vbo_draw_func draw )
98 {
99 struct gl_client_array tmp_arrays[VBO_ATTRIB_MAX];
100 const struct gl_client_array *tmp_array_pointers[VBO_ATTRIB_MAX];
101
102 struct _mesa_index_buffer tmp_ib;
103 struct _mesa_prim *tmp_prims = NULL;
104 void *tmp_indices = NULL;
105 GLuint i;
106
107 assert(min_index != 0);
108
109 if (0)
110 printf("%s %d..%d\n", __FUNCTION__, min_index, max_index);
111
112
113 if (ib) {
114 /* Unfortunately need to adjust each index individually.
115 */
116 GLboolean map_ib = ib->obj->Name && !ib->obj->Pointer;
117 void *ptr;
118
119 if (map_ib)
120 ctx->Driver.MapBufferRange(ctx, 0, ib->obj->Size, GL_MAP_READ_BIT,
121 ib->obj);
122
123
124 ptr = ADD_POINTERS(ib->obj->Pointer, ib->ptr);
125
126 /* Some users might prefer it if we translated elements to
127 * GLuints here. Others wouldn't...
128 */
129 switch (ib->type) {
130 case GL_UNSIGNED_INT:
131 tmp_indices = rebase_GLuint( ptr, ib->count, min_index );
132 break;
133 case GL_UNSIGNED_SHORT:
134 tmp_indices = rebase_GLushort( ptr, ib->count, min_index );
135 break;
136 case GL_UNSIGNED_BYTE:
137 tmp_indices = rebase_GLubyte( ptr, ib->count, min_index );
138 break;
139 }
140
141 if (map_ib)
142 ctx->Driver.UnmapBuffer(ctx, ib->obj);
143
144 tmp_ib.obj = ctx->Shared->NullBufferObj;
145 tmp_ib.ptr = tmp_indices;
146 tmp_ib.count = ib->count;
147 tmp_ib.type = ib->type;
148
149 ib = &tmp_ib;
150 }
151 else {
152 /* Otherwise the primitives need adjustment.
153 */
154 tmp_prims = (struct _mesa_prim *)malloc(sizeof(*prim) * nr_prims);
155
156 for (i = 0; i < nr_prims; i++) {
157 /* If this fails, it could indicate an application error:
158 */
159 assert(prim[i].start >= min_index);
160
161 tmp_prims[i] = prim[i];
162 tmp_prims[i].start -= min_index;
163 }
164
165 prim = tmp_prims;
166 }
167
168 /* Just need to adjust the pointer values on each incoming array.
169 * This works for VBO and non-vbo rendering and shouldn't pesimize
170 * VBO-based upload schemes. However this may still not be a fast
171 * path for hardware tnl for VBO based rendering as most machines
172 * will be happier if you just specify a starting vertex value in
173 * each primitive.
174 *
175 * For drivers with hardware tnl, you only want to do this if you
176 * are forced to, eg non-VBO indexed rendering with start != 0.
177 */
178 for (i = 0; i < VBO_ATTRIB_MAX; i++) {
179 tmp_arrays[i] = *arrays[i];
180 tmp_arrays[i].Ptr += min_index * tmp_arrays[i].StrideB;
181 tmp_array_pointers[i] = &tmp_arrays[i];
182 }
183
184 /* Re-issue the draw call.
185 */
186 draw( ctx,
187 tmp_array_pointers,
188 prim,
189 nr_prims,
190 ib,
191 GL_TRUE,
192 0,
193 max_index - min_index);
194
195 if (tmp_indices)
196 free(tmp_indices);
197
198 if (tmp_prims)
199 free(tmp_prims);
200 }
201
202
203