t_draw.c revision 188a4db49c4c22429bfa7ae87d4b1a0c35bf0285
1c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
2c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell/*
3c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * Mesa 3-D graphics library
4c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * Version:  6.5
5c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell *
6c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
7c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell *
8c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a
9c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * copy of this software and associated documentation files (the "Software"),
10c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * to deal in the Software without restriction, including without limitation
11c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * and/or sell copies of the Software, and to permit persons to whom the
13c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * Software is furnished to do so, subject to the following conditions:
14c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell *
15c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * The above copyright notice and this permission notice shall be included
16c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * in all copies or substantial portions of the Software.
17c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell *
18c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell *
25c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * Authors:
26c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell *    Keith Whitwell <keith@tungstengraphics.com>
27c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell */
28c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
29c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell#include "glheader.h"
30c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell#include "context.h"
31c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell#include "imports.h"
32c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell#include "state.h"
33c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell#include "mtypes.h"
34c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell#include "macros.h"
35c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
36c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell#include "t_context.h"
37c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell#include "t_pipeline.h"
38c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell#include "t_vp_build.h"
39c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell#include "t_vertex.h"
40c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell#include "tnl.h"
41c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
42c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
43c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
44188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwellstatic GLubyte *get_space(GLcontext *ctx, GLuint bytes)
45c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell{
46c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   TNLcontext *tnl = TNL_CONTEXT(ctx);
47c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   GLubyte *space = _mesa_malloc(bytes);
48c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
49c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   tnl->block[tnl->nr_blocks++] = space;
50188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell   return space;
51c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell}
52c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
53c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
54c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellstatic void free_space(GLcontext *ctx)
55c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell{
56c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   TNLcontext *tnl = TNL_CONTEXT(ctx);
57c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   GLuint i;
58c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   for (i = 0; i < tnl->nr_blocks; i++)
59c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      _mesa_free(tnl->block[i]);
60c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   tnl->nr_blocks = 0;
61c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell}
62c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
63c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
649827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell/* Convert the incoming array to GLfloats.  Understands the
659827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell * array->Normalized flag and selects the correct conversion method.
669827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell */
679827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell#define CONVERT( TYPE, MACRO ) do {		\
689827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell   GLuint i, j;					\
699827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell   if (input->Normalized) {			\
709827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell      for (i = 0; i < count; i++) {		\
719827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell	 const TYPE *in = (TYPE *)ptr;		\
729827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell	 for (j = 0; j < sz; j++) {		\
739827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell	    *fptr++ = MACRO(*in);		\
749827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell	    in++;				\
759827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell	 }					\
769827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell	 ptr += input->StrideB;			\
779827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell      }						\
789827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell   } else {					\
799827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell      for (i = 0; i < count; i++) {		\
809827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell	 const TYPE *in = (TYPE *)ptr;		\
819827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell	 for (j = 0; j < sz; j++) {		\
829827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell	    *fptr++ = (GLfloat)(*in);		\
839827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell	    in++;				\
849827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell	 }					\
859827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell	 ptr += input->StrideB;			\
869827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell      }						\
879827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell   }						\
889827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell} while (0)
899827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
909827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
919827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
92c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell/* Adjust pointer to point at first requested element, convert to
93c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * floating point, populate VB->AttribPtr[].
94c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell */
95c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellstatic void _tnl_import_array( GLcontext *ctx,
96c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			       GLuint attrib,
97c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			       GLuint start,
98c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			       GLuint end,
99c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			       const struct gl_client_array *input,
100188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell			       const GLubyte *ptr )
101c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell{
102c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   TNLcontext *tnl = TNL_CONTEXT(ctx);
103c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   struct vertex_buffer *VB = &tnl->vb;
104c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   const GLuint count = end - start;
105c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   GLuint stride = input->StrideB;
106c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
107c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   ptr += start * stride;
108c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
109c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   if (input->Type != GL_FLOAT) {
110c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      const GLuint sz = input->Size;
111188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell      GLubyte *buf = get_space(ctx, count * sz * sizeof(GLfloat));
1127e9c3684ef45e0df8426317f28c883d16f27c031Keith Whitwell      GLfloat *fptr = (GLfloat *)buf;
113c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
114c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      switch (input->Type) {
115c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      case GL_BYTE:
116c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 CONVERT(GLbyte, BYTE_TO_FLOAT);
117c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 break;
118c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      case GL_UNSIGNED_BYTE:
119c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 CONVERT(GLubyte, UBYTE_TO_FLOAT);
120c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 break;
121c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      case GL_SHORT:
122c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 CONVERT(GLshort, SHORT_TO_FLOAT);
123c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 break;
124c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      case GL_UNSIGNED_SHORT:
125c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 CONVERT(GLushort, USHORT_TO_FLOAT);
126c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 break;
127c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      case GL_INT:
128c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 CONVERT(GLint, INT_TO_FLOAT);
129c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 break;
130c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      case GL_UNSIGNED_INT:
131c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 CONVERT(GLuint, UINT_TO_FLOAT);
132c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 break;
133c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      case GL_DOUBLE:
134c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 CONVERT(GLdouble, (GLfloat));
135c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 break;
136c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      default:
137c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 assert(0);
138c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 break;
139c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      }
140c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
1417e9c3684ef45e0df8426317f28c883d16f27c031Keith Whitwell      ptr = buf;
142c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      stride = sz * sizeof(GLfloat);
143c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   }
144c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
145c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->AttribPtr[attrib] = &tnl->tmp_inputs[attrib];
146c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->AttribPtr[attrib]->data = (GLfloat (*)[4])ptr;
147c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->AttribPtr[attrib]->start = (GLfloat *)ptr;
148c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->AttribPtr[attrib]->count = count;
149c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->AttribPtr[attrib]->stride = stride;
150c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->AttribPtr[attrib]->size = input->Size;
151c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
152c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   /* This should die, but so should the whole GLvector4f concept:
153c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    */
154c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->AttribPtr[attrib]->flags = (((1<<input->Size)-1) |
155c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell				   VEC_NOT_WRITEABLE |
156c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell				   (stride == 4*sizeof(GLfloat) ? 0 : VEC_BAD_STRIDE));
157c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
158c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->AttribPtr[attrib]->storage = NULL;
159c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell}
160c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
1619827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell#define CLIPVERTS  ((6 + MAX_CLIP_PLANES) * 2)
1629827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
1639827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
1649827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwellstatic GLboolean *_tnl_import_edgeflag( GLcontext *ctx,
1659827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell					const GLvector4f *input,
1669827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell					GLuint count)
1679827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell{
1689827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell   const GLubyte *ptr = (const GLubyte *)input->data;
1699827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell   const GLuint stride = input->stride;
1709827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell   GLboolean *space = (GLboolean *)get_space(ctx, count + CLIPVERTS);
1719827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell   GLboolean *bptr = space;
1729827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell   GLuint i;
1739827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
1749827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell   for (i = 0; i < count; i++) {
1759827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell      *bptr++ = ((GLfloat *)ptr)[0] == 1.0;
1769827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell      ptr += stride;
1779827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell   }
1789827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
1799827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell   return space;
1809827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell}
1819827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
182c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
183c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellstatic void bind_inputs( GLcontext *ctx,
184c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			 const struct gl_client_array *inputs[],
185c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			 GLint start, GLint end,
186c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			 struct gl_buffer_object **bo,
187c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			 GLuint *nr_bo )
188c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell{
189c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   TNLcontext *tnl = TNL_CONTEXT(ctx);
190c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   struct vertex_buffer *VB = &tnl->vb;
191c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   GLuint i;
192c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
193c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   /* Map all the VBOs
194c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    */
195c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   for (i = 0; i < VERT_ATTRIB_MAX; i++) {
196c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      const void *ptr;
197c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
198c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      if (inputs[i]->BufferObj->Name) {
199c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 if (!inputs[i]->BufferObj->Pointer) {
200c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	    bo[*nr_bo] = inputs[i]->BufferObj;
2017e9c3684ef45e0df8426317f28c883d16f27c031Keith Whitwell	    (*nr_bo)++;
202c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	    ctx->Driver.MapBuffer(ctx,
203c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell				  GL_ARRAY_BUFFER,
204c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell				  GL_READ_ONLY_ARB,
205c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell				  inputs[i]->BufferObj);
206c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
207c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	    assert(inputs[i]->BufferObj->Pointer);
208c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 }
209c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
210c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 ptr = ADD_POINTERS(inputs[i]->BufferObj->Pointer,
211c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			    inputs[i]->Ptr);
212c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      }
213c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      else
214c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell	 ptr = inputs[i]->Ptr;
215c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
216c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      /* Just make sure the array is floating point, otherwise convert to
217c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell       * temporary storage.  Rebase arrays so that 'start' becomes
218c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell       * element zero.
219c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell       *
220c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell       * XXX: remove the GLvector4f type at some stage and just use
221c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell       * client arrays.
222c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell       */
223c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      _tnl_import_array(ctx, i, start, end, inputs[i], ptr);
224c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   }
225c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
226c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   /* Legacy pointers -- remove one day.
227c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    */
228c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->ObjPtr = VB->AttribPtr[_TNL_ATTRIB_POS];
229c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->NormalPtr = VB->AttribPtr[_TNL_ATTRIB_NORMAL];
230c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->ColorPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR0];
231c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->ColorPtr[1] = NULL;
232c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->IndexPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR_INDEX];
233c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->IndexPtr[1] = NULL;
234c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->SecondaryColorPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR1];
235c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->SecondaryColorPtr[1] = NULL;
236c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->FogCoordPtr = VB->AttribPtr[_TNL_ATTRIB_FOG];
237c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
238c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
239c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      VB->TexCoordPtr[i] = VB->AttribPtr[_TNL_ATTRIB_TEX0 + i];
240c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   }
241c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
2429827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell   /* Clipping and drawing code still requires this to be a packed
2439827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    * array of ubytes which can be written into.  TODO: Fix and
2449827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    * remove.
245c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    */
246c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   if (ctx->Polygon.FrontMode != GL_FILL ||
247c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell       ctx->Polygon.BackMode != GL_FILL)
248c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   {
2499827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell      VB->EdgeFlag = _tnl_import_edgeflag( ctx,
2509827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell					   VB->AttribPtr[_TNL_ATTRIB_EDGEFLAG],
2519827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell					   VB->Count );
252c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   }
253c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
254c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell}
255c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
256c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
257c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell/* Translate indices to GLuints and store in VB->Elts.
258c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell */
259c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellstatic void bind_indicies( GLcontext *ctx,
260c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			   const struct _mesa_index_buffer *ib,
261c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			   struct gl_buffer_object **bo,
262c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			   GLuint *nr_bo)
263c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell{
264c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   TNLcontext *tnl = TNL_CONTEXT(ctx);
265c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   struct vertex_buffer *VB = &tnl->vb;
266188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell   GLuint i;
267188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell   void *ptr;
268c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
269c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   if (!ib)
270c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      return;
271c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
272c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   if (ib->obj->Name && !ib->obj->Pointer) {
273c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      bo[*nr_bo] = ib->obj;
2747e9c3684ef45e0df8426317f28c883d16f27c031Keith Whitwell      (*nr_bo)++;
275c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      ctx->Driver.MapBuffer(ctx,
276c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			    GL_ELEMENT_ARRAY_BUFFER,
277c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			    GL_READ_ONLY_ARB,
278c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			    ib->obj);
279c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
280c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      assert(ib->obj->Pointer);
281c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   }
282c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
283188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell   ptr = ADD_POINTERS(ib->obj->Pointer, ib->ptr);
284c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
285188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell   if (ib->type == GL_UNSIGNED_INT) {
286188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell      VB->Elts = (GLuint *) ptr;
287188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell      VB->Elts += ib->rebase;
288188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell   }
289188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell   else {
290188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell      GLuint *elts = (GLuint *)get_space(ctx, ib->count * sizeof(GLuint));
291188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell      VB->Elts = elts;
292188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell
293188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell      switch (ib->type) {
294188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell      case GL_UNSIGNED_SHORT: {
295188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell	 const GLushort *in = ((GLushort *)ptr) + ib->rebase;
296188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell	 for (i = 0; i < ib->count; i++)
297188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell	    *elts++ = *in++;
298188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell	 break;
299188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell      }
300188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell      case GL_UNSIGNED_BYTE: {
301188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell	 const GLubyte *in = ((GLubyte *)ptr) + ib->rebase;
302188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell	 for (i = 0; i < ib->count; i++)
303188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell	    *elts++ = *in++;
304188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell	 break;
305188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell      }
306188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell      }
307c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   }
308c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell}
309c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
310c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellstatic void unmap_vbos( GLcontext *ctx,
311c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			struct gl_buffer_object **bo,
312c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			GLuint nr_bo )
313c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell{
314c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   GLuint i;
315c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   for (i = 0; i < nr_bo; i++) {
316c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell      ctx->Driver.UnmapBuffer(ctx,
317c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			      0, /* target -- I don't see why this would be needed */
318c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell			      bo[i]);
319c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   }
320c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell}
321c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
322c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
323c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
324c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell/* This is the main entrypoint into the slimmed-down software tnl
325c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * module.  In a regular swtnl driver, this can be plugged straight
326c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * into the vbo->Driver.DrawPrims() callback.
327c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell */
328c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellvoid _tnl_draw_prims( GLcontext *ctx,
329c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell		      const struct gl_client_array *arrays[],
330c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell		      const struct _mesa_prim *prim,
331c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell		      GLuint nr_prims,
332c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell		      const struct _mesa_index_buffer *ib,
333c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell		      GLuint min_index,
334c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell		      GLuint max_index)
335c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell{
336c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   TNLcontext *tnl = TNL_CONTEXT(ctx);
337c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   struct vertex_buffer *VB = &tnl->vb;
338c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
339c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   /* May need to map a vertex buffer object for every attribute plus
340c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    * one for the index buffer.
341c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    */
342c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   struct gl_buffer_object *bo[VERT_ATTRIB_MAX + 1];
343c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   GLuint nr_bo = 0;
344c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
345c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   /* Binding inputs may imply mapping some vertex buffer objects.
346c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    * They will need to be unmapped below.
347c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    */
348c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   bind_inputs(ctx, arrays, min_index, max_index, bo, &nr_bo);
349c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   bind_indicies(ctx, ib, bo, &nr_bo);
350c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
351c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->Primitive = prim;
352c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->PrimitiveCount = nr_prims;
353c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   VB->Count = max_index - min_index;
354c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
355c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   TNL_CONTEXT(ctx)->Driver.RunPipeline(ctx);
356c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
357c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   unmap_vbos(ctx, bo, nr_bo);
358c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell   free_space(ctx);
359c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell}
360c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
361