mesa: update state after binding vertex list in dlist path
[mesa.git] / src / mesa / vbo / vbo_save_draw.c
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 */
45 static 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], *data = vertex;
50 GLuint i, offset;
51
52 if (node->count)
53 offset = (node->buffer_offset +
54 (node->count-1) * node->vertex_size * sizeof(GLfloat));
55 else
56 offset = node->buffer_offset;
57
58 ctx->Driver.GetBufferSubData( ctx, 0, offset,
59 node->vertex_size * sizeof(GLfloat),
60 data, node->vertex_store->bufferobj );
61
62 data += node->attrsz[0]; /* skip vertex position */
63
64 for (i = VBO_ATTRIB_POS+1 ; i < VBO_ATTRIB_MAX ; i++) {
65 if (node->attrsz[i]) {
66 GLfloat *current = (GLfloat *)vbo->currval[i].Ptr;
67 GLfloat tmp[4];
68
69 COPY_CLEAN_4V(tmp,
70 node->attrsz[i],
71 data);
72
73 if (memcmp(current, tmp, 4 * sizeof(GLfloat)) != 0)
74 {
75 memcpy(current, tmp, 4 * sizeof(GLfloat));
76
77 vbo->currval[i].Size = node->attrsz[i];
78
79 if (i >= VBO_ATTRIB_FIRST_MATERIAL &&
80 i <= VBO_ATTRIB_LAST_MATERIAL)
81 ctx->NewState |= _NEW_LIGHT;
82
83 ctx->NewState |= _NEW_CURRENT_ATTRIB;
84 }
85
86 data += node->attrsz[i];
87 }
88 }
89
90 /* Colormaterial -- this kindof sucks.
91 */
92 if (ctx->Light.ColorMaterialEnabled) {
93 _mesa_update_color_material(ctx, ctx->Current.Attrib[VBO_ATTRIB_COLOR0]);
94 }
95
96 /* CurrentExecPrimitive
97 */
98 if (node->prim_count) {
99 const struct _mesa_prim *prim = &node->prim[node->prim_count - 1];
100 if (prim->end)
101 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
102 else
103 ctx->Driver.CurrentExecPrimitive = prim->mode;
104 }
105 }
106
107
108
109 /* Treat the vertex storage as a VBO, define vertex arrays pointing
110 * into it:
111 */
112 static void vbo_bind_vertex_list( GLcontext *ctx,
113 const struct vbo_save_vertex_list *node )
114 {
115 struct vbo_context *vbo = vbo_context(ctx);
116 struct vbo_save_context *save = &vbo->save;
117 struct gl_client_array *arrays = save->arrays;
118 GLuint data = node->buffer_offset;
119 const GLuint *map;
120 GLuint attr;
121 GLuint varying_inputs = 0;
122
123 /* Install the default (ie Current) attributes first, then overlay
124 * all active ones.
125 */
126 switch (get_program_mode(ctx)) {
127 case VP_NONE:
128 for (attr = 0; attr < 16; attr++) {
129 save->inputs[attr] = &vbo->legacy_currval[attr];
130 }
131 for (attr = 0; attr < MAT_ATTRIB_MAX; attr++) {
132 save->inputs[attr + 16] = &vbo->mat_currval[attr];
133 }
134 map = vbo->map_vp_none;
135 break;
136 case VP_NV:
137 case VP_ARB:
138 /* The aliasing of attributes for NV vertex programs has already
139 * occurred. NV vertex programs cannot access material values,
140 * nor attributes greater than VERT_ATTRIB_TEX7.
141 */
142 for (attr = 0; attr < 16; attr++) {
143 save->inputs[attr] = &vbo->legacy_currval[attr];
144 save->inputs[attr + 16] = &vbo->generic_currval[attr];
145 }
146 map = vbo->map_vp_arb;
147 break;
148 }
149
150 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
151 GLuint src = map[attr];
152
153 if (node->attrsz[src]) {
154 /* override the default array set above */
155 save->inputs[attr] = &arrays[attr];
156
157 arrays[attr].Ptr = (const GLubyte *) data;
158 arrays[attr].Size = node->attrsz[src];
159 arrays[attr].StrideB = node->vertex_size * sizeof(GLfloat);
160 arrays[attr].Stride = node->vertex_size * sizeof(GLfloat);
161 arrays[attr].Type = GL_FLOAT;
162 arrays[attr].Enabled = 1;
163 _mesa_reference_buffer_object(ctx,
164 &arrays[attr].BufferObj,
165 node->vertex_store->bufferobj);
166 arrays[attr]._MaxElement = node->count; /* ??? */
167
168 assert(arrays[attr].BufferObj->Name);
169
170 data += node->attrsz[src] * sizeof(GLfloat);
171 varying_inputs |= 1<<attr;
172 }
173 }
174
175 _mesa_set_varying_vp_inputs( ctx, varying_inputs );
176 }
177
178 static void vbo_save_loopback_vertex_list( GLcontext *ctx,
179 const struct vbo_save_vertex_list *list )
180 {
181 const char *buffer = ctx->Driver.MapBuffer(ctx,
182 GL_ARRAY_BUFFER_ARB,
183 GL_READ_ONLY, /* ? */
184 list->vertex_store->bufferobj);
185
186 vbo_loopback_vertex_list( ctx,
187 (const GLfloat *)(buffer + list->buffer_offset),
188 list->attrsz,
189 list->prim,
190 list->prim_count,
191 list->wrap_count,
192 list->vertex_size);
193
194 ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER_ARB,
195 list->vertex_store->bufferobj);
196 }
197
198
199 /**
200 * Execute the buffer and save copied verts.
201 */
202 void vbo_save_playback_vertex_list( GLcontext *ctx, void *data )
203 {
204 const struct vbo_save_vertex_list *node = (const struct vbo_save_vertex_list *) data;
205 struct vbo_save_context *save = &vbo_context(ctx)->save;
206
207 FLUSH_CURRENT(ctx, 0);
208
209 if (node->prim_count > 0 && node->count > 0) {
210
211 if (ctx->Driver.CurrentExecPrimitive != PRIM_OUTSIDE_BEGIN_END &&
212 node->prim[0].begin) {
213
214 /* Degenerate case: list is called inside begin/end pair and
215 * includes operations such as glBegin or glDrawArrays.
216 */
217 if (0)
218 _mesa_printf("displaylist recursive begin");
219
220 vbo_save_loopback_vertex_list( ctx, node );
221 return;
222 }
223 else if (save->replay_flags) {
224 /* Various degnerate cases: translate into immediate mode
225 * calls rather than trying to execute in place.
226 */
227 vbo_save_loopback_vertex_list( ctx, node );
228 return;
229 }
230
231 if (ctx->NewState)
232 _mesa_update_state( ctx );
233
234 /* XXX also need to check if shader enabled, but invalid */
235 if ((ctx->VertexProgram.Enabled && !ctx->VertexProgram._Enabled) ||
236 (ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled)) {
237 _mesa_error(ctx, GL_INVALID_OPERATION,
238 "glBegin (invalid vertex/fragment program)");
239 return;
240 }
241
242 vbo_bind_vertex_list( ctx, node );
243
244 /* Again...
245 */
246 if (ctx->NewState)
247 _mesa_update_state( ctx );
248
249 vbo_context(ctx)->draw_prims( ctx,
250 save->inputs,
251 node->prim,
252 node->prim_count,
253 NULL,
254 0, /* Node is a VBO, so this is ok */
255 node->count - 1);
256 }
257
258 /* Copy to current?
259 */
260 _playback_copy_to_current( ctx, node );
261 }