vbo: whitespace fixes in vbo_save_draw.c
[mesa.git] / src / mesa / vbo / vbo_save_draw.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /* Author:
26 * Keith Whitwell <keithw@vmware.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/mtypes.h"
34 #include "main/macros.h"
35 #include "main/light.h"
36 #include "main/state.h"
37 #include "util/bitscan.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 fi_type vertex[VBO_ATTRIB_MAX * 4];
52 fi_type *data;
53 GLbitfield64 mask;
54 GLuint offset;
55
56 if (node->current_size == 0)
57 return;
58
59 if (node->current_data) {
60 data = node->current_data;
61 }
62 else {
63 data = vertex;
64
65 if (node->count)
66 offset = (node->buffer_offset +
67 (node->count - 1) * node->vertex_size * sizeof(GLfloat));
68 else
69 offset = node->buffer_offset;
70
71 ctx->Driver.GetBufferSubData(ctx, offset,
72 node->vertex_size * sizeof(GLfloat),
73 data, node->vertex_store->bufferobj);
74
75 data += node->attrsz[0]; /* skip vertex position */
76 }
77
78 mask = node->enabled & (~BITFIELD64_BIT(VBO_ATTRIB_POS));
79 while (mask) {
80 const int i = u_bit_scan64(&mask);
81 fi_type *current = (fi_type *)vbo->currval[i].Ptr;
82 fi_type tmp[4];
83 assert(node->attrsz[i]);
84
85 COPY_CLEAN_4V_TYPE_AS_UNION(tmp,
86 node->attrsz[i],
87 data,
88 node->attrtype[i]);
89
90 if (node->attrtype[i] != vbo->currval[i].Type ||
91 memcmp(current, tmp, 4 * sizeof(GLfloat)) != 0) {
92 memcpy(current, tmp, 4 * sizeof(GLfloat));
93
94 vbo->currval[i].Size = node->attrsz[i];
95 vbo->currval[i]._ElementSize = vbo->currval[i].Size * sizeof(GLfloat);
96 vbo->currval[i].Type = node->attrtype[i];
97 vbo->currval[i].Integer =
98 vbo_attrtype_to_integer_flag(node->attrtype[i]);
99
100 if (i >= VBO_ATTRIB_FIRST_MATERIAL &&
101 i <= VBO_ATTRIB_LAST_MATERIAL)
102 ctx->NewState |= _NEW_LIGHT;
103
104 ctx->NewState |= _NEW_CURRENT_ATTRIB;
105 }
106
107 data += node->attrsz[i];
108 }
109
110 /* Colormaterial -- this kindof sucks.
111 */
112 if (ctx->Light.ColorMaterialEnabled) {
113 _mesa_update_color_material(ctx, ctx->Current.Attrib[VBO_ATTRIB_COLOR0]);
114 }
115
116 /* CurrentExecPrimitive
117 */
118 if (node->prim_count) {
119 const struct _mesa_prim *prim = &node->prim[node->prim_count - 1];
120 if (prim->end)
121 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
122 else
123 ctx->Driver.CurrentExecPrimitive = prim->mode;
124 }
125 }
126
127
128
129 /**
130 * Treat the vertex storage as a VBO, define vertex arrays pointing
131 * into it:
132 */
133 static void
134 vbo_bind_vertex_list(struct gl_context *ctx,
135 const struct vbo_save_vertex_list *node)
136 {
137 struct vbo_context *vbo = vbo_context(ctx);
138 struct vbo_save_context *save = &vbo->save;
139 struct gl_vertex_array *arrays = save->arrays;
140 GLuint buffer_offset = node->buffer_offset;
141 const GLuint *map;
142 GLuint attr;
143 GLubyte node_attrsz[VBO_ATTRIB_MAX]; /* copy of node->attrsz[] */
144 GLenum node_attrtype[VBO_ATTRIB_MAX]; /* copy of node->attrtype[] */
145 GLbitfield varying_inputs = 0x0;
146
147 memcpy(node_attrsz, node->attrsz, sizeof(node->attrsz));
148 memcpy(node_attrtype, node->attrtype, sizeof(node->attrtype));
149
150 /* Install the default (ie Current) attributes first, then overlay
151 * all active ones.
152 */
153 switch (get_program_mode(ctx)) {
154 case VP_NONE:
155 for (attr = 0; attr < VERT_ATTRIB_FF_MAX; attr++) {
156 save->inputs[attr] = &vbo->currval[VBO_ATTRIB_POS+attr];
157 }
158 for (attr = 0; attr < MAT_ATTRIB_MAX; attr++) {
159 save->inputs[VERT_ATTRIB_GENERIC(attr)] =
160 &vbo->currval[VBO_ATTRIB_MAT_FRONT_AMBIENT+attr];
161 }
162 map = vbo->map_vp_none;
163 break;
164 case VP_ARB:
165 for (attr = 0; attr < VERT_ATTRIB_FF_MAX; attr++) {
166 save->inputs[attr] = &vbo->currval[VBO_ATTRIB_POS+attr];
167 }
168 for (attr = 0; attr < VERT_ATTRIB_GENERIC_MAX; attr++) {
169 save->inputs[VERT_ATTRIB_GENERIC(attr)] =
170 &vbo->currval[VBO_ATTRIB_GENERIC0+attr];
171 }
172 map = vbo->map_vp_arb;
173
174 /* check if VERT_ATTRIB_POS is not read but VERT_BIT_GENERIC0 is read.
175 * In that case we effectively need to route the data from
176 * glVertexAttrib(0, val) calls to feed into the GENERIC0 input.
177 */
178 if ((ctx->VertexProgram._Current->info.inputs_read &
179 VERT_BIT_POS) == 0 &&
180 (ctx->VertexProgram._Current->info.inputs_read &
181 VERT_BIT_GENERIC0)) {
182 save->inputs[VERT_ATTRIB_GENERIC0] = save->inputs[0];
183 node_attrsz[VERT_ATTRIB_GENERIC0] = node_attrsz[0];
184 node_attrtype[VERT_ATTRIB_GENERIC0] = node_attrtype[0];
185 node_attrsz[0] = 0;
186 }
187 break;
188 default:
189 assert(0);
190 }
191
192 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
193 const GLuint src = map[attr];
194
195 if (node_attrsz[src]) {
196 /* override the default array set above */
197 save->inputs[attr] = &arrays[attr];
198
199 arrays[attr].Ptr = (const GLubyte *) NULL + buffer_offset;
200 arrays[attr].Size = node_attrsz[src];
201 arrays[attr].StrideB = node->vertex_size * sizeof(GLfloat);
202 arrays[attr].Type = node_attrtype[src];
203 arrays[attr].Integer =
204 vbo_attrtype_to_integer_flag(node_attrtype[src]);
205 arrays[attr].Format = GL_RGBA;
206 arrays[attr]._ElementSize = arrays[attr].Size * sizeof(GLfloat);
207 _mesa_reference_buffer_object(ctx,
208 &arrays[attr].BufferObj,
209 node->vertex_store->bufferobj);
210
211 assert(arrays[attr].BufferObj->Name);
212
213 buffer_offset += node_attrsz[src] * sizeof(GLfloat);
214 varying_inputs |= VERT_BIT(attr);
215 }
216 }
217
218 _mesa_set_varying_vp_inputs(ctx, varying_inputs);
219 ctx->NewDriverState |= ctx->DriverFlags.NewArray;
220 }
221
222
223 static void
224 vbo_save_loopback_vertex_list(struct gl_context *ctx,
225 const struct vbo_save_vertex_list *list)
226 {
227 const char *buffer =
228 ctx->Driver.MapBufferRange(ctx, 0,
229 list->vertex_store->bufferobj->Size,
230 GL_MAP_READ_BIT, /* ? */
231 list->vertex_store->bufferobj,
232 MAP_INTERNAL);
233
234 vbo_loopback_vertex_list(ctx,
235 (const GLfloat *)(buffer + list->buffer_offset),
236 list->attrsz,
237 list->prim,
238 list->prim_count,
239 list->wrap_count,
240 list->vertex_size);
241
242 ctx->Driver.UnmapBuffer(ctx, list->vertex_store->bufferobj,
243 MAP_INTERNAL);
244 }
245
246
247 /**
248 * Execute the buffer and save copied verts.
249 * This is called from the display list code when executing
250 * a drawing command.
251 */
252 void
253 vbo_save_playback_vertex_list(struct gl_context *ctx, void *data)
254 {
255 const struct vbo_save_vertex_list *node =
256 (const struct vbo_save_vertex_list *) data;
257 struct vbo_save_context *save = &vbo_context(ctx)->save;
258 GLboolean remap_vertex_store = GL_FALSE;
259
260 if (save->vertex_store && save->vertex_store->buffer) {
261 /* The vertex store is currently mapped but we're about to replay
262 * a display list. This can happen when a nested display list is
263 * being build with GL_COMPILE_AND_EXECUTE.
264 * We never want to have mapped vertex buffers when we're drawing.
265 * Unmap the vertex store, execute the list, then remap the vertex
266 * store.
267 */
268 vbo_save_unmap_vertex_store(ctx, save->vertex_store);
269 remap_vertex_store = GL_TRUE;
270 }
271
272 FLUSH_CURRENT(ctx, 0);
273
274 if (node->prim_count > 0) {
275
276 if (_mesa_inside_begin_end(ctx) && node->prim[0].begin) {
277 /* Error: we're about to begin a new primitive but we're already
278 * inside a glBegin/End pair.
279 */
280 _mesa_error(ctx, GL_INVALID_OPERATION,
281 "draw operation inside glBegin/End");
282 goto end;
283 }
284 else if (save->replay_flags) {
285 /* Various degenerate cases: translate into immediate mode
286 * calls rather than trying to execute in place.
287 */
288 vbo_save_loopback_vertex_list(ctx, node);
289
290 goto end;
291 }
292
293 if (ctx->NewState)
294 _mesa_update_state(ctx);
295
296 /* XXX also need to check if shader enabled, but invalid */
297 if ((ctx->VertexProgram.Enabled &&
298 !_mesa_arb_vertex_program_enabled(ctx)) ||
299 (ctx->FragmentProgram.Enabled &&
300 !_mesa_arb_fragment_program_enabled(ctx))) {
301 _mesa_error(ctx, GL_INVALID_OPERATION,
302 "glBegin (invalid vertex/fragment program)");
303 return;
304 }
305
306 vbo_bind_vertex_list(ctx, node);
307
308 vbo_draw_method(vbo_context(ctx), DRAW_DISPLAY_LIST);
309
310 /* Again...
311 */
312 if (ctx->NewState)
313 _mesa_update_state(ctx);
314
315 if (node->count > 0) {
316 vbo_context(ctx)->draw_prims(ctx,
317 node->prim,
318 node->prim_count,
319 NULL,
320 GL_TRUE,
321 0, /* Node is a VBO, so this is ok */
322 node->count - 1,
323 NULL, 0, NULL);
324 }
325 }
326
327 /* Copy to current?
328 */
329 _playback_copy_to_current(ctx, node);
330
331 end:
332 if (remap_vertex_store) {
333 save->buffer_ptr = vbo_save_map_vertex_store(ctx, save->vertex_store);
334 }
335 }