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