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