fix bug in _playback_copy_to_current(): need to skip version position data (see bug...
[mesa.git] / src / mesa / vbo / vbo_save_draw.c
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 */
44 static 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 version 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 */
103 static 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 if (node->attrsz[attr]) {
136 arrays[attr].Ptr = (const GLubyte *)data;
137 arrays[attr].Size = node->attrsz[attr];
138 arrays[attr].StrideB = node->vertex_size * sizeof(GLfloat);
139 arrays[attr].Stride = node->vertex_size * sizeof(GLfloat);
140 arrays[attr].Type = GL_FLOAT;
141 arrays[attr].Enabled = 1;
142 arrays[attr].BufferObj = node->vertex_store->bufferobj;
143 arrays[attr]._MaxElement = node->count; /* ??? */
144
145 assert(arrays[attr].BufferObj->Name);
146
147 data += node->attrsz[attr] * sizeof(GLfloat);
148 }
149 }
150 }
151
152 static void vbo_save_loopback_vertex_list( GLcontext *ctx,
153 const struct vbo_save_vertex_list *list )
154 {
155 const char *buffer = ctx->Driver.MapBuffer(ctx,
156 GL_ARRAY_BUFFER_ARB,
157 GL_READ_ONLY, /* ? */
158 list->vertex_store->bufferobj);
159
160 vbo_loopback_vertex_list( ctx,
161 (const GLfloat *)(buffer + list->buffer_offset),
162 list->attrsz,
163 list->prim,
164 list->prim_count,
165 list->wrap_count,
166 list->vertex_size);
167
168 ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER_ARB,
169 list->vertex_store->bufferobj);
170 }
171
172
173 /**
174 * Execute the buffer and save copied verts.
175 */
176 void vbo_save_playback_vertex_list( GLcontext *ctx, void *data )
177 {
178 const struct vbo_save_vertex_list *node = (const struct vbo_save_vertex_list *) data;
179 struct vbo_save_context *save = &vbo_context(ctx)->save;
180
181 FLUSH_CURRENT(ctx, 0);
182
183 if (node->prim_count > 0 && node->count > 0) {
184
185 if (ctx->Driver.CurrentExecPrimitive != PRIM_OUTSIDE_BEGIN_END &&
186 node->prim[0].begin) {
187
188 /* Degenerate case: list is called inside begin/end pair and
189 * includes operations such as glBegin or glDrawArrays.
190 */
191 if (0)
192 _mesa_printf("displaylist recursive begin");
193
194 vbo_save_loopback_vertex_list( ctx, node );
195 return;
196 }
197 else if (save->replay_flags) {
198 /* Various degnerate cases: translate into immediate mode
199 * calls rather than trying to execute in place.
200 */
201 vbo_save_loopback_vertex_list( ctx, node );
202 return;
203 }
204
205 if (ctx->NewState)
206 _mesa_update_state( ctx );
207
208 /* XXX also need to check if shader enabled, but invalid */
209 if ((ctx->VertexProgram.Enabled && !ctx->VertexProgram._Enabled) ||
210 (ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled)) {
211 _mesa_error(ctx, GL_INVALID_OPERATION,
212 "glBegin (invalid vertex/fragment program)");
213 return;
214 }
215
216 vbo_bind_vertex_list( ctx, node );
217
218 vbo_context(ctx)->draw_prims( ctx,
219 save->inputs,
220 node->prim,
221 node->prim_count,
222 NULL,
223 0, /* Node is a VBO, so this is ok */
224 node->count - 1);
225 }
226
227 /* Copy to current?
228 */
229 _playback_copy_to_current( ctx, node );
230 }