-/* $Id: t_array_api.c,v 1.1 2000/12/26 05:09:32 keithw Exp $ */
-
/*
* Mesa 3-D graphics library
- * Version: 3.5
+ * Version: 6.1
*
- * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
+ * Copyright (C) 1999-2004 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"),
* 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.
- *
- * Authors:
- * Keith Whitwell <keithw@valinux.com>
+ */
+
+/**
+ * \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 "mmath.h"
-#include "mem.h"
-#include "state.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_save_api.h"
#include "t_context.h"
#include "t_pipeline.h"
+static void fallback_drawarrays( GLcontext *ctx, GLenum mode, GLint start,
+ GLsizei count )
+{
+ GLint i;
+
+ assert(!ctx->CompileFlag);
+ assert(ctx->Driver.CurrentExecPrimitive == GL_POLYGON+1);
+
+ GL_CALL(Begin)(mode);
+ for (i = 0; i < count; i++)
+ GL_CALL(ArrayElement)( start + i );
+ GL_CALL(End)();
+}
+
+
+static void fallback_drawelements( GLcontext *ctx, GLenum mode, GLsizei count,
+ const GLuint *indices)
+{
+ GLint i;
+
+ assert(!ctx->CompileFlag);
+ assert(ctx->Driver.CurrentExecPrimitive == GL_POLYGON+1);
+
+ /* Here, indices will already reflect the buffer object if active */
+
+ GL_CALL(Begin)(mode);
+ for (i = 0 ; i < count ; i++) {
+ GL_CALL(ArrayElement)( indices[i] );
+ }
+ GL_CALL(End)();
+}
+
+
+/* Note this function no longer takes a 'start' value, the range is
+ * assumed to start at zero. The old trick of subtracting 'start'
+ * from each index won't work if the indices are not in writeable
+ * memory.
+ */
+static void _tnl_draw_range_elements( GLcontext *ctx, GLenum mode,
+ GLuint max_index,
+ GLsizei index_count, GLuint *indices )
+
+{
+ TNLcontext *tnl = TNL_CONTEXT(ctx);
+ struct tnl_prim prim;
+ FLUSH_CURRENT( ctx, 0 );
+
+ if (tnl->pipeline.build_state_changes)
+ _tnl_validate_pipeline( ctx );
+
+ _tnl_vb_bind_arrays( ctx, 0, max_index );
+ tnl->vb.Primitive = &prim;
+ tnl->vb.Primitive[0].mode = mode | PRIM_BEGIN | PRIM_END;
+ tnl->vb.Primitive[0].start = 0;
+ tnl->vb.Primitive[0].count = index_count;
+ tnl->vb.PrimitiveCount = 1;
+ tnl->vb.Elts = (GLuint *)indices;
+
+ if (ctx->Array.LockCount)
+ tnl->Driver.RunPipeline( ctx );
+ else {
+ /* The lower 16 bits represent the conventional arrays while the
+ * upper 16 bits represent the generic arrays. OR those bits
+ * together to indicate which vertex attribs are in effect.
+ */
+ GLuint enabledArrays = ctx->Array._Enabled | (ctx->Array._Enabled >> 16);
+ /* Note that arrays may have changed before/after execution.
+ */
+ tnl->pipeline.run_input_changes |= enabledArrays & 0xffff;
+ tnl->Driver.RunPipeline( ctx );
+ tnl->pipeline.run_input_changes |= enabledArrays & 0xffff;
+ }
+}
-void
+
+/**
+ * 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;
+ GLuint enabledArrays;
+
+ 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 ))
if (tnl->pipeline.build_state_changes)
_tnl_validate_pipeline( ctx );
- if (!ctx->CompileFlag && count - start < ctx->Const.MaxArrayLockSize) {
- FLUSH_CURRENT( ctx, 0 );
-
- if (ctx->Array.LockCount)
- {
- if (start < ctx->Array.LockFirst) start = ctx->Array.LockFirst;
- if (count > ctx->Array.LockCount) count = ctx->Array.LockCount;
- if (start >= count) return;
+ assert(!ctx->CompileFlag);
- /* 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 - start;
- _tnl_run_pipeline( ctx );
- } else {
- /* The arrays are small enough to fit in a single VB; just bind
- * them and go. Any untransformed data will be copied on
- * clipping.
- *
- * Invalidate any locked data dependent on these arrays.
- */
- _tnl_vb_bind_arrays( ctx, start, count );
- VB->FirstPrimitive = 0;
- VB->Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
- VB->PrimitiveLength[0] = count - start;
- tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
- _tnl_run_pipeline( ctx );
- tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
- }
+ 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, count );
}
- else {
- /* Need to produce immediate structs, either for compiling or
- * because the array range is too large to process in a single
- * VB. In GL_EXECUTE mode, this introduces two redundant
- * operations: producing the flag array and computing the orflag
- * of the flag array.
- */
-#if 0
- if (_tnl_hard_begin( ctx, mode )) {
- GLuint j;
- for (j = 0 ; j < count ; ) {
- struct immediate *IM = TNL_CURRENT_IM(ctx);
- GLuint nr = MIN2( IMM_MAXDATA - IM->Start, count - j );
- GLuint sf = IM->Flag[IM->Start];
+ else if (start >= (GLint) ctx->Array.LockFirst &&
+ start + count <= (GLint)(ctx->Array.LockFirst + ctx->Array.LockCount)) {
+
+ struct tnl_prim prim;
- _tnl_fill_immediate_drawarrays( ctx, IM, j, j+nr );
+ /* Locked primitives which can fit in a single vertex buffer:
+ */
+ FLUSH_CURRENT( ctx, 0 );
- if (j == 0) IM->Flag[IM->Start] |= sf;
+ /* Locked drawarrays. Reuse any previously transformed data.
+ */
+ _tnl_vb_bind_arrays( ctx, ctx->Array.LockFirst,
+ ctx->Array.LockFirst + ctx->Array.LockCount );
- IM->Count = IM->Start + nr;
- j += nr;
+ tnl->vb.Primitive = &prim;
+ tnl->vb.Primitive[0].mode = mode | PRIM_BEGIN | PRIM_END;
+ tnl->vb.Primitive[0].start = start;
+ tnl->vb.Primitive[0].count = count;
+ tnl->vb.PrimitiveCount = 1;
- if (j == count)
- _tnl_end( ctx );
+ tnl->Driver.RunPipeline( ctx );
+ }
+ else {
+ int bufsz = 256; /* Use a small buffer for cache goodness */
+ int j, nr;
+ int minimum, modulo, skip;
- _tnl_flush_immediate( IM );
- }
- }
-#else
- /* Simple alternative to above code.
+ /* Large primitives requiring decomposition to multiple vertex
+ * buffers:
*/
-/* if (_tnl_hard_begin( ctx, mode )) */
- _tnl_begin(ctx,mode);
- {
- GLuint i;
- for (i=start;i<count;i++) {
- _tnl_array_element( ctx, i );
+ 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, count );
+ return;
}
- _tnl_end( ctx );
}
-#endif
- }
-}
+ FLUSH_CURRENT( ctx, 0 );
+ bufsz -= bufsz % modulo;
+ bufsz -= minimum;
+ count += start;
-static void _tnl_draw_range_elements( GLcontext *ctx, GLenum mode,
- GLuint start, GLuint end,
- GLsizei count, const GLuint *indices )
-
-{
- TNLcontext *tnl = TNL_CONTEXT(ctx);
- FLUSH_CURRENT( ctx, 0 );
-
- _tnl_vb_bind_arrays( ctx, start, end );
+ for (j = start + minimum ; j < count ; j += nr + skip ) {
- 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;
+ struct tnl_prim prim;
- if (ctx->Array.LockCount)
- _tnl_run_pipeline( ctx );
- else {
- /* Note that arrays may have changed before/after execution.
- */
- tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
- _tnl_run_pipeline( ctx );
- tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
- }
-}
+ nr = MIN2( bufsz, count - j );
+ /* XXX is the last parameter a count or index into the array??? */
+ _tnl_vb_bind_arrays( ctx, j - minimum, j + nr );
+ tnl->vb.Primitive = &prim;
+ tnl->vb.Primitive[0].mode = mode;
+ if (j == start + minimum)
+ tnl->vb.Primitive[0].mode |= PRIM_BEGIN;
-static void _tnl_draw_elements( GLcontext *ctx, GLenum mode, GLsizei count,
- const GLuint *indices)
-{
-#if 1
- /* Optimized code that fakes the effect of calling
- * _tnl_array_element for each index in the list.
- */
- if (_tnl_hard_begin( ctx, mode )) {
- GLuint i,j;
- for (j = 0 ; j < count ; ) {
- struct immediate *IM = TNL_CURRENT_IM(ctx);
- GLuint start = IM->Start;
- GLuint nr = MIN2( IMM_MAXDATA - start, count - j ) + start;
- GLuint sf = IM->Flag[start];
- IM->FlushElt |= 1;
-
- for (i = start ; i < nr ; i++) {
- IM->Elt[i] = (GLuint) *indices++;
- IM->Flag[i] = VERT_ELT;
- }
-
- if (j == 0) IM->Flag[start] |= sf;
-
- IM->Count = nr;
- j += nr - start;
+ if (j + nr + skip >= count)
+ tnl->vb.Primitive[0].mode |= PRIM_END;
- if (j == count)
- _tnl_end( ctx );
+ tnl->vb.Primitive[0].start = 0;
+ tnl->vb.Primitive[0].count = nr + minimum;
+ tnl->vb.PrimitiveCount = 1;
- _tnl_flush_immediate( IM );
+ /* The lower 16 bits represent the conventional arrays while the
+ * upper 16 bits represent the generic arrays. OR those bits
+ * together to indicate which vertex attribs are in effect.
+ */
+ enabledArrays = ctx->Array._Enabled | (ctx->Array._Enabled >> 16);
+ /* Note that arrays may have changed before/after execution.
+ */
+ tnl->pipeline.run_input_changes |= enabledArrays;
+ tnl->Driver.RunPipeline( ctx );
+ tnl->pipeline.run_input_changes |= enabledArrays;
}
}
-#else
- /* Simple version of the above code.
- */
- if (_tnl_hard_begin(ctx, mode)) {
- GLuint i;
- for (i = 0 ; i < count ; i++)
- _tnl_array_element( ctx, indices[i] );
- _tnl_end( ctx );
- }
-#endif
}
-void
+/**
+ * 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);
- TNLcontext *tnl = TNL_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;
+ }
+ /* actual address is the sum of pointers */
+ indices = (const GLvoid *)
+ ADD_POINTERS(ctx->Array.ElementArrayBufferObj->Data,
+ (const GLubyte *) indices);
+ }
+
/* Check arguments, etc.
*/
- if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count,
+ if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count,
type, indices ))
return;
- if (tnl->pipeline.build_state_changes)
- _tnl_validate_pipeline( ctx );
-
- ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
+ ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
count, type, indices );
-
+
+
+ assert(!ctx->CompileFlag);
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,
+
+ if (start == 0 && ctx->Array.LockFirst == 0 &&
+ end < (ctx->Array.LockFirst + ctx->Array.LockCount))
+ _tnl_draw_range_elements( ctx, mode,
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?
- */
- gl_problem( ctx,
- "DrawRangeElements references "
- "elements outside locked range.");
+ fallback_drawelements( ctx, mode, count, ui_indices );
}
}
- else if (end - start < ctx->Const.MaxArrayLockSize) {
- /* The arrays aren't locked but we can still fit them inside a single
- * vertexbuffer.
+ else if (start == 0 && end < 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, count, ui_indices );
- } else {
+ _tnl_draw_range_elements( ctx, mode, end + 1, count, ui_indices );
+ }
+ else {
/* Range is too big to optimize:
*/
- _tnl_draw_elements( ctx, mode, count, ui_indices );
+ fallback_drawelements( ctx, mode, count, ui_indices );
}
}
-void
-_tnl_DrawElements(GLenum mode, GLsizei count, GLenum type,
+/**
+ * Called via the GL API dispatcher.
+ */
+void GLAPIENTRY
+_tnl_DrawElements(GLenum mode, GLsizei count, GLenum type,
const GLvoid *indices)
{
GET_CURRENT_CONTEXT(ctx);
- TNLcontext *tnl = TNL_CONTEXT(ctx);
GLuint *ui_indices;
- /* Check arguments, etc.
- */
+ if (MESA_VERBOSE & VERBOSE_API)
+ _mesa_debug(NULL, "_tnl_DrawElements %d\n", count);
+
+ /* Check arguments, etc. */
if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
return;
- if (tnl->pipeline.build_state_changes)
- _tnl_validate_pipeline( ctx );
+ if (ctx->Array.ElementArrayBufferObj->Name) {
+ /* actual address is the sum of pointers */
+ indices = (const GLvoid *)
+ ADD_POINTERS(ctx->Array.ElementArrayBufferObj->Data,
+ (const GLubyte *) indices);
+ }
- ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
+ ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
count, type, indices );
-
+
+ assert(!ctx->CompileFlag);
+
if (ctx->Array.LockCount) {
- _tnl_draw_range_elements( ctx, mode,
- ctx->Array.LockFirst,
- ctx->Array.LockCount,
- count, ui_indices );
- }
+ if (ctx->Array.LockFirst == 0)
+ _tnl_draw_range_elements( ctx, mode,
+ ctx->Array.LockCount,
+ count, ui_indices );
+ else
+ fallback_drawelements( ctx, mode, count, ui_indices );
+ }
else {
/* Scan the index list and see if we can use the locked path anyway.
*/
GLuint max_elt = 0;
- GLuint i;
+ GLint i;
- for (i = 0 ; i < count ; i++)
- if (ui_indices[i] > max_elt) max_elt = ui_indices[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 < count) /* do we want to use it? */
- _tnl_draw_range_elements( ctx, mode, 0, max_elt, count, ui_indices );
+ max_elt < (GLuint) count) /* do we want to use it? */
+ _tnl_draw_range_elements( ctx, mode, max_elt+1, count, ui_indices );
else
- _tnl_draw_elements( ctx, mode, count, ui_indices );
+ 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);
+ struct tnl_vertex_arrays *tmp = &tnl->array_inputs;
+ GLvertexformat *vfmt = &(TNL_CONTEXT(ctx)->exec_vtxfmt);
GLuint i;
vfmt->DrawArrays = _tnl_DrawArrays;
/* Setup vector pointers that will be used to bind arrays to VB's.
*/
- gl_vector4f_init( &tmp->Obj, 0, 0 );
- gl_vector3f_init( &tmp->Normal, 0, 0 );
- gl_vector4ub_init( &tmp->Color, 0, 0 );
- gl_vector4ub_init( &tmp->SecondaryColor, 0, 0 );
- gl_vector1f_init( &tmp->FogCoord, 0, 0 );
- gl_vector1ui_init( &tmp->Index, 0, 0 );
- gl_vector1ub_init( &tmp->EdgeFlag, 0, 0 );
-
- for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
- gl_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);
+ _mesa_vector4f_init( &tmp->Obj, 0, NULL);
+ _mesa_vector4f_init( &tmp->Normal, 0, NULL);
+ _mesa_vector4f_init( &tmp->FogCoord, 0, NULL);
+ _mesa_vector4f_init( &tmp->Index, 0, NULL);
+
+ for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
+ _mesa_vector4f_init( &tmp->TexCoord[i], 0, NULL);
}
+/**
+ * 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);
+ (void) ctx;
}