vbo: remove vbo_context::legacy_currval
[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/mfeatures.h"
34 #include "main/mtypes.h"
35 #include "main/macros.h"
36 #include "main/light.h"
37 #include "main/state.h"
38
39 #include "vbo_context.h"
40
41
42 #if FEATURE_dlist
43
44
45 /**
46 * After playback, copy everything but the position from the
47 * last vertex to the saved state
48 */
49 static void
50 _playback_copy_to_current(struct gl_context *ctx,
51 const struct vbo_save_vertex_list *node)
52 {
53 struct vbo_context *vbo = vbo_context(ctx);
54 GLfloat vertex[VBO_ATTRIB_MAX * 4];
55 GLfloat *data;
56 GLuint i, offset;
57
58 if (node->current_size == 0)
59 return;
60
61 if (node->current_data) {
62 data = node->current_data;
63 }
64 else {
65 data = vertex;
66
67 if (node->count)
68 offset = (node->buffer_offset +
69 (node->count-1) * node->vertex_size * sizeof(GLfloat));
70 else
71 offset = node->buffer_offset;
72
73 ctx->Driver.GetBufferSubData( ctx, offset,
74 node->vertex_size * sizeof(GLfloat),
75 data, node->vertex_store->bufferobj );
76
77 data += node->attrsz[0]; /* skip vertex position */
78 }
79
80 for (i = VBO_ATTRIB_POS+1 ; i < VBO_ATTRIB_MAX ; i++) {
81 if (node->attrsz[i]) {
82 GLfloat *current = (GLfloat *)vbo->currval[i].Ptr;
83 GLfloat tmp[4];
84
85 COPY_CLEAN_4V(tmp,
86 node->attrsz[i],
87 data);
88
89 if (memcmp(current, tmp, 4 * sizeof(GLfloat)) != 0) {
90 memcpy(current, tmp, 4 * sizeof(GLfloat));
91
92 vbo->currval[i].Size = node->attrsz[i];
93 assert(vbo->currval[i].Type == GL_FLOAT);
94 vbo->currval[i]._ElementSize = vbo->currval[i].Size * sizeof(GLfloat);
95
96 if (i >= VBO_ATTRIB_FIRST_MATERIAL &&
97 i <= VBO_ATTRIB_LAST_MATERIAL)
98 ctx->NewState |= _NEW_LIGHT;
99
100 ctx->NewState |= _NEW_CURRENT_ATTRIB;
101 }
102
103 data += node->attrsz[i];
104 }
105 }
106
107 /* Colormaterial -- this kindof sucks.
108 */
109 if (ctx->Light.ColorMaterialEnabled) {
110 _mesa_update_color_material(ctx, ctx->Current.Attrib[VBO_ATTRIB_COLOR0]);
111 }
112
113 /* CurrentExecPrimitive
114 */
115 if (node->prim_count) {
116 const struct _mesa_prim *prim = &node->prim[node->prim_count - 1];
117 if (prim->end)
118 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
119 else
120 ctx->Driver.CurrentExecPrimitive = prim->mode;
121 }
122 }
123
124
125
126 /**
127 * Treat the vertex storage as a VBO, define vertex arrays pointing
128 * into it:
129 */
130 static void vbo_bind_vertex_list(struct gl_context *ctx,
131 const struct vbo_save_vertex_list *node)
132 {
133 struct vbo_context *vbo = vbo_context(ctx);
134 struct vbo_save_context *save = &vbo->save;
135 struct gl_client_array *arrays = save->arrays;
136 GLuint buffer_offset = node->buffer_offset;
137 const GLuint *map;
138 GLuint attr;
139 GLubyte node_attrsz[VBO_ATTRIB_MAX]; /* copy of node->attrsz[] */
140 GLbitfield64 varying_inputs = 0x0;
141
142 memcpy(node_attrsz, node->attrsz, sizeof(node->attrsz));
143
144 /* Install the default (ie Current) attributes first, then overlay
145 * all active ones.
146 */
147 switch (get_program_mode(ctx)) {
148 case VP_NONE:
149 for (attr = 0; attr < VERT_ATTRIB_FF_MAX; attr++) {
150 save->inputs[attr] = &vbo->currval[VBO_ATTRIB_POS+attr];
151 }
152 for (attr = 0; attr < MAT_ATTRIB_MAX; attr++) {
153 save->inputs[VERT_ATTRIB_GENERIC(attr)] = &vbo->mat_currval[attr];
154 }
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 for (attr = 0; attr < VERT_ATTRIB_FF_MAX; attr++) {
164 save->inputs[attr] = &vbo->currval[VBO_ATTRIB_POS+attr];
165 }
166 for (attr = 0; attr < VERT_ATTRIB_GENERIC_MAX; attr++) {
167 save->inputs[VERT_ATTRIB_GENERIC(attr)] = &vbo->generic_currval[attr];
168 }
169 map = vbo->map_vp_arb;
170
171 /* check if VERT_ATTRIB_POS is not read but VERT_BIT_GENERIC0 is read.
172 * In that case we effectively need to route the data from
173 * glVertexAttrib(0, val) calls to feed into the GENERIC0 input.
174 */
175 if ((ctx->VertexProgram._Current->Base.InputsRead & VERT_BIT_POS) == 0 &&
176 (ctx->VertexProgram._Current->Base.InputsRead & VERT_BIT_GENERIC0)) {
177 save->inputs[VERT_ATTRIB_GENERIC0] = save->inputs[0];
178 node_attrsz[VERT_ATTRIB_GENERIC0] = node_attrsz[0];
179 node_attrsz[0] = 0;
180 }
181 break;
182 default:
183 assert(0);
184 }
185
186 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
187 const GLuint src = map[attr];
188
189 if (node_attrsz[src]) {
190 /* override the default array set above */
191 save->inputs[attr] = &arrays[attr];
192
193 arrays[attr].Ptr = (const GLubyte *) NULL + buffer_offset;
194 arrays[attr].Size = node_attrsz[src];
195 arrays[attr].StrideB = node->vertex_size * sizeof(GLfloat);
196 arrays[attr].Stride = node->vertex_size * sizeof(GLfloat);
197 arrays[attr].Type = GL_FLOAT;
198 arrays[attr].Format = GL_RGBA;
199 arrays[attr].Enabled = 1;
200 arrays[attr]._ElementSize = arrays[attr].Size * sizeof(GLfloat);
201 _mesa_reference_buffer_object(ctx,
202 &arrays[attr].BufferObj,
203 node->vertex_store->bufferobj);
204 arrays[attr]._MaxElement = node->count; /* ??? */
205
206 assert(arrays[attr].BufferObj->Name);
207
208 buffer_offset += node_attrsz[src] * sizeof(GLfloat);
209 varying_inputs |= VERT_BIT(attr);
210 ctx->NewState |= _NEW_ARRAY;
211 }
212 }
213
214 _mesa_set_varying_vp_inputs( ctx, varying_inputs );
215 }
216
217
218 static void
219 vbo_save_loopback_vertex_list(struct gl_context *ctx,
220 const struct vbo_save_vertex_list *list)
221 {
222 const char *buffer =
223 ctx->Driver.MapBufferRange(ctx, 0,
224 list->vertex_store->bufferobj->Size,
225 GL_MAP_READ_BIT, /* ? */
226 list->vertex_store->bufferobj);
227
228 vbo_loopback_vertex_list(ctx,
229 (const GLfloat *)(buffer + list->buffer_offset),
230 list->attrsz,
231 list->prim,
232 list->prim_count,
233 list->wrap_count,
234 list->vertex_size);
235
236 ctx->Driver.UnmapBuffer(ctx, list->vertex_store->bufferobj);
237 }
238
239
240 /**
241 * Execute the buffer and save copied verts.
242 * This is called from the display list code when executing
243 * a drawing command.
244 */
245 void
246 vbo_save_playback_vertex_list(struct gl_context *ctx, void *data)
247 {
248 const struct vbo_save_vertex_list *node =
249 (const struct vbo_save_vertex_list *) data;
250 struct vbo_save_context *save = &vbo_context(ctx)->save;
251 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
252 GLboolean remap_vertex_store = GL_FALSE;
253
254 if (save->vertex_store->buffer) {
255 /* The vertex store is currently mapped but we're about to replay
256 * a display list. This can happen when a nested display list is
257 * being build with GL_COMPILE_AND_EXECUTE.
258 * We never want to have mapped vertex buffers when we're drawing.
259 * Unmap the vertex store, execute the list, then remap the vertex
260 * store.
261 */
262 vbo_save_unmap_vertex_store(ctx, save->vertex_store);
263 remap_vertex_store = GL_TRUE;
264 }
265
266 FLUSH_CURRENT(ctx, 0);
267
268 if (node->prim_count > 0) {
269
270 if (ctx->Driver.CurrentExecPrimitive != PRIM_OUTSIDE_BEGIN_END &&
271 node->prim[0].begin) {
272
273 /* Degenerate case: list is called inside begin/end pair and
274 * includes operations such as glBegin or glDrawArrays.
275 */
276 if (0)
277 printf("displaylist recursive begin");
278
279 vbo_save_loopback_vertex_list( ctx, node );
280
281 goto end;
282 }
283 else if (save->replay_flags) {
284 /* Various degnerate cases: translate into immediate mode
285 * calls rather than trying to execute in place.
286 */
287 vbo_save_loopback_vertex_list( ctx, node );
288
289 goto end;
290 }
291
292 if (ctx->NewState)
293 _mesa_update_state( ctx );
294
295 /* XXX also need to check if shader enabled, but invalid */
296 if ((ctx->VertexProgram.Enabled && !ctx->VertexProgram._Enabled) ||
297 (ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled)) {
298 _mesa_error(ctx, GL_INVALID_OPERATION,
299 "glBegin (invalid vertex/fragment program)");
300 return;
301 }
302
303 vbo_bind_vertex_list( ctx, node );
304
305 vbo_draw_method(exec, DRAW_DISPLAY_LIST);
306
307 /* Again...
308 */
309 if (ctx->NewState)
310 _mesa_update_state( ctx );
311
312 if (node->count > 0) {
313 vbo_context(ctx)->draw_prims(ctx,
314 save->inputs,
315 node->prim,
316 node->prim_count,
317 NULL,
318 GL_TRUE,
319 0, /* Node is a VBO, so this is ok */
320 node->count - 1,
321 NULL);
322 }
323 }
324
325 /* Copy to current?
326 */
327 _playback_copy_to_current( ctx, node );
328
329 end:
330 if (remap_vertex_store) {
331 save->buffer_ptr = vbo_save_map_vertex_store(ctx, save->vertex_store);
332 }
333 }
334
335
336 #endif /* FEATURE_dlist */