vbo: Use static const VERT_ATTRIB->VBO_ATTRIB maps.
[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 <stdbool.h>
30 #include "main/glheader.h"
31 #include "main/bufferobj.h"
32 #include "main/context.h"
33 #include "main/imports.h"
34 #include "main/mtypes.h"
35 #include "main/macros.h"
36 #include "main/light.h"
37 #include "main/state.h"
38 #include "main/varray.h"
39 #include "util/bitscan.h"
40
41 #include "vbo_private.h"
42
43
44 /**
45 * After playback, copy everything but the position from the
46 * last vertex to the saved state
47 */
48 static void
49 playback_copy_to_current(struct gl_context *ctx,
50 const struct vbo_save_vertex_list *node)
51 {
52 struct vbo_context *vbo = vbo_context(ctx);
53 fi_type vertex[VBO_ATTRIB_MAX * 4];
54 fi_type *data;
55 GLbitfield64 mask;
56
57 if (node->current_size == 0)
58 return;
59
60 if (node->current_data) {
61 data = node->current_data;
62 }
63 else {
64 /* Position of last vertex */
65 const GLuint pos = node->vertex_count > 0 ? node->vertex_count - 1 : 0;
66 /* Offset to last vertex in the vertex buffer */
67 const GLuint offset = node->buffer_offset
68 + pos * node->vertex_size * sizeof(GLfloat);
69
70 data = vertex;
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 attr;
142 GLbitfield varying_inputs = 0x0;
143 bool generic_from_pos = false;
144
145 const enum vp_mode program_mode = get_vp_mode(ctx);
146 const GLubyte * const map = _vbo_attribute_alias_map[program_mode];
147
148 /* Install the default (ie Current) attributes first */
149 for (attr = 0; attr < VERT_ATTRIB_FF_MAX; attr++) {
150 save->inputs[attr] = &vbo->currval[VBO_ATTRIB_POS + attr];
151 }
152
153 /* Overlay other active attributes */
154 switch (program_mode) {
155 case VP_FF:
156 for (attr = 0; attr < VERT_ATTRIB_MAT0; attr++) {
157 save->inputs[VERT_ATTRIB_GENERIC(attr)] =
158 &vbo->currval[VBO_ATTRIB_GENERIC0+attr];
159 }
160 for (attr = 0; attr < VERT_ATTRIB_MAT_MAX; attr++) {
161 save->inputs[VERT_ATTRIB_MAT(attr)] =
162 &vbo->currval[VBO_ATTRIB_MAT_FRONT_AMBIENT+attr];
163 }
164 break;
165 case VP_SHADER:
166 for (attr = 0; attr < VERT_ATTRIB_GENERIC_MAX; attr++) {
167 save->inputs[VERT_ATTRIB_GENERIC(attr)] =
168 &vbo->currval[VBO_ATTRIB_GENERIC0+attr];
169 }
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 const GLbitfield64 inputs_read =
176 ctx->VertexProgram._Current->info.inputs_read;
177 if ((inputs_read & VERT_BIT_POS) == 0 &&
178 (inputs_read & VERT_BIT_GENERIC0)) {
179 generic_from_pos = true;
180 }
181 break;
182 default:
183 unreachable("Bad vertex program mode");
184 }
185
186 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
187 const GLuint src = map[attr];
188 const GLubyte size = node->attrsz[src];
189
190 if (size) {
191 struct gl_vertex_array *array = &arrays[attr];
192 const GLenum16 type = node->attrtype[src];
193
194 /* override the default array set above */
195 save->inputs[attr] = array;
196
197 array->Ptr = (const GLubyte *) NULL + node->offsets[src];
198 array->Size = size;
199 array->StrideB = node->vertex_size * sizeof(GLfloat);
200 array->Type = type;
201 array->Integer = vbo_attrtype_to_integer_flag(type);
202 array->Format = GL_RGBA;
203 array->_ElementSize = size * sizeof(GLfloat);
204 _mesa_reference_buffer_object(ctx,
205 &array->BufferObj,
206 node->vertex_store->bufferobj);
207
208 assert(array->BufferObj->Name);
209
210 varying_inputs |= VERT_BIT(attr);
211 }
212 }
213
214 if (generic_from_pos) {
215 varying_inputs |= (varying_inputs & VERT_BIT_POS) << VERT_ATTRIB_GENERIC0;
216 save->inputs[VERT_ATTRIB_GENERIC0] = save->inputs[VERT_ATTRIB_POS];
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 unsigned buffer_offset =
236 aligned_vertex_buffer_offset(list) ? 0 : list->buffer_offset;
237
238 vbo_loopback_vertex_list(ctx,
239 (const GLfloat *) (buffer + buffer_offset),
240 list->attrsz,
241 list->prims,
242 list->prim_count,
243 list->wrap_count,
244 list->vertex_size);
245
246 ctx->Driver.UnmapBuffer(ctx, list->vertex_store->bufferobj,
247 MAP_INTERNAL);
248 }
249
250
251 /**
252 * Execute the buffer and save copied verts.
253 * This is called from the display list code when executing
254 * a drawing command.
255 */
256 void
257 vbo_save_playback_vertex_list(struct gl_context *ctx, void *data)
258 {
259 const struct vbo_save_vertex_list *node =
260 (const struct vbo_save_vertex_list *) data;
261 struct vbo_context *vbo = vbo_context(ctx);
262 struct vbo_save_context *save = &vbo->save;
263 GLboolean remap_vertex_store = GL_FALSE;
264
265 if (save->vertex_store && save->vertex_store->buffer_map) {
266 /* The vertex store is currently mapped but we're about to replay
267 * a display list. This can happen when a nested display list is
268 * being build with GL_COMPILE_AND_EXECUTE.
269 * We never want to have mapped vertex buffers when we're drawing.
270 * Unmap the vertex store, execute the list, then remap the vertex
271 * store.
272 */
273 vbo_save_unmap_vertex_store(ctx, save->vertex_store);
274 remap_vertex_store = GL_TRUE;
275 }
276
277 FLUSH_CURRENT(ctx, 0);
278
279 if (node->prim_count > 0) {
280
281 if (_mesa_inside_begin_end(ctx) && node->prims[0].begin) {
282 /* Error: we're about to begin a new primitive but we're already
283 * inside a glBegin/End pair.
284 */
285 _mesa_error(ctx, GL_INVALID_OPERATION,
286 "draw operation inside glBegin/End");
287 goto end;
288 }
289 else if (save->replay_flags) {
290 /* Various degenerate cases: translate into immediate mode
291 * calls rather than trying to execute in place.
292 */
293 loopback_vertex_list(ctx, node);
294
295 goto end;
296 }
297
298 if (ctx->NewState)
299 _mesa_update_state(ctx);
300
301 /* XXX also need to check if shader enabled, but invalid */
302 if ((ctx->VertexProgram.Enabled &&
303 !_mesa_arb_vertex_program_enabled(ctx)) ||
304 (ctx->FragmentProgram.Enabled &&
305 !_mesa_arb_fragment_program_enabled(ctx))) {
306 _mesa_error(ctx, GL_INVALID_OPERATION,
307 "glBegin (invalid vertex/fragment program)");
308 return;
309 }
310
311 bind_vertex_list(ctx, node);
312
313 _mesa_set_drawing_arrays(ctx, vbo->save.inputs);
314
315 /* Again...
316 */
317 if (ctx->NewState)
318 _mesa_update_state(ctx);
319
320 if (node->vertex_count > 0) {
321 GLuint min_index = node->start_vertex;
322 GLuint max_index = min_index + node->vertex_count - 1;
323 vbo->draw_prims(ctx,
324 node->prims,
325 node->prim_count,
326 NULL,
327 GL_TRUE,
328 min_index, max_index,
329 NULL, 0, NULL);
330 }
331 }
332
333 /* Copy to current?
334 */
335 playback_copy_to_current(ctx, node);
336
337 end:
338 if (remap_vertex_store) {
339 save->buffer_ptr = vbo_save_map_vertex_store(ctx, save->vertex_store);
340 }
341 }