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