summaryrefslogtreecommitdiff
path: root/src/mesa/tnl/t_array_api.c
blob: b0228b74c7f61b0485954dc8ca48ebef3f96c6c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 * Mesa 3-D graphics library
 * Version:  5.1
 *
 * Copyright (C) 1999-2003  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * \file t_array_api.c
 * \brief Vertex array API functions (glDrawArrays, etc)
 * \author Keith Whitwell
 */

#include "glheader.h"
#include "api_validate.h"
#include "context.h"
#include "imports.h"
#include "macros.h"
#include "mtypes.h"
#include "state.h"

#include "array_cache/acache.h"

#include "t_array_api.h"
#include "t_array_import.h"
#include "t_imm_api.h"
#include "t_imm_exec.h"
#include "t_context.h"
#include "t_pipeline.h"

static void fallback_drawarrays( GLcontext *ctx, GLenum mode, GLint start,
				 GLsizei count )
{
   if (_tnl_hard_begin( ctx, mode )) {
      GLint i;
      for (i = start; i < count; i++) 
	 glArrayElement( i );
      glEnd();
   }
}


static void fallback_drawelements( GLcontext *ctx, GLenum mode, GLsizei count,
				   const GLuint *indices)
{
   if (_tnl_hard_begin(ctx, mode)) {
      GLint i;
      if (ctx->Array.ElementArrayBufferObj->Name) {
         /* use indices in the buffer object */
         ASSERT(ctx->Array.ElementArrayBufferObj->Data);
         indices = (const GLuint *) ctx->Array.ElementArrayBufferObj->Data;
      }
      for (i = 0 ; i < count ; i++) {
         glArrayElement( indices[i] );
      }
      glEnd();
   }
}


static void _tnl_draw_range_elements( GLcontext *ctx, GLenum mode,
				      GLuint start, GLuint end,
				      GLsizei count, GLuint *indices )

{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   int i;
   FLUSH_CURRENT( ctx, 0 );
   
   /*  _mesa_debug(ctx, "%s\n", __FUNCTION__); */
   if (tnl->pipeline.build_state_changes)
      _tnl_validate_pipeline( ctx );

   _tnl_vb_bind_arrays( ctx, start, end );

   tnl->vb.FirstPrimitive = 0;
   tnl->vb.Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
   tnl->vb.PrimitiveLength[0] = count;
   tnl->vb.Elts = (GLuint *)indices;

   for (i = 0 ; i < count ; i++)
      indices[i] -= start;

   if (ctx->Array.LockCount)
      tnl->Driver.RunPipeline( ctx );
   else {
      /* Note that arrays may have changed before/after execution.
       */
      tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
      tnl->Driver.RunPipeline( ctx );
      tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
   }

   for (i = 0 ; i < count ; i++)
      indices[i] += start;
}



/**
 * Called via the GL API dispatcher.
 */
