5cfa7a01a966f6c2ad7a46200d047ecada9d8b1c
[mesa.git] / src / mesa / vbo / vbo_exec_array.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * 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
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "glheader.h"
29 #include "context.h"
30 #include "state.h"
31 #include "api_validate.h"
32 #include "api_noop.h"
33 #include "dispatch.h"
34
35 #include "vbo_context.h"
36
37 static GLuint get_max_index( GLuint count, GLuint type,
38 const GLvoid *indices )
39 {
40 GLint i;
41
42 /* Compute max element. This is only needed for upload of non-VBO,
43 * non-constant data elements.
44 *
45 * XXX: Postpone this calculation until it is known that it is
46 * needed. Otherwise could scan this pointlessly in the all-vbo
47 * case.
48 */
49 switch(type) {
50 case GL_UNSIGNED_INT: {
51 const GLuint *ui_indices = (const GLuint *)indices;
52 GLuint max_ui = 0;
53 for (i = 0; i < count; i++)
54 if (ui_indices[i] > max_ui)
55 max_ui = ui_indices[i];
56 return max_ui;
57 }
58 case GL_UNSIGNED_SHORT: {
59 const GLushort *us_indices = (const GLushort *)indices;
60 GLuint max_us = 0;
61 for (i = 0; i < count; i++)
62 if (us_indices[i] > max_us)
63 max_us = us_indices[i];
64 return max_us;
65 }
66 case GL_UNSIGNED_BYTE: {
67 const GLubyte *ub_indices = (const GLubyte *)indices;
68 GLuint max_ub = 0;
69 for (i = 0; i < count; i++)
70 if (ub_indices[i] > max_ub)
71 max_ub = ub_indices[i];
72 return max_ub;
73 }
74 default:
75 return 0;
76 }
77 }
78
79
80 /* Just translate the arrayobj into a sane layout.
81 */
82 static void bind_array_obj( GLcontext *ctx )
83 {
84 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
85 GLuint i;
86
87 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
88 * rather than as individual named arrays. Then this function can
89 * go away.
90 */
91 exec->array.legacy_array[VERT_ATTRIB_POS] = &ctx->Array.ArrayObj->Vertex;
92 exec->array.legacy_array[VERT_ATTRIB_NORMAL] = &ctx->Array.ArrayObj->Normal;
93 exec->array.legacy_array[VERT_ATTRIB_COLOR0] = &ctx->Array.ArrayObj->Color;
94 exec->array.legacy_array[VERT_ATTRIB_COLOR1] = &ctx->Array.ArrayObj->SecondaryColor;
95 exec->array.legacy_array[VERT_ATTRIB_FOG] = &ctx->Array.ArrayObj->FogCoord;
96 exec->array.legacy_array[VERT_ATTRIB_COLOR_INDEX] = &ctx->Array.ArrayObj->Index;
97 exec->array.legacy_array[VBO_ATTRIB_EDGEFLAG] = &ctx->Array.ArrayObj->EdgeFlag;
98
99 for (i = 0; i < 8; i++)
100 exec->array.legacy_array[VBO_ATTRIB_TEX0 + i] = &ctx->Array.ArrayObj->TexCoord[i];
101
102 for (i = 0; i < VERT_ATTRIB_MAX; i++)
103 exec->array.generic_array[i] = &ctx->Array.ArrayObj->VertexAttrib[i];
104
105 exec->array.array_obj = ctx->Array.ArrayObj->Name;
106 }
107
108 static void recalculate_input_bindings( GLcontext *ctx )
109 {
110 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
111 const struct gl_client_array **inputs = &exec->array.inputs[0];
112 GLuint i;
113
114 exec->array.program_mode = get_program_mode(ctx);
115 exec->array.enabled_flags = ctx->Array.ArrayObj->_Enabled;
116
117 /* TODO: Get rid of NV_program (please!).
118 */
119 switch (exec->array.program_mode) {
120 case VP_NONE:
121 /* When no vertex program is active, we put the material values
122 * into the generic slots. This is the only situation where
123 * material values are available as per-vertex attributes.
124 */
125 for (i = 0; i <= VERT_ATTRIB_TEX7; i++) {
126 if (exec->array.legacy_array[i]->Enabled)
127 inputs[i] = exec->array.legacy_array[i];
128 else
129 inputs[i] = &exec->legacy_currval[i];
130 }
131
132 for (i = 0; i < MAT_ATTRIB_MAX; i++) {
133 inputs[VERT_ATTRIB_GENERIC0 + i] = &exec->mat_currval[i];
134 }
135 break;
136 case VP_NV:
137 /* NV_vertex_program - attribute arrays alias and override
138 * conventional, legacy arrays. No materials, and the generic
139 * slots are vacant.
140 */
141 for (i = 0; i <= VERT_ATTRIB_TEX7; i++) {
142 if (exec->array.generic_array[i]->Enabled)
143 inputs[i] = exec->array.generic_array[i];
144 else if (exec->array.legacy_array[i]->Enabled)
145 inputs[i] = exec->array.legacy_array[i];
146 else
147 inputs[i] = &exec->legacy_currval[i];
148 }
149 break;
150 case VP_ARB:
151 /* ARB_vertex_program - Only the attribute zero (position) array
152 * aliases and overrides the legacy position array.
153 *
154 * Otherwise, legacy attributes available in the legacy slots,
155 * generic attributes in the generic slots and materials are not
156 * available as per-vertex attributes.
157 */
158 if (exec->array.generic_array[0]->Enabled)
159 inputs[0] = exec->array.generic_array[0];
160 else if (exec->array.legacy_array[0]->Enabled)
161 inputs[0] = exec->array.legacy_array[0];
162 else
163 inputs[0] = &exec->legacy_currval[0];
164
165
166 for (i = 1; i <= VERT_ATTRIB_TEX7; i++) {
167 if (exec->array.legacy_array[i]->Enabled)
168 inputs[i] = exec->array.legacy_array[i];
169 else
170 inputs[i] = &exec->legacy_currval[i];
171 }
172
173 for (i = 0; i < 16; i++) {
174 if (exec->array.generic_array[0]->Enabled)
175 inputs[VERT_ATTRIB_GENERIC0 + i] = exec->array.generic_array[i];
176 else
177 inputs[VERT_ATTRIB_GENERIC0 + i] = &exec->generic_currval[i];
178 }
179 break;
180 }
181 }
182
183 static void bind_arrays( GLcontext *ctx )
184 {
185 #if 0
186 if (ctx->Array.ArrayObj.Name != exec->array.array_obj) {
187 bind_array_obj(ctx);
188 recalculate_input_bindings(ctx);
189 }
190 else if (exec->array.program_mode != get_program_mode(ctx) ||
191 exec->array.enabled_flags != ctx->Array.ArrayObj->_Enabled) {
192
193 recalculate_input_bindings(ctx);
194 }
195 #else
196 bind_array_obj(ctx);
197 recalculate_input_bindings(ctx);
198 #endif
199 }
200
201
202
203 /***********************************************************************
204 * API functions.
205 */
206
207 static void GLAPIENTRY
208 vbo_exec_DrawArrays(GLenum mode, GLint start, GLsizei count)
209 {
210 GET_CURRENT_CONTEXT(ctx);
211 struct vbo_context *vbo = vbo_context(ctx);
212 struct vbo_exec_context *exec = &vbo->exec;
213 struct _mesa_prim prim[1];
214
215 if (!_mesa_validate_DrawArrays( ctx, mode, start, count ))
216 return;
217
218 FLUSH_CURRENT( ctx, 0 );
219
220 if (ctx->NewState)
221 _mesa_update_state( ctx );
222
223 bind_arrays( ctx );
224
225 prim[0].begin = 1;
226 prim[0].end = 1;
227 prim[0].weak = 0;
228 prim[0].pad = 0;
229
230 if (exec->array.inputs[0]->BufferObj->Name) {
231 /* Use vertex attribute as a hint to tell us if we expect all
232 * arrays to be in VBO's and if so, don't worry about avoiding
233 * the upload of elements < start.
234 */
235 prim[0].mode = mode;
236 prim[0].start = start;
237 prim[0].count = count;
238 prim[0].indexed = 0;
239
240 vbo->draw_prims( ctx, exec->array.inputs, prim, 1, NULL, 0, start + count );
241 }
242 else {
243 /* If not using VBO's, we don't want to upload any more elements
244 * than necessary from the arrays as they will not be valid next
245 * time the application tries to draw with them.
246 */
247 prim[0].mode = mode;
248 prim[0].start = 0;
249 prim[0].count = count;
250 prim[0].indexed = 0;
251
252 vbo->draw_prims( ctx, exec->array.inputs, prim, 1, NULL, start, start + count );
253 }
254 }
255
256
257
258 static void GLAPIENTRY
259 vbo_exec_DrawRangeElements(GLenum mode,
260 GLuint start, GLuint end,
261 GLsizei count, GLenum type, const GLvoid *indices)
262 {
263 GET_CURRENT_CONTEXT(ctx);
264 struct vbo_context *vbo = vbo_context(ctx);
265 struct vbo_exec_context *exec = &vbo->exec;
266 struct _mesa_index_buffer ib;
267 struct _mesa_prim prim[1];
268
269 if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count, type, indices ))
270 return;
271
272 FLUSH_CURRENT( ctx, 0 );
273
274 if (ctx->NewState)
275 _mesa_update_state( ctx );
276
277 ib.count = count;
278 ib.type = type;
279 ib.obj = ctx->Array.ElementArrayBufferObj;
280 ib.ptr = indices;
281
282 if (ctx->Array.ElementArrayBufferObj->Name) {
283 /* Use the fact that indices are in a VBO as a hint that the
284 * program has put all the arrays in VBO's and we don't have to
285 * worry about performance implications of start > 0.
286 *
287 * XXX: consider passing start as min_index to draw_prims instead.
288 */
289 ib.rebase = 0;
290 }
291 else {
292 ib.rebase = start;
293 }
294
295 prim[0].begin = 1;
296 prim[0].end = 1;
297 prim[0].weak = 0;
298 prim[0].pad = 0;
299 prim[0].mode = mode;
300 prim[0].start = 0;
301 prim[0].count = count;
302 prim[0].indexed = 1;
303
304 vbo->draw_prims( ctx, exec->array.inputs, prim, 1, &ib, ib.rebase, end+1 );
305 }
306
307
308 static void GLAPIENTRY
309 vbo_exec_DrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
310 {
311 GET_CURRENT_CONTEXT(ctx);
312 GLuint max_index;
313
314 if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
315 return;
316
317 if (ctx->Array.ElementArrayBufferObj->Name) {
318 const GLvoid *map = ctx->Driver.MapBuffer(ctx,
319 GL_ELEMENT_ARRAY_BUFFER_ARB,
320 GL_DYNAMIC_READ_ARB,
321 ctx->Array.ElementArrayBufferObj);
322
323 max_index = get_max_index(count, type, ADD_POINTERS(map, indices));
324
325 ctx->Driver.UnmapBuffer(ctx,
326 GL_ELEMENT_ARRAY_BUFFER_ARB,
327 ctx->Array.ElementArrayBufferObj);
328 }
329 else {
330 max_index = get_max_index(count, type, indices);
331 }
332
333 vbo_exec_DrawRangeElements(mode, 0, max_index, count, type, indices);
334 }
335
336
337 /***********************************************************************
338 * Initialization
339 */
340
341
342
343
344 void vbo_exec_array_init( struct vbo_exec_context *exec )
345 {
346 GLcontext *ctx = exec->ctx;
347
348 #if 1
349 exec->vtxfmt.DrawArrays = vbo_exec_DrawArrays;
350 exec->vtxfmt.DrawElements = vbo_exec_DrawElements;
351 exec->vtxfmt.DrawRangeElements = vbo_exec_DrawRangeElements;
352 #else
353 exec->vtxfmt.DrawArrays = _mesa_noop_DrawArrays;
354 exec->vtxfmt.DrawElements = _mesa_noop_DrawElements;
355 exec->vtxfmt.DrawRangeElements = _mesa_noop_DrawRangeElements;
356 #endif
357
358 exec->array.index_obj = ctx->Driver.NewBufferObject(ctx, 1, GL_ARRAY_BUFFER_ARB);
359 }
360
361
362 void vbo_exec_array_destroy( struct vbo_exec_context *exec )
363 {
364 GLcontext *ctx = exec->ctx;
365
366 ctx->Driver.DeleteBuffer(ctx, exec->array.index_obj);
367 }