vbo_exec_draw.c revision fd2756006a0baf63f60548d8f509de5b9a143608
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 * Authors:
25 *    Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28#include "glheader.h"
29#include "context.h"
30#include "enums.h"
31#include "state.h"
32#include "macros.h"
33
34#include "vbo_context.h"
35
36
37static void vbo_exec_debug_verts( struct vbo_exec_context *exec )
38{
39   GLuint count = exec->vtx.vert_count;
40   GLuint i;
41
42   _mesa_printf("%s: %u vertices %d primitives, %d vertsize\n",
43		__FUNCTION__,
44		count,
45		exec->vtx.prim_count,
46		exec->vtx.vertex_size);
47
48   for (i = 0 ; i < exec->vtx.prim_count ; i++) {
49      struct _mesa_prim *prim = &exec->vtx.prim[i];
50      _mesa_printf("   prim %d: %s%s %d..%d %s %s\n",
51		   i,
52		   _mesa_lookup_enum_by_nr(prim->mode),
53		   prim->weak ? " (weak)" : "",
54		   prim->start,
55		   prim->start + prim->count,
56		   prim->begin ? "BEGIN" : "(wrap)",
57		   prim->end ? "END" : "(wrap)");
58   }
59}
60
61
62/*
63 * NOTE: Need to have calculated primitives by this point -- do it on the fly.
64 * NOTE: Old 'parity' issue is gone.
65 */
66static GLuint vbo_copy_vertices( struct vbo_exec_context *exec )
67{
68   GLuint nr = exec->vtx.prim[exec->vtx.prim_count-1].count;
69   GLuint ovf, i;
70   GLuint sz = exec->vtx.vertex_size;
71   GLfloat *dst = exec->vtx.copied.buffer;
72   GLfloat *src = ((GLfloat *)exec->vtx.buffer_map +
73		   exec->vtx.prim[exec->vtx.prim_count-1].start *
74		   exec->vtx.vertex_size);
75
76
77   switch( exec->ctx->Driver.CurrentExecPrimitive )
78   {
79   case GL_POINTS:
80      return 0;
81   case GL_LINES:
82      ovf = nr&1;
83      for (i = 0 ; i < ovf ; i++)
84	 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
85      return i;
86   case GL_TRIANGLES:
87      ovf = nr%3;
88      for (i = 0 ; i < ovf ; i++)
89	 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
90      return i;
91   case GL_QUADS:
92      ovf = nr&3;
93      for (i = 0 ; i < ovf ; i++)
94	 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
95      return i;
96   case GL_LINE_STRIP:
97      if (nr == 0)
98	 return 0;
99      else {
100	 _mesa_memcpy( dst, src+(nr-1)*sz, sz * sizeof(GLfloat) );
101	 return 1;
102      }
103   case GL_LINE_LOOP:
104   case GL_TRIANGLE_FAN:
105   case GL_POLYGON:
106      if (nr == 0)
107	 return 0;
108      else if (nr == 1) {
109	 _mesa_memcpy( dst, src+0, sz * sizeof(GLfloat) );
110	 return 1;
111      } else {
112	 _mesa_memcpy( dst, src+0, sz * sizeof(GLfloat) );
113	 _mesa_memcpy( dst+sz, src+(nr-1)*sz, sz * sizeof(GLfloat) );
114	 return 2;
115      }
116   case GL_TRIANGLE_STRIP:
117   case GL_QUAD_STRIP:
118      switch (nr) {
119      case 0: ovf = 0; break;
120      case 1: ovf = 1; break;
121      default: ovf = 2 + (nr&1); break;
122      }
123      for (i = 0 ; i < ovf ; i++)
124	 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
125      return i;
126   case GL_POLYGON+1:
127      return 0;
128   default:
129      assert(0);
130      return 0;
131   }
132}
133
134
135
136/* TODO: populate these as the vertex is defined:
137 */
138static void vbo_exec_bind_arrays( GLcontext *ctx )
139{
140   struct vbo_context *vbo = vbo_context(ctx);
141   struct vbo_exec_context *exec = &vbo->exec;
142   struct gl_client_array *arrays = exec->vtx.arrays;
143   GLuint count = exec->vtx.vert_count;
144   GLubyte *data = exec->vtx.buffer_map;
145   const GLuint *map;
146   GLuint attr;
147
148   /* Install the default (ie Current) attributes first, then overlay
149    * all active ones.
150    */
151   switch (get_program_mode(exec->ctx)) {
152   case VP_NONE:
153      memcpy(arrays,      vbo->legacy_currval, 16 * sizeof(arrays[0]));
154      memcpy(arrays + 16, vbo->mat_currval,    MAT_ATTRIB_MAX * sizeof(arrays[0]));
155      map = vbo->map_vp_none;
156      break;
157   case VP_NV:
158   case VP_ARB:
159      /* The aliasing of attributes for NV vertex programs has already
160       * occurred.  NV vertex programs cannot access material values,
161       * nor attributes greater than VERT_ATTRIB_TEX7.
162       */
163      memcpy(arrays,      vbo->legacy_currval,  16 * sizeof(arrays[0]));
164      memcpy(arrays + 16, vbo->generic_currval, 16 * sizeof(arrays[0]));
165      map = vbo->map_vp_arb;
166      break;
167   }
168
169   /* Make all active attributes (including edgeflag) available as
170    * arrays of floats.
171    */
172   for (attr = 0; attr < VERT_ATTRIB_MAX ; attr++) {
173      GLuint src = map[attr];
174
175      if (exec->vtx.attrsz[src]) {
176	 arrays[attr].Ptr = (void *)data;
177	 arrays[attr].Size = exec->vtx.attrsz[src];
178	 arrays[attr].StrideB = exec->vtx.vertex_size * sizeof(GLfloat);
179	 arrays[attr].Stride = exec->vtx.vertex_size * sizeof(GLfloat);
180	 arrays[attr].Type = GL_FLOAT;
181	 arrays[attr].Enabled = 1;
182	 arrays[attr].BufferObj = exec->vtx.bufferobj; /* NullBufferObj */
183	 arrays[attr]._MaxElement = count; /* ??? */
184
185	 data += exec->vtx.attrsz[attr] * sizeof(GLfloat);
186      }
187   }
188}
189
190
191/**
192 * Execute the buffer and save copied verts.
193 */
194void vbo_exec_vtx_flush( struct vbo_exec_context *exec )
195{
196   if (0)
197      vbo_exec_debug_verts( exec );
198
199
200   if (exec->vtx.prim_count &&
201       exec->vtx.vert_count) {
202
203      exec->vtx.copied.nr = vbo_copy_vertices( exec );
204
205      if (exec->vtx.copied.nr != exec->vtx.vert_count) {
206	 GLcontext *ctx = exec->ctx;
207
208	 vbo_exec_bind_arrays( ctx );
209
210	 vbo_context(ctx)->draw_prims( ctx,
211				       exec->vtx.inputs,
212				       exec->vtx.prim,
213				       exec->vtx.prim_count,
214				       NULL,
215				       0,
216				       exec->vtx.vert_count );
217      }
218   }
219
220   exec->vtx.prim_count = 0;
221   exec->vtx.vert_count = 0;
222   exec->vtx.vbptr = (GLfloat *)exec->vtx.buffer_map;
223}
224