void GLAPIENTRY
_tnl_DrawArrays(GLenum mode, GLint start, GLsizei count)
{
   GET_CURRENT_CONTEXT(ctx);
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   struct vertex_buffer *VB = &tnl->vb;
   GLuint thresh = (ctx->Driver.NeedFlush & FLUSH_STORED_VERTICES) ? 30 : 10;
   
   if (MESA_VERBOSE & VERBOSE_API)
      _mesa_debug(NULL, "_tnl_DrawArrays %d %d\n", start, count); 
   
   /* Check arguments, etc.
    */
   if (!_mesa_validate_DrawArrays( ctx, mode, start, count ))
      return;

   if (tnl->pipeline.build_state_changes)
      _tnl_validate_pipeline( ctx );

   if (ctx->CompileFlag) {
      fallback_drawarrays( ctx, mode, start, start + count );
   }    
   else if (!ctx->Array.LockCount && (GLuint) count < thresh) {
      /* Small primitives: attempt to share a vb (at the expense of
       * using the immediate interface).
      */
      fallback_drawarrays( ctx, mode, start, start + count );
   } 
   else if (ctx->Array.LockCount && 
	    count < (GLint) ctx->Const.MaxArrayLockSize) {
      
      /* Locked primitives which can fit in a single vertex buffer:
       */
      FLUSH_CURRENT( ctx, 0 );

      if (start < (GLint) ctx->Array.LockFirst)
	 start = ctx->Array.LockFirst;
      if (start + count > (GLint) ctx->Array.LockCount)
	 count = ctx->Array.LockCount - start;
      
      /* Locked drawarrays.  Reuse any previously transformed data.
       */
      _tnl_vb_bind_arrays( ctx, ctx->Array.LockFirst, ctx->Array.LockCount );
      VB->FirstPrimitive = start;
      VB->Primitive[start] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
      VB->PrimitiveLength[start] = count;
      tnl->Driver.RunPipeline( ctx );
   } 
   else {
      int bufsz = 256;		/* Use a small buffer for cache goodness */
      int j, nr;
      int minimum, modulo, skip;

      /* Large primitives requiring decomposition to multiple vertex
       * buffers:
       */
      switch (mode) {
      case GL_POINTS:
	 minimum = 0;
	 modulo = 1;
	 skip = 0;
	 break;
      case GL_LINES:
	 minimum = 1;
	 modulo = 2;
	 skip = 1;
	 break;
      case GL_LINE_STRIP:
	 minimum = 1;
	 modulo = 1;
	 skip = 0;
	 break;
      case GL_TRIANGLES:
	 minimum = 2;
	 modulo = 3;
	 skip = 2;
	 break;
      case GL_TRIANGLE_STRIP:
	 minimum = 2;
	 modulo = 1;
	 skip = 0;
	 break;
      case GL_QUADS:
	 minimum = 3;
	 modulo = 4;
	 skip = 3;
	 break;
      case GL_QUAD_STRIP:
	 minimum = 3;
	 modulo = 2;
	 skip = 0;
	 break;
      case GL_LINE_LOOP:
      case GL_TRIANGLE_FAN:
      case GL_POLYGON:
      default:
	 /* Primitives requiring a copied vertex (fan-like primitives)
	  * must use the slow path if they cannot fit in a single
	  * vertex buffer.  
	  */
	 if (count < (GLint) ctx->Const.MaxArrayLockSize) {
	    bufsz = ctx->Const.MaxArrayLockSize;
	    minimum = 0;
	    modulo = 1;
	    skip = 0;
	 }
	 else {
	    fallback_drawarrays( ctx, mode, start, start + count );
	    return;
	 }
      }

      FLUSH_CURRENT( ctx, 0 );

      bufsz -= bufsz % modulo;
      bufsz -= minimum;
      count += start;

      for (j = start + minimum ; j < count ; j += nr + skip ) {

	 nr = MIN2( bufsz, count - j );

	 _tnl_vb_bind_arrays( ctx, j - minimum, j + nr );

	 VB->FirstPrimitive = 0;
	 VB->Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
	 VB->PrimitiveLength[0] = nr + minimum;
	 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
	 tnl->Driver.RunPipeline( ctx );
	 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
      }
   }
}


/**
 * Called via the GL API dispatcher.
 */
void GLAPIENTRY
_tnl_DrawRangeElements(GLenum mode,
		       GLuint start, GLuint end,
		       GLsizei count, GLenum type, const GLvoid *indices)
{
   GET_CURRENT_CONTEXT(ctx);
   GLuint *ui_indices;

   if (MESA_VERBOSE & VERBOSE_API)
      _mesa_debug(NULL, "_tnl_DrawRangeElements %d %d %d\n", start, end, count); 

   if (ctx->Array.ElementArrayBufferObj->Name) {
      /* use indices in the buffer object */
      if (!ctx->Array.ElementArrayBufferObj->Data) {
         _mesa_warning(ctx,
                       "DrawRangeElements with empty vertex elements buffer!");
         return;
      }
      indices = (GLvoid *) ctx->Array.ElementArrayBufferObj->Data;
   }

   /* Check arguments, etc.
    */
   if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count,
                                          type, indices ))
      return;

   ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
					       count, type, indices );


   if (ctx->CompileFlag) {
      /* Can't do anything when compiling:
       */
      fallback_drawelements( ctx, mode, count, ui_indices );
   }
   else if (ctx->Array.LockCount) {
      /* Are the arrays already locked?  If so we currently have to look
       * at the whole locked range.
       */
      if (start >= ctx->Array.LockFirst && end <= ctx->Array.LockCount)
	 _tnl_draw_range_elements( ctx, mode,
				   ctx->Array.LockFirst,
				   ctx->Array.LockCount,
				   count, ui_indices );
      else {
	 /* The spec says referencing elements outside the locked
	  * range is undefined.  I'm going to make it a noop this time
	  * round, maybe come up with something beter before 3.6.
	  *
	  * May be able to get away with just setting LockCount==0,
	  * though this raises the problems of dependent state.  May
	  * have to call glUnlockArrays() directly?
	  *
	  * Or scan the list and replace bad indices?
	  */
	 _mesa_problem( ctx,
		     "DrawRangeElements references "
		     "elements outside locked range.");
      }
   }
   else if (end + 1 - start < ctx->Const.MaxArrayLockSize) {
      /* The arrays aren't locked but we can still fit them inside a
       * single vertexbuffer.
       */
      _tnl_draw_range_elements( ctx, mode, start, end + 1, count, ui_indices );
   } else {
      /* Range is too big to optimize:
       */
      fallback_drawelements( ctx, mode, count, ui_indices );
   }
}



