Implement GL_ELEMENT_ARRAY_BUFFER_ARB for buffer objects.
[mesa.git] / src / mesa / tnl / t_array_api.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 5.1
4 *
5 * Copyright (C) 1999-2003 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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file t_array_api.c
27 * \brief Vertex array API functions (glDrawArrays, etc)
28 * \author Keith Whitwell
29 */
30
31 #include "glheader.h"
32 #include "api_validate.h"
33 #include "context.h"
34 #include "imports.h"
35 #include "macros.h"
36 #include "mtypes.h"
37 #include "state.h"
38
39 #include "array_cache/acache.h"
40
41 #include "t_array_api.h"
42 #include "t_array_import.h"
43 #include "t_imm_api.h"
44 #include "t_imm_exec.h"
45 #include "t_context.h"
46 #include "t_pipeline.h"
47
48 static void fallback_drawarrays( GLcontext *ctx, GLenum mode, GLint start,
49 GLsizei count )
50 {
51 if (_tnl_hard_begin( ctx, mode )) {
52 GLint i;
53 for (i = start; i < count; i++)
54 glArrayElement( i );
55 glEnd();
56 }
57 }
58
59
60 static void fallback_drawelements( GLcontext *ctx, GLenum mode, GLsizei count,
61 const GLuint *indices)
62 {
63 if (_tnl_hard_begin(ctx, mode)) {
64 GLint i;
65 if (ctx->Array.ElementArrayBufferObj->Name) {
66 /* use indices in the buffer object */
67 ASSERT(ctx->Array.ElementArrayBufferObj->Data);
68 indices = (const GLuint *) ctx->Array.ElementArrayBufferObj->Data;
69 }
70 for (i = 0 ; i < count ; i++) {
71 glArrayElement( indices[i] );
72 }
73 glEnd();
74 }
75 }
76
77
78 static void _tnl_draw_range_elements( GLcontext *ctx, GLenum mode,
79 GLuint start, GLuint end,
80 GLsizei count, GLuint *indices )
81
82 {
83 TNLcontext *tnl = TNL_CONTEXT(ctx);
84 int i;
85 FLUSH_CURRENT( ctx, 0 );
86
87 /* _mesa_debug(ctx, "%s\n", __FUNCTION__); */
88 if (tnl->pipeline.build_state_changes)
89 _tnl_validate_pipeline( ctx );
90
91 _tnl_vb_bind_arrays( ctx, start, end );
92
93 tnl->vb.FirstPrimitive = 0;
94 tnl->vb.Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
95 tnl->vb.PrimitiveLength[0] = count;
96 tnl->vb.Elts = (GLuint *)indices;
97
98 for (i = 0 ; i < count ; i++)
99 indices[i] -= start;
100
101 if (ctx->Array.LockCount)
102 tnl->Driver.RunPipeline( ctx );
103 else {
104 /* Note that arrays may have changed before/after execution.
105 */
106 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
107 tnl->Driver.RunPipeline( ctx );
108 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
109 }
110
111 for (i = 0 ; i < count ; i++)
112 indices[i] += start;
113 }
114
115
116
117 /**
118 * Called via the GL API dispatcher.
119 */
120 void
121 _tnl_DrawArrays(GLenum mode, GLint start, GLsizei count)
122 {
123 GET_CURRENT_CONTEXT(ctx);
124 TNLcontext *tnl = TNL_CONTEXT(ctx);
125 struct vertex_buffer *VB = &tnl->vb;
126 GLuint thresh = (ctx->Driver.NeedFlush & FLUSH_STORED_VERTICES) ? 30 : 10;
127
128 if (MESA_VERBOSE & VERBOSE_API)
129 _mesa_debug(NULL, "_tnl_DrawArrays %d %d\n", start, count);
130
131 /* Check arguments, etc.
132 */
133 if (!_mesa_validate_DrawArrays( ctx, mode, start, count ))
134 return;
135
136 if (tnl->pipeline.build_state_changes)
137 _tnl_validate_pipeline( ctx );
138
139 if (ctx->CompileFlag) {
140 fallback_drawarrays( ctx, mode, start, start + count );
141 }
142 else if (!ctx->Array.LockCount && (GLuint) count < thresh) {
143 /* Small primitives: attempt to share a vb (at the expense of
144 * using the immediate interface).
145 */
146 fallback_drawarrays( ctx, mode, start, start + count );
147 }
148 else if (ctx->Array.LockCount &&
149 count < (GLint) ctx->Const.MaxArrayLockSize) {
150
151 /* Locked primitives which can fit in a single vertex buffer:
152 */
153 FLUSH_CURRENT( ctx, 0 );
154
155 if (start < (GLint) ctx->Array.LockFirst)
156 start = ctx->Array.LockFirst;
157 if (start + count > (GLint) ctx->Array.LockCount)
158 count = ctx->Array.LockCount - start;
159
160 /* Locked drawarrays. Reuse any previously transformed data.
161 */
162 _tnl_vb_bind_arrays( ctx, ctx->Array.LockFirst, ctx->Array.LockCount );
163 VB->FirstPrimitive = start;
164 VB->Primitive[start] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
165 VB->PrimitiveLength[start] = count;
166 tnl->Driver.RunPipeline( ctx );
167 }
168 else {
169 int bufsz = 256; /* Use a small buffer for cache goodness */
170 int j, nr;
171 int minimum, modulo, skip;
172
173 /* Large primitives requiring decomposition to multiple vertex
174 * buffers:
175 */
176 switch (mode) {
177 case GL_POINTS:
178 minimum = 0;
179 modulo = 1;
180 skip = 0;
181 break;
182 case GL_LINES:
183 minimum = 1;
184 modulo = 2;
185 skip = 1;
186 break;
187 case GL_LINE_STRIP:
188 minimum = 1;
189 modulo = 1;
190 skip = 0;
191 break;
192 case GL_TRIANGLES:
193 minimum = 2;
194 modulo = 3;
195 skip = 2;
196 break;
197 case GL_TRIANGLE_STRIP:
198 minimum = 2;
199 modulo = 1;
200 skip = 0;
201 break;
202 case GL_QUADS:
203 minimum = 3;
204 modulo = 4;
205 skip = 3;
206 break;
207 case GL_QUAD_STRIP:
208 minimum = 3;
209 modulo = 2;
210 skip = 0;
211 break;
212 case GL_LINE_LOOP:
213 case GL_TRIANGLE_FAN:
214 case GL_POLYGON:
215 default:
216 /* Primitives requiring a copied vertex (fan-like primitives)
217 * must use the slow path if they cannot fit in a single
218 * vertex buffer.
219 */
220 if (count < (GLint) ctx->Const.MaxArrayLockSize) {
221 bufsz = ctx->Const.MaxArrayLockSize;
222 minimum = 0;
223 modulo = 1;
224 skip = 0;
225 }
226 else {
227 fallback_drawarrays( ctx, mode, start, start + count );
228 return;
229 }
230 }
231
232 FLUSH_CURRENT( ctx, 0 );
233
234 bufsz -= bufsz % modulo;
235 bufsz -= minimum;
236 count += start;
237
238 for (j = start + minimum ; j < count ; j += nr + skip ) {
239
240 nr = MIN2( bufsz, count - j );
241
242 _tnl_vb_bind_arrays( ctx, j - minimum, j + nr );
243
244 VB->FirstPrimitive = 0;
245 VB->Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
246 VB->PrimitiveLength[0] = nr + minimum;
247 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
248 tnl->Driver.RunPipeline( ctx );
249 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
250 }
251 }
252 }
253
254
255 /**
256 * Called via the GL API dispatcher.
257 */
258 void
259 _tnl_DrawRangeElements(GLenum mode,
260 GLuint start, GLuint end,
261 GLsizei count, GLenum type, const GLvoid *indices)
262 {
263 GET_CURRENT_CONTEXT(ctx);
264 GLuint *ui_indices;
265
266 if (MESA_VERBOSE & VERBOSE_API)
267 _mesa_debug(NULL, "_tnl_DrawRangeElements %d %d %d\n", start, end, count);
268
269 if (ctx->Array.ElementArrayBufferObj->Name) {
270 /* use indices in the buffer object */
271 ASSERT(ctx->Array.ElementArrayBufferObj->Data);
272 indices = (GLuint *) ctx->Array.ElementArrayBufferObj->Data;
273 }
274
275 /* Check arguments, etc.
276 */
277 if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count,
278 type, indices ))
279 return;
280
281 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
282 count, type, indices );
283
284
285 if (ctx->CompileFlag) {
286 /* Can't do anything when compiling:
287 */
288 fallback_drawelements( ctx, mode, count, ui_indices );
289 }
290 else if (ctx->Array.LockCount) {
291 /* Are the arrays already locked? If so we currently have to look
292 * at the whole locked range.
293 */
294 if (start >= ctx->Array.LockFirst && end <= ctx->Array.LockCount)
295 _tnl_draw_range_elements( ctx, mode,
296 ctx->Array.LockFirst,
297 ctx->Array.LockCount,
298 count, ui_indices );
299 else {
300 /* The spec says referencing elements outside the locked
301 * range is undefined. I'm going to make it a noop this time
302 * round, maybe come up with something beter before 3.6.
303 *
304 * May be able to get away with just setting LockCount==0,
305 * though this raises the problems of dependent state. May
306 * have to call glUnlockArrays() directly?
307 *
308 * Or scan the list and replace bad indices?
309 */
310 _mesa_problem( ctx,
311 "DrawRangeElements references "
312 "elements outside locked range.");
313 }
314 }
315 else if (end + 1 - start < ctx->Const.MaxArrayLockSize) {
316 /* The arrays aren't locked but we can still fit them inside a
317 * single vertexbuffer.
318 */
319 _tnl_draw_range_elements( ctx, mode, start, end + 1, count, ui_indices );
320 } else {
321 /* Range is too big to optimize:
322 */
323 fallback_drawelements( ctx, mode, count, ui_indices );
324 }
325 }
326
327
328
329 /**
330 * Called via the GL API dispatcher.
331 */
332 void
333 _tnl_DrawElements(GLenum mode, GLsizei count, GLenum type,
334 const GLvoid *indices)
335 {
336 GET_CURRENT_CONTEXT(ctx);
337 GLuint *ui_indices;
338
339 if (MESA_VERBOSE & VERBOSE_API)
340 _mesa_debug(NULL, "_tnl_DrawElements %d\n", count);
341
342 if (ctx->Array.ElementArrayBufferObj->Name) {
343 /* use indices in the buffer object */
344 ASSERT(ctx->Array.ElementArrayBufferObj->Data);
345 indices = (const GLvoid *) ctx->Array.ElementArrayBufferObj->Data;
346 }
347
348 /* Check arguments, etc.
349 */
350 if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
351 return;
352
353 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
354 count, type, indices );
355
356 if (ctx->CompileFlag) {
357 /* Can't do anything when compiling:
358 */
359 fallback_drawelements( ctx, mode, count, ui_indices );
360 }
361 else if (ctx->Array.LockCount) {
362 _tnl_draw_range_elements( ctx, mode,
363 ctx->Array.LockFirst,
364 ctx->Array.LockCount,
365 count, ui_indices );
366 }
367 else {
368 /* Scan the index list and see if we can use the locked path anyway.
369 */
370 GLuint max_elt = 0;
371 GLint i;
372
373 for (i = 0 ; i < count ; i++)
374 if (ui_indices[i] > max_elt)
375 max_elt = ui_indices[i];
376
377 if (max_elt < ctx->Const.MaxArrayLockSize && /* can we use it? */
378 max_elt < (GLuint) count) /* do we want to use it? */
379 _tnl_draw_range_elements( ctx, mode, 0, max_elt+1, count, ui_indices );
380 else
381 fallback_drawelements( ctx, mode, count, ui_indices );
382 }
383 }
384
385
386 /**
387 * Initialize context's vertex array fields. Called during T 'n L context
388 * creation.
389 */
390 void _tnl_array_init( GLcontext *ctx )
391 {
392 TNLcontext *tnl = TNL_CONTEXT(ctx);
393 struct vertex_arrays *tmp = &tnl->array_inputs;
394 GLvertexformat *vfmt = &(TNL_CONTEXT(ctx)->vtxfmt);
395 GLuint i;
396
397 vfmt->DrawArrays = _tnl_DrawArrays;
398 vfmt->DrawElements = _tnl_DrawElements;
399 vfmt->DrawRangeElements = _tnl_DrawRangeElements;
400
401 /* Setup vector pointers that will be used to bind arrays to VB's.
402 */
403 _mesa_vector4f_init( &tmp->Obj, 0, 0 );
404 _mesa_vector4f_init( &tmp->Normal, 0, 0 );
405 _mesa_vector4f_init( &tmp->FogCoord, 0, 0 );
406 _mesa_vector1ui_init( &tmp->Index, 0, 0 );
407 _mesa_vector1ub_init( &tmp->EdgeFlag, 0, 0 );
408
409 for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
410 _mesa_vector4f_init( &tmp->TexCoord[i], 0, 0);
411
412 tnl->tmp_primitive = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
413 tnl->tmp_primitive_length = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
414 }
415
416
417 /**
418 * Destroy the context's vertex array stuff.
419 * Called during T 'n L context destruction.
420 */
421 void _tnl_array_destroy( GLcontext *ctx )
422 {
423 TNLcontext *tnl = TNL_CONTEXT(ctx);
424 if (tnl->tmp_primitive_length) FREE(tnl->tmp_primitive_length);
425 if (tnl->tmp_primitive) FREE(tnl->tmp_primitive);
426 }