vbo: Include mfeatures.h in files that perform feature tests.
[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, 0, 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
94 if (i >= VBO_ATTRIB_FIRST_MATERIAL &&
95 i <= VBO_ATTRIB_LAST_MATERIAL)
96 ctx->NewState |= _NEW_LIGHT;
97
98 ctx->NewState |= _NEW_CURRENT_ATTRIB;
99 }
100
101 data += node->attrsz[i];
102 }
103 }
104
105 /* Colormaterial -- this kindof sucks.
106 */
107 if (ctx->Light.ColorMaterialEnabled) {
108 _mesa_update_color_material(ctx, ctx->Current.Attrib[VBO_ATTRIB_COLOR0]);
109 }
110
111 /* CurrentExecPrimitive
112 */
113 if (node->prim_count) {
114 const struct _mesa_prim *prim = &node->prim[node->prim_count - 1];
115 if (prim->end)
116 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
117 else
118 ctx->Driver.CurrentExecPrimitive = prim->mode;
119 }
120 }
121
122
123
124 /**
125 * Treat the vertex storage as a VBO, define vertex arrays pointing
126 * into it:
127 */
128 static void vbo_bind_vertex_list(struct gl_context *ctx,
129 const struct vbo_save_vertex_list *node)
130 {
131 struct vbo_context *vbo = vbo_context(ctx);
132 struct vbo_save_context *save = &vbo->save;
133 struct gl_client_array *arrays = save->arrays;
134 GLuint buffer_offset = node->buffer_offset;
135 const GLuint *map;
136 GLuint attr;
137 GLubyte node_attrsz[VBO_ATTRIB_MAX]; /* copy of node->attrsz[] */
138 GLbitfield varying_inputs = 0x0;
139
140 memcpy(node_attrsz, node->attrsz, sizeof(node->attrsz));
141
142 /* Install the default (ie Current) attributes first, then overlay
143 * all active ones.
144 */
145 switch (get_program_mode(ctx)) {
146 case VP_NONE:
147 for (attr = 0; attr < 16; attr++) {
148 save->inputs[attr] = &vbo->legacy_currval[attr];
149 }
150 for (attr = 0; attr < MAT_ATTRIB_MAX; attr++) {
151 save->inputs[attr + 16] = &vbo->mat_currval[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 < 16; attr++) {
162 save->inputs[attr] = &vbo->legacy_currval[attr];
163 save->inputs[attr + 16] = &vbo->generic_currval[attr];
164 }
165 map = vbo->map_vp_arb;
166
167 /* check if VERT_ATTRIB_POS is not read but VERT_BIT_GENERIC0 is read.
168 * In that case we effectively need to route the data from
169 * glVertexAttrib(0, val) calls to feed into the GENERIC0 input.
170 */
171 if ((ctx->VertexProgram._Current->Base.InputsRead & VERT_BIT_POS) == 0 &&
172 (ctx->VertexProgram._Current->Base.InputsRead & VERT_BIT_GENERIC0)) {
173 save->inputs[16] = save->inputs[0];
174 node_attrsz[16] = node_attrsz[0];
175 node_attrsz[0] = 0;
176 }
177 break;
178 default:
179 assert(0);
180 }
181
182 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
183 const GLuint src = map[attr];
184
185 if (node_attrsz[src]) {
186 /* override the default array set above */
187 save->inputs[attr] = &arrays[attr];
188
189 arrays[attr].Ptr = (const GLubyte *) NULL + buffer_offset;
190 arrays[attr].Size = node->attrsz[src];
191 arrays[attr].StrideB = node->vertex_size * sizeof(GLfloat);
192 arrays[attr].Stride = node->vertex_size * sizeof(GLfloat);
193 arrays[attr].Type = GL_FLOAT;
194 arrays[attr].Format = GL_RGBA;
195 arrays[attr].Enabled = 1;
196 _mesa_reference_buffer_object(ctx,
197 &arrays[attr].BufferObj,
198 node->vertex_store->bufferobj);
199 arrays[attr]._MaxElement = node->count; /* ??? */
200
201 assert(arrays[attr].BufferObj->Name);
202
203 buffer_offset += node->attrsz[src] * sizeof(GLfloat);
204 varying_inputs |= 1<<attr;
205 }
206 }
207
208 _mesa_set_varying_vp_inputs( ctx, varying_inputs );
209 }
210
211
212 static void
213 vbo_save_loopback_vertex_list(struct gl_context *ctx,
214 const struct vbo_save_vertex_list *list)
215 {
216 const char *buffer = ctx->Driver.MapBuffer(ctx,
217 GL_ARRAY_BUFFER_ARB,
218 GL_READ_ONLY, /* ? */
219 list->vertex_store->bufferobj);
220
221 vbo_loopback_vertex_list(ctx,
222 (const GLfloat *)(buffer + list->buffer_offset),
223 list->attrsz,
224 list->prim,
225 list->prim_count,
226 list->wrap_count,
227 list->vertex_size);
228
229 ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER_ARB,
230 list->vertex_store->bufferobj);
231 }
232
233
234 /**
235 * Execute the buffer and save copied verts.
236 * This is called from the display list code when executing
237 * a drawing command.
238 */
239 void
240 vbo_save_playback_vertex_list(struct gl_context *ctx, void *data)
241 {
242 const struct vbo_save_vertex_list *node =
243 (const struct vbo_save_vertex_list *) data;
244 struct vbo_save_context *save = &vbo_context(ctx)->save;
245
246 FLUSH_CURRENT(ctx, 0);
247
248 if (node->prim_count > 0 && node->count > 0) {
249
250 if (ctx->Driver.CurrentExecPrimitive != PRIM_OUTSIDE_BEGIN_END &&
251 node->prim[0].begin) {
252
253 /* Degenerate case: list is called inside begin/end pair and
254 * includes operations such as glBegin or glDrawArrays.
255 */
256 if (0)
257 printf("displaylist recursive begin");
258
259 vbo_save_loopback_vertex_list( ctx, node );
260 return;
261 }
262 else if (save->replay_flags) {
263 /* Various degnerate cases: translate into immediate mode
264 * calls rather than trying to execute in place.
265 */
266 vbo_save_loopback_vertex_list( ctx, node );
267 return;
268 }
269
270 if (ctx->NewState)
271 _mesa_update_state( ctx );
272
273 /* XXX also need to check if shader enabled, but invalid */
274 if ((ctx->VertexProgram.Enabled && !ctx->VertexProgram._Enabled) ||
275 (ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled)) {
276 _mesa_error(ctx, GL_INVALID_OPERATION,
277 "glBegin (invalid vertex/fragment program)");
278 return;
279 }
280
281 vbo_bind_vertex_list( ctx, node );
282
283 /* Again...
284 */
285 if (ctx->NewState)
286 _mesa_update_state( ctx );
287
288 vbo_context(ctx)->draw_prims(ctx,
289 save->inputs,
290 node->prim,
291 node->prim_count,
292 NULL,
293 GL_TRUE,
294 0, /* Node is a VBO, so this is ok */
295 node->count - 1);
296 }
297
298 /* Copy to current?
299 */
300 _playback_copy_to_current( ctx, node );
301 }
302
303
304 #endif /* FEATURE_dlist */