vbo_save_draw.c revision 7117f1b4
1/*
2 * Mesa 3-D graphics library
3 * Version:  6.1
4 *
5 * Copyright (C) 1999-2004  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/* Author:
26 *    Keith Whitwell <keith@tungstengraphics.com>
27 */
28
29#include "glheader.h"
30#include "context.h"
31#include "imports.h"
32#include "mtypes.h"
33#include "macros.h"
34#include "light.h"
35#include "state.h"
36
37#include "vbo_context.h"
38
39
40/*
41 * After playback, copy everything but the position from the
42 * last vertex to the saved state
43 */
44static void _playback_copy_to_current( GLcontext *ctx,
45				       const struct vbo_save_vertex_list *node )
46{
47   struct vbo_context *vbo = vbo_context(ctx);
48   GLfloat vertex[VBO_ATTRIB_MAX * 4], *data = vertex;
49   GLuint i, offset;
50
51   if (node->count)
52      offset = (node->buffer_offset +
53		(node->count-1) * node->vertex_size * sizeof(GLfloat));
54   else
55      offset = node->buffer_offset;
56
57   ctx->Driver.GetBufferSubData( ctx, 0, offset,
58				 node->vertex_size * sizeof(GLfloat),
59				 data, node->vertex_store->bufferobj );
60
61   data += node->attrsz[0]; /* skip vertex position */
62
63   for (i = VBO_ATTRIB_POS+1 ; i < VBO_ATTRIB_MAX ; i++) {
64      if (node->attrsz[i]) {
65	 GLfloat *current = (GLfloat *)vbo->currval[i].Ptr;
66
67	 COPY_CLEAN_4V(current,
68		       node->attrsz[i],
69		       data);
70
71	 vbo->currval[i].Size = node->attrsz[i];
72
73	 data += node->attrsz[i];
74
75	 if (i >= VBO_ATTRIB_FIRST_MATERIAL &&
76	     i <= VBO_ATTRIB_LAST_MATERIAL)
77	    ctx->NewState |= _NEW_LIGHT;
78      }
79   }
80
81   /* Colormaterial -- this kindof sucks.
82    */
83   if (ctx->Light.ColorMaterialEnabled) {
84      _mesa_update_color_material(ctx, ctx->Current.Attrib[VBO_ATTRIB_COLOR0]);
85   }
86
87   /* CurrentExecPrimitive
88    */
89   if (node->prim_count) {
90      const struct _mesa_prim *prim = &node->prim[node->prim_count - 1];
91      if (prim->end)
92	 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
93      else
94	 ctx->Driver.CurrentExecPrimitive = prim->mode;
95   }
96}
97
98
99
100/* Treat the vertex storage as a VBO, define vertex arrays pointing
101 * into it:
102 */
103static void vbo_bind_vertex_list( GLcontext *ctx,
104                                   const struct vbo_save_vertex_list *node )
105{
106   struct vbo_context *vbo = vbo_context(ctx);
107   struct vbo_save_context *save = &vbo->save;
108   struct gl_client_array *arrays = save->arrays;
109   GLuint data = node->buffer_offset;
110   const GLuint *map;
111   GLuint attr;
112
113   /* Install the default (ie Current) attributes first, then overlay
114    * all active ones.
115    */
116   switch (get_program_mode(ctx)) {
117   case VP_NONE:
118      memcpy(arrays,      vbo->legacy_currval, 16 * sizeof(arrays[0]));
119      memcpy(arrays + 16, vbo->mat_currval,    MAT_ATTRIB_MAX * sizeof(arrays[0]));
120      map = vbo->map_vp_none;
121      break;
122   case VP_NV:
123   case VP_ARB:
124      /* The aliasing of attributes for NV vertex programs has already
125       * occurred.  NV vertex programs cannot access material values,
126       * nor attributes greater than VERT_ATTRIB_TEX7.
127       */
128      memcpy(arrays,      vbo->legacy_currval,  16 * sizeof(arrays[0]));
129      memcpy(arrays + 16, vbo->generic_currval, 16 * sizeof(arrays[0]));
130      map = vbo->map_vp_arb;
131      break;
132   }
133
134   for (attr = 0; attr < VBO_ATTRIB_MAX; attr++) {
135      GLuint src = map[attr];
136
137      if (node->attrsz[src]) {
138	 arrays[attr].Ptr = (const GLubyte *)data;
139	 arrays[attr].Size = node->attrsz[src];
140	 arrays[attr].StrideB = node->vertex_size * sizeof(GLfloat);
141	 arrays[attr].Stride = node->vertex_size * sizeof(GLfloat);
142	 arrays[attr].Type = GL_FLOAT;
143	 arrays[attr].Enabled = 1;
144	 arrays[attr].BufferObj = node->vertex_store->bufferobj;
145	 arrays[attr]._MaxElement = node->count; /* ??? */
146
147	 assert(arrays[attr].BufferObj->Name);
148
149	 data += node->attrsz[attr] * sizeof(GLfloat);
150      }
151   }
152}
153
154static void vbo_save_loopback_vertex_list( GLcontext *ctx,
155					   const struct vbo_save_vertex_list *list )
156{
157   const char *buffer = ctx->Driver.MapBuffer(ctx,
158					      GL_ARRAY_BUFFER_ARB,
159					      GL_READ_ONLY, /* ? */
160					       list->vertex_store->bufferobj);
161
162   vbo_loopback_vertex_list( ctx,
163			     (const GLfloat *)(buffer + list->buffer_offset),
164			     list->attrsz,
165			     list->prim,
166			     list->prim_count,
167			     list->wrap_count,
168			     list->vertex_size);
169
170   ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER_ARB,
171			   list->vertex_store->bufferobj);
172}
173
174
175/**
176 * Execute the buffer and save copied verts.
177 */
178void vbo_save_playback_vertex_list( GLcontext *ctx, void *data )
179{
180   const struct vbo_save_vertex_list *node = (const struct vbo_save_vertex_list *) data;
181   struct vbo_save_context *save = &vbo_context(ctx)->save;
182
183   FLUSH_CURRENT(ctx, 0);
184
185   if (node->prim_count > 0 && node->count > 0) {
186
187      if (ctx->Driver.CurrentExecPrimitive != PRIM_OUTSIDE_BEGIN_END &&
188	  node->prim[0].begin) {
189
190	 /* Degenerate case: list is called inside begin/end pair and
191	  * includes operations such as glBegin or glDrawArrays.
192	  */
193	 if (0)
194	    _mesa_printf("displaylist recursive begin");
195
196	 vbo_save_loopback_vertex_list( ctx, node );
197	 return;
198      }
199      else if (save->replay_flags) {
200	 /* Various degnerate cases: translate into immediate mode
201	  * calls rather than trying to execute in place.
202	  */
203	 vbo_save_loopback_vertex_list( ctx, node );
204	 return;
205      }
206
207      if (ctx->NewState)
208	 _mesa_update_state( ctx );
209
210      /* XXX also need to check if shader enabled, but invalid */
211      if ((ctx->VertexProgram.Enabled && !ctx->VertexProgram._Enabled) ||
212          (ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled)) {
213         _mesa_error(ctx, GL_INVALID_OPERATION,
214                     "glBegin (invalid vertex/fragment program)");
215         return;
216      }
217
218      vbo_bind_vertex_list( ctx, node );
219
220      vbo_context(ctx)->draw_prims( ctx,
221				    save->inputs,
222				    node->prim,
223				    node->prim_count,
224				    NULL,
225				    0,	/* Node is a VBO, so this is ok */
226				    node->count - 1);
227   }
228
229   /* Copy to current?
230    */
231   _playback_copy_to_current( ctx, node );
232}
233