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