OSDN Git Service

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