/**
 * Called via the GL API dispatcher.
 */
void GLAPIENTRY
_tnl_DrawElements(GLenum mode, GLsizei count, GLenum type,
		  const GLvoid *indices)
{
   GET_CURRENT_CONTEXT(ctx);
   GLuint *ui_indices;

   if (MESA_VERBOSE & VERBOSE_API)
      _mesa_debug(NULL, "_tnl_DrawElements %d\n", count); 

   if (ctx->Array.ElementArrayBufferObj->Name) {
      /* use indices in the buffer object */
      if (!ctx->Array.ElementArrayBufferObj->Data) {
         _mesa_warning(ctx, "DrawElements with empty vertex elements buffer!");
         return;
      }
      indices = (const GLvoid *) ctx->Array.ElementArrayBufferObj->Data;
   }

   /* Check arguments, etc.
    */
   if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
      return;

   ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
					       count, type, indices );

   if (ctx->CompileFlag) {
      /* Can't do anything when compiling:
       */
      fallback_drawelements( ctx, mode, count, ui_indices );
   }
   else if (ctx->Array.LockCount) {
      _tnl_draw_range_elements( ctx, mode,
				ctx->Array.LockFirst,
				ctx->Array.LockCount,
				count, ui_indices );
   }
   else {
      /* Scan the index list and see if we can use the locked path anyway.
       */
      GLuint max_elt = 0;
      GLint i;

      for (i = 0 ; i < count ; i++)
	 if (ui_indices[i] > max_elt)
            max_elt = ui_indices[i];

      if (max_elt < ctx->Const.MaxArrayLockSize && /* can we use it? */
	  max_elt < (GLuint) count) 	           /* do we want to use it? */
	 _tnl_draw_range_elements( ctx, mode, 0, max_elt+1, count, ui_indices );
      else
	 fallback_drawelements( ctx, mode, count, ui_indices );
   }
}


/**
 * Initialize context's vertex array fields.  Called during T 'n L context
 * creation.
 */
void _tnl_array_init( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   struct vertex_arrays *tmp = &tnl->array_inputs;
   GLvertexformat *vfmt = &(TNL_CONTEXT(ctx)->vtxfmt);
   GLuint i;

   vfmt->DrawArrays = _tnl_DrawArrays;
   vfmt->DrawElements = _tnl_DrawElements;
   vfmt->DrawRangeElements = _tnl_DrawRangeElements;

   /* Setup vector pointers that will be used to bind arrays to VB's.
    */
   _mesa_vector4f_init( &tmp->Obj, 0, 0 );
   _mesa_vector4f_init( &tmp->Normal, 0, 0 );   
   _mesa_vector4f_init( &tmp->FogCoord, 0, 0 );
   _mesa_vector1ui_init( &tmp->Index, 0, 0 );
   _mesa_vector1ub_init( &tmp->EdgeFlag, 0, 0 );

   for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
      _mesa_vector4f_init( &tmp->TexCoord[i], 0, 0);

   tnl->tmp_primitive = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
   tnl->tmp_primitive_length = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
}


/**
 * Destroy the context's vertex array stuff.
 * Called during T 'n L context destruction.
 */
void _tnl_array_destroy( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   if (tnl->tmp_primitive_length) FREE(tnl->tmp_primitive_length);
   if (tnl->tmp_primitive) FREE(tnl->tmp_primitive);